Debian Codegraph @

tests/validation/test_known_directives.py

raw
1 from functools import partial
2
3 from graphql.utilities import build_schema
4 from graphql.validation import KnownDirectivesRule
5
6 from .harness import assert_validation_errors, assert_sdl_validation_errors
7
8 schema_with_directives = build_schema("""
9 type Query {
10 dummy: String
11 }
12
13 directive @onQuery on QUERY
14 directive @onMutation on MUTATION
15 directive @onSubscription on SUBSCRIPTION
16 directive @onField on FIELD
17 directive @onFragmentDefinition on FRAGMENT_DEFINITION
18 directive @onFragmentSpread on FRAGMENT_SPREAD
19 directive @onInlineFragment on INLINE_FRAGMENT
20 directive @onVariableDefinition on VARIABLE_DEFINITION
21 """)
22
23 schema_with_sdl_directives = build_schema("""
24 directive @onSchema on SCHEMA
25 directive @onScalar on SCALAR
26 directive @onObject on OBJECT
27 directive @onFieldDefinition on FIELD_DEFINITION
28 directive @onArgumentDefinition on ARGUMENT_DEFINITION
29 directive @onInterface on INTERFACE
30 directive @onUnion on UNION
31 directive @onEnum on ENUM
32 directive @onEnumValue on ENUM_VALUE
33 directive @onInputObject on INPUT_OBJECT
34 directive @onInputFieldDefinition on INPUT_FIELD_DEFINITION
35 directive @onDirective on DIRECTIVE_DEFINITION
36 """)
37
38 assert_errors = partial(
39 assert_validation_errors, KnownDirectivesRule, schema=schema_with_directives
40 )
41
42 assert_valid = partial(assert_errors, errors=[])
43
44 assert_sdl_errors = partial(assert_sdl_validation_errors, KnownDirectivesRule)
45
46 assert_sdl_valid = partial(assert_sdl_errors, errors=[])
47
48
49 def describe_known_directives():
50 def with_no_directives():
51 assert_valid("""
52 query Foo {
53 name
54 ...Frag
55 }
56
57 fragment Frag on Dog {
58 name
59 }
60 """)
61
62 def with_standard_directives():
63 assert_valid("""
64 {
65 human @skip(if: false) {
66 name
67 pets {
68 ... on Dog @include(if: true) {
69 name
70 }
71 }
72 }
73 }
74 """)
75
76 def with_unknown_directive():
77 assert_errors(
78 """
79 {
80 human @unknown(directive: "value") {
81 name
82 }
83 }
84 """,
85 [{"message": "Unknown directive '@unknown'.", "locations": [(3, 21)]}],
86 )
87
88 def with_many_unknown_directives():
89 assert_errors(
90 """
91 {
92 __typename @unknown
93 human @unknown {
94 name
95 pets @unknown {
96 name
97 }
98 }
99 }
100 """,
101 [
102 {"message": "Unknown directive '@unknown'.", "locations": [(3, 26)]},
103 {"message": "Unknown directive '@unknown'.", "locations": [(4, 21)]},
104 {"message": "Unknown directive '@unknown'.", "locations": [(6, 22)]},
105 ],
106 )
107
108 def with_well_placed_directives():
109 assert_valid("""
110 query ($var: Boolean @onVariableDefinition) @onQuery {
111 human @onField {
112 ...Frag @onFragmentSpread
113 ... @onInlineFragment {
114 name @onField
115 }
116 }
117 }
118
119 mutation @onMutation {
120 someField @onField
121 }
122
123 subscription @onSubscription {
124 someField @onField
125 }
126
127 fragment Frag on Human @onFragmentDefinition {
128 name @onField
129 }
130 """)
131
132 def with_misplaced_directives():
133 assert_errors(
134 """
135 query ($var: Boolean @onQuery) @onMutation {
136 human @onQuery {
137 ...Frag @onQuery
138 ... @onQuery {
139 name @onQuery
140 }
141 }
142 }
143
144 mutation @onQuery {
145 someField @onQuery
146 }
147
148 subscription @onQuery {
149 someField @onQuery
150 }
151
152 fragment Frag on Human @onQuery {
153 name @onQuery
154 }
155 """,
156 [
157 {
158 "message": "Directive '@onQuery'"
159 " may not be used on variable definition.",
160 "locations": [(2, 34)],
161 },
162 {
163 "message": "Directive '@onMutation' may not be used on query.",
164 "locations": [(2, 44)],
165 },
166 {
167 "message": "Directive '@onQuery' may not be used on field.",
168 "locations": [(3, 21)],
169 },
170 {
171 "message": "Directive '@onQuery'"
172 " may not be used on fragment spread.",
173 "locations": [(4, 25)],
174 },
175 {
176 "message": "Directive '@onQuery'"
177 " may not be used on inline fragment.",
178 "locations": [(5, 21)],
179 },
180 {
181 "message": "Directive '@onQuery' may not be used on field.",
182 "locations": [(6, 24)],
183 },
184 {
185 "message": "Directive '@onQuery' may not be used on mutation.",
186 "locations": [(11, 22)],
187 },
188 {
189 "message": "Directive '@onQuery' may not be used on field.",
190 "locations": [(12, 25)],
191 },
192 {
193 "message": "Directive '@onQuery' may not be used on subscription.",
194 "locations": [(15, 26)],
195 },
196 {
197 "message": "Directive '@onQuery' may not be used on field.",
198 "locations": [(16, 25)],
199 },
200 {
201 "message": "Directive '@onQuery'"
202 " may not be used on fragment definition.",
203 "locations": [(19, 36)],
204 },
205 {
206 "message": "Directive '@onQuery' may not be used on field.",
207 "locations": [(20, 20)],
208 },
209 ],
210 )
211
212 def with_misplaced_variable_definition_directive():
213 assert_errors(
214 """
215 query Foo($var: Boolean @onField) {
216 name
217 }
218 """,
219 [
220 {
221 "message": "Directive '@onField'"
222 " may not be used on variable definition.",
223 "locations": [(2, 37)],
224 },
225 ],
226 )
227
228 def describe_within_sdl():
229 def with_directive_defined_inside_sdl():
230 assert_sdl_valid("""
231 type Query {
232 foo: String @test
233 }
234
235 directive @test on FIELD_DEFINITION
236 """)
237
238 def with_standard_directive():
239 assert_sdl_valid("""
240 type Query {
241 foo: String @deprecated
242 }
243 """)
244
245 def with_overridden_standard_directive():
246 assert_sdl_valid("""
247 schema @deprecated {
248 query: Query
249 }
250 directive @deprecated on SCHEMA
251 """)
252
253 def with_directive_defined_in_schema_extension():
254 schema = build_schema("""
255 type Query {
256 foo: String
257 }
258 """)
259 assert_sdl_valid(
260 """
261 directive @test on OBJECT
262
263 extend type Query @test
264 """,
265 schema=schema,
266 )
267
268 def with_directive_used_in_schema_extension():
269 schema = build_schema("""
270 directive @test on OBJECT
271
272 type Query {
273 foo: String
274 }
275 """)
276 assert_sdl_valid(
277 """
278 extend type Query @test
279 """,
280 schema=schema,
281 )
282
283 def with_unknown_directive_in_schema_extension():
284 schema = build_schema("""
285 type Query {
286 foo: String
287 }
288 """)
289 assert_sdl_errors(
290 """
291 extend type Query @unknown
292 """,
293 [{"message": "Unknown directive '@unknown'.", "locations": [(2, 35)]}],
294 schema,
295 )
296
297 def with_well_placed_directives():
298 assert_sdl_valid(
299 """
300 type MyObj implements MyInterface @onObject {
301 myField(myArg: Int @onArgumentDefinition): String @onFieldDefinition
302 }
303
304 extend type MyObj @onObject
305
306 scalar MyScalar @onScalar
307
308 extend scalar MyScalar @onScalar
309
310 interface MyInterface @onInterface {
311 myField(myArg: Int @onArgumentDefinition): String @onFieldDefinition
312 }
313
314 extend interface MyInterface @onInterface
315
316 union MyUnion @onUnion = MyObj | Other
317
318 extend union MyUnion @onUnion
319
320 enum MyEnum @onEnum {
321 MY_VALUE @onEnumValue
322 }
323
324 extend enum MyEnum @onEnum
325
326 input MyInput @onInputObject {
327 myField: Int @onInputFieldDefinition
328 }
329
330 extend input MyInput @onInputObject
331
332 schema @onSchema {
333 query: MyQuery
334 }
335
336 extend schema @onSchema
337
338 directive @myDirective on OBJECT
339 """,
340 schema=schema_with_sdl_directives,
341 )
342
343 def with_misplaced_directives():
344 assert_sdl_errors(
345 """
346 type MyObj implements MyInterface @onInterface {
347 myField(myArg: Int @onInputFieldDefinition): String @onInputFieldDefinition
348 }
349
350 scalar MyScalar @onEnum
351
352 interface MyInterface @onObject {
353 myField(myArg: Int @onInputFieldDefinition): String @onInputFieldDefinition
354 }
355
356 union MyUnion @onEnumValue = MyObj | Other
357
358 enum MyEnum @onScalar {
359 MY_VALUE @onUnion
360 }
361
362 input MyInput @onEnum {
363 myField: Int @onArgumentDefinition
364 }
365
366 schema @onObject {
367 query: MyQuery
368 }
369
370 extend schema @onObject
371
372 extend type MyObj @onDirective
373 """, # noqa: E501
374 [
375 {
376 "message": "Directive '@onInterface'"
377 " may not be used on object.",
378 "locations": [(2, 51)],
379 },
380 {
381 "message": "Directive '@onInputFieldDefinition'"
382 " may not be used on argument definition.",
383 "locations": [(3, 38)],
384 },
385 {
386 "message": "Directive '@onInputFieldDefinition'"
387 " may not be used on field definition.",
388 "locations": [(3, 71)],
389 },
390 {
391 "message": "Directive '@onEnum' may not be used on scalar.",
392 "locations": [(6, 33)],
393 },
394 {
395 "message": "Directive '@onObject'"
396 " may not be used on interface.",
397 "locations": [(8, 39)],
398 },
399 {
400 "message": "Directive '@onInputFieldDefinition'"
401 " may not be used on argument definition.",
402 "locations": [(9, 38)],
403 },
404 {
405 "message": "Directive '@onInputFieldDefinition'"
406 " may not be used on field definition.",
407 "locations": [(9, 71)],
408 },
409 {
410 "message": "Directive '@onEnumValue' may not be used on union.",
411 "locations": [(12, 31)],
412 },
413 {
414 "message": "Directive '@onScalar' may not be used on enum.",
415 "locations": [(14, 29)],
416 },
417 {
418 "message": "Directive '@onUnion'"
419 " may not be used on enum value.",
420 "locations": [(15, 28)],
421 },
422 {
423 "message": "Directive '@onEnum'"
424 " may not be used on input object.",
425 "locations": [(18, 31)],
426 },
427 {
428 "message": "Directive '@onArgumentDefinition'"
429 " may not be used on input field definition.",
430 "locations": [(19, 32)],
431 },
432 {
433 "message": "Directive '@onObject' may not be used on schema.",
434 "locations": [(22, 24)],
435 },
436 {
437 "message": "Directive '@onObject' may not be used on schema.",
438 "locations": [(26, 31)],
439 },
440 {
441 "message": "Directive '@onDirective'"
442 " may not be used on object.",
443 "locations": [(28, 35)],
444 },
445 ],
446 schema_with_sdl_directives,
447 )