@@ -4,7 +4,7 @@ use std::{error::Error, fmt, string::FromUtf8Error, sync::Arc};
4
4
5
5
use http_body_util:: BodyExt as _;
6
6
use hyper:: {
7
- body,
7
+ body:: Body ,
8
8
header:: { self , HeaderValue } ,
9
9
Method , Request , Response , StatusCode ,
10
10
} ;
@@ -15,14 +15,7 @@ use juniper::{
15
15
use serde_json:: error:: Error as SerdeError ;
16
16
use url:: form_urlencoded;
17
17
18
- pub async fn graphql_sync <
19
- CtxT ,
20
- QueryT ,
21
- MutationT ,
22
- SubscriptionT ,
23
- S ,
24
- T : body:: Body < Error = impl Error + ' static > ,
25
- > (
18
+ pub async fn graphql_sync < CtxT , QueryT , MutationT , SubscriptionT , S , T > (
26
19
root_node : Arc < RootNode < ' static , QueryT , MutationT , SubscriptionT , S > > ,
27
20
context : Arc < CtxT > ,
28
21
req : Request < T > ,
@@ -36,21 +29,15 @@ where
36
29
SubscriptionT :: TypeInfo : Sync ,
37
30
CtxT : Sync ,
38
31
S : ScalarValue + Send + Sync ,
32
+ T : Body < Error : fmt:: Display > ,
39
33
{
40
34
match parse_req ( req) . await {
41
35
Ok ( req) => execute_request_sync ( root_node, context, req) . await ,
42
36
Err ( resp) => resp,
43
37
}
44
38
}
45
39
46
- pub async fn graphql <
47
- CtxT ,
48
- QueryT ,
49
- MutationT ,
50
- SubscriptionT ,
51
- S ,
52
- T : body:: Body < Error = impl Error + ' static > ,
53
- > (
40
+ pub async fn graphql < CtxT , QueryT , MutationT , SubscriptionT , S , T > (
54
41
root_node : Arc < RootNode < ' static , QueryT , MutationT , SubscriptionT , S > > ,
55
42
context : Arc < CtxT > ,
56
43
req : Request < T > ,
@@ -64,16 +51,19 @@ where
64
51
SubscriptionT :: TypeInfo : Sync ,
65
52
CtxT : Sync ,
66
53
S : ScalarValue + Send + Sync ,
54
+ T : Body < Error : fmt:: Display > ,
67
55
{
68
56
match parse_req ( req) . await {
69
57
Ok ( req) => execute_request ( root_node, context, req) . await ,
70
58
Err ( resp) => resp,
71
59
}
72
60
}
73
61
74
- async fn parse_req < S : ScalarValue , T : body:: Body < Error = impl Error + ' static > > (
75
- req : Request < T > ,
76
- ) -> Result < GraphQLBatchRequest < S > , Response < String > > {
62
+ async fn parse_req < S , T > ( req : Request < T > ) -> Result < GraphQLBatchRequest < S > , Response < String > >
63
+ where
64
+ S : ScalarValue ,
65
+ T : Body < Error : fmt:: Display > ,
66
+ {
77
67
match * req. method ( ) {
78
68
Method :: GET => parse_get_req ( req) ,
79
69
Method :: POST => {
@@ -92,9 +82,11 @@ async fn parse_req<S: ScalarValue, T: body::Body<Error = impl Error + 'static>>(
92
82
. map_err ( render_error)
93
83
}
94
84
95
- fn parse_get_req < S : ScalarValue , T : body:: Body < Error = impl Error + ' static > > (
96
- req : Request < T > ,
97
- ) -> Result < GraphQLBatchRequest < S > , GraphQLRequestError < T > > {
85
+ fn parse_get_req < S , T > ( req : Request < T > ) -> Result < GraphQLBatchRequest < S > , GraphQLRequestError < T > >
86
+ where
87
+ S : ScalarValue ,
88
+ T : Body ,
89
+ {
98
90
req. uri ( )
99
91
. query ( )
100
92
. map ( |q| gql_request_from_get ( q) . map ( GraphQLBatchRequest :: Single ) )
@@ -105,9 +97,13 @@ fn parse_get_req<S: ScalarValue, T: body::Body<Error = impl Error + 'static>>(
105
97
} )
106
98
}
107
99
108
- async fn parse_post_json_req < S : ScalarValue , T : body :: Body < Error = impl Error + ' static > > (
100
+ async fn parse_post_json_req < S , T > (
109
101
body : T ,
110
- ) -> Result < GraphQLBatchRequest < S > , GraphQLRequestError < T > > {
102
+ ) -> Result < GraphQLBatchRequest < S > , GraphQLRequestError < T > >
103
+ where
104
+ S : ScalarValue ,
105
+ T : Body ,
106
+ {
111
107
let chunk = body
112
108
. collect ( )
113
109
. await
@@ -120,9 +116,13 @@ async fn parse_post_json_req<S: ScalarValue, T: body::Body<Error = impl Error +
120
116
. map_err ( GraphQLRequestError :: BodyJSONError )
121
117
}
122
118
123
- async fn parse_post_graphql_req < S : ScalarValue , T : body :: Body > (
119
+ async fn parse_post_graphql_req < S , T > (
124
120
body : T ,
125
- ) -> Result < GraphQLBatchRequest < S > , GraphQLRequestError < T > > {
121
+ ) -> Result < GraphQLBatchRequest < S > , GraphQLRequestError < T > >
122
+ where
123
+ S : ScalarValue ,
124
+ T : Body ,
125
+ {
126
126
let chunk = body
127
127
. collect ( )
128
128
. await
@@ -157,9 +157,10 @@ pub async fn playground(
157
157
resp
158
158
}
159
159
160
- fn render_error < T : body:: Body < Error = impl Error + ' static > > (
161
- err : GraphQLRequestError < T > ,
162
- ) -> Response < String > {
160
+ fn render_error < T > ( err : GraphQLRequestError < T > ) -> Response < String >
161
+ where
162
+ T : Body < Error : fmt:: Display > ,
163
+ {
163
164
let mut resp = new_response ( StatusCode :: BAD_REQUEST ) ;
164
165
* resp. body_mut ( ) = err. to_string ( ) ;
165
166
resp
@@ -227,11 +228,12 @@ where
227
228
resp
228
229
}
229
230
230
- fn gql_request_from_get < S , T : body :: Body > (
231
+ fn gql_request_from_get < S , T > (
231
232
input : & str ,
232
233
) -> Result < JuniperGraphQLRequest < S > , GraphQLRequestError < T > >
233
234
where
234
235
S : ScalarValue ,
236
+ T : Body ,
235
237
{
236
238
let mut query = None ;
237
239
let mut operation_name = None ;
@@ -272,7 +274,7 @@ where
272
274
}
273
275
}
274
276
275
- fn invalid_err < T : body :: Body > ( parameter_name : & str ) -> GraphQLRequestError < T > {
277
+ fn invalid_err < T : Body > ( parameter_name : & str ) -> GraphQLRequestError < T > {
276
278
GraphQLRequestError :: Invalid ( format ! (
277
279
"`{parameter_name}` parameter is specified multiple times" ,
278
280
) )
@@ -293,38 +295,57 @@ fn new_html_response(code: StatusCode) -> Response<String> {
293
295
resp
294
296
}
295
297
296
- #[ derive( Debug ) ]
297
- enum GraphQLRequestError < T : body:: Body > {
298
+ enum GraphQLRequestError < T : Body > {
298
299
BodyHyper ( T :: Error ) ,
299
300
BodyUtf8 ( FromUtf8Error ) ,
300
301
BodyJSONError ( SerdeError ) ,
301
302
Variables ( SerdeError ) ,
302
303
Invalid ( String ) ,
303
304
}
304
305
305
- impl < T : body:: Body < Error = impl Error + ' static > > fmt:: Display for GraphQLRequestError < T > {
306
+ // NOTE: Manual implementation instead of `#[derive(Debug)]` is used to omit imposing unnecessary
307
+ // `T: Debug` bound on the implementation.
308
+ impl < T > fmt:: Debug for GraphQLRequestError < T >
309
+ where
310
+ T : Body < Error : fmt:: Debug > ,
311
+ {
312
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
313
+ match self {
314
+ Self :: BodyHyper ( e) => fmt:: Debug :: fmt ( e, f) ,
315
+ Self :: BodyUtf8 ( e) => fmt:: Debug :: fmt ( e, f) ,
316
+ Self :: BodyJSONError ( e) => fmt:: Debug :: fmt ( e, f) ,
317
+ Self :: Variables ( e) => fmt:: Debug :: fmt ( e, f) ,
318
+ Self :: Invalid ( e) => fmt:: Debug :: fmt ( e, f) ,
319
+ }
320
+ }
321
+ }
322
+
323
+ impl < T > fmt:: Display for GraphQLRequestError < T >
324
+ where
325
+ T : Body < Error : fmt:: Display > ,
326
+ {
306
327
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
307
328
match self {
308
- GraphQLRequestError :: BodyHyper ( err ) => fmt:: Display :: fmt ( err , f) ,
309
- GraphQLRequestError :: BodyUtf8 ( err ) => fmt:: Display :: fmt ( err , f) ,
310
- GraphQLRequestError :: BodyJSONError ( err ) => fmt:: Display :: fmt ( err , f) ,
311
- GraphQLRequestError :: Variables ( err ) => fmt:: Display :: fmt ( err , f) ,
312
- GraphQLRequestError :: Invalid ( err ) => fmt:: Display :: fmt ( err , f) ,
329
+ Self :: BodyHyper ( e ) => fmt:: Display :: fmt ( e , f) ,
330
+ Self :: BodyUtf8 ( e ) => fmt:: Display :: fmt ( e , f) ,
331
+ Self :: BodyJSONError ( e ) => fmt:: Display :: fmt ( e , f) ,
332
+ Self :: Variables ( e ) => fmt:: Display :: fmt ( e , f) ,
333
+ Self :: Invalid ( e ) => fmt:: Display :: fmt ( e , f) ,
313
334
}
314
335
}
315
336
}
316
337
317
- impl < T : body :: Body < Error = impl Error + ' static > + std :: fmt :: Debug > Error for GraphQLRequestError < T >
338
+ impl < T > Error for GraphQLRequestError < T >
318
339
where
319
- < T as body :: Body > :: Error : std :: fmt :: Debug ,
340
+ T : Body < Error : Error + ' static > ,
320
341
{
321
342
fn source ( & self ) -> Option < & ( dyn Error + ' static ) > {
322
343
match self {
323
- GraphQLRequestError :: BodyHyper ( err ) => Some ( err ) ,
324
- GraphQLRequestError :: BodyUtf8 ( err ) => Some ( err ) ,
325
- GraphQLRequestError :: BodyJSONError ( err ) => Some ( err ) ,
326
- GraphQLRequestError :: Variables ( err ) => Some ( err ) ,
327
- GraphQLRequestError :: Invalid ( _) => None ,
344
+ Self :: BodyHyper ( e ) => Some ( e ) ,
345
+ Self :: BodyUtf8 ( e ) => Some ( e ) ,
346
+ Self :: BodyJSONError ( e ) => Some ( e ) ,
347
+ Self :: Variables ( e ) => Some ( e ) ,
348
+ Self :: Invalid ( _) => None ,
328
349
}
329
350
}
330
351
}
@@ -335,7 +356,7 @@ mod tests {
335
356
convert:: Infallible , error:: Error , net:: SocketAddr , panic, sync:: Arc , time:: Duration ,
336
357
} ;
337
358
338
- use http_body_util:: BodyExt ;
359
+ use http_body_util:: BodyExt as _ ;
339
360
use hyper:: {
340
361
body:: Incoming , server:: conn:: http1, service:: service_fn, Method , Request , Response ,
341
362
StatusCode ,
@@ -513,14 +534,12 @@ mod tests {
513
534
}
514
535
515
536
#[ tokio:: test]
516
- /// run test for a custom request type - `Request<Vec<u8>>`
517
- async fn test_custom_hyper_integration ( ) {
537
+ async fn test_custom_request_hyper_integration ( ) {
518
538
run_hyper_integration ( 3002 , false , false ) . await
519
539
}
520
540
521
541
#[ tokio:: test]
522
- /// run test for a custom request type - `Request<Vec<u8>>` in sync mode
523
- async fn test_custom_sync_hyper_integration ( ) {
542
+ async fn test_custom_request_sync_hyper_integration ( ) {
524
543
run_hyper_integration ( 3003 , true , true ) . await
525
544
}
526
545
}
0 commit comments