Make juniper_hyper functions return Response<Body> where applicable (#889)

* graphql, graphiql, playground return Response<Body>

* And don't forget about `graphql_sync`!

* fix juniper_hyper tests

Co-authored-by: Christian Legnitto <LegNeato@users.noreply.github.com>
This commit is contained in:
Samuel Ainsworth 2021-04-01 21:08:01 -07:00 committed by GitHub
parent 09637fba94
commit 7a76be7407
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 18 deletions

View file

@ -1,4 +1,4 @@
use std::sync::Arc; use std::{convert::Infallible, sync::Arc};
use hyper::{ use hyper::{
service::{make_service_fn, service_fn}, service::{make_service_fn, service_fn},
@ -31,7 +31,7 @@ async fn main() {
let root_node = root_node.clone(); let root_node = root_node.clone();
let ctx = ctx.clone(); let ctx = ctx.clone();
async { 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, "/") => juniper_hyper::graphiql("/graphql", None).await,
(&Method::GET, "/graphql") | (&Method::POST, "/graphql") => { (&Method::GET, "/graphql") | (&Method::POST, "/graphql") => {
juniper_hyper::graphql(root_node, ctx, req).await juniper_hyper::graphql(root_node, ctx, req).await
@ -39,9 +39,9 @@ async fn main() {
_ => { _ => {
let mut response = Response::new(Body::empty()); let mut response = Response::new(Body::empty());
*response.status_mut() = StatusCode::NOT_FOUND; *response.status_mut() = StatusCode::NOT_FOUND;
Ok(response) response
} }
} })
} }
})) }))
} }

View file

@ -17,7 +17,7 @@ pub async fn graphql_sync<CtxT, QueryT, MutationT, SubscriptionT, S>(
root_node: Arc<RootNode<'static, QueryT, MutationT, SubscriptionT, S>>, root_node: Arc<RootNode<'static, QueryT, MutationT, SubscriptionT, S>>,
context: Arc<CtxT>, context: Arc<CtxT>,
req: Request<Body>, req: Request<Body>,
) -> Result<Response<Body>, hyper::Error> ) -> Response<Body>
where where
QueryT: GraphQLType<S, Context = CtxT>, QueryT: GraphQLType<S, Context = CtxT>,
QueryT::TypeInfo: Sync, QueryT::TypeInfo: Sync,
@ -28,17 +28,17 @@ where
CtxT: Sync, CtxT: Sync,
S: ScalarValue + Send + 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, Ok(req) => execute_request_sync(root_node, context, req).await,
Err(resp) => resp, Err(resp) => resp,
}) }
} }
pub async fn graphql<CtxT, QueryT, MutationT, SubscriptionT, S>( pub async fn graphql<CtxT, QueryT, MutationT, SubscriptionT, S>(
root_node: Arc<RootNode<'static, QueryT, MutationT, SubscriptionT, S>>, root_node: Arc<RootNode<'static, QueryT, MutationT, SubscriptionT, S>>,
context: Arc<CtxT>, context: Arc<CtxT>,
req: Request<Body>, req: Request<Body>,
) -> Result<Response<Body>, hyper::Error> ) -> Response<Body>
where where
QueryT: GraphQLTypeAsync<S, Context = CtxT>, QueryT: GraphQLTypeAsync<S, Context = CtxT>,
QueryT::TypeInfo: Sync, QueryT::TypeInfo: Sync,
@ -49,10 +49,10 @@ where
CtxT: Sync, CtxT: Sync,
S: ScalarValue + Send + 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, Ok(req) => execute_request(root_node, context, req).await,
Err(resp) => resp, Err(resp) => resp,
}) }
} }
async fn parse_req<S: ScalarValue>( async fn parse_req<S: ScalarValue>(
@ -121,26 +121,26 @@ async fn parse_post_graphql_req<S: ScalarValue>(
pub async fn graphiql( pub async fn graphiql(
graphql_endpoint: &str, graphql_endpoint: &str,
subscriptions_endpoint: Option<&str>, subscriptions_endpoint: Option<&str>,
) -> Result<Response<Body>, hyper::Error> { ) -> Response<Body> {
let mut resp = new_html_response(StatusCode::OK); let mut resp = new_html_response(StatusCode::OK);
// XXX: is the call to graphiql_source blocking? // XXX: is the call to graphiql_source blocking?
*resp.body_mut() = Body::from(juniper::http::graphiql::graphiql_source( *resp.body_mut() = Body::from(juniper::http::graphiql::graphiql_source(
graphql_endpoint, graphql_endpoint,
subscriptions_endpoint, subscriptions_endpoint,
)); ));
Ok(resp) resp
} }
pub async fn playground( pub async fn playground(
graphql_endpoint: &str, graphql_endpoint: &str,
subscriptions_endpoint: Option<&str>, subscriptions_endpoint: Option<&str>,
) -> Result<Response<Body>, hyper::Error> { ) -> Response<Body> {
let mut resp = new_html_response(StatusCode::OK); let mut resp = new_html_response(StatusCode::OK);
*resp.body_mut() = Body::from(juniper::http::playground::playground_source( *resp.body_mut() = Body::from(juniper::http::playground::playground_source(
graphql_endpoint, graphql_endpoint,
subscriptions_endpoint, subscriptions_endpoint,
)); ));
Ok(resp) resp
} }
fn render_error(err: GraphQLRequestError) -> Response<Body> { fn render_error(err: GraphQLRequestError) -> Response<Body> {
@ -321,7 +321,7 @@ mod tests {
EmptyMutation, EmptySubscription, RootNode, EmptyMutation, EmptySubscription, RootNode,
}; };
use reqwest::{self, blocking::Response as ReqwestResponse}; 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 { struct TestHyperIntegration {
port: u16, port: u16,
@ -404,7 +404,7 @@ mod tests {
} }
}; };
async move { async move {
if matches { Ok::<_, Infallible>(if matches {
if is_sync { if is_sync {
super::graphql_sync(root_node, ctx, req).await super::graphql_sync(root_node, ctx, req).await
} else { } else {
@ -413,8 +413,8 @@ mod tests {
} else { } else {
let mut resp = Response::new(Body::empty()); let mut resp = Response::new(Body::empty());
*resp.status_mut() = StatusCode::NOT_FOUND; *resp.status_mut() = StatusCode::NOT_FOUND;
Ok(resp) resp
} })
} }
})) }))
} }