From e0ddeecf3c4318416b241d737c777f7e57c8dff6 Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Mon, 19 Sep 2022 17:59:33 +0200 Subject: [PATCH] Change body type of `juniper_hyper` functions from `Body` to `String` (#1101, #1096) --- juniper_hyper/CHANGELOG.md | 4 +++ juniper_hyper/examples/hyper_server.rs | 4 +-- juniper_hyper/src/lib.rs | 45 ++++++++++++-------------- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/juniper_hyper/CHANGELOG.md b/juniper_hyper/CHANGELOG.md index e0e722ea..435c134e 100644 --- a/juniper_hyper/CHANGELOG.md +++ b/juniper_hyper/CHANGELOG.md @@ -11,6 +11,10 @@ All user visible changes to `juniper_hyper` crate will be documented in this fil ### BC Breaks - Switched to 0.16 version of [`juniper` crate]. +- Changed return type of all functions from `Response` to `Response`. ([#1101], [#1096]) + +[#1096]: /../../issues/1096 +[#1101]: /../../pull/1101 diff --git a/juniper_hyper/examples/hyper_server.rs b/juniper_hyper/examples/hyper_server.rs index afa1d4e0..069e0c6f 100644 --- a/juniper_hyper/examples/hyper_server.rs +++ b/juniper_hyper/examples/hyper_server.rs @@ -3,7 +3,7 @@ use std::{convert::Infallible, sync::Arc}; use hyper::{ server::Server, service::{make_service_fn, service_fn}, - Body, Method, Response, StatusCode, + Method, Response, StatusCode, }; use juniper::{ tests::fixtures::starwars::schema::{Database, Query}, @@ -38,7 +38,7 @@ async fn main() { juniper_hyper::graphql(root_node, ctx, req).await } _ => { - let mut response = Response::new(Body::empty()); + let mut response = Response::new(String::new()); *response.status_mut() = StatusCode::NOT_FOUND; response } diff --git a/juniper_hyper/src/lib.rs b/juniper_hyper/src/lib.rs index eb8dafe3..4836a66c 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, -) -> Response +) -> Response where QueryT: GraphQLType, QueryT::TypeInfo: Sync, @@ -38,7 +38,7 @@ pub async fn graphql( root_node: Arc>, context: Arc, req: Request, -) -> Response +) -> Response where QueryT: GraphQLTypeAsync, QueryT::TypeInfo: Sync, @@ -57,7 +57,7 @@ where async fn parse_req( req: Request, -) -> Result, Response> { +) -> Result, Response> { match *req.method() { Method::GET => parse_get_req(req), Method::POST => { @@ -121,32 +121,27 @@ async fn parse_post_graphql_req( pub async fn graphiql( graphql_endpoint: &str, subscriptions_endpoint: Option<&str>, -) -> Response { +) -> 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, - )); + *resp.body_mut() = + juniper::http::graphiql::graphiql_source(graphql_endpoint, subscriptions_endpoint); resp } pub async fn playground( graphql_endpoint: &str, subscriptions_endpoint: Option<&str>, -) -> Response { +) -> Response { let mut resp = new_html_response(StatusCode::OK); - *resp.body_mut() = Body::from(juniper::http::playground::playground_source( - graphql_endpoint, - subscriptions_endpoint, - )); + *resp.body_mut() = + juniper::http::playground::playground_source(graphql_endpoint, subscriptions_endpoint); resp } -fn render_error(err: GraphQLRequestError) -> Response { - let message = err.to_string(); +fn render_error(err: GraphQLRequestError) -> Response { let mut resp = new_response(StatusCode::BAD_REQUEST); - *resp.body_mut() = Body::from(message); + *resp.body_mut() = err.to_string(); resp } @@ -154,7 +149,7 @@ async fn execute_request_sync( root_node: Arc>, context: Arc, request: GraphQLBatchRequest, -) -> Response +) -> Response where QueryT: GraphQLType, QueryT::TypeInfo: Sync, @@ -166,7 +161,7 @@ where S: ScalarValue + Send + Sync, { let res = request.execute_sync(&*root_node, &context); - let body = Body::from(serde_json::to_string_pretty(&res).unwrap()); + let body = serde_json::to_string_pretty(&res).unwrap(); let code = if res.is_ok() { StatusCode::OK } else { @@ -185,7 +180,7 @@ async fn execute_request( root_node: Arc>, context: Arc, request: GraphQLBatchRequest, -) -> Response +) -> Response where QueryT: GraphQLTypeAsync, QueryT::TypeInfo: Sync, @@ -197,7 +192,7 @@ where S: ScalarValue + Send + Sync, { let res = request.execute(&*root_node, &context).await; - let body = Body::from(serde_json::to_string_pretty(&res).unwrap()); + let body = serde_json::to_string_pretty(&res).unwrap(); let code = if res.is_ok() { StatusCode::OK } else { @@ -260,13 +255,13 @@ fn invalid_err(parameter_name: &str) -> GraphQLRequestError { )) } -fn new_response(code: StatusCode) -> Response { - let mut r = Response::new(Body::empty()); +fn new_response(code: StatusCode) -> Response { + let mut r = Response::new(String::new()); *r.status_mut() = code; r } -fn new_html_response(code: StatusCode) -> Response { +fn new_html_response(code: StatusCode) -> Response { let mut resp = new_response(code); resp.headers_mut().insert( header::CONTENT_TYPE, @@ -313,7 +308,7 @@ mod tests { use hyper::{ server::Server, service::{make_service_fn, service_fn}, - Body, Method, Response, StatusCode, + Method, Response, StatusCode, }; use juniper::{ http::tests as http_tests, @@ -409,7 +404,7 @@ mod tests { super::graphql(root_node, ctx, req).await } } else { - let mut resp = Response::new(Body::empty()); + let mut resp = Response::new(String::new()); *resp.status_mut() = StatusCode::NOT_FOUND; resp })