async_graphql_poem/
response.rs

1use std::str::FromStr;
2
3use http::{HeaderName, HeaderValue};
4use poem::{IntoResponse, Response, web::Json};
5
6/// Response for `async_graphql::Request`.
7pub struct GraphQLResponse(pub async_graphql::Response);
8
9impl From<async_graphql::Response> for GraphQLResponse {
10    fn from(resp: async_graphql::Response) -> Self {
11        Self(resp)
12    }
13}
14
15impl IntoResponse for GraphQLResponse {
16    fn into_response(self) -> Response {
17        GraphQLBatchResponse(self.0.into()).into_response()
18    }
19}
20
21/// Response for `async_graphql::BatchRequest`.
22pub struct GraphQLBatchResponse(pub async_graphql::BatchResponse);
23
24impl From<async_graphql::BatchResponse> for GraphQLBatchResponse {
25    fn from(resp: async_graphql::BatchResponse) -> Self {
26        Self(resp)
27    }
28}
29
30impl IntoResponse for GraphQLBatchResponse {
31    fn into_response(self) -> Response {
32        let mut resp = Json(&self.0).into_response();
33
34        if self.0.is_ok() {
35            if let Some(cache_control) = self.0.cache_control().value() {
36                if let Ok(value) = cache_control.try_into() {
37                    resp.headers_mut().insert("cache-control", value);
38                }
39            }
40        }
41
42        resp.headers_mut()
43            .extend(self.0.http_headers().iter().filter_map(|(name, value)| {
44                HeaderName::from_str(name.as_str())
45                    .ok()
46                    .zip(HeaderValue::from_bytes(value.as_bytes()).ok())
47            }));
48        resp
49    }
50}