Merge pull request #742 from daly4/update-to-axum-v0.4

updated axum integration to axum v0.4
This commit is contained in:
Sunli 2021-12-08 13:41:35 +08:00 committed by GitHub
commit a9c96adbd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 19 deletions

View File

@ -32,7 +32,7 @@ async-graphql-parser = { path = "parser", version = "3.0.13" }
async-stream = "0.3.0"
async-trait = "0.1.48"
fnv = "1.0.7"
futures-util = { version = "0.3.13", default-features = false, features = ["io", "sink"] }
futures-util = { version = "0.3.18", default-features = false, features = ["io", "sink"] }
indexmap = "1.6.2"
once_cell = "1.7.2"
pin-project-lite = "0.2.6"

View File

@ -15,7 +15,7 @@ categories = ["network-programming", "asynchronous"]
async-graphql = { path = "../..", version = "3.0.13" }
async-trait = "0.1.51"
axum = { version = "0.3.2", features = ["ws", "headers"] }
axum = { version = "0.4", features = ["ws", "headers"] }
bytes = "1.0.1"
http-body = "0.4.2"
serde_json = "1.0.66"

View File

@ -25,7 +25,7 @@ impl GraphQLRequest {
/// Rejection response types.
pub mod rejection {
use async_graphql::ParseRequestError;
use axum::body::Body;
use axum::body::{boxed, Body, BoxBody};
use axum::http;
use axum::http::StatusCode;
use axum::response::IntoResponse;
@ -34,18 +34,15 @@ pub mod rejection {
pub struct GraphQLRejection(pub ParseRequestError);
impl IntoResponse for GraphQLRejection {
type Body = axum::body::Body;
type BodyError = <Self::Body as axum::body::HttpBody>::Error;
fn into_response(self) -> http::Response<Body> {
fn into_response(self) -> http::Response<BoxBody> {
match self.0 {
ParseRequestError::PayloadTooLarge => http::Response::builder()
.status(StatusCode::PAYLOAD_TOO_LARGE)
.body(Body::empty())
.body(boxed(Body::empty()))
.unwrap(),
bad_request => http::Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(format!("{:?}", bad_request)))
.body(boxed(Body::from(format!("{:?}", bad_request))))
.unwrap(),
}
}

View File

@ -1,4 +1,4 @@
use axum::body::Body;
use axum::body::{boxed, Body, BoxBody};
use axum::http;
use axum::http::header::HeaderName;
use axum::http::{HeaderValue, Response};
@ -23,11 +23,9 @@ impl From<async_graphql::BatchResponse> for GraphQLResponse {
}
impl IntoResponse for GraphQLResponse {
type Body = Body;
type BodyError = <Self::Body as axum::body::HttpBody>::Error;
fn into_response(self) -> Response<Body> {
let mut resp = Response::new(serde_json::to_string(&self.0).unwrap().into());
fn into_response(self) -> Response<BoxBody> {
let body: Body = serde_json::to_string(&self.0).unwrap().into();
let mut resp = Response::new(boxed(body));
resp.headers_mut().insert(
http::header::CONTENT_TYPE,
HeaderValue::from_static("application/json"),

View File

@ -6,7 +6,7 @@ use std::str::FromStr;
use async_graphql::futures_util::task::{Context, Poll};
use async_graphql::http::{WebSocketProtocols, WsMessage, ALL_WEBSOCKET_PROTOCOLS};
use async_graphql::{Data, ObjectType, Result, Schema, SubscriptionType};
use axum::body::{box_body, BoxBody, HttpBody};
use axum::body::{boxed, BoxBody, HttpBody};
use axum::extract::ws::{CloseFrame, Message};
use axum::extract::{FromRequest, RequestParts, WebSocketUpgrade};
use axum::http::{self, Request, Response, StatusCode};
@ -94,11 +94,11 @@ where
let mut parts = RequestParts::new(req);
let protocol = match GraphQLProtocol::from_request(&mut parts).await {
Ok(protocol) => protocol,
Err(err) => return Ok(err.into_response().map(box_body)),
Err(err) => return Ok(err.into_response().map(boxed)),
};
let upgrade = match WebSocketUpgrade::from_request(&mut parts).await {
Ok(protocol) => protocol,
Err(err) => return Ok(err.into_response().map(box_body)),
Err(err) => return Ok(err.into_response().map(boxed)),
};
let schema = schema.clone();
@ -106,7 +106,7 @@ where
let resp = upgrade
.protocols(ALL_WEBSOCKET_PROTOCOLS)
.on_upgrade(move |stream| GraphQLWebSocket::new(stream, schema, protocol).serve());
Ok(resp.into_response().map(box_body))
Ok(resp.into_response().map(boxed))
})
}
}