From 7a76be7407d19b3a0bff3f2c2d32429de638d3fd Mon Sep 17 00:00:00 2001 From: Samuel Ainsworth Date: Thu, 1 Apr 2021 21:08:01 -0700 Subject: [PATCH] Make juniper_hyper functions return `Response` where applicable (#889) * graphql, graphiql, playground return Response * And don't forget about `graphql_sync`! * fix juniper_hyper tests Co-authored-by: Christian Legnitto --- juniper_hyper/examples/hyper_server.rs | 8 ++++---- juniper_hyper/src/lib.rs | 28 +++++++++++++------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/juniper_hyper/examples/hyper_server.rs b/juniper_hyper/examples/hyper_server.rs index 91222649..58488eb9 100644 --- a/juniper_hyper/examples/hyper_server.rs +++ b/juniper_hyper/examples/hyper_server.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::{convert::Infallible, sync::Arc}; use hyper::{ service::{make_service_fn, service_fn}, @@ -31,7 +31,7 @@ async fn main() { let root_node = root_node.clone(); let ctx = ctx.clone(); async { - match (req.method(), req.uri().path()) { + Ok::<_, Infallible>(match (req.method(), req.uri().path()) { (&Method::GET, "/") => juniper_hyper::graphiql("/graphql", None).await, (&Method::GET, "/graphql") | (&Method::POST, "/graphql") => { juniper_hyper::graphql(root_node, ctx, req).await @@ -39,9 +39,9 @@ async fn main() { _ => { let mut response = Response::new(Body::empty()); *response.status_mut() = StatusCode::NOT_FOUND; - Ok(response) + response } - } + }) } })) } diff --git a/juniper_hyper/src/lib.rs b/juniper_hyper/src/lib.rs index f42bc0f6..7e7364d4 100644 --- a/juniper_hyper/src/lib.rs +++ b/juniper_hyper/src/lib.rs @@ -17,7 +17,7 @@ pub async fn graphql_sync( root_node: Arc>, context: Arc, req: Request, -) -> Result, hyper::Error> +) -> Response where QueryT: GraphQLType, QueryT::TypeInfo: Sync, @@ -28,17 +28,17 @@ where CtxT: Sync, S: ScalarValue + Send + Sync, { - Ok(match parse_req(req).await { + match parse_req(req).await { Ok(req) => execute_request_sync(root_node, context, req).await, Err(resp) => resp, - }) + } } pub async fn graphql( root_node: Arc>, context: Arc, req: Request, -) -> Result, hyper::Error> +) -> Response where QueryT: GraphQLTypeAsync, QueryT::TypeInfo: Sync, @@ -49,10 +49,10 @@ where CtxT: Sync, S: ScalarValue + Send + Sync, { - Ok(match parse_req(req).await { + match parse_req(req).await { Ok(req) => execute_request(root_node, context, req).await, Err(resp) => resp, - }) + } } async fn parse_req( @@ -121,26 +121,26 @@ async fn parse_post_graphql_req( pub async fn graphiql( graphql_endpoint: &str, subscriptions_endpoint: Option<&str>, -) -> Result, hyper::Error> { +) -> Response { let mut resp = new_html_response(StatusCode::OK); // XXX: is the call to graphiql_source blocking? *resp.body_mut() = Body::from(juniper::http::graphiql::graphiql_source( graphql_endpoint, subscriptions_endpoint, )); - Ok(resp) + resp } pub async fn playground( graphql_endpoint: &str, subscriptions_endpoint: Option<&str>, -) -> Result, hyper::Error> { +) -> Response { let mut resp = new_html_response(StatusCode::OK); *resp.body_mut() = Body::from(juniper::http::playground::playground_source( graphql_endpoint, subscriptions_endpoint, )); - Ok(resp) + resp } fn render_error(err: GraphQLRequestError) -> Response { @@ -321,7 +321,7 @@ mod tests { EmptyMutation, EmptySubscription, RootNode, }; use reqwest::{self, blocking::Response as ReqwestResponse}; - use std::{net::SocketAddr, sync::Arc, thread, time::Duration}; + use std::{convert::Infallible, net::SocketAddr, sync::Arc, thread, time::Duration}; struct TestHyperIntegration { port: u16, @@ -404,7 +404,7 @@ mod tests { } }; async move { - if matches { + Ok::<_, Infallible>(if matches { if is_sync { super::graphql_sync(root_node, ctx, req).await } else { @@ -413,8 +413,8 @@ mod tests { } else { let mut resp = Response::new(Body::empty()); *resp.status_mut() = StatusCode::NOT_FOUND; - Ok(resp) - } + resp + }) } })) }