@@ -9,14 +9,30 @@ enum Sample {
9
9
Two ,
10
10
}
11
11
12
+ struct Interface { }
13
+
12
14
struct Root { }
13
15
14
16
graphql_enum ! ( Sample as "SampleEnum" {
15
17
Sample :: One => "ONE" ,
16
18
Sample :: Two => "TWO" ,
17
19
} ) ;
18
20
21
+ graphql_interface ! ( Interface : ( ) as "SampleInterface" |& self | {
22
+ description: "A sample interface"
23
+
24
+ field sample_enum( ) -> FieldResult <Sample > as "A sample field in the interface" {
25
+ Ok ( Sample :: One )
26
+ }
27
+
28
+ instance_resolvers: |& _| [
29
+ Some ( Root { } ) ,
30
+ ]
31
+ } ) ;
32
+
19
33
graphql_object ! ( Root : ( ) as "Root" |& self | {
34
+ interfaces: [ Interface ]
35
+
20
36
field sample_enum( ) -> FieldResult <Sample > {
21
37
Ok ( Sample :: One )
22
38
}
@@ -27,6 +43,9 @@ fn enum_introspection() {
27
43
let doc = r#"
28
44
{
29
45
__type(name: "SampleEnum") {
46
+ name
47
+ kind
48
+ description
30
49
enumValues {
31
50
name
32
51
description
@@ -45,10 +64,16 @@ fn enum_introspection() {
45
64
46
65
println ! ( "Result: {:?}" , result) ;
47
66
48
- let values = result
67
+ let type_info = result
49
68
. as_object_value ( ) . expect ( "Result is not an object" )
50
69
. get ( "__type" ) . expect ( "__type field missing" )
51
- . as_object_value ( ) . expect ( "__type field not an object value" )
70
+ . as_object_value ( ) . expect ( "__type field not an object value" ) ;
71
+
72
+ assert_eq ! ( type_info. get( "name" ) , Some ( & Value :: string( "SampleEnum" ) ) ) ;
73
+ assert_eq ! ( type_info. get( "kind" ) , Some ( & Value :: string( "ENUM" ) ) ) ;
74
+ assert_eq ! ( type_info. get( "description" ) , Some ( & Value :: null( ) ) ) ;
75
+
76
+ let values = type_info
52
77
. get ( "enumValues" ) . expect ( "enumValues field missing" )
53
78
. as_list_value ( ) . expect ( "enumValues not a list" ) ;
54
79
@@ -68,3 +93,85 @@ fn enum_introspection() {
68
93
( "deprecationReason" , Value :: null( ) ) ,
69
94
] . into_iter( ) . collect( ) ) ) ) ;
70
95
}
96
+
97
+ #[ test]
98
+ fn interface_introspection ( ) {
99
+ let doc = r#"
100
+ {
101
+ __type(name: "SampleInterface") {
102
+ name
103
+ kind
104
+ description
105
+ possibleTypes {
106
+ name
107
+ }
108
+ fields {
109
+ name
110
+ description
111
+ args {
112
+ name
113
+ }
114
+ type {
115
+ name
116
+ kind
117
+ ofType {
118
+ name
119
+ kind
120
+ }
121
+ }
122
+ isDeprecated
123
+ deprecationReason
124
+ }
125
+ }
126
+ }
127
+ "# ;
128
+ let schema = RootNode :: new ( Root { } , ( ) ) ;
129
+
130
+ let ( result, errs) = :: execute ( doc, None , & schema, & HashMap :: new ( ) , & ( ) )
131
+ . expect ( "Execution failed" ) ;
132
+
133
+ assert_eq ! ( errs, [ ] ) ;
134
+
135
+ println ! ( "Result: {:?}" , result) ;
136
+
137
+ let type_info = result
138
+ . as_object_value ( ) . expect ( "Result is not an object" )
139
+ . get ( "__type" ) . expect ( "__type field missing" )
140
+ . as_object_value ( ) . expect ( "__type field not an object value" ) ;
141
+
142
+ assert_eq ! ( type_info. get( "name" ) , Some ( & Value :: string( "SampleInterface" ) ) ) ;
143
+ assert_eq ! ( type_info. get( "kind" ) , Some ( & Value :: string( "INTERFACE" ) ) ) ;
144
+ assert_eq ! ( type_info. get( "description" ) , Some ( & Value :: string( "A sample interface" ) ) ) ;
145
+
146
+ let possible_types = type_info
147
+ . get ( "possibleTypes" ) . expect ( "possibleTypes field missing" )
148
+ . as_list_value ( ) . expect ( "possibleTypes not a list" ) ;
149
+
150
+ assert_eq ! ( possible_types. len( ) , 1 ) ;
151
+
152
+ assert ! ( possible_types. contains( & Value :: object( vec![
153
+ ( "name" , Value :: string( "Root" ) ) ,
154
+ ] . into_iter( ) . collect( ) ) ) ) ;
155
+
156
+ let fields = type_info
157
+ . get ( "fields" ) . expect ( "fields field missing" )
158
+ . as_list_value ( ) . expect ( "fields field not an object value" ) ;
159
+
160
+ assert_eq ! ( fields. len( ) , 2 ) ;
161
+
162
+ assert ! ( fields. contains( & Value :: object( vec![
163
+ ( "name" , Value :: string( "sampleEnum" ) ) ,
164
+ ( "description" , Value :: string( "A sample field in the interface" ) ) ,
165
+ ( "args" , Value :: list( vec![ ] ) ) ,
166
+ ( "type" , Value :: object( vec![
167
+ ( "name" , Value :: null( ) ) ,
168
+ ( "kind" , Value :: string( "NON_NULL" ) ) ,
169
+ ( "ofType" , Value :: object( vec![
170
+ ( "name" , Value :: string( "SampleEnum" ) ) ,
171
+ ( "kind" , Value :: string( "ENUM" ) ) ,
172
+ ] . into_iter( ) . collect( ) ) ) ,
173
+ ] . into_iter( ) . collect( ) ) ) ,
174
+ ( "isDeprecated" , Value :: boolean( false ) ) ,
175
+ ( "deprecationReason" , Value :: null( ) ) ,
176
+ ] . into_iter( ) . collect( ) ) ) ) ;
177
+ }
0 commit comments