Skip to content

Commit 3d9e204

Browse files
authored
fix(VariablesInAllowedPositionRule): allow fragments with variables prior to operations (#4837)
1 parent 92275cd commit 3d9e204

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

src/validation/__tests__/VariablesInAllowedPositionRule-test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,28 @@ describe('Validate: Variables are in allowed positions', () => {
397397
});
398398

399399
describe('Fragment arguments are validated', () => {
400+
it('validates fragment variables defined before the operation', () => {
401+
expectErrors(`
402+
fragment A($intVar: Int) on ComplicatedArgs {
403+
nonNullIntArgField(nonNullIntArg: $intVar)
404+
}
405+
query Query($intVar: Int!) {
406+
complicatedArgs {
407+
...A(i: $intVar)
408+
}
409+
}
410+
`).toDeepEqual([
411+
{
412+
message:
413+
'Variable "$intVar" of type "Int" used in position expecting type "Int!".',
414+
locations: [
415+
{ line: 2, column: 20 },
416+
{ line: 3, column: 45 },
417+
],
418+
},
419+
]);
420+
});
421+
400422
it('Boolean => Boolean', () => {
401423
expectValid(`
402424
query Query($booleanArg: Boolean)

src/validation/rules/VariablesInAllowedPositionRule.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,13 @@ export function VariablesInAllowedPositionRule(
6666

6767
return {
6868
OperationDefinition: {
69-
enter() {
69+
enter(operation) {
7070
varDefMap = new Map();
71+
if (operation.variableDefinitions) {
72+
for (const varDef of operation.variableDefinitions) {
73+
varDefMap.set(varDef.variable.name.value, varDef);
74+
}
75+
}
7176
},
7277
leave(operation) {
7378
const usages = context.getRecursiveVariableUsages(operation);
@@ -125,9 +130,6 @@ export function VariablesInAllowedPositionRule(
125130
}
126131
},
127132
},
128-
VariableDefinition(node) {
129-
varDefMap.set(node.variable.name.value, node);
130-
},
131133
};
132134
}
133135

0 commit comments

Comments
 (0)