@@ -43,14 +43,19 @@ graphql_interface!(Interface: () as "SampleInterface" |&self| {
43
43
} ) ;
44
44
45
45
graphql_object ! ( Root : ( ) as "Root" |& self | {
46
+ description: "The root query object in the schema"
47
+
46
48
interfaces: [ Interface ]
47
49
48
50
field sample_enum( ) -> FieldResult <Sample > {
49
51
Ok ( Sample :: One )
50
52
}
51
53
52
- field sample_scalar( ) -> FieldResult <Scalar > {
53
- Ok ( Scalar ( 123 ) )
54
+ field sample_scalar(
55
+ first: i64 as "The first number" ,
56
+ second = 123 : i64 as "The second number"
57
+ ) -> FieldResult <Scalar > {
58
+ Ok ( Scalar ( first + second) )
54
59
}
55
60
} ) ;
56
61
@@ -59,7 +64,8 @@ fn test_execution() {
59
64
let doc = r#"
60
65
{
61
66
sampleEnum
62
- sampleScalar
67
+ first: sampleScalar(first: 0)
68
+ second: sampleScalar(first: 10 second: 20)
63
69
}
64
70
"# ;
65
71
let schema = RootNode :: new ( Root { } , ( ) ) ;
@@ -73,7 +79,8 @@ fn test_execution() {
73
79
74
80
assert_eq ! ( result, Value :: object( vec![
75
81
( "sampleEnum" , Value :: string( "ONE" ) ) ,
76
- ( "sampleScalar" , Value :: int( 123 ) ) ,
82
+ ( "first" , Value :: int( 123 ) ) ,
83
+ ( "second" , Value :: int( 30 ) ) ,
77
84
] . into_iter( ) . collect( ) ) ) ;
78
85
}
79
86
@@ -231,6 +238,91 @@ fn interface_introspection() {
231
238
] . into_iter( ) . collect( ) ) ) ) ;
232
239
}
233
240
241
+ #[ test]
242
+ fn object_introspection ( ) {
243
+ let doc = r#"
244
+ {
245
+ __type(name: "Root") {
246
+ name
247
+ kind
248
+ description
249
+ fields {
250
+ name
251
+ description
252
+ args {
253
+ name
254
+ }
255
+ type {
256
+ name
257
+ kind
258
+ ofType {
259
+ name
260
+ kind
261
+ }
262
+ }
263
+ isDeprecated
264
+ deprecationReason
265
+ }
266
+ possibleTypes { name }
267
+ interfaces { name }
268
+ enumValues { name }
269
+ inputFields { name }
270
+ ofType { name }
271
+ }
272
+ }
273
+ "# ;
274
+ let schema = RootNode :: new ( Root { } , ( ) ) ;
275
+
276
+ let ( result, errs) = :: execute ( doc, None , & schema, & HashMap :: new ( ) , & ( ) )
277
+ . expect ( "Execution failed" ) ;
278
+
279
+ assert_eq ! ( errs, [ ] ) ;
280
+
281
+ println ! ( "Result: {:?}" , result) ;
282
+
283
+ let type_info = result
284
+ . as_object_value ( ) . expect ( "Result is not an object" )
285
+ . get ( "__type" ) . expect ( "__type field missing" )
286
+ . as_object_value ( ) . expect ( "__type field not an object value" ) ;
287
+
288
+ assert_eq ! ( type_info. get( "name" ) , Some ( & Value :: string( "Root" ) ) ) ;
289
+ assert_eq ! ( type_info. get( "kind" ) , Some ( & Value :: string( "OBJECT" ) ) ) ;
290
+ assert_eq ! ( type_info. get( "description" ) , Some ( & Value :: string( "The root query object in the schema" ) ) ) ;
291
+ assert_eq ! (
292
+ type_info. get( "interfaces" ) ,
293
+ Some ( & Value :: list( vec![
294
+ Value :: object( vec![
295
+ ( "name" , Value :: string( "SampleInterface" ) ) ,
296
+ ] . into_iter( ) . collect( ) ) ,
297
+ ] ) ) ) ;
298
+ assert_eq ! ( type_info. get( "enumValues" ) , Some ( & Value :: null( ) ) ) ;
299
+ assert_eq ! ( type_info. get( "inputFields" ) , Some ( & Value :: null( ) ) ) ;
300
+ assert_eq ! ( type_info. get( "ofType" ) , Some ( & Value :: null( ) ) ) ;
301
+ assert_eq ! ( type_info. get( "possibleTypes" ) , Some ( & Value :: null( ) ) ) ;
302
+
303
+ let fields = type_info
304
+ . get ( "fields" ) . expect ( "fields field missing" )
305
+ . as_list_value ( ) . expect ( "fields field not an object value" ) ;
306
+
307
+ assert_eq ! ( fields. len( ) , 5 ) ; // The two fields, __typename, __type, __schema
308
+
309
+ assert ! ( fields. contains( & Value :: object( vec![
310
+ ( "name" , Value :: string( "sampleEnum" ) ) ,
311
+ ( "description" , Value :: null( ) ) ,
312
+ ( "args" , Value :: list( vec![ ] ) ) ,
313
+ ( "type" , Value :: object( vec![
314
+ ( "name" , Value :: null( ) ) ,
315
+ ( "kind" , Value :: string( "NON_NULL" ) ) ,
316
+ ( "ofType" , Value :: object( vec![
317
+ ( "name" , Value :: string( "SampleEnum" ) ) ,
318
+ ( "kind" , Value :: string( "ENUM" ) ) ,
319
+ ] . into_iter( ) . collect( ) ) ) ,
320
+ ] . into_iter( ) . collect( ) ) ) ,
321
+ ( "isDeprecated" , Value :: boolean( false ) ) ,
322
+ ( "deprecationReason" , Value :: null( ) ) ,
323
+ ] . into_iter( ) . collect( ) ) ) ) ;
324
+ }
325
+
234
326
#[ test]
235
327
fn scalar_introspection ( ) {
236
328
let doc = r#"
0 commit comments