async_graphql/
executor.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#[cfg(not(feature = "boxed-trait"))]
use std::future::Future;
use std::sync::Arc;

use futures_util::stream::{BoxStream, FuturesOrdered, StreamExt};

use crate::{BatchRequest, BatchResponse, Data, Request, Response};

/// Represents a GraphQL executor
#[cfg_attr(feature = "boxed-trait", async_trait::async_trait)]
pub trait Executor: Unpin + Clone + Send + Sync + 'static {
    /// Execute a GraphQL query.
    #[cfg(feature = "boxed-trait")]
    async fn execute(&self, request: Request) -> Response;

    /// Execute a GraphQL query.
    #[cfg(not(feature = "boxed-trait"))]
    fn execute(&self, request: Request) -> impl Future<Output = Response> + Send;

    /// Execute a GraphQL batch query.
    #[cfg(feature = "boxed-trait")]
    async fn execute_batch(&self, batch_request: BatchRequest) -> BatchResponse {
        match batch_request {
            BatchRequest::Single(request) => BatchResponse::Single(self.execute(request).await),
            BatchRequest::Batch(requests) => BatchResponse::Batch(
                FuturesOrdered::from_iter(
                    requests.into_iter().map(|request| self.execute(request)),
                )
                .collect()
                .await,
            ),
        }
    }

    /// Execute a GraphQL batch query.
    #[cfg(not(feature = "boxed-trait"))]
    fn execute_batch(
        &self,
        batch_request: BatchRequest,
    ) -> impl Future<Output = BatchResponse> + Send {
        async {
            match batch_request {
                BatchRequest::Single(request) => BatchResponse::Single(self.execute(request).await),
                BatchRequest::Batch(requests) => BatchResponse::Batch(
                    FuturesOrdered::from_iter(
                        requests.into_iter().map(|request| self.execute(request)),
                    )
                    .collect()
                    .await,
                ),
            }
        }
    }

    /// Execute a GraphQL subscription with session data.
    fn execute_stream(
        &self,
        request: Request,
        session_data: Option<Arc<Data>>,
    ) -> BoxStream<'static, Response>;
}