@@ -59,33 +59,48 @@ use rocket::Request;
59
59
use juniper:: http;
60
60
use juniper:: InputValue ;
61
61
62
+ use juniper:: serde:: Deserialize ;
63
+ use juniper:: DefaultScalarValue ;
62
64
use juniper:: FieldError ;
63
65
use juniper:: GraphQLType ;
64
66
use juniper:: RootNode ;
67
+ use juniper:: ScalarRefValue ;
68
+ use juniper:: ScalarValue ;
65
69
66
70
#[ derive( Debug , Deserialize , PartialEq ) ]
67
71
#[ serde( untagged) ]
68
- enum GraphQLBatchRequest {
69
- Single ( http:: GraphQLRequest ) ,
70
- Batch ( Vec < http:: GraphQLRequest > ) ,
72
+ #[ serde( bound = "InputValue<S>: Deserialize<'de>" ) ]
73
+ enum GraphQLBatchRequest < S = DefaultScalarValue >
74
+ where
75
+ S : ScalarValue ,
76
+ {
77
+ Single ( http:: GraphQLRequest < S > ) ,
78
+ Batch ( Vec < http:: GraphQLRequest < S > > ) ,
71
79
}
72
80
73
81
#[ derive( Serialize ) ]
74
82
#[ serde( untagged) ]
75
- enum GraphQLBatchResponse < ' a > {
76
- Single ( http:: GraphQLResponse < ' a > ) ,
77
- Batch ( Vec < http:: GraphQLResponse < ' a > > ) ,
83
+ enum GraphQLBatchResponse < ' a , S = DefaultScalarValue >
84
+ where
85
+ S : ScalarValue ,
86
+ {
87
+ Single ( http:: GraphQLResponse < ' a , S > ) ,
88
+ Batch ( Vec < http:: GraphQLResponse < ' a , S > > ) ,
78
89
}
79
90
80
- impl GraphQLBatchRequest {
91
+ impl < S > GraphQLBatchRequest < S >
92
+ where
93
+ S : ScalarValue ,
94
+ for < ' b > & ' b S : ScalarRefValue < ' b > ,
95
+ {
81
96
pub fn execute < ' a , CtxT , QueryT , MutationT > (
82
97
& ' a self ,
83
- root_node : & RootNode < QueryT , MutationT > ,
98
+ root_node : & ' a RootNode < QueryT , MutationT , S > ,
84
99
context : & CtxT ,
85
- ) -> GraphQLBatchResponse < ' a >
100
+ ) -> GraphQLBatchResponse < ' a , S >
86
101
where
87
- QueryT : GraphQLType < Context = CtxT > ,
88
- MutationT : GraphQLType < Context = CtxT > ,
102
+ QueryT : GraphQLType < S , Context = CtxT > ,
103
+ MutationT : GraphQLType < S , Context = CtxT > ,
89
104
{
90
105
match self {
91
106
& GraphQLBatchRequest :: Single ( ref request) => {
@@ -101,7 +116,10 @@ impl GraphQLBatchRequest {
101
116
}
102
117
}
103
118
104
- impl < ' a > GraphQLBatchResponse < ' a > {
119
+ impl < ' a , S > GraphQLBatchResponse < ' a , S >
120
+ where
121
+ S : ScalarValue ,
122
+ {
105
123
fn is_ok ( & self ) -> bool {
106
124
match self {
107
125
& GraphQLBatchResponse :: Single ( ref response) => response. is_ok ( ) ,
@@ -118,7 +136,9 @@ impl<'a> GraphQLBatchResponse<'a> {
118
136
/// automatically from both GET and POST routes by implementing the `FromForm`
119
137
/// and `FromData` traits.
120
138
#[ derive( Debug , PartialEq ) ]
121
- pub struct GraphQLRequest ( GraphQLBatchRequest ) ;
139
+ pub struct GraphQLRequest < S = DefaultScalarValue > ( GraphQLBatchRequest < S > )
140
+ where
141
+ S : ScalarValue ;
122
142
123
143
/// Simple wrapper around the result of executing a GraphQL query
124
144
pub struct GraphQLResponse ( pub Status , pub String ) ;
@@ -128,16 +148,20 @@ pub fn graphiql_source(graphql_endpoint_url: &str) -> content::Html<String> {
128
148
content:: Html ( juniper:: graphiql:: graphiql_source ( graphql_endpoint_url) )
129
149
}
130
150
131
- impl GraphQLRequest {
151
+ impl < S > GraphQLRequest < S >
152
+ where
153
+ S : ScalarValue ,
154
+ for < ' b > & ' b S : ScalarRefValue < ' b > ,
155
+ {
132
156
/// Execute an incoming GraphQL query
133
157
pub fn execute < CtxT , QueryT , MutationT > (
134
158
& self ,
135
- root_node : & RootNode < QueryT , MutationT > ,
159
+ root_node : & RootNode < QueryT , MutationT , S > ,
136
160
context : & CtxT ,
137
161
) -> GraphQLResponse
138
162
where
139
- QueryT : GraphQLType < Context = CtxT > ,
140
- MutationT : GraphQLType < Context = CtxT > ,
163
+ QueryT : GraphQLType < S , Context = CtxT > ,
164
+ MutationT : GraphQLType < S , Context = CtxT > ,
141
165
{
142
166
let response = self . 0 . execute ( root_node, context) ;
143
167
let status = if response. is_ok ( ) {
@@ -205,7 +229,10 @@ impl GraphQLResponse {
205
229
}
206
230
}
207
231
208
- impl < ' f > FromForm < ' f > for GraphQLRequest {
232
+ impl < ' f , S > FromForm < ' f > for GraphQLRequest < S >
233
+ where
234
+ S : ScalarValue ,
235
+ {
209
236
type Error = String ;
210
237
211
238
fn from_form ( form_items : & mut FormItems < ' f > , strict : bool ) -> Result < Self , String > {
@@ -248,8 +275,10 @@ impl<'f> FromForm<'f> for GraphQLRequest {
248
275
Ok ( v) => decoded = v,
249
276
Err ( e) => return Err ( e. description ( ) . to_string ( ) ) ,
250
277
}
251
- variables = Some ( serde_json:: from_str :: < InputValue > ( & decoded)
252
- . map_err ( |err| err. description ( ) . to_owned ( ) ) ?) ;
278
+ variables = Some (
279
+ serde_json:: from_str :: < InputValue < _ > > ( & decoded)
280
+ . map_err ( |err| err. description ( ) . to_owned ( ) ) ?,
281
+ ) ;
253
282
}
254
283
}
255
284
_ => {
@@ -270,7 +299,10 @@ impl<'f> FromForm<'f> for GraphQLRequest {
270
299
}
271
300
}
272
301
273
- impl FromData for GraphQLRequest {
302
+ impl < S > FromData for GraphQLRequest < S >
303
+ where
304
+ S : ScalarValue ,
305
+ {
274
306
type Error = String ;
275
307
276
308
fn from_data ( request : & Request , data : Data ) -> FromDataOutcome < Self , Self :: Error > {
@@ -311,7 +343,7 @@ mod fromform_tests {
311
343
312
344
fn check_error ( input : & str , error : & str , strict : bool ) {
313
345
let mut items = FormItems :: from ( input) ;
314
- let result = GraphQLRequest :: from_form ( & mut items, strict) ;
346
+ let result: Result < GraphQLRequest , _ > = GraphQLRequest :: from_form ( & mut items, strict) ;
315
347
assert ! ( result. is_err( ) ) ;
316
348
assert_eq ! ( result. unwrap_err( ) , error) ;
317
349
}
@@ -401,7 +433,7 @@ mod fromform_tests {
401
433
fn test_url_decode ( ) {
402
434
let form_string = "query=%25foo%20bar+baz%26%3F&operation_name=test" ;
403
435
let mut items = FormItems :: from ( form_string) ;
404
- let result = GraphQLRequest :: from_form ( & mut items, false ) ;
436
+ let result: Result < GraphQLRequest , _ > = GraphQLRequest :: from_form ( & mut items, false ) ;
405
437
assert ! ( result. is_ok( ) ) ;
406
438
let expected = GraphQLRequest ( GraphQLBatchRequest :: Single ( http:: GraphQLRequest :: new (
407
439
"%foo bar baz&?" . to_string ( ) ,
@@ -477,8 +509,7 @@ mod tests {
477
509
. manage ( Schema :: new (
478
510
Database :: new ( ) ,
479
511
EmptyMutation :: < Database > :: new ( ) ,
480
- ) )
481
- . mount ( "/" , routes ! [ post_graphql_handler, get_graphql_handler] )
512
+ ) ) . mount ( "/" , routes ! [ post_graphql_handler, get_graphql_handler] )
482
513
}
483
514
484
515
fn make_test_response < ' r > ( request : & LocalRequest < ' r > ) -> http_tests:: TestResponse {
0 commit comments