Debian Codegraph @

tests/validation/test_variables_in_allowed_position.py

raw
1 from functools import partial
2
3 from graphql.validation import VariablesInAllowedPositionRule
4
5 from .harness import assert_validation_errors
6
7 assert_errors = partial(assert_validation_errors, VariablesInAllowedPositionRule)
8
9 assert_valid = partial(assert_errors, errors=[])
10
11
12 def describe_validate_variables_are_in_allowed_positions():
13 def boolean_to_boolean():
14 assert_valid("""
15 query Query($booleanArg: Boolean)
16 {
17 complicatedArgs {
18 booleanArgField(booleanArg: $booleanArg)
19 }
20 }
21 """)
22
23 def boolean_to_boolean_in_fragment():
24 assert_valid("""
25 fragment booleanArgFrag on ComplicatedArgs {
26 booleanArgField(booleanArg: $booleanArg)
27 }
28 query Query($booleanArg: Boolean)
29 {
30 complicatedArgs {
31 ...booleanArgFrag
32 }
33 }
34 """)
35
36 assert_valid("""
37 query Query($booleanArg: Boolean)
38 {
39 complicatedArgs {
40 ...booleanArgFrag
41 }
42 }
43 fragment booleanArgFrag on ComplicatedArgs {
44 booleanArgField(booleanArg: $booleanArg)
45 }
46 """)
47
48 def non_null_boolean_to_boolean():
49 assert_valid("""
50 query Query($nonNullBooleanArg: Boolean!)
51 {
52 complicatedArgs {
53 booleanArgField(booleanArg: $nonNullBooleanArg)
54 }
55 }
56 """)
57
58 def non_null_boolean_to_boolean_within_fragment():
59 assert_valid("""
60 fragment booleanArgFrag on ComplicatedArgs {
61 booleanArgField(booleanArg: $nonNullBooleanArg)
62 }
63
64 query Query($nonNullBooleanArg: Boolean!)
65 {
66 complicatedArgs {
67 ...booleanArgFrag
68 }
69 }
70 """)
71
72 def array_of_string_to_array_of_string():
73 assert_valid("""
74 query Query($stringListVar: [String])
75 {
76 complicatedArgs {
77 stringListArgField(stringListArg: $stringListVar)
78 }
79 }
80 """)
81
82 def array_of_non_null_string_to_array_of_string():
83 assert_valid("""
84 query Query($stringListVar: [String!])
85 {
86 complicatedArgs {
87 stringListArgField(stringListArg: $stringListVar)
88 }
89 }
90 """)
91
92 def string_to_array_of_string_in_item_position():
93 assert_valid("""
94 query Query($stringVar: String)
95 {
96 complicatedArgs {
97 stringListArgField(stringListArg: [$stringVar])
98 }
99 }
100 """)
101
102 def non_null_string_to_array_of_string_in_item_position():
103 assert_valid("""
104 query Query($stringVar: String!)
105 {
106 complicatedArgs {
107 stringListArgField(stringListArg: [$stringVar])
108 }
109 }
110 """)
111
112 def complex_input_to_complex_input():
113 assert_valid("""
114 query Query($complexVar: ComplexInput)
115 {
116 complicatedArgs {
117 complexArgField(complexArg: $complexVar)
118 }
119 }
120 """)
121
122 def complex_input_to_complex_input_in_field_position():
123 assert_valid("""
124 query Query($boolVar: Boolean = false)
125 {
126 complicatedArgs {
127 complexArgField(complexArg: {requiredArg: $boolVar})
128 }
129 }
130 """)
131
132 def non_null_boolean_to_non_null_boolean_in_directive():
133 assert_valid("""
134 query Query($boolVar: Boolean!)
135 {
136 dog @include(if: $boolVar)
137 }
138 """)
139
140 def int_to_non_null_int():
141 assert_errors(
142 """
143 query Query($intArg: Int) {
144 complicatedArgs {
145 nonNullIntArgField(nonNullIntArg: $intArg)
146 }
147 }
148 """,
149 [
150 {
151 "message": "Variable '$intArg' of type 'Int'"
152 " used in position expecting type 'Int!'.",
153 "locations": [(2, 25), (4, 51)],
154 }
155 ],
156 )
157
158 def int_to_non_null_int_within_fragment():
159 assert_errors(
160 """
161 fragment nonNullIntArgFieldFrag on ComplicatedArgs {
162 nonNullIntArgField(nonNullIntArg: $intArg)
163 }
164
165 query Query($intArg: Int) {
166 complicatedArgs {
167 ...nonNullIntArgFieldFrag
168 }
169 }
170 """,
171 [
172 {
173 "message": "Variable '$intArg' of type 'Int'"
174 " used in position expecting type 'Int!'.",
175 "locations": [(6, 25), (3, 49)],
176 }
177 ],
178 )
179
180 def int_to_non_null_int_within_nested_fragment():
181 assert_errors(
182 """
183 fragment outerFrag on ComplicatedArgs {
184 ...nonNullIntArgFieldFrag
185 }
186
187 fragment nonNullIntArgFieldFrag on ComplicatedArgs {
188 nonNullIntArgField(nonNullIntArg: $intArg)
189 }
190
191 query Query($intArg: Int) {
192 complicatedArgs {
193 ...outerFrag
194 }
195 }
196 """,
197 [
198 {
199 "message": "Variable '$intArg' of type 'Int'"
200 " used in position expecting type 'Int!'.",
201 "locations": [(10, 25), (7, 49)],
202 }
203 ],
204 )
205
206 def string_to_boolean():
207 assert_errors(
208 """
209 query Query($stringVar: String) {
210 complicatedArgs {
211 booleanArgField(booleanArg: $stringVar)
212 }
213 }
214 """,
215 [
216 {
217 "message": "Variable '$stringVar' of type 'String'"
218 " used in position expecting type 'Boolean'.",
219 "locations": [(2, 25), (4, 45)],
220 }
221 ],
222 )
223
224 def string_to_array_of_string():
225 assert_errors(
226 """
227 query Query($stringVar: String) {
228 complicatedArgs {
229 stringListArgField(stringListArg: $stringVar)
230 }
231 }
232 """,
233 [
234 {
235 "message": "Variable '$stringVar' of type 'String'"
236 " used in position expecting type '[String]'.",
237 "locations": [(2, 25), (4, 51)],
238 }
239 ],
240 )
241
242 def boolean_to_non_null_boolean_in_directive():
243 assert_errors(
244 """
245 query Query($boolVar: Boolean) {
246 dog @include(if: $boolVar)
247 }
248 """,
249 [
250 {
251 "message": "Variable '$boolVar' of type 'Boolean'"
252 " used in position expecting type 'Boolean!'.",
253 "locations": [(2, 25), (3, 32)],
254 }
255 ],
256 )
257
258 def string_to_non_null_boolean_in_directive():
259 assert_errors(
260 """
261 query Query($stringVar: String) {
262 dog @include(if: $stringVar)
263 }
264 """,
265 [
266 {
267 "message": "Variable '$stringVar' of type 'String'"
268 " used in position expecting type 'Boolean!'.",
269 "locations": [(2, 25), (3, 32)],
270 }
271 ],
272 )
273
274 def array_of_string_to_array_of_non_null_string():
275 assert_errors(
276 """
277 query Query($stringListVar: [String])
278 {
279 complicatedArgs {
280 stringListNonNullArgField(stringListNonNullArg: $stringListVar)
281 }
282 }
283 """,
284 [
285 {
286 "message": "Variable '$stringListVar' of type '[String]'"
287 " used in position expecting type '[String!]'.",
288 "locations": [(2, 25), (5, 65)],
289 }
290 ],
291 )
292
293 def describe_allows_optional_nullable_variables_with_default_values():
294 def int_to_non_null_int_fails_when_var_provides_null_default_value():
295 assert_errors(
296 """
297 query Query($intVar: Int = null) {
298 complicatedArgs {
299 nonNullIntArgField(nonNullIntArg: $intVar)
300 }
301 }
302 """,
303 [
304 {
305 "message": "Variable '$intVar' of type 'Int'"
306 " used in position expecting type 'Int!'.",
307 "locations": [(2, 29), (4, 55)],
308 }
309 ],
310 )
311
312 def int_to_non_null_int_when_var_provides_non_null_default_value():
313 assert_valid("""
314 query Query($intVar: Int = 1) {
315 complicatedArgs {
316 nonNullIntArgField(nonNullIntArg: $intVar)
317 }
318 }
319 """)
320
321 def int_to_non_null_int_when_optional_arg_provides_default_value():
322 assert_valid("""
323 query Query($intVar: Int) {
324 complicatedArgs {
325 nonNullFieldWithDefault(nonNullIntArg: $intVar)
326 }
327 }
328 """)
329
330 def bool_to_non_null_bool_in_directive_with_default_value_with_option():
331 assert_valid("""
332 query Query($boolVar: Boolean = false) {
333 dog @include(if: $boolVar)
334 }
335 """)
336
337 def describe_validates_one_of_input_objects():
338 def allows_exactly_one_non_nullable_variable():
339 assert_valid("""
340 query ($string: String!) {
341 complicatedArgs {
342 oneOfArgField(oneOfArg: { stringField: $string })
343 }
344 }
345 """)
346
347 def undefined_variable_in_one_of_input_object():
348 assert_errors(
349 """
350 {
351 complicatedArgs {
352 oneOfArgField(oneOfArg: { stringField: $undefinedVariable })
353 }
354 }
355 """,
356 [],
357 )
358
359 def forbids_one_nullable_variable():
360 assert_errors(
361 """
362 query ($string: String) {
363 complicatedArgs {
364 oneOfArgField(oneOfArg: { stringField: $string })
365 }
366 }
367 """,
368 [
369 {
370 "message": "Variable '$string' is of type 'String'"
371 " but must be non-nullable to be used for OneOf"
372 " Input Object 'OneOfInput'.",
373 "locations": [(2, 24), (4, 60)],
374 }
375 ],
376 )