Skip to content

Commit e0ddeec

Browse files
Change body type of juniper_hyper functions from Body to String (#1101, #1096)
1 parent 2b1a2a7 commit e0ddeec

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

juniper_hyper/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ All user visible changes to `juniper_hyper` crate will be documented in this fil
1111
### BC Breaks
1212

1313
- Switched to 0.16 version of [`juniper` crate].
14+
- Changed return type of all functions from `Response<Body>` to `Response<String>`. ([#1101], [#1096])
15+
16+
[#1096]: /../../issues/1096
17+
[#1101]: /../../pull/1101
1418

1519

1620

juniper_hyper/examples/hyper_server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{convert::Infallible, sync::Arc};
33
use hyper::{
44
server::Server,
55
service::{make_service_fn, service_fn},
6-
Body, Method, Response, StatusCode,
6+
Method, Response, StatusCode,
77
};
88
use juniper::{
99
tests::fixtures::starwars::schema::{Database, Query},
@@ -38,7 +38,7 @@ async fn main() {
3838
juniper_hyper::graphql(root_node, ctx, req).await
3939
}
4040
_ => {
41-
let mut response = Response::new(Body::empty());
41+
let mut response = Response::new(String::new());
4242
*response.status_mut() = StatusCode::NOT_FOUND;
4343
response
4444
}

juniper_hyper/src/lib.rs

Lines changed: 20 additions & 25 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-
) -> Response<Body>
20+
) -> Response<String>
2121
where
2222
QueryT: GraphQLType<S, Context = CtxT>,
2323
QueryT::TypeInfo: Sync,
@@ -38,7 +38,7 @@ 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-
) -> Response<Body>
41+
) -> Response<String>
4242
where
4343
QueryT: GraphQLTypeAsync<S, Context = CtxT>,
4444
QueryT::TypeInfo: Sync,
@@ -57,7 +57,7 @@ where
5757

5858
async fn parse_req<S: ScalarValue>(
5959
req: Request<Body>,
60-
) -> Result<GraphQLBatchRequest<S>, Response<Body>> {
60+
) -> Result<GraphQLBatchRequest<S>, Response<String>> {
6161
match *req.method() {
6262
Method::GET => parse_get_req(req),
6363
Method::POST => {
@@ -121,40 +121,35 @@ async fn parse_post_graphql_req<S: ScalarValue>(
121121
pub async fn graphiql(
122122
graphql_endpoint: &str,
123123
subscriptions_endpoint: Option<&str>,
124-
) -> Response<Body> {
124+
) -> Response<String> {
125125
let mut resp = new_html_response(StatusCode::OK);
126126
// XXX: is the call to graphiql_source blocking?
127-
*resp.body_mut() = Body::from(juniper::http::graphiql::graphiql_source(
128-
graphql_endpoint,
129-
subscriptions_endpoint,
130-
));
127+
*resp.body_mut() =
128+
juniper::http::graphiql::graphiql_source(graphql_endpoint, subscriptions_endpoint);
131129
resp
132130
}
133131

134132
pub async fn playground(
135133
graphql_endpoint: &str,
136134
subscriptions_endpoint: Option<&str>,
137-
) -> Response<Body> {
135+
) -> Response<String> {
138136
let mut resp = new_html_response(StatusCode::OK);
139-
*resp.body_mut() = Body::from(juniper::http::playground::playground_source(
140-
graphql_endpoint,
141-
subscriptions_endpoint,
142-
));
137+
*resp.body_mut() =
138+
juniper::http::playground::playground_source(graphql_endpoint, subscriptions_endpoint);
143139
resp
144140
}
145141

146-
fn render_error(err: GraphQLRequestError) -> Response<Body> {
147-
let message = err.to_string();
142+
fn render_error(err: GraphQLRequestError) -> Response<String> {
148143
let mut resp = new_response(StatusCode::BAD_REQUEST);
149-
*resp.body_mut() = Body::from(message);
144+
*resp.body_mut() = err.to_string();
150145
resp
151146
}
152147

153148
async fn execute_request_sync<CtxT, QueryT, MutationT, SubscriptionT, S>(
154149
root_node: Arc<RootNode<'static, QueryT, MutationT, SubscriptionT, S>>,
155150
context: Arc<CtxT>,
156151
request: GraphQLBatchRequest<S>,
157-
) -> Response<Body>
152+
) -> Response<String>
158153
where
159154
QueryT: GraphQLType<S, Context = CtxT>,
160155
QueryT::TypeInfo: Sync,
@@ -166,7 +161,7 @@ where
166161
S: ScalarValue + Send + Sync,
167162
{
168163
let res = request.execute_sync(&*root_node, &context);
169-
let body = Body::from(serde_json::to_string_pretty(&res).unwrap());
164+
let body = serde_json::to_string_pretty(&res).unwrap();
170165
let code = if res.is_ok() {
171166
StatusCode::OK
172167
} else {
@@ -185,7 +180,7 @@ async fn execute_request<CtxT, QueryT, MutationT, SubscriptionT, S>(
185180
root_node: Arc<RootNode<'static, QueryT, MutationT, SubscriptionT, S>>,
186181
context: Arc<CtxT>,
187182
request: GraphQLBatchRequest<S>,
188-
) -> Response<Body>
183+
) -> Response<String>
189184
where
190185
QueryT: GraphQLTypeAsync<S, Context = CtxT>,
191186
QueryT::TypeInfo: Sync,
@@ -197,7 +192,7 @@ where
197192
S: ScalarValue + Send + Sync,
198193
{
199194
let res = request.execute(&*root_node, &context).await;
200-
let body = Body::from(serde_json::to_string_pretty(&res).unwrap());
195+
let body = serde_json::to_string_pretty(&res).unwrap();
201196
let code = if res.is_ok() {
202197
StatusCode::OK
203198
} else {
@@ -260,13 +255,13 @@ fn invalid_err(parameter_name: &str) -> GraphQLRequestError {
260255
))
261256
}
262257

263-
fn new_response(code: StatusCode) -> Response<Body> {
264-
let mut r = Response::new(Body::empty());
258+
fn new_response(code: StatusCode) -> Response<String> {
259+
let mut r = Response::new(String::new());
265260
*r.status_mut() = code;
266261
r
267262
}
268263

269-
fn new_html_response(code: StatusCode) -> Response<Body> {
264+
fn new_html_response(code: StatusCode) -> Response<String> {
270265
let mut resp = new_response(code);
271266
resp.headers_mut().insert(
272267
header::CONTENT_TYPE,
@@ -313,7 +308,7 @@ mod tests {
313308
use hyper::{
314309
server::Server,
315310
service::{make_service_fn, service_fn},
316-
Body, Method, Response, StatusCode,
311+
Method, Response, StatusCode,
317312
};
318313
use juniper::{
319314
http::tests as http_tests,
@@ -409,7 +404,7 @@ mod tests {
409404
super::graphql(root_node, ctx, req).await
410405
}
411406
} else {
412-
let mut resp = Response::new(Body::empty());
407+
let mut resp = Response::new(String::new());
413408
*resp.status_mut() = StatusCode::NOT_FOUND;
414409
resp
415410
})

0 commit comments

Comments
 (0)