Skip to content

Commit b1bfadf

Browse files
committed
[Validation] Include variable definition node when reporting bad var type
Related GraphQL-js commits: graphql/graphql-js@1d14db7, graphql/graphql-js@e81cf39
1 parent 68633ff commit b1bfadf

File tree

2 files changed

+21
-27
lines changed

2 files changed

+21
-27
lines changed

graphql/core/validation/rules/variables_in_allowed_position.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ def leave_OperationDefinition(self, operation, key, parent, path, ancestors):
2525
type = usage.type
2626
var_name = node.name.value
2727
var_def = self.var_def_map.get(var_name)
28-
var_type = var_def and type_from_ast(self.context.get_schema(), var_def.type)
29-
if var_type and type and not self.var_type_allowed_for_type(self.effective_type(var_type, var_def), type):
30-
self.context.report_error(GraphQLError(
31-
self.bad_var_pos_message(var_name, var_type, type),
32-
[node]
33-
))
28+
if var_def and type:
29+
var_type = type_from_ast(self.context.get_schema(), var_def.type)
30+
if var_type and not self.var_type_allowed_for_type(self.effective_type(var_type, var_def), type):
31+
self.context.report_error(GraphQLError(
32+
self.bad_var_pos_message(var_name, var_type, type),
33+
[var_def, node]
34+
))
3435

3536
def enter_VariableDefinition(self, node, key, parent, path, ancestors):
3637
self.var_def_map[node.variable.name.value] = node

tests/core_validation/test_variables_in_allowed_position.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -153,31 +153,29 @@ def test_boolean_non_null_boolean_in_directive_with_default():
153153

154154
def test_int_non_null_int():
155155
expect_fails_rule(VariablesInAllowedPosition, '''
156-
query Query($intArg: Int)
157-
{
156+
query Query($intArg: Int) {
158157
complicatedArgs {
159158
nonNullIntArgField(nonNullIntArg: $intArg)
160159
}
161160
}
162161
''', [
163162
{ 'message': VariablesInAllowedPosition.bad_var_pos_message('intArg', 'Int', 'Int!'),
164-
'locations': [SourceLocation(5, 45)] }
163+
'locations': [SourceLocation(4, 45), SourceLocation(2, 19)] }
165164
])
166165

167166
def test_int_non_null_int_within_fragment():
168167
expect_fails_rule(VariablesInAllowedPosition, '''
169168
fragment nonNullIntArgFieldFrag on ComplicatedArgs {
170169
nonNullIntArgField(nonNullIntArg: $intArg)
171170
}
172-
query Query($intArg: Int)
173-
{
171+
query Query($intArg: Int) {
174172
complicatedArgs {
175173
...nonNullIntArgFieldFrag
176174
}
177175
}
178176
''', [
179177
{ 'message': VariablesInAllowedPosition.bad_var_pos_message('intArg', 'Int', 'Int!'),
180-
'locations': [SourceLocation(3, 43)] }
178+
'locations': [SourceLocation(5, 19), SourceLocation(3, 43)] }
181179
])
182180

183181
def test_int_non_null_int_within_nested_fragment():
@@ -188,61 +186,56 @@ def test_int_non_null_int_within_nested_fragment():
188186
fragment nonNullIntArgFieldFrag on ComplicatedArgs {
189187
nonNullIntArgField(nonNullIntArg: $intArg)
190188
}
191-
query Query($intArg: Int)
192-
{
189+
query Query($intArg: Int) {
193190
complicatedArgs {
194191
...outerFrag
195192
}
196193
}
197194
''', [
198195
{ 'message': VariablesInAllowedPosition.bad_var_pos_message('intArg', 'Int', 'Int!'),
199-
'locations': [SourceLocation(6, 43)] }
196+
'locations': [SourceLocation(8, 19), SourceLocation(6, 43)] }
200197
])
201198

202199
def test_string_over_boolean():
203200
expect_fails_rule(VariablesInAllowedPosition, '''
204-
query Query($stringVar: String)
205-
{
201+
query Query($stringVar: String) {
206202
complicatedArgs {
207203
booleanArgField(booleanArg: $stringVar)
208204
}
209205
}
210206
''', [
211207
{ 'message': VariablesInAllowedPosition.bad_var_pos_message('stringVar', 'String', 'Boolean'),
212-
'locations': [SourceLocation(5, 39)] }
208+
'locations': [SourceLocation(2, 19), SourceLocation(4, 39)] }
213209
])
214210

215211
def test_string_string_fail():
216212
expect_fails_rule(VariablesInAllowedPosition, '''
217-
query Query($stringVar: String)
218-
{
213+
query Query($stringVar: String) {
219214
complicatedArgs {
220215
stringListArgField(stringListArg: $stringVar)
221216
}
222217
}
223218
''', [
224219
{ 'message': VariablesInAllowedPosition.bad_var_pos_message('stringVar', 'String', '[String]'),
225-
'locations': [SourceLocation(5, 45)]}
220+
'locations': [SourceLocation(2, 19), SourceLocation(4, 45)]}
226221
])
227222

228223
def test_boolean_non_null_boolean_in_directive():
229224
expect_fails_rule(VariablesInAllowedPosition, '''
230-
query Query($boolVar: Boolean)
231-
{
225+
query Query($boolVar: Boolean) {
232226
dog @include(if: $boolVar)
233227
}
234228
''', [
235229
{ 'message': VariablesInAllowedPosition.bad_var_pos_message('boolVar', 'Boolean', 'Boolean!'),
236-
'locations': [SourceLocation(4, 26)]
230+
'locations': [SourceLocation(2, 19), SourceLocation(3, 26)]
237231
}])
238232

239233
def test_string_non_null_boolean_in_directive():
240234
expect_fails_rule(VariablesInAllowedPosition, '''
241-
query Query($stringVar: String)
242-
{
235+
query Query($stringVar: String) {
243236
dog @include(if: $stringVar)
244237
}
245238
''', [
246239
{ 'message': VariablesInAllowedPosition.bad_var_pos_message('stringVar', 'String', 'Boolean!'),
247-
'locations': [SourceLocation(4, 26)] }
240+
'locations': [SourceLocation(2, 19), SourceLocation(3, 26)] }
248241
])

0 commit comments

Comments
 (0)