@@ -3,40 +3,12 @@ use serde::ser::SerializeMap;
3
3
use std:: fmt;
4
4
use std:: collections:: HashMap ;
5
5
6
- use :: { GraphQLError , Value } ;
6
+ use :: { GraphQLError , Value , Variables } ;
7
7
use ast:: InputValue ;
8
8
use executor:: ExecutionError ;
9
9
use parser:: { ParseError , Spanning , SourcePosition } ;
10
10
use validation:: RuleError ;
11
- use serde_json:: Value as Json ;
12
-
13
-
14
- impl InputValue {
15
- /// Converts serde_json::Value to juniper::InputValue
16
- pub fn from_json ( json : Json ) -> InputValue {
17
- match json {
18
- Json :: Number ( num) => {
19
- if let Some ( number) = num. as_i64 ( ) {
20
- InputValue :: int ( number)
21
- }
22
- else if let Some ( number) = num. as_f64 ( ) {
23
- InputValue :: float ( number)
24
- }
25
- else if let Some ( number) = num. as_u64 ( ) {
26
- InputValue :: float ( number as f64 )
27
- }
28
- else {
29
- panic ! ( "Invalid number data type was found." ) ;
30
- }
31
- }
32
- Json :: String ( s) => InputValue :: string ( s) ,
33
- Json :: Bool ( b) => InputValue :: boolean ( b) ,
34
- Json :: Array ( a) => InputValue :: list ( a. into_iter ( ) . map ( InputValue :: from_json) . collect ( ) ) ,
35
- Json :: Object ( o) => InputValue :: object ( o. into_iter ( ) . map ( |( k, v) | ( k, InputValue :: from_json ( v) ) ) . collect ( ) ) ,
36
- Json :: Null => InputValue :: null ( ) ,
37
- }
38
- }
39
- }
11
+
40
12
41
13
impl ser:: Serialize for ExecutionError {
42
14
fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
@@ -242,3 +214,84 @@ impl ser::Serialize for Value {
242
214
}
243
215
}
244
216
}
217
+
218
+ /// The expected structure of the decoded JSON Document for either Post or Get requests.
219
+ #[ cfg( feature="iron-handlers" ) ]
220
+ #[ derive( Deserialize ) ]
221
+ pub struct GraphQlQuery {
222
+ query : String ,
223
+ #[ serde( rename = "operationName" ) ]
224
+ operation_name : Option < String > ,
225
+ variables : Option < InputValue >
226
+ }
227
+
228
+ impl GraphQlQuery {
229
+
230
+ pub fn new ( query : String ,
231
+ operation_name : Option < String > ,
232
+ variables : Option < InputValue >
233
+ ) -> GraphQlQuery {
234
+ GraphQlQuery {
235
+ query : query,
236
+ operation_name : operation_name,
237
+ variables : variables
238
+ }
239
+ }
240
+
241
+ pub fn query ( & self ) -> & str {
242
+ self . query . as_str ( )
243
+ }
244
+
245
+ pub fn operation_name ( & self ) -> Option < & str > {
246
+ self . operation_name . as_ref ( ) . map ( |oper_name| & * * oper_name)
247
+ }
248
+
249
+ pub fn variables ( & self ) -> Variables {
250
+ self . variables . as_ref ( ) . and_then ( |iv| {
251
+ iv. to_object_value ( ) . map ( |o| {
252
+ o. into_iter ( ) . map ( |( k, v) | ( k. to_owned ( ) , v. clone ( ) ) ) . collect ( )
253
+ } )
254
+ } ) . unwrap_or_default ( )
255
+ }
256
+
257
+ }
258
+
259
+
260
+ #[ cfg( feature="iron-handlers" ) ]
261
+ pub struct WrappedGraphQLResult < ' a > ( Result < ( Value , Vec < ExecutionError > ) , GraphQLError < ' a > > ) ;
262
+
263
+ impl < ' a > WrappedGraphQLResult < ' a > {
264
+ pub fn new ( result : Result < ( Value , Vec < ExecutionError > ) ,
265
+ GraphQLError < ' a > >
266
+ ) -> WrappedGraphQLResult < ' a > {
267
+ WrappedGraphQLResult ( result)
268
+ }
269
+ }
270
+
271
+ impl < ' a > ser:: Serialize for WrappedGraphQLResult < ' a > {
272
+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
273
+ where S : ser:: Serializer ,
274
+ {
275
+ match self . 0 {
276
+ Ok ( ( ref res, ref err) ) => {
277
+ let mut map = try!( serializer. serialize_map ( None ) ) ;
278
+
279
+ try!( map. serialize_key ( "data" ) ) ;
280
+ try!( map. serialize_value ( res) ) ;
281
+
282
+ if !err. is_empty ( ) {
283
+ try!( map. serialize_key ( "errors" ) ) ;
284
+ try!( map. serialize_value ( err) ) ;
285
+ }
286
+
287
+ map. end ( )
288
+ } ,
289
+ Err ( ref err) => {
290
+ let mut map = try!( serializer. serialize_map ( Some ( 1 ) ) ) ;
291
+ try!( map. serialize_key ( "errors" ) ) ;
292
+ try!( map. serialize_value ( err) ) ;
293
+ map. end ( )
294
+ } ,
295
+ }
296
+ }
297
+ }
0 commit comments