Skip to content

Commit

Permalink
Make juniper_hyper functions return Response<Body> where applicable (
Browse files Browse the repository at this point in the history
…graphql-rust#889)

* graphql, graphiql, playground return Response<Body>

* And don't forget about `graphql_sync`!

* fix juniper_hyper tests

Co-authored-by: Christian Legnitto <[email protected]>
  • Loading branch information
samuela and LegNeato authored Apr 2, 2021
1 parent 09637fb commit 7a76be7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
8 changes: 4 additions & 4 deletions juniper_hyper/examples/hyper_server.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{convert::Infallible, sync::Arc};

use hyper::{
service::{make_service_fn, service_fn},
Expand Down Expand Up @@ -31,17 +31,17 @@ 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
}
_ => {
let mut response = Response::new(Body::empty());
*response.status_mut() = StatusCode::NOT_FOUND;
Ok(response)
response
}
}
})
}
}))
}
Expand Down
28 changes: 14 additions & 14 deletions juniper_hyper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub async fn graphql_sync<CtxT, QueryT, MutationT, SubscriptionT, S>(
root_node: Arc<RootNode<'static, QueryT, MutationT, SubscriptionT, S>>,
context: Arc<CtxT>,
req: Request<Body>,
) -> Result<Response<Body>, hyper::Error>
) -> Response<Body>
where
QueryT: GraphQLType<S, Context = CtxT>,
QueryT::TypeInfo: Sync,
Expand All @@ -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<CtxT, QueryT, MutationT, SubscriptionT, S>(
root_node: Arc<RootNode<'static, QueryT, MutationT, SubscriptionT, S>>,
context: Arc<CtxT>,
req: Request<Body>,
) -> Result<Response<Body>, hyper::Error>
) -> Response<Body>
where
QueryT: GraphQLTypeAsync<S, Context = CtxT>,
QueryT::TypeInfo: Sync,
Expand All @@ -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<S: ScalarValue>(
Expand Down Expand Up @@ -121,26 +121,26 @@ async fn parse_post_graphql_req<S: ScalarValue>(
pub async fn graphiql(
graphql_endpoint: &str,
subscriptions_endpoint: Option<&str>,
) -> Result<Response<Body>, hyper::Error> {
) -> Response<Body> {
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<Response<Body>, hyper::Error> {
) -> Response<Body> {
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<Body> {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -413,8 +413,8 @@ mod tests {
} else {
let mut resp = Response::new(Body::empty());
*resp.status_mut() = StatusCode::NOT_FOUND;
Ok(resp)
}
resp
})
}
}))
}
Expand Down

0 comments on commit 7a76be7

Please sign in to comment.