hive_router_plan_executor/executors/
common.rs

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