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
use async_graphql::Executor;
use poem::{async_trait, Endpoint, FromRequest, Request, Result};
use crate::{GraphQLBatchRequest, GraphQLBatchResponse};
pub struct GraphQL<E> {
executor: E,
}
impl<E> GraphQL<E> {
pub fn new(executor: E) -> Self {
Self { executor }
}
}
#[async_trait]
impl<E> Endpoint for GraphQL<E>
where
E: Executor,
{
type Output = GraphQLBatchResponse;
async fn call(&self, req: Request) -> Result<Self::Output> {
let (req, mut body) = req.split();
let req = GraphQLBatchRequest::from_request(&req, &mut body).await?;
Ok(GraphQLBatchResponse(
self.executor.execute_batch(req.0).await,
))
}
}