Skip to content

Commit

Permalink
Change usage of FromRequest to FromRequestParts where needed
Browse files Browse the repository at this point in the history
In axum 0.6.* `FromRequest` always consumes the request's body. `FromRequestParts` was
also introduced for when you don't need to consume the body.

You can read about it more in axum's changelog.
  • Loading branch information
tomerze committed Oct 9, 2022
1 parent 9e09918 commit 9ff549f
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions integrations/axum/src/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use axum::{
body::{boxed, BoxBody, HttpBody},
extract::{
ws::{CloseFrame, Message},
FromRequest, RequestParts, WebSocketUpgrade,
FromRequestParts, WebSocketUpgrade,
},
http::{self, Request, Response, StatusCode},
http::{self, Request, Response, StatusCode, request::Parts},
response::IntoResponse,
Error,
};
Expand All @@ -30,11 +30,14 @@ use tower_service::Service;
pub struct GraphQLProtocol(WebSocketProtocols);

#[async_trait::async_trait]
impl<B: Send> FromRequest<B> for GraphQLProtocol {
impl<S> FromRequestParts<S> for GraphQLProtocol
where
S: Send + Sync
{
type Rejection = StatusCode;

async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
req.headers()
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
parts.headers
.get(http::header::SEC_WEBSOCKET_PROTOCOL)
.and_then(|value| value.to_str().ok())
.and_then(|protocols| {
Expand Down Expand Up @@ -97,12 +100,13 @@ where
let schema = self.schema.clone();

Box::pin(async move {
let mut parts = RequestParts::new(req);
let protocol = match GraphQLProtocol::from_request(&mut parts).await {
let (mut parts, _body) = req.into_parts();

let protocol = match GraphQLProtocol::from_request_parts(&mut parts, &()).await {
Ok(protocol) => protocol,
Err(err) => return Ok(err.into_response().map(boxed)),
};
let upgrade = match WebSocketUpgrade::from_request(&mut parts).await {
let upgrade = match WebSocketUpgrade::from_request_parts(&mut parts, &()).await {
Ok(protocol) => protocol,
Err(err) => return Ok(err.into_response().map(boxed)),
};
Expand Down

0 comments on commit 9ff549f

Please sign in to comment.