@@ -9,10 +9,22 @@ enum Sample {
9
9
Two ,
10
10
}
11
11
12
+ struct Scalar ( i64 ) ;
13
+
12
14
struct Interface { }
13
15
14
16
struct Root { }
15
17
18
+ graphql_scalar ! ( Scalar as "SampleScalar" {
19
+ resolve( & self ) -> Value {
20
+ Value :: int( self . 0 )
21
+ }
22
+
23
+ from_input_value( v: & InputValue ) -> Option <Scalar > {
24
+ v. as_int_value( ) . map( |i| Scalar ( i) )
25
+ }
26
+ } ) ;
27
+
16
28
graphql_enum ! ( Sample as "SampleEnum" {
17
29
Sample :: One => "ONE" ,
18
30
Sample :: Two => "TWO" ,
@@ -36,8 +48,35 @@ graphql_object!(Root: () as "Root" |&self| {
36
48
field sample_enum( ) -> FieldResult <Sample > {
37
49
Ok ( Sample :: One )
38
50
}
51
+
52
+ field sample_scalar( ) -> FieldResult <Scalar > {
53
+ Ok ( Scalar ( 123 ) )
54
+ }
39
55
} ) ;
40
56
57
+ #[ test]
58
+ fn test_execution ( ) {
59
+ let doc = r#"
60
+ {
61
+ sampleEnum
62
+ sampleScalar
63
+ }
64
+ "# ;
65
+ let schema = RootNode :: new ( Root { } , ( ) ) ;
66
+
67
+ let ( result, errs) = :: execute ( doc, None , & schema, & HashMap :: new ( ) , & ( ) )
68
+ . expect ( "Execution failed" ) ;
69
+
70
+ assert_eq ! ( errs, [ ] ) ;
71
+
72
+ println ! ( "Result: {:?}" , result) ;
73
+
74
+ assert_eq ! ( result, Value :: object( vec![
75
+ ( "sampleEnum" , Value :: string( "ONE" ) ) ,
76
+ ( "sampleScalar" , Value :: int( 123 ) ) ,
77
+ ] . into_iter( ) . collect( ) ) ) ;
78
+ }
79
+
41
80
#[ test]
42
81
fn enum_introspection ( ) {
43
82
let doc = r#"
@@ -175,3 +214,34 @@ fn interface_introspection() {
175
214
( "deprecationReason" , Value :: null( ) ) ,
176
215
] . into_iter( ) . collect( ) ) ) ) ;
177
216
}
217
+
218
+ #[ test]
219
+ fn scalar_introspection ( ) {
220
+ let doc = r#"
221
+ {
222
+ __type(name: "SampleScalar") {
223
+ name
224
+ kind
225
+ description
226
+ }
227
+ }
228
+ "# ;
229
+ let schema = RootNode :: new ( Root { } , ( ) ) ;
230
+
231
+ let ( result, errs) = :: execute ( doc, None , & schema, & HashMap :: new ( ) , & ( ) )
232
+ . expect ( "Execution failed" ) ;
233
+
234
+ assert_eq ! ( errs, [ ] ) ;
235
+
236
+ println ! ( "Result: {:?}" , result) ;
237
+
238
+ let type_info = result
239
+ . as_object_value ( ) . expect ( "Result is not an object" )
240
+ . get ( "__type" ) . expect ( "__type field missing" ) ;
241
+
242
+ assert_eq ! ( type_info, & Value :: object( vec![
243
+ ( "name" , Value :: string( "SampleScalar" ) ) ,
244
+ ( "kind" , Value :: string( "SCALAR" ) ) ,
245
+ ( "description" , Value :: null( ) ) ,
246
+ ] . into_iter( ) . collect( ) ) ) ;
247
+ }
0 commit comments