Skip to content

Commit 7a76be7

Browse files
samuelaLegNeato
andauthored
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 <[email protected]>
1 parent 09637fb commit 7a76be7

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

juniper_hyper/examples/hyper_server.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::Arc;
1+
use std::{convert::Infallible, sync::Arc};
22

33
use hyper::{
44
service::{make_service_fn, service_fn},
@@ -31,17 +31,17 @@ async fn main() {
3131
let root_node = root_node.clone();
3232
let ctx = ctx.clone();
3333
async {
34-
match (req.method(), req.uri().path()) {
34+
Ok::<_, Infallible>(match (req.method(), req.uri().path()) {
3535
(&Method::GET, "/") => juniper_hyper::graphiql("/graphql", None).await,
3636
(&Method::GET, "/graphql") | (&Method::POST, "/graphql") => {
3737
juniper_hyper::graphql(root_node, ctx, req).await
3838
}
3939
_ => {
4040
let mut response = Response::new(Body::empty());
4141
*response.status_mut() = StatusCode::NOT_FOUND;
42-
Ok(response)
42+
response
4343
}
44-
}
44+
})
4545
}
4646
}))
4747
}

juniper_hyper/src/lib.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub async fn graphql_sync<CtxT, QueryT, MutationT, SubscriptionT, S>(
1717
root_node: Arc<RootNode<'static, QueryT, MutationT, SubscriptionT, S>>,
1818
context: Arc<CtxT>,
1919
req: Request<Body>,
20-
) -> Result<Response<Body>, hyper::Error>
20+
) -> Response<Body>
2121
where
2222
QueryT: GraphQLType<S, Context = CtxT>,
2323
QueryT::TypeInfo: Sync,
@@ -28,17 +28,17 @@ where
2828
CtxT: Sync,
2929
S: ScalarValue + Send + Sync,
3030
{
31-
Ok(match parse_req(req).await {
31+
match parse_req(req).await {
3232
Ok(req) => execute_request_sync(root_node, context, req).await,
3333
Err(resp) => resp,
34-
})
34+
}
3535
}
3636

3737
pub async fn graphql<CtxT, QueryT, MutationT, SubscriptionT, S>(
3838
root_node: Arc<RootNode<'static, QueryT, MutationT, SubscriptionT, S>>,
3939
context: Arc<CtxT>,
4040
req: Request<Body>,
41-
) -> Result<Response<Body>, hyper::Error>
41+
) -> Response<Body>
4242
where
4343
QueryT: GraphQLTypeAsync<S, Context = CtxT>,
4444
QueryT::TypeInfo: Sync,
@@ -49,10 +49,10 @@ where
4949
CtxT: Sync,
5050
S: ScalarValue + Send + Sync,
5151
{
52-
Ok(match parse_req(req).await {
52+
match parse_req(req).await {
5353
Ok(req) => execute_request(root_node, context, req).await,
5454
Err(resp) => resp,
55-
})
55+
}
5656
}
5757

5858
async fn parse_req<S: ScalarValue>(
@@ -121,26 +121,26 @@ async fn parse_post_graphql_req<S: ScalarValue>(
121121
pub async fn graphiql(
122122
graphql_endpoint: &str,
123123
subscriptions_endpoint: Option<&str>,
124-
) -> Result<Response<Body>, hyper::Error> {
124+
) -> Response<Body> {
125125
let mut resp = new_html_response(StatusCode::OK);
126126
// XXX: is the call to graphiql_source blocking?
127127
*resp.body_mut() = Body::from(juniper::http::graphiql::graphiql_source(
128128
graphql_endpoint,
129129
subscriptions_endpoint,
130130
));
131-
Ok(resp)
131+
resp
132132
}
133133

134134
pub async fn playground(
135135
graphql_endpoint: &str,
136136
subscriptions_endpoint: Option<&str>,
137-
) -> Result<Response<Body>, hyper::Error> {
137+
) -> Response<Body> {
138138
let mut resp = new_html_response(StatusCode::OK);
139139
*resp.body_mut() = Body::from(juniper::http::playground::playground_source(
140140
graphql_endpoint,
141141
subscriptions_endpoint,
142142
));
143-
Ok(resp)
143+
resp
144144
}
145145

146146
fn render_error(err: GraphQLRequestError) -> Response<Body> {
@@ -321,7 +321,7 @@ mod tests {
321321
EmptyMutation, EmptySubscription, RootNode,
322322
};
323323
use reqwest::{self, blocking::Response as ReqwestResponse};
324-
use std::{net::SocketAddr, sync::Arc, thread, time::Duration};
324+
use std::{convert::Infallible, net::SocketAddr, sync::Arc, thread, time::Duration};
325325

326326
struct TestHyperIntegration {
327327
port: u16,
@@ -404,7 +404,7 @@ mod tests {
404404
}
405405
};
406406
async move {
407-
if matches {
407+
Ok::<_, Infallible>(if matches {
408408
if is_sync {
409409
super::graphql_sync(root_node, ctx, req).await
410410
} else {
@@ -413,8 +413,8 @@ mod tests {
413413
} else {
414414
let mut resp = Response::new(Body::empty());
415415
*resp.status_mut() = StatusCode::NOT_FOUND;
416-
Ok(resp)
417-
}
416+
resp
417+
})
418418
}
419419
}))
420420
}

0 commit comments

Comments
 (0)