hive_router_plan_executor/executors/
common.rs

1use std::{collections::HashMap, sync::Arc, time::Duration};
2
3use async_trait::async_trait;
4use http::HeaderMap;
5use sonic_rs::Value;
6
7use crate::response::subgraph_response::SubgraphResponse;
8
9#[async_trait]
10pub trait SubgraphExecutor {
11    async fn execute<'a>(
12        &self,
13        execution_request: SubgraphExecutionRequest<'a>,
14        timeout: Option<Duration>,
15    ) -> SubgraphResponse<'a>;
16
17    fn to_boxed_arc<'a>(self) -> Arc<Box<dyn SubgraphExecutor + Send + Sync + 'a>>
18    where
19        Self: Sized + Send + Sync + 'a,
20    {
21        Arc::new(Box::new(self))
22    }
23}
24
25pub type SubgraphExecutorType = dyn crate::executors::common::SubgraphExecutor + Send + Sync;
26
27pub type SubgraphExecutorBoxedArc = Arc<Box<SubgraphExecutorType>>;
28
29pub type SubgraphRequestExtensions = HashMap<String, Value>;
30
31pub struct SubgraphExecutionRequest<'a> {
32    pub query: &'a str,
33    pub dedupe: bool,
34    pub operation_name: Option<&'a str>,
35    // TODO: variables could be stringified before even executing the request
36    pub variables: Option<HashMap<&'a str, &'a sonic_rs::Value>>,
37    pub headers: HeaderMap,
38    pub representations: Option<Vec<u8>>,
39    pub extensions: Option<SubgraphRequestExtensions>,
40}
41
42impl SubgraphExecutionRequest<'_> {
43    pub fn add_request_extensions_field(&mut self, key: String, value: Value) {
44        self.extensions
45            .get_or_insert_with(HashMap::new)
46            .insert(key, value);
47    }
48}