Skip to main content

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