[async-graphql-poem] Export the HTTP headers in the Context
.
This commit is contained in:
parent
d62aca8052
commit
64328444f1
2
examples
2
examples
@ -1 +1 @@
|
|||||||
Subproject commit 58e92b7ffec6aee23b59584331dbe4e101aa049f
|
Subproject commit b3915aa777e434e9049c4d4a2448ebbd2524f6b1
|
@ -14,7 +14,7 @@ categories = ["network-programming", "asynchronous"]
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
async-graphql = { path = "../..", version = "=2.11.0" }
|
async-graphql = { path = "../..", version = "=2.11.0" }
|
||||||
|
|
||||||
poem = { version = "1.0.13", features = ["websocket"] }
|
poem = { version = "1.0.19", features = ["websocket"] }
|
||||||
futures-util = { version = "0.3.13", default-features = false }
|
futures-util = { version = "0.3.13", default-features = false }
|
||||||
serde_json = "1.0.66"
|
serde_json = "1.0.66"
|
||||||
tokio-util = { version = "0.6.7", features = ["compat"] }
|
tokio-util = { version = "0.6.7", features = ["compat"] }
|
||||||
|
@ -4,8 +4,10 @@
|
|||||||
|
|
||||||
mod extractor;
|
mod extractor;
|
||||||
mod query;
|
mod query;
|
||||||
|
mod response;
|
||||||
mod subscription;
|
mod subscription;
|
||||||
|
|
||||||
pub use extractor::{GraphQLBatchRequest, GraphQLRequest};
|
pub use extractor::{GraphQLBatchRequest, GraphQLRequest};
|
||||||
pub use query::GraphQL;
|
pub use query::GraphQL;
|
||||||
|
pub use response::{GraphQLBatchResponse, GraphQLResponse};
|
||||||
pub use subscription::GraphQLSubscription;
|
pub use subscription::GraphQLSubscription;
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
use async_graphql::{BatchResponse as GraphQLBatchResponse, ObjectType, Schema, SubscriptionType};
|
use async_graphql::{ObjectType, Schema, SubscriptionType};
|
||||||
use poem::web::Json;
|
|
||||||
use poem::{async_trait, Endpoint, FromRequest, Request, Result};
|
use poem::{async_trait, Endpoint, FromRequest, Request, Result};
|
||||||
|
|
||||||
use crate::GraphQLBatchRequest;
|
use crate::{GraphQLBatchRequest, GraphQLBatchResponse};
|
||||||
|
|
||||||
/// A GraphQL query endpoint.
|
/// A GraphQL query endpoint.
|
||||||
///
|
///
|
||||||
@ -45,11 +44,11 @@ where
|
|||||||
Mutation: ObjectType + 'static,
|
Mutation: ObjectType + 'static,
|
||||||
Subscription: SubscriptionType + 'static,
|
Subscription: SubscriptionType + 'static,
|
||||||
{
|
{
|
||||||
type Output = Result<Json<GraphQLBatchResponse>>;
|
type Output = Result<GraphQLBatchResponse>;
|
||||||
|
|
||||||
async fn call(&self, req: Request) -> Self::Output {
|
async fn call(&self, req: Request) -> Self::Output {
|
||||||
let (req, mut body) = req.split();
|
let (req, mut body) = req.split();
|
||||||
let req = GraphQLBatchRequest::from_request(&req, &mut body).await?;
|
let req = GraphQLBatchRequest::from_request(&req, &mut body).await?;
|
||||||
Ok(Json(self.schema.execute_batch(req.0).await))
|
Ok(GraphQLBatchResponse(self.schema.execute_batch(req.0).await))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
50
integrations/poem/src/response.rs
Normal file
50
integrations/poem/src/response.rs
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
use poem::http::header::HeaderName;
|
||||||
|
use poem::web::Json;
|
||||||
|
use poem::{IntoResponse, Response};
|
||||||
|
|
||||||
|
/// Response for `async_graphql::Request`.
|
||||||
|
pub struct GraphQLResponse(pub async_graphql::Response);
|
||||||
|
|
||||||
|
impl From<async_graphql::Response> for GraphQLResponse {
|
||||||
|
fn from(resp: async_graphql::Response) -> Self {
|
||||||
|
Self(resp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoResponse for GraphQLResponse {
|
||||||
|
fn into_response(self) -> Response {
|
||||||
|
GraphQLBatchResponse(self.0.into()).into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Response for `async_graphql::BatchRequest`.
|
||||||
|
pub struct GraphQLBatchResponse(pub async_graphql::BatchResponse);
|
||||||
|
|
||||||
|
impl From<async_graphql::BatchResponse> for GraphQLBatchResponse {
|
||||||
|
fn from(resp: async_graphql::BatchResponse) -> Self {
|
||||||
|
Self(resp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoResponse for GraphQLBatchResponse {
|
||||||
|
fn into_response(self) -> Response {
|
||||||
|
let mut resp = Json(&self.0).into_response();
|
||||||
|
|
||||||
|
if self.0.is_ok() {
|
||||||
|
if let Some(cache_control) = self.0.cache_control().value() {
|
||||||
|
if let Ok(value) = cache_control.try_into() {
|
||||||
|
resp.headers_mut().insert("cache-control", value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (name, value) in self.0.http_headers() {
|
||||||
|
if let (Ok(name), Ok(value)) = (TryInto::<HeaderName>::try_into(name), value.try_into())
|
||||||
|
{
|
||||||
|
resp.headers_mut().append(name, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resp
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user