Skip to content

Commit cd89e95

Browse files
committed
Add test cases for NoUndefined/NoUnused/InAllowedPosition
1 parent 6667c65 commit cd89e95

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

src/validation/__tests__/NoUndefinedVariablesRule-test.ts

+63
Original file line numberDiff line numberDiff line change
@@ -404,4 +404,67 @@ describe('Validate: No undefined variables', () => {
404404
},
405405
]);
406406
});
407+
408+
it('fragment defined arguments are not undefined variables', () => {
409+
expectValid(`
410+
query Foo {
411+
...FragA
412+
}
413+
fragment FragA($a: String) on Type {
414+
field1(a: $a)
415+
}
416+
`);
417+
});
418+
419+
it('defined variables used as fragment arguments are not undefined variables', () => {
420+
expectValid(`
421+
query Foo($b: String) {
422+
...FragA(a: $b)
423+
}
424+
fragment FragA($a: String) on Type {
425+
field1
426+
}
427+
`);
428+
});
429+
430+
it('variables used as fragment arguments may be undefined variables', () => {
431+
expectErrors(`
432+
query Foo {
433+
...FragA(a: $a)
434+
}
435+
fragment FragA($a: String) on Type {
436+
field1
437+
}
438+
`).toDeepEqual([
439+
{
440+
message: 'Variable "$a" is not defined by operation "Foo".',
441+
locations: [
442+
{ line: 3, column: 21 },
443+
{ line: 2, column: 7 },
444+
],
445+
},
446+
]);
447+
});
448+
449+
it('variables shadowed by parent fragment arguments are still undefined variables', () => {
450+
expectErrors(`
451+
query Foo {
452+
...FragA
453+
}
454+
fragment FragA($a: String) on Type {
455+
...FragB
456+
}
457+
fragment FragB on Type {
458+
field1(a: $a)
459+
}
460+
`).toDeepEqual([
461+
{
462+
message: 'Variable "$a" is not defined by operation "Foo".',
463+
locations: [
464+
{ line: 9, column: 19 },
465+
{ line: 2, column: 7 },
466+
],
467+
},
468+
]);
469+
});
407470
});

src/validation/__tests__/NoUnusedVariablesRule-test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,26 @@ describe('Validate: No unused variables', () => {
230230
},
231231
]);
232232
});
233+
234+
it('fragment defined arguments are not unused variables', () => {
235+
expectValid(`
236+
query Foo {
237+
...FragA
238+
}
239+
fragment FragA($a: String) on Type {
240+
field1(a: $a)
241+
}
242+
`);
243+
});
244+
245+
it('defined variables used as fragment arguments are not unused variables', () => {
246+
expectValid(`
247+
query Foo($b: String) {
248+
...FragA(a: $b)
249+
}
250+
fragment FragA($a: String) on Type {
251+
field1
252+
}
253+
`);
254+
});
233255
});

src/validation/__tests__/VariablesInAllowedPositionRule-test.ts

+61
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,65 @@ describe('Validate: Variables are in allowed positions', () => {
357357
}`);
358358
});
359359
});
360+
361+
describe('Fragment arguments are validated', () => {
362+
it('Boolean => Boolean', () => {
363+
expectValid(`
364+
query Query($booleanArg: Boolean)
365+
{
366+
complicatedArgs {
367+
...A(b: $booleanArg)
368+
}
369+
}
370+
fragment A($b: Boolean) on ComplicatedArgs {
371+
booleanArgField(booleanArg: $b)
372+
}
373+
`);
374+
});
375+
376+
it('Boolean => Boolean!', () => {
377+
expectErrors(`
378+
query Query($fb: Boolean)
379+
{
380+
complicatedArgs {
381+
...A(b: $fb)
382+
}
383+
}
384+
fragment A($b: Boolean!) on ComplicatedArgs {
385+
booleanArgField(booleanArg: $b)
386+
}
387+
`).toDeepEqual([
388+
{
389+
message:
390+
'Variable "$booleanArg" of type "Boolean" used in position expecting type "Boolean!".',
391+
locations: [
392+
{ line: 2, column: 21 },
393+
{ line: 4, column: 47 },
394+
],
395+
},
396+
]);
397+
});
398+
399+
it('Int => Int! fails when variable provides null default value', () => {
400+
expectErrors(`
401+
query Query($intVar: Int = null) {
402+
complicatedArgs {
403+
...A(i: $intVar)
404+
}
405+
}
406+
fragment A($i: Int!) on ComplicatedArgs {
407+
nonNullIntArgField(nonNullIntArg: $i)
408+
}
409+
`).toDeepEqual([
410+
{
411+
message:
412+
'Variable "$intVar" of type "Int" used in position expecting type "Int!".',
413+
locations: [
414+
{ line: 2, column: 21 },
415+
{ line: 4, column: 47 },
416+
],
417+
},
418+
]);
419+
});
420+
});
360421
});

0 commit comments

Comments
 (0)