Debian Codegraph @

tests/test_star_wars_introspection.py

raw
1 from typing import Any
2
3 from graphql import graphql_sync
4
5 from .star_wars_schema import star_wars_schema
6
7
8 def query_star_wars(source: str) -> Any:
9 result = graphql_sync(star_wars_schema, source)
10 assert result.errors is None
11 return result.data
12
13
14 def describe_star_wars_introspection_tests():
15 def describe_basic_introspection():
16 def allows_querying_the_schema_for_types():
17 data = query_star_wars("""
18 {
19 __schema {
20 types {
21 name
22 }
23 }
24 }
25 """)
26 # Include all types used by StarWars schema, introspection types and
27 # standard directives. For example, `Boolean` is used in `@skip`,
28 # `@include` and also inside introspection types.
29 assert data == {
30 "__schema": {
31 "types": [
32 {"name": "Human"},
33 {"name": "Character"},
34 {"name": "String"},
35 {"name": "Episode"},
36 {"name": "Droid"},
37 {"name": "Query"},
38 {"name": "Boolean"},
39 {"name": "__Schema"},
40 {"name": "__Type"},
41 {"name": "__TypeKind"},
42 {"name": "__Field"},
43 {"name": "__InputValue"},
44 {"name": "__EnumValue"},
45 {"name": "__Directive"},
46 {"name": "__DirectiveLocation"},
47 ]
48 }
49 }
50
51 def allows_querying_the_schema_for_query_type():
52 data = query_star_wars("""
53 {
54 __schema {
55 queryType {
56 name
57 }
58 }
59 }
60 """)
61
62 assert data == {"__schema": {"queryType": {"name": "Query"}}}
63
64 def allows_querying_the_schema_for_a_specific_type():
65 data = query_star_wars("""
66 {
67 __type(name: "Droid") {
68 name
69 }
70 }
71 """)
72 assert data == {"__type": {"name": "Droid"}}
73
74 def allows_querying_the_schema_for_an_object_kind():
75 data = query_star_wars("""
76 {
77 __type(name: "Droid") {
78 name
79 kind
80 }
81 }
82 """)
83 assert data == {"__type": {"name": "Droid", "kind": "OBJECT"}}
84
85 def allows_querying_the_schema_for_an_interface_kind():
86 data = query_star_wars("""
87 {
88 __type(name: "Character") {
89 name
90 kind
91 }
92 }
93 """)
94 assert data == {"__type": {"name": "Character", "kind": "INTERFACE"}}
95
96 def allows_querying_the_schema_for_object_fields():
97 data = query_star_wars("""
98 {
99 __type(name: "Droid") {
100 name
101 fields {
102 name
103 type {
104 name
105 kind
106 }
107 }
108 }
109 }
110 """)
111 assert data == {
112 "__type": {
113 "name": "Droid",
114 "fields": [
115 {"name": "id", "type": {"name": None, "kind": "NON_NULL"}},
116 {"name": "name", "type": {"name": "String", "kind": "SCALAR"}},
117 {"name": "friends", "type": {"name": None, "kind": "LIST"}},
118 {"name": "appearsIn", "type": {"name": None, "kind": "LIST"}},
119 {
120 "name": "secretBackstory",
121 "type": {"name": "String", "kind": "SCALAR"},
122 },
123 {
124 "name": "primaryFunction",
125 "type": {"name": "String", "kind": "SCALAR"},
126 },
127 ],
128 }
129 }
130
131 def allows_querying_the_schema_for_nested_object_fields():
132 data = query_star_wars("""
133 {
134 __type(name: "Droid") {
135 name
136 fields {
137 name
138 type {
139 name
140 kind
141 ofType {
142 name
143 kind
144 }
145 }
146 }
147 }
148 }
149 """)
150 assert data == {
151 "__type": {
152 "name": "Droid",
153 "fields": [
154 {
155 "name": "id",
156 "type": {
157 "name": None,
158 "kind": "NON_NULL",
159 "ofType": {"name": "String", "kind": "SCALAR"},
160 },
161 },
162 {
163 "name": "name",
164 "type": {
165 "name": "String",
166 "kind": "SCALAR",
167 "ofType": None,
168 },
169 },
170 {
171 "name": "friends",
172 "type": {
173 "name": None,
174 "kind": "LIST",
175 "ofType": {"name": "Character", "kind": "INTERFACE"},
176 },
177 },
178 {
179 "name": "appearsIn",
180 "type": {
181 "name": None,
182 "kind": "LIST",
183 "ofType": {"name": "Episode", "kind": "ENUM"},
184 },
185 },
186 {
187 "name": "secretBackstory",
188 "type": {
189 "name": "String",
190 "kind": "SCALAR",
191 "ofType": None,
192 },
193 },
194 {
195 "name": "primaryFunction",
196 "type": {
197 "name": "String",
198 "kind": "SCALAR",
199 "ofType": None,
200 },
201 },
202 ],
203 }
204 }
205
206 def allows_querying_the_schema_for_field_args():
207 data = query_star_wars("""
208 {
209 __schema {
210 queryType {
211 fields {
212 name
213 args {
214 name
215 description
216 type {
217 name
218 kind
219 ofType {
220 name
221 kind
222 }
223 }
224 defaultValue
225 }
226 }
227 }
228 }
229 }
230 """)
231
232 assert data == {
233 "__schema": {
234 "queryType": {
235 "fields": [
236 {
237 "name": "hero",
238 "args": [
239 {
240 "defaultValue": None,
241 "description": "If omitted, returns the hero of"
242 " the whole saga. If provided, returns the hero"
243 " of that particular episode.",
244 "name": "episode",
245 "type": {
246 "kind": "ENUM",
247 "name": "Episode",
248 "ofType": None,
249 },
250 }
251 ],
252 },
253 {
254 "name": "human",
255 "args": [
256 {
257 "name": "id",
258 "description": "id of the human",
259 "type": {
260 "kind": "NON_NULL",
261 "name": None,
262 "ofType": {
263 "kind": "SCALAR",
264 "name": "String",
265 },
266 },
267 "defaultValue": None,
268 }
269 ],
270 },
271 {
272 "name": "droid",
273 "args": [
274 {
275 "name": "id",
276 "description": "id of the droid",
277 "type": {
278 "kind": "NON_NULL",
279 "name": None,
280 "ofType": {
281 "kind": "SCALAR",
282 "name": "String",
283 },
284 },
285 "defaultValue": None,
286 }
287 ],
288 },
289 ]
290 }
291 }
292 }
293
294 def allows_querying_the_schema_for_documentation():
295 data = query_star_wars("""
296 {
297 __type(name: "Droid") {
298 name
299 description
300 }
301 }
302 """)
303
304 assert data == {
305 "__type": {
306 "name": "Droid",
307 "description": "A mechanical creature in the Star Wars universe.",
308 }
309 }