async_graphql/
executor.rs

1#[cfg(not(feature = "boxed-trait"))]
2use std::future::Future;
3use std::sync::Arc;
4
5use futures_util::stream::{BoxStream, FuturesOrdered, StreamExt};
6
7use crate::{BatchRequest, BatchResponse, Data, Request, Response};
8
9/// Represents a GraphQL executor
10#[cfg_attr(feature = "boxed-trait", async_trait::async_trait)]
11pub trait Executor: Unpin + Clone + Send + Sync + 'static {
12    /// Execute a GraphQL query.
13    #[cfg(feature = "boxed-trait")]
14    async fn execute(&self, request: Request) -> Response;
15
16    /// Execute a GraphQL query.
17    #[cfg(not(feature = "boxed-trait"))]
18    fn execute(&self, request: Request) -> impl Future<Output = Response> + Send;
19
20    /// Execute a GraphQL batch query.
21    #[cfg(feature = "boxed-trait")]
22    async fn execute_batch(&self, batch_request: BatchRequest) -> BatchResponse {
23        match batch_request {
24            BatchRequest::Single(request) => BatchResponse::Single(self.execute(request).await),
25            BatchRequest::Batch(requests) => BatchResponse::Batch(
26                FuturesOrdered::from_iter(
27                    requests.into_iter().map(|request| self.execute(request)),
28                )
29                .collect()
30                .await,
31            ),
32        }
33    }
34
35    /// Execute a GraphQL batch query.
36    #[cfg(not(feature = "boxed-trait"))]
37    fn execute_batch(
38        &self,
39        batch_request: BatchRequest,
40    ) -> impl Future<Output = BatchResponse> + Send {
41        async {
42            match batch_request {
43                BatchRequest::Single(request) => BatchResponse::Single(self.execute(request).await),
44                BatchRequest::Batch(requests) => BatchResponse::Batch(
45                    FuturesOrdered::from_iter(
46                        requests.into_iter().map(|request| self.execute(request)),
47                    )
48                    .collect()
49                    .await,
50                ),
51            }
52        }
53    }
54
55    /// Execute a GraphQL subscription with session data.
56    fn execute_stream(
57        &self,
58        request: Request,
59        session_data: Option<Arc<Data>>,
60    ) -> BoxStream<'static, Response>;
61}