Skip to content

Commit 1d14db7

Browse files
committed
[Validation] Include variable definition node when reporting bad var type
1 parent 81e7596 commit 1d14db7

File tree

2 files changed

+23
-29
lines changed

2 files changed

+23
-29
lines changed

src/validation/__tests__/VariablesInAllowedPosition.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,14 @@ describe('Validate: Variables are in allowed positions', () => {
177177

178178
it('Int => Int!', () => {
179179
expectFailsRule(VariablesInAllowedPosition, `
180-
query Query($intArg: Int)
181-
{
180+
query Query($intArg: Int) {
182181
complicatedArgs {
183182
nonNullIntArgField(nonNullIntArg: $intArg)
184183
}
185184
}
186185
`, [
187186
{ message: badVarPosMessage('intArg', 'Int', 'Int!'),
188-
locations: [ { line: 5, column: 45 } ] }
187+
locations: [ { line: 4, column: 45 }, { line: 2, column: 19 } ] },
189188
]);
190189
});
191190

@@ -195,15 +194,14 @@ describe('Validate: Variables are in allowed positions', () => {
195194
nonNullIntArgField(nonNullIntArg: $intArg)
196195
}
197196
198-
query Query($intArg: Int)
199-
{
197+
query Query($intArg: Int) {
200198
complicatedArgs {
201199
...nonNullIntArgFieldFrag
202200
}
203201
}
204202
`, [
205203
{ message: badVarPosMessage('intArg', 'Int', 'Int!'),
206-
locations: [ { line: 3, column: 43 } ] }
204+
locations: [ { line: 3, column: 43 }, { line: 6, column: 19 } ] }
207205
]);
208206
});
209207

@@ -217,67 +215,62 @@ describe('Validate: Variables are in allowed positions', () => {
217215
nonNullIntArgField(nonNullIntArg: $intArg)
218216
}
219217
220-
query Query($intArg: Int)
221-
{
218+
query Query($intArg: Int) {
222219
complicatedArgs {
223220
...outerFrag
224221
}
225222
}
226223
`, [
227224
{ message: badVarPosMessage('intArg', 'Int', 'Int!'),
228-
locations: [ { line: 7, column: 43 } ] }
225+
locations: [ { line: 7, column: 43 }, { line: 10, column: 19 } ] }
229226
]);
230227
});
231228

232229
it('String over Boolean', () => {
233230
expectFailsRule(VariablesInAllowedPosition, `
234-
query Query($stringVar: String)
235-
{
231+
query Query($stringVar: String) {
236232
complicatedArgs {
237233
booleanArgField(booleanArg: $stringVar)
238234
}
239235
}
240236
`, [
241237
{ message: badVarPosMessage('stringVar', 'String', 'Boolean'),
242-
locations: [ { line: 5, column: 39 } ] }
238+
locations: [ { line: 4, column: 39 }, { line: 2, column: 19 } ] }
243239
]);
244240
});
245241

246242
it('String => [String]', () => {
247243
expectFailsRule(VariablesInAllowedPosition, `
248-
query Query($stringVar: String)
249-
{
244+
query Query($stringVar: String) {
250245
complicatedArgs {
251246
stringListArgField(stringListArg: $stringVar)
252247
}
253248
}
254249
`, [
255250
{ message: badVarPosMessage('stringVar', 'String', '[String]'),
256-
locations: [ { line: 5, column: 45 } ] }
251+
locations: [ { line: 4, column: 45 }, { line: 2, column: 19 } ] }
257252
]);
258253
});
259254

260255
it('Boolean => Boolean! in directive', () => {
261256
expectFailsRule(VariablesInAllowedPosition, `
262-
query Query($boolVar: Boolean)
263-
{
257+
query Query($boolVar: Boolean) {
264258
dog @include(if: $boolVar)
265259
}
266260
`, [
267261
{ message: badVarPosMessage('boolVar', 'Boolean', 'Boolean!'),
268-
locations: [ { line: 4, column: 26 } ] }
262+
locations: [ { line: 3, column: 26 }, { line: 2, column: 19 } ] }
269263
]);
270264
});
271265

272266
it('String => Boolean! in directive', () => {
273267
expectFailsRule(VariablesInAllowedPosition, `
274-
query Query($stringVar: String)
275-
{
268+
query Query($stringVar: String) {
276269
dog @include(if: $stringVar)
277270
}
278271
`, [
279272
{ message: badVarPosMessage('stringVar', 'String', 'Boolean!'),
280-
locations: [ { line: 4, column: 26 } ] }
273+
locations: [ { line: 3, column: 26 }, { line: 2, column: 19 } ] }
281274
]);
282275
});
283276

src/validation/rules/VariablesInAllowedPosition.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ export function VariablesInAllowedPosition(context: ValidationContext): any {
4040
usages.forEach(({ node, type }) => {
4141
const varName = node.name.value;
4242
const varDef = varDefMap[varName];
43-
const varType =
44-
varDef && typeFromAST(context.getSchema(), varDef.type);
45-
if (varType && type &&
46-
!varTypeAllowedForType(effectiveType(varType, varDef), type)) {
47-
context.reportError(new GraphQLError(
48-
badVarPosMessage(varName, varType, type),
49-
[ node ]
50-
));
43+
if (varDef && type) {
44+
const varType = typeFromAST(context.getSchema(), varDef.type);
45+
if (varType &&
46+
!varTypeAllowedForType(effectiveType(varType, varDef), type)) {
47+
context.reportError(new GraphQLError(
48+
badVarPosMessage(varName, varType, type),
49+
[ node, varDef ]
50+
));
51+
}
5152
}
5253
});
5354
}

0 commit comments

Comments
 (0)