Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom error type in axum request extractor #945

Merged
merged 3 commits into from Jun 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 22 additions & 11 deletions integrations/axum/src/extract.rs
@@ -1,19 +1,23 @@
use std::io::ErrorKind;
use std::{io::ErrorKind, marker::PhantomData};

use async_graphql::{futures_util::TryStreamExt, http::MultipartOptions, ParseRequestError};
use axum::{
extract::{BodyStream, FromRequest, RequestParts},
http,
http::Method,
response::IntoResponse,
BoxError,
};
use bytes::Bytes;
use tokio_util::compat::TokioAsyncReadCompatExt;

/// Extractor for GraphQL request.
pub struct GraphQLRequest(pub async_graphql::Request);
pub struct GraphQLRequest<R = rejection::GraphQLRejection>(
pub async_graphql::Request,
PhantomData<R>,
);

impl GraphQLRequest {
impl<R> GraphQLRequest<R> {
/// Unwraps the value to `async_graphql::Request`.
#[must_use]
pub fn into_inner(self) -> async_graphql::Request {
Expand Down Expand Up @@ -57,28 +61,33 @@ pub mod rejection {
}

#[async_trait::async_trait]
impl<B> FromRequest<B> for GraphQLRequest
impl<B, R> FromRequest<B> for GraphQLRequest<R>
where
B: http_body::Body + Unpin + Send + Sync + 'static,
B::Data: Into<Bytes>,
B::Error: Into<BoxError>,
R: IntoResponse + From<ParseRequestError>,
{
type Rejection = rejection::GraphQLRejection;
type Rejection = R;

async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
Ok(GraphQLRequest(
GraphQLBatchRequest::from_request(req)
GraphQLBatchRequest::<R>::from_request(req)
.await?
.0
.into_single()?,
PhantomData,
))
}
}

/// Extractor for GraphQL batch request.
pub struct GraphQLBatchRequest(pub async_graphql::BatchRequest);
pub struct GraphQLBatchRequest<R = rejection::GraphQLRejection>(
pub async_graphql::BatchRequest,
PhantomData<R>,
);

impl GraphQLBatchRequest {
impl<R> GraphQLBatchRequest<R> {
/// Unwraps the value to `async_graphql::BatchRequest`.
#[must_use]
pub fn into_inner(self) -> async_graphql::BatchRequest {
Expand All @@ -87,13 +96,14 @@ impl GraphQLBatchRequest {
}

#[async_trait::async_trait]
impl<B> FromRequest<B> for GraphQLBatchRequest
impl<B, R> FromRequest<B> for GraphQLBatchRequest<R>
where
B: http_body::Body + Unpin + Send + Sync + 'static,
B::Data: Into<Bytes>,
B::Error: Into<BoxError>,
R: IntoResponse + From<ParseRequestError>,
{
type Rejection = rejection::GraphQLRejection;
type Rejection = R;

async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
if let (&Method::GET, uri) = (req.method(), req.uri()) {
Expand All @@ -103,7 +113,7 @@ where
format!("failed to parse graphql request from uri query: {}", err),
))
});
Ok(Self(async_graphql::BatchRequest::Single(res?)))
Ok(Self(async_graphql::BatchRequest::Single(res?), PhantomData))
} else {
let content_type = req
.headers()
Expand All @@ -127,6 +137,7 @@ where
MultipartOptions::default(),
)
.await?,
PhantomData,
))
}
}
Expand Down