async_graphql_axum/
response.rs1use axum::{
2 body::Body,
3 http,
4 http::HeaderValue,
5 response::{IntoResponse, Response},
6};
7
8pub struct GraphQLResponse(pub async_graphql::BatchResponse);
13
14impl From<async_graphql::Response> for GraphQLResponse {
15 fn from(resp: async_graphql::Response) -> Self {
16 Self(resp.into())
17 }
18}
19
20impl From<async_graphql::BatchResponse> for GraphQLResponse {
21 fn from(resp: async_graphql::BatchResponse) -> Self {
22 Self(resp)
23 }
24}
25
26impl IntoResponse for GraphQLResponse {
27 fn into_response(self) -> Response {
28 let body: Body = serde_json::to_string(&self.0).unwrap().into();
29 let mut resp = Response::new(body);
30 resp.headers_mut().insert(
31 http::header::CONTENT_TYPE,
32 HeaderValue::from_static("application/json"),
33 );
34 if self.0.is_ok() {
35 if let Some(cache_control) = self.0.cache_control().value() {
36 if let Ok(value) = HeaderValue::from_str(&cache_control) {
37 resp.headers_mut()
38 .insert(http::header::CACHE_CONTROL, value);
39 }
40 }
41 }
42
43 resp.headers_mut().extend(self.0.http_headers());
44 resp
45 }
46}