Debian Codegraph @

tests/validation/test_max_introspection_depth_rule.py

raw
1 from functools import partial
2
3 from graphql.utilities import get_introspection_query
4 from graphql.validation import MaxIntrospectionDepthRule
5
6 from .harness import assert_validation_errors
7
8 assert_errors = partial(assert_validation_errors, MaxIntrospectionDepthRule)
9
10 assert_valid = partial(assert_errors, errors=[])
11
12
13 def describe_validate_max_introspection_nodes_rule():
14 def default_introspection_query():
15 assert_valid(get_introspection_query())
16
17 def all_ptions_introspection_query():
18 assert_valid(
19 get_introspection_query(
20 descriptions=True,
21 specified_by_url=True,
22 directive_is_repeatable=True,
23 schema_description=True,
24 input_value_deprecation=True,
25 )
26 )
27
28 def three_flat_fields_introspection_query():
29 assert_valid("""
30 {
31 __type(name: "Query") {
32 trueFields: fields(includeDeprecated: true) {
33 name
34 }
35 falseFields: fields(includeDeprecated: false) {
36 name
37 }
38 omittedFields: fields {
39 name
40 }
41 }
42 }
43 """)
44
45 def three_fields_deep_introspection_query_from_schema():
46 assert_errors(
47 """
48 {
49 __schema {
50 types {
51 fields {
52 type {
53 fields {
54 type {
55 fields {
56 name
57 }
58 }
59 }
60 }
61 }
62 }
63 }
64 }
65 """,
66 [
67 {
68 "message": "Maximum introspection depth exceeded",
69 "locations": [(3, 15)],
70 },
71 ],
72 )
73
74 def three_interfaces_deep_introspection_query_from_schema():
75 assert_errors(
76 """
77 {
78 __schema {
79 types {
80 interfaces {
81 interfaces {
82 interfaces {
83 name
84 }
85 }
86 }
87 }
88 }
89 }
90 """,
91 [
92 {
93 "message": "Maximum introspection depth exceeded",
94 "locations": [(3, 15)],
95 },
96 ],
97 )
98
99 def three_possible_types_deep_introspection_query_from_schema():
100 assert_errors(
101 """
102 {
103 __schema {
104 types {
105 possibleTypes {
106 possibleTypes {
107 possibleTypes {
108 name
109 }
110 }
111 }
112 }
113 }
114 }
115 """,
116 [
117 {
118 "message": "Maximum introspection depth exceeded",
119 "locations": [(3, 15)],
120 },
121 ],
122 )
123
124 def three_input_fields_deep_introspection_query_from_schema():
125 assert_errors(
126 """
127 {
128 __schema {
129 types {
130 inputFields {
131 type {
132 inputFields {
133 type {
134 inputFields {
135 type {
136 name
137 }
138 }
139 }
140 }
141 }
142 }
143 }
144 }
145 }
146 """,
147 [
148 {
149 "message": "Maximum introspection depth exceeded",
150 "locations": [(3, 15)],
151 },
152 ],
153 )
154
155 def three_fields_deep_introspection_query_from_multiple_schema():
156 assert_errors(
157 """
158 {
159 one: __schema {
160 types {
161 fields {
162 type {
163 fields {
164 type {
165 fields {
166 name
167 }
168 }
169 }
170 }
171 }
172 }
173 }
174 two: __schema {
175 types {
176 fields {
177 type {
178 fields {
179 type {
180 fields {
181 name
182 }
183 }
184 }
185 }
186 }
187 }
188 }
189 three: __schema {
190 types {
191 fields {
192 type {
193 fields {
194 type {
195 fields {
196 name
197 }
198 }
199 }
200 }
201 }
202 }
203 }
204 }
205 """,
206 [
207 {
208 "message": "Maximum introspection depth exceeded",
209 "locations": [(3, 15)],
210 },
211 {
212 "message": "Maximum introspection depth exceeded",
213 "locations": [(18, 15)],
214 },
215 {
216 "message": "Maximum introspection depth exceeded",
217 "locations": [(33, 15)],
218 },
219 ],
220 )
221
222 def three_fields_deep_introspection_query_from_type():
223 assert_errors(
224 """
225 {
226 __type(name: "Query") {
227 types {
228 fields {
229 type {
230 fields {
231 type {
232 fields {
233 name
234 }
235 }
236 }
237 }
238 }
239 }
240 }
241 }
242 """,
243 [
244 {
245 "message": "Maximum introspection depth exceeded",
246 "locations": [(3, 15)],
247 },
248 ],
249 )
250
251 def three_fields_deep_introspection_query_from_multiple_type():
252 assert_errors(
253 """
254 {
255 one: __type(name: "Query") {
256 types {
257 fields {
258 type {
259 fields {
260 type {
261 fields {
262 name
263 }
264 }
265 }
266 }
267 }
268 }
269 }
270 two: __type(name: "Query") {
271 types {
272 fields {
273 type {
274 fields {
275 type {
276 fields {
277 name
278 }
279 }
280 }
281 }
282 }
283 }
284 }
285 three: __type(name: "Query") {
286 types {
287 fields {
288 type {
289 fields {
290 type {
291 fields {
292 name
293 }
294 }
295 }
296 }
297 }
298 }
299 }
300 }
301 """,
302 [
303 {
304 "message": "Maximum introspection depth exceeded",
305 "locations": [(3, 15)],
306 },
307 {
308 "message": "Maximum introspection depth exceeded",
309 "locations": [(18, 15)],
310 },
311 {
312 "message": "Maximum introspection depth exceeded",
313 "locations": [(33, 15)],
314 },
315 ],
316 )
317
318 def one_fields_deep_with_three_fields_introspection_query():
319 assert_valid("""
320 {
321 __schema {
322 types {
323 fields {
324 type {
325 oneFields: fields {
326 name
327 }
328 twoFields: fields {
329 name
330 }
331 threeFields: fields {
332 name
333 }
334 }
335 }
336 }
337 }
338 }
339 """)
340
341 def three_fields_deep_from_varying_parents_introspection_query():
342 assert_errors(
343 """
344 {
345 __schema {
346 types {
347 fields {
348 type {
349 fields {
350 type {
351 ofType {
352 fields {
353 name
354 }
355 }
356 }
357 }
358 }
359 }
360 }
361 }
362 }
363 """,
364 [
365 {
366 "message": "Maximum introspection depth exceeded",
367 "locations": [(3, 15)],
368 },
369 ],
370 )
371
372 def three_fields_deep_introspection_query_with_inline_fragments():
373 assert_errors(
374 """
375 query test {
376 __schema {
377 types {
378 ... on __Type {
379 fields {
380 type {
381 ... on __Type {
382 ofType {
383 fields {
384 type {
385 ... on __Type {
386 fields {
387 name
388 }
389 }
390 }
391 }
392 }
393 }
394 }
395 }
396 }
397 }
398 }
399 }
400 """,
401 [
402 {
403 "message": "Maximum introspection depth exceeded",
404 "locations": [(3, 15)],
405 },
406 ],
407 )
408
409 def three_fields_deep_introspection_query_with_fragments():
410 assert_errors(
411 """
412 query test {
413 __schema {
414 types {
415 ...One
416 }
417 }
418 }
419
420 fragment One on __Type {
421 fields {
422 type {
423 ...Two
424 }
425 }
426 }
427
428 fragment Two on __Type {
429 fields {
430 type {
431 ...Three
432 }
433 }
434 }
435
436 fragment Three on __Type {
437 fields {
438 name
439 }
440 }
441 """,
442 [
443 {
444 "message": "Maximum introspection depth exceeded",
445 "locations": [(3, 15)],
446 },
447 ],
448 )
449
450 def three_fields_deep_inline_fragment_on_query():
451 assert_errors(
452 """
453 {
454 ... {
455 __schema { types { fields { type { fields { type { fields { name } } } } } } }
456 }
457 }
458 """, # noqa: E501
459 [
460 {
461 "message": "Maximum introspection depth exceeded",
462 "locations": [(4, 17)],
463 },
464 ],
465 )
466
467 def opts_out_if_fragment_is_missing():
468 assert_valid("""
469 query test {
470 __schema {
471 types {
472 ...Missing
473 }
474 }
475 }
476 """)
477
478 def does_not_infinitely_recurse_on_fragment_cycle():
479 assert_valid("""
480 query test {
481 __schema {
482 types {
483 ...Cycle
484 }
485 }
486 }
487 fragment Cycle on __Type {
488 ...Cycle
489 }
490 """)