Update async-graphql-warp 2

This commit is contained in:
Sunli 2021-11-12 21:48:18 +08:00
parent 2e27f1bfbf
commit 64c1028c5f
4 changed files with 27 additions and 26 deletions

View File

@ -8,7 +8,7 @@ use warp::hyper::header::HeaderName;
use warp::reply::Response as WarpResponse; use warp::reply::Response as WarpResponse;
use warp::{Buf, Filter, Rejection, Reply}; use warp::{Buf, Filter, Rejection, Reply};
use crate::BadRequest; use crate::GraphQLBadRequest;
/// GraphQL batch request filter /// GraphQL batch request filter
/// ///
@ -53,7 +53,7 @@ where
opts, opts,
) )
.await .await
.map_err(|e| warp::reject::custom(BadRequest(e))) .map_err(|e| warp::reject::custom(GraphQLBadRequest(e)))
})) }))
.unify() .unify()
.map(move |res| (schema.clone(), res)) .map(move |res| (schema.clone(), res))
@ -61,15 +61,15 @@ where
/// Reply for `async_graphql::BatchRequest`. /// Reply for `async_graphql::BatchRequest`.
#[derive(Debug)] #[derive(Debug)]
pub struct BatchResponse(pub async_graphql::BatchResponse); pub struct GraphQLBatchResponse(pub async_graphql::BatchResponse);
impl From<async_graphql::BatchResponse> for BatchResponse { impl From<async_graphql::BatchResponse> for GraphQLBatchResponse {
fn from(resp: async_graphql::BatchResponse) -> Self { fn from(resp: async_graphql::BatchResponse) -> Self {
BatchResponse(resp) GraphQLBatchResponse(resp)
} }
} }
impl Reply for BatchResponse { impl Reply for GraphQLBatchResponse {
fn into_response(self) -> WarpResponse { fn into_response(self) -> WarpResponse {
let mut resp = warp::reply::with_header( let mut resp = warp::reply::with_header(
warp::reply::json(&self.0), warp::reply::json(&self.0),

View File

@ -12,9 +12,9 @@ use warp::Reply;
/// It's a wrapper of `async_graphql::ParseRequestError`. It is also a `Reply` - by default it just /// It's a wrapper of `async_graphql::ParseRequestError`. It is also a `Reply` - by default it just
/// returns a response containing the error message in plain text. /// returns a response containing the error message in plain text.
#[derive(Debug)] #[derive(Debug)]
pub struct BadRequest(pub ParseRequestError); pub struct GraphQLBadRequest(pub ParseRequestError);
impl BadRequest { impl GraphQLBadRequest {
/// Get the appropriate status code of the error. /// Get the appropriate status code of the error.
#[must_use] #[must_use]
pub fn status(&self) -> StatusCode { pub fn status(&self) -> StatusCode {
@ -25,21 +25,21 @@ impl BadRequest {
} }
} }
impl Display for BadRequest { impl Display for GraphQLBadRequest {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.0.fmt(f) self.0.fmt(f)
} }
} }
impl Error for BadRequest { impl Error for GraphQLBadRequest {
fn source(&self) -> Option<&(dyn Error + 'static)> { fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.0) Some(&self.0)
} }
} }
impl Reject for BadRequest {} impl Reject for GraphQLBadRequest {}
impl Reply for BadRequest { impl Reply for GraphQLBadRequest {
fn into_response(self) -> Response<Body> { fn into_response(self) -> Response<Body> {
Response::builder() Response::builder()
.status(self.status()) .status(self.status())
@ -48,13 +48,14 @@ impl Reply for BadRequest {
} }
} }
impl From<ParseRequestError> for BadRequest { impl From<ParseRequestError> for GraphQLBadRequest {
fn from(e: ParseRequestError) -> Self { fn from(e: ParseRequestError) -> Self {
Self(e) Self(e)
} }
} }
impl From<BadRequest> for ParseRequestError {
fn from(e: BadRequest) -> Self { impl From<GraphQLBadRequest> for ParseRequestError {
fn from(e: GraphQLBadRequest) -> Self {
e.0 e.0
} }
} }

View File

@ -9,7 +9,7 @@ mod error;
mod request; mod request;
mod subscription; mod subscription;
pub use batch_request::{graphql_batch, graphql_batch_opts, BatchResponse}; pub use batch_request::{graphql_batch, graphql_batch_opts, GraphQLBatchResponse};
pub use error::BadRequest; pub use error::GraphQLBadRequest;
pub use request::{graphql, graphql_opts, Response}; pub use request::{graphql, graphql_opts, GraphQLResponse};
pub use subscription::{graphql_protocol, graphql_subscription, GraphQLWebSocket}; pub use subscription::{graphql_protocol, graphql_subscription, GraphQLWebSocket};

View File

@ -3,7 +3,7 @@ use async_graphql::{BatchRequest, ObjectType, Request, Schema, SubscriptionType}
use warp::reply::Response as WarpResponse; use warp::reply::Response as WarpResponse;
use warp::{Filter, Rejection, Reply}; use warp::{Filter, Rejection, Reply};
use crate::{graphql_batch_opts, BadRequest, BatchResponse}; use crate::{graphql_batch_opts, GraphQLBadRequest, GraphQLBatchResponse};
/// GraphQL request filter /// GraphQL request filter
/// ///
@ -35,7 +35,7 @@ use crate::{graphql_batch_opts, BadRequest, BatchResponse};
/// let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription); /// let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
/// let filter = async_graphql_warp::graphql(schema) /// let filter = async_graphql_warp::graphql(schema)
/// .and_then(|(schema, request): (MySchema, async_graphql::Request)| async move { /// .and_then(|(schema, request): (MySchema, async_graphql::Request)| async move {
/// Ok::<_, Infallible>(async_graphql_warp::Response::from(schema.execute(request).await)) /// Ok::<_, Infallible>(async_graphql_warp::GraphQLResponse::from(schema.execute(request).await))
/// }); /// });
/// warp::serve(filter).run(([0, 0, 0, 0], 8000)).await; /// warp::serve(filter).run(([0, 0, 0, 0], 8000)).await;
/// }); /// });
@ -72,23 +72,23 @@ where
schema, schema,
batch batch
.into_single() .into_single()
.map_err(|e| warp::reject::custom(BadRequest(e)))?, .map_err(|e| warp::reject::custom(GraphQLBadRequest(e)))?,
)) ))
}) })
} }
/// Reply for `async_graphql::Request`. /// Reply for `async_graphql::Request`.
#[derive(Debug)] #[derive(Debug)]
pub struct Response(pub async_graphql::Response); pub struct GraphQLResponse(pub async_graphql::Response);
impl From<async_graphql::Response> for Response { impl From<async_graphql::Response> for GraphQLResponse {
fn from(resp: async_graphql::Response) -> Self { fn from(resp: async_graphql::Response) -> Self {
Response(resp) GraphQLResponse(resp)
} }
} }
impl Reply for Response { impl Reply for GraphQLResponse {
fn into_response(self) -> WarpResponse { fn into_response(self) -> WarpResponse {
BatchResponse(self.0.into()).into_response() GraphQLBatchResponse(self.0.into()).into_response()
} }
} }