Debian Codegraph @

tests/validation/test_no_undefined_variables.py

raw
1 from functools import partial
2
3 from graphql.validation import NoUndefinedVariablesRule
4
5 from .harness import assert_validation_errors
6
7 assert_errors = partial(assert_validation_errors, NoUndefinedVariablesRule)
8
9 assert_valid = partial(assert_errors, errors=[])
10
11
12 def describe_validate_no_undefined_variables():
13 def all_variables_defined():
14 assert_valid("""
15 query Foo($a: String, $b: String, $c: String) {
16 field(a: $a, b: $b, c: $c)
17 }
18 """)
19
20 def all_variables_deeply_defined():
21 assert_valid("""
22 query Foo($a: String, $b: String, $c: String) {
23 field(a: $a) {
24 field(b: $b) {
25 field(c: $c)
26 }
27 }
28 }
29 """)
30
31 def all_variables_deeply_in_inline_fragments_defined():
32 assert_valid("""
33 query Foo($a: String, $b: String, $c: String) {
34 ... on Type {
35 field(a: $a) {
36 field(b: $b) {
37 ... on Type {
38 field(c: $c)
39 }
40 }
41 }
42 }
43 }
44 """)
45
46 def all_variables_in_fragments_deeply_defined():
47 assert_valid("""
48 query Foo($a: String, $b: String, $c: String) {
49 ...FragA
50 }
51 fragment FragA on Type {
52 field(a: $a) {
53 ...FragB
54 }
55 }
56 fragment FragB on Type {
57 field(b: $b) {
58 ...FragC
59 }
60 }
61 fragment FragC on Type {
62 field(c: $c)
63 }
64 """)
65
66 def variable_within_single_fragment_defined_in_multiple_operations():
67 assert_valid("""
68 query Foo($a: String) {
69 ...FragA
70 }
71 query Bar($a: String) {
72 ...FragA
73 }
74 fragment FragA on Type {
75 field(a: $a)
76 }
77 """)
78
79 def variable_within_fragments_defined_in_operations():
80 assert_valid("""
81 query Foo($a: String) {
82 ...FragA
83 }
84 query Bar($b: String) {
85 ...FragB
86 }
87 fragment FragA on Type {
88 field(a: $a)
89 }
90 fragment FragB on Type {
91 field(b: $b)
92 }
93 """)
94
95 def variable_within_recursive_fragment_defined():
96 assert_valid("""
97 query Foo($a: String) {
98 ...FragA
99 }
100 fragment FragA on Type {
101 field(a: $a) {
102 ...FragA
103 }
104 }
105 """)
106
107 def variable_not_defined():
108 assert_errors(
109 """
110 query Foo($a: String, $b: String, $c: String) {
111 field(a: $a, b: $b, c: $c, d: $d)
112 }
113 """,
114 [
115 {
116 "message": "Variable '$d' is not defined by operation 'Foo'.",
117 "locations": [(3, 45), (2, 13)],
118 },
119 ],
120 )
121
122 def variable_not_defined_by_unnamed_query():
123 assert_errors(
124 """
125 {
126 field(a: $a)
127 }
128 """,
129 [
130 {
131 "message": "Variable '$a' is not defined.",
132 "locations": [(3, 24), (2, 13)],
133 },
134 ],
135 )
136
137 def multiple_variables_not_defined():
138 assert_errors(
139 """
140 query Foo($b: String) {
141 field(a: $a, b: $b, c: $c)
142 }
143 """,
144 [
145 {
146 "message": "Variable '$a' is not defined by operation 'Foo'.",
147 "locations": [(3, 24), (2, 13)],
148 },
149 {
150 "message": "Variable '$c' is not defined by operation 'Foo'.",
151 "locations": [(3, 38), (2, 13)],
152 },
153 ],
154 )
155
156 def variable_in_fragment_not_defined_by_unnamed_query():
157 assert_errors(
158 """
159 {
160 ...FragA
161 }
162 fragment FragA on Type {
163 field(a: $a)
164 }
165 """,
166 [
167 {
168 "message": "Variable '$a' is not defined.",
169 "locations": [(6, 24), (2, 13)],
170 },
171 ],
172 )
173
174 def variable_in_fragment_not_defined_by_operation():
175 assert_errors(
176 """
177 query Foo($a: String, $b: String) {
178 ...FragA
179 }
180 fragment FragA on Type {
181 field(a: $a) {
182 ...FragB
183 }
184 }
185 fragment FragB on Type {
186 field(b: $b) {
187 ...FragC
188 }
189 }
190 fragment FragC on Type {
191 field(c: $c)
192 }
193 """,
194 [
195 {
196 "message": "Variable '$c' is not defined by operation 'Foo'.",
197 "locations": [(16, 24), (2, 13)],
198 },
199 ],
200 )
201
202 def multiple_variables_in_fragments_not_defined():
203 assert_errors(
204 """
205 query Foo($b: String) {
206 ...FragA
207 }
208 fragment FragA on Type {
209 field(a: $a) {
210 ...FragB
211 }
212 }
213 fragment FragB on Type {
214 field(b: $b) {
215 ...FragC
216 }
217 }
218 fragment FragC on Type {
219 field(c: $c)
220 }
221 """,
222 [
223 {
224 "message": "Variable '$a' is not defined by operation 'Foo'.",
225 "locations": [(6, 24), (2, 13)],
226 },
227 {
228 "message": "Variable '$c' is not defined by operation 'Foo'.",
229 "locations": [(16, 24), (2, 13)],
230 },
231 ],
232 )
233
234 def single_variable_in_fragment_not_defined_by_multiple_operations():
235 assert_errors(
236 """
237 query Foo($a: String) {
238 ...FragAB
239 }
240 query Bar($a: String) {
241 ...FragAB
242 }
243 fragment FragAB on Type {
244 field(a: $a, b: $b)
245 }
246 """,
247 [
248 {
249 "message": "Variable '$b' is not defined by operation 'Foo'.",
250 "locations": [(9, 31), (2, 13)],
251 },
252 {
253 "message": "Variable '$b' is not defined by operation 'Bar'.",
254 "locations": [(9, 31), (5, 13)],
255 },
256 ],
257 )
258
259 def variables_in_fragment_not_defined_by_multiple_operations():
260 assert_errors(
261 """
262 query Foo($b: String) {
263 ...FragAB
264 }
265 query Bar($a: String) {
266 ...FragAB
267 }
268 fragment FragAB on Type {
269 field(a: $a, b: $b)
270 }
271 """,
272 [
273 {
274 "message": "Variable '$a' is not defined by operation 'Foo'.",
275 "locations": [(9, 24), (2, 13)],
276 },
277 {
278 "message": "Variable '$b' is not defined by operation 'Bar'.",
279 "locations": [(9, 31), (5, 13)],
280 },
281 ],
282 )
283
284 def variable_in_fragment_used_by_other_operation():
285 assert_errors(
286 """
287 query Foo($b: String) {
288 ...FragA
289 }
290 query Bar($a: String) {
291 ...FragB
292 }
293 fragment FragA on Type {
294 field(a: $a)
295 }
296 fragment FragB on Type {
297 field(b: $b)
298 }
299 """,
300 [
301 {
302 "message": "Variable '$a' is not defined by operation 'Foo'.",
303 "locations": [(9, 24), (2, 13)],
304 },
305 {
306 "message": "Variable '$b' is not defined by operation 'Bar'.",
307 "locations": [(12, 24), (5, 13)],
308 },
309 ],
310 )
311
312 def multiple_undefined_variables_produce_multiple_errors():
313 assert_errors(
314 """
315 query Foo($b: String) {
316 ...FragAB
317 }
318 query Bar($a: String) {
319 ...FragAB
320 }
321 fragment FragAB on Type {
322 field1(a: $a, b: $b)
323 ...FragC
324 field3(a: $a, b: $b)
325 }
326 fragment FragC on Type {
327 field2(c: $c)
328 }
329 """,
330 [
331 {
332 "message": "Variable '$a' is not defined by operation 'Foo'.",
333 "locations": [(9, 25), (2, 13)],
334 },
335 {
336 "message": "Variable '$a' is not defined by operation 'Foo'.",
337 "locations": [(11, 25), (2, 13)],
338 },
339 {
340 "message": "Variable '$c' is not defined by operation 'Foo'.",
341 "locations": [(14, 25), (2, 13)],
342 },
343 {
344 "message": "Variable '$b' is not defined by operation 'Bar'.",
345 "locations": [(9, 32), (5, 13)],
346 },
347 {
348 "message": "Variable '$b' is not defined by operation 'Bar'.",
349 "locations": [(11, 32), (5, 13)],
350 },
351 {
352 "message": "Variable '$c' is not defined by operation 'Bar'.",
353 "locations": [(14, 25), (5, 13)],
354 },
355 ],
356 )