Skip to content

Commit 4bc24e2

Browse files
committed
Improve validation error for unused variable
Related GraphQL-js commit: graphql/graphql-js@1b639e3
1 parent 200c611 commit 4bc24e2

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

graphql/core/validation/rules/no_unused_variables.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,23 @@ def enter_OperationDefinition(self, node, key, parent, path, ancestors):
1515
def leave_OperationDefinition(self, operation, key, parent, path, ancestors):
1616
variable_name_used = set()
1717
usages = self.context.get_recursive_variable_usages(operation)
18+
op_name = operation.name and operation.name.value or None
1819

1920
for variable_usage in usages:
2021
variable_name_used.add(variable_usage.node.name.value)
2122

2223
for variable_definition in self.variable_definitions:
2324
if variable_definition.variable.name.value not in variable_name_used:
2425
self.context.report_error(GraphQLError(
25-
self.unused_variable_message(variable_definition.variable.name.value),
26+
self.unused_variable_message(variable_definition.variable.name.value, op_name),
2627
[variable_definition]
2728
))
2829

2930
def enter_VariableDefinition(self, node, key, parent, path, ancestors):
3031
self.variable_definitions.append(node)
3132

3233
@staticmethod
33-
def unused_variable_message(variable_name):
34+
def unused_variable_message(variable_name, op_name):
35+
if op_name:
36+
return 'Variable "${}" is never used in operation "{}".'.format(variable_name, op_name)
3437
return 'Variable "${}" is never used.'.format(variable_name)

tests/core_validation/test_no_unused_variables.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
from .utils import expect_fails_rule, expect_passes_rule
44

55

6-
def unused_variable(variable_name, line, column):
6+
def unused_variable(variable_name, op_name, line, column):
77
return {
8-
'message': NoUnusedVariables.unused_variable_message(variable_name),
8+
'message': NoUnusedVariables.unused_variable_message(variable_name, op_name),
99
'locations': [SourceLocation(line, column)]
1010
}
1111

1212

1313
def test_uses_all_variables():
1414
expect_passes_rule(NoUnusedVariables, '''
15-
query Foo($a: String, $b: String, $c: String) {
15+
query ($a: String, $b: String, $c: String) {
1616
field(a: $a, b: $b, c: $c)
1717
}
1818
''')
@@ -99,11 +99,11 @@ def test_variable_used_by_recursive_fragment():
9999

100100
def test_variable_not_used():
101101
expect_fails_rule(NoUnusedVariables, '''
102-
query Foo($a: String, $b: String, $c: String) {
102+
query ($a: String, $b: String, $c: String) {
103103
field(a: $a, b: $b)
104104
}
105105
''', [
106-
unused_variable('c', 2, 41)
106+
unused_variable('c', None, 2, 38)
107107
])
108108

109109

@@ -113,8 +113,8 @@ def test_multiple_variables_not_used():
113113
field(b: $b)
114114
}
115115
''', [
116-
unused_variable('a', 2, 17),
117-
unused_variable('c', 2, 41)
116+
unused_variable('a', 'Foo', 2, 17),
117+
unused_variable('c', 'Foo', 2, 41)
118118
])
119119

120120

@@ -137,7 +137,7 @@ def test_variable_not_used_in_fragments():
137137
field
138138
}
139139
''', [
140-
unused_variable('c', 2, 41)
140+
unused_variable('c', 'Foo', 2, 41)
141141
])
142142

143143

@@ -160,8 +160,8 @@ def test_multiple_variables_not_used_in_fragments():
160160
field
161161
}
162162
''', [
163-
unused_variable('a', 2, 17),
164-
unused_variable('c', 2, 41)
163+
unused_variable('a', 'Foo', 2, 17),
164+
unused_variable('c', 'Foo', 2, 41)
165165
])
166166

167167

@@ -177,7 +177,7 @@ def test_variable_not_used_by_unreferenced_fragment():
177177
field(b: $b)
178178
}
179179
''', [
180-
unused_variable('b', 2, 17),
180+
unused_variable('b', 'Foo', 2, 17),
181181
])
182182

183183

@@ -196,6 +196,6 @@ def test_variable_not_used_by_fragment_used_by_other_operation():
196196
field(b: $b)
197197
}
198198
''', [
199-
unused_variable('b', 2, 17),
200-
unused_variable('a', 5, 17),
199+
unused_variable('b', 'Foo', 2, 17),
200+
unused_variable('a', 'Bar', 5, 17),
201201
])

0 commit comments

Comments
 (0)