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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use async_graphql::http::MultipartOptions;
use poem::error::BadRequest;
use poem::http::{header, Method};
use poem::web::Query;
use poem::{async_trait, Error, FromRequest, Request, RequestBody, Result};
use tokio_util::compat::TokioAsyncReadCompatExt;
pub struct GraphQLRequest(pub async_graphql::Request);
#[async_trait]
impl<'a> FromRequest<'a> for GraphQLRequest {
type Error = Error;
async fn from_request(req: &'a Request, body: &mut RequestBody) -> Result<Self> {
Ok(GraphQLRequest(
GraphQLBatchRequest::from_request(req, body)
.await?
.0
.into_single()
.map_err(BadRequest)?,
))
}
}
pub struct GraphQLBatchRequest(pub async_graphql::BatchRequest);
#[async_trait]
impl<'a> FromRequest<'a> for GraphQLBatchRequest {
type Error = Error;
async fn from_request(req: &'a Request, body: &mut RequestBody) -> Result<Self> {
if req.method() == Method::GET {
let req = Query::from_request(req, body).await?.0;
Ok(Self(async_graphql::BatchRequest::Single(req)))
} else {
let content_type = req
.headers()
.get(header::CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.map(ToString::to_string);
Ok(Self(
async_graphql::http::receive_batch_body(
content_type,
body.take()?.into_async_read().compat(),
MultipartOptions::default(),
)
.await?,
))
}
}
}