Update syntax for Axum 0.2.0

This commit is contained in:
Shane Sveller 2021-08-21 16:43:20 -05:00
parent 34e99d1642
commit 2fe076ed00
3 changed files with 26 additions and 14 deletions

View File

@ -1,11 +1,13 @@
use std::fmt::Display;
use std::io::ErrorKind; use std::io::ErrorKind;
use async_graphql::futures_util::TryStreamExt; use async_graphql::futures_util::TryStreamExt;
use async_graphql::http::MultipartOptions; use async_graphql::http::MultipartOptions;
use async_graphql::ParseRequestError; use async_graphql::ParseRequestError;
use axum::extract::{BodyStream, FromRequest, RequestParts}; use axum::{
use bytes::Buf; extract::{BodyStream, FromRequest, RequestParts},
BoxError,
};
use bytes::Bytes;
use http::Method; use http::Method;
use tokio_util::compat::TokioAsyncReadCompatExt; use tokio_util::compat::TokioAsyncReadCompatExt;
@ -30,6 +32,9 @@ pub mod rejection {
pub struct GraphQLRejection(pub ParseRequestError); pub struct GraphQLRejection(pub ParseRequestError);
impl IntoResponse for GraphQLRejection { 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<Body> {
todo!() todo!()
} }
@ -46,8 +51,8 @@ pub mod rejection {
impl<B> FromRequest<B> for GraphQLRequest impl<B> FromRequest<B> for GraphQLRequest
where where
B: http_body::Body + Unpin + Send + Sync + 'static, B: http_body::Body + Unpin + Send + Sync + 'static,
B::Data: Buf + Send, B::Data: Into<Bytes>,
B::Error: Display + Send, B::Error: Into<BoxError>,
{ {
type Rejection = rejection::GraphQLRejection; type Rejection = rejection::GraphQLRejection;
@ -76,13 +81,13 @@ impl GraphQLBatchRequest {
impl<B> FromRequest<B> for GraphQLBatchRequest impl<B> FromRequest<B> for GraphQLBatchRequest
where where
B: http_body::Body + Unpin + Send + Sync + 'static, B: http_body::Body + Unpin + Send + Sync + 'static,
B::Data: Buf + Send, B::Data: Into<Bytes>,
B::Error: Display + Send, B::Error: Into<BoxError>,
{ {
type Rejection = rejection::GraphQLRejection; type Rejection = rejection::GraphQLRejection;
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> { async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
if let (Some(&Method::GET), Some(uri)) = (req.method(), req.uri()) { if let (&Method::GET, uri) = (req.method(), req.uri()) {
let res = serde_urlencoded::from_str(uri.query().unwrap_or_default()).map_err(|err| { let res = serde_urlencoded::from_str(uri.query().unwrap_or_default()).map_err(|err| {
ParseRequestError::Io(std::io::Error::new( ParseRequestError::Io(std::io::Error::new(
ErrorKind::Other, ErrorKind::Other,

View File

@ -1,7 +1,7 @@
use std::convert::TryFrom; use std::convert::TryFrom;
use axum::body::Body; use axum::body::Body;
use axum::prelude::response::IntoResponse; use axum::response::IntoResponse;
use headers::HeaderName; use headers::HeaderName;
use http::{HeaderValue, Response}; use http::{HeaderValue, Response};
@ -24,6 +24,9 @@ impl From<async_graphql::BatchResponse> for GraphQLResponse {
} }
impl IntoResponse for GraphQLResponse { impl IntoResponse for GraphQLResponse {
type Body = Body;
type BodyError = <Self::Body as axum::body::HttpBody>::Error;
fn into_response(self) -> Response<Body> { fn into_response(self) -> Response<Body> {
let mut resp = Response::new(serde_json::to_string(&self.0).unwrap().into()); let mut resp = Response::new(serde_json::to_string(&self.0).unwrap().into());
resp.headers_mut().insert( resp.headers_mut().insert(

View File

@ -1,8 +1,9 @@
use std::borrow::Cow;
use std::future::Future; use std::future::Future;
use async_graphql::http::{WebSocketProtocols, WsMessage}; use async_graphql::http::{WebSocketProtocols, WsMessage};
use async_graphql::{Data, ObjectType, Result, Schema, SubscriptionType}; use async_graphql::{Data, ObjectType, Result, Schema, SubscriptionType};
use axum::ws::{Message, WebSocket}; use axum::extract::ws::{CloseFrame, Message, WebSocket};
use futures_util::{future, SinkExt, StreamExt}; use futures_util::{future, SinkExt, StreamExt};
use headers::{Header, HeaderName, HeaderValue}; use headers::{Header, HeaderName, HeaderValue};
@ -77,19 +78,22 @@ pub async fn graphql_subscription_with_data<Query, Mutation, Subscription, F, R>
.take_while(|res| future::ready(res.is_ok())) .take_while(|res| future::ready(res.is_ok()))
.map(Result::unwrap) .map(Result::unwrap)
.filter_map(|msg| { .filter_map(|msg| {
if msg.is_text() || msg.is_binary() { if let Message::Text(_) | Message::Binary(_) = msg {
future::ready(Some(msg)) future::ready(Some(msg))
} else { } else {
future::ready(None) future::ready(None)
} }
}) })
.map(Message::into_bytes); .map(Message::into_data);
let mut stream = let mut stream =
async_graphql::http::WebSocket::with_data(schema, input, initializer, protocol.0).map( async_graphql::http::WebSocket::with_data(schema, input, initializer, protocol.0).map(
|msg| match msg { |msg| match msg {
WsMessage::Text(text) => Message::text(text), WsMessage::Text(text) => Message::Text(text),
WsMessage::Close(code, status) => Message::close_with(code, status), WsMessage::Close(code, status) => Message::Close(Some(CloseFrame {
code,
reason: Cow::from(status),
})),
}, },
); );