Skip to content

Commit

Permalink
Change body type of juniper-hyper functions from Body to String
Browse files Browse the repository at this point in the history
All responses were created from strings anyway (via `Body::from`), but
`String` implements the `Body` trait directly. With this change, it is
easy to inspect the response body, which is not true for `Body`.

Fixes #1096
  • Loading branch information
LukasKalbertodt committed Sep 12, 2022
1 parent 4b89d61 commit 2175920
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
1 change: 1 addition & 0 deletions juniper_hyper/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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 the return type of all functions from `Response<Body>` to `Response<String>`



Expand Down
37 changes: 18 additions & 19 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>,
) -> Response<Body>
) -> Response<String>
where
QueryT: GraphQLType<S, Context = CtxT>,
QueryT::TypeInfo: Sync,
Expand All @@ -38,7 +38,7 @@ pub async fn graphql<CtxT, QueryT, MutationT, SubscriptionT, S>(
root_node: Arc<RootNode<'static, QueryT, MutationT, SubscriptionT, S>>,
context: Arc<CtxT>,
req: Request<Body>,
) -> Response<Body>
) -> Response<String>
where
QueryT: GraphQLTypeAsync<S, Context = CtxT>,
QueryT::TypeInfo: Sync,
Expand All @@ -57,7 +57,7 @@ where

async fn parse_req<S: ScalarValue>(
req: Request<Body>,
) -> Result<GraphQLBatchRequest<S>, Response<Body>> {
) -> Result<GraphQLBatchRequest<S>, Response<String>> {
match *req.method() {
Method::GET => parse_get_req(req),
Method::POST => {
Expand Down Expand Up @@ -121,40 +121,39 @@ async fn parse_post_graphql_req<S: ScalarValue>(
pub async fn graphiql(
graphql_endpoint: &str,
subscriptions_endpoint: Option<&str>,
) -> Response<Body> {
) -> Response<String> {
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(
*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<Body> {
) -> Response<String> {
let mut resp = new_html_response(StatusCode::OK);
*resp.body_mut() = Body::from(juniper::http::playground::playground_source(
*resp.body_mut() = juniper::http::playground::playground_source(
graphql_endpoint,
subscriptions_endpoint,
));
);
resp
}

fn render_error(err: GraphQLRequestError) -> Response<Body> {
let message = err.to_string();
fn render_error(err: GraphQLRequestError) -> Response<String> {
let mut resp = new_response(StatusCode::BAD_REQUEST);
*resp.body_mut() = Body::from(message);
*resp.body_mut() = err.to_string();
resp
}

async fn execute_request_sync<CtxT, QueryT, MutationT, SubscriptionT, S>(
root_node: Arc<RootNode<'static, QueryT, MutationT, SubscriptionT, S>>,
context: Arc<CtxT>,
request: GraphQLBatchRequest<S>,
) -> Response<Body>
) -> Response<String>
where
QueryT: GraphQLType<S, Context = CtxT>,
QueryT::TypeInfo: Sync,
Expand All @@ -166,7 +165,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 {
Expand All @@ -185,7 +184,7 @@ async fn execute_request<CtxT, QueryT, MutationT, SubscriptionT, S>(
root_node: Arc<RootNode<'static, QueryT, MutationT, SubscriptionT, S>>,
context: Arc<CtxT>,
request: GraphQLBatchRequest<S>,
) -> Response<Body>
) -> Response<String>
where
QueryT: GraphQLTypeAsync<S, Context = CtxT>,
QueryT::TypeInfo: Sync,
Expand All @@ -197,7 +196,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 {
Expand Down Expand Up @@ -260,13 +259,13 @@ fn invalid_err(parameter_name: &str) -> GraphQLRequestError {
))
}

fn new_response(code: StatusCode) -> Response<Body> {
let mut r = Response::new(Body::empty());
fn new_response(code: StatusCode) -> Response<String> {
let mut r = Response::new(String::new());
*r.status_mut() = code;
r
}

fn new_html_response(code: StatusCode) -> Response<Body> {
fn new_html_response(code: StatusCode) -> Response<String> {
let mut resp = new_response(code);
resp.headers_mut().insert(
header::CONTENT_TYPE,
Expand Down

0 comments on commit 2175920

Please sign in to comment.