Skip to content

Commit 15ce17b

Browse files
committed
Improve validation error for unused variable
Commit: 1b639e3a6538d2184e1a2b96c410d164a20c08ed [1b639e3] Parents: 71b7b4aa2d Author: Lee Byron <[email protected]> Date: 2 February 2016 at 11:07:08 AM SGT
1 parent 8e99c57 commit 15ce17b

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

rules.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,13 @@ func NoUnusedFragmentsRule(context *ValidationContext) *ValidationRuleInstance {
10001000
}
10011001
}
10021002

1003+
func UnusedVariableMessage(varName string, opName string) string {
1004+
if opName != "" {
1005+
return fmt.Sprintf(`Variable "$%v" is never used in operation "%v".`, varName, opName)
1006+
}
1007+
return fmt.Sprintf(`Variable "$%v" is never used.`, varName)
1008+
}
1009+
10031010
/**
10041011
* NoUnusedVariablesRule
10051012
* No unused variables
@@ -1037,10 +1044,14 @@ func NoUnusedVariablesRule(context *ValidationContext) *ValidationRuleInstance {
10371044
if variableDef != nil && variableDef.Variable != nil && variableDef.Variable.Name != nil {
10381045
variableName = variableDef.Variable.Name.Value
10391046
}
1047+
opName := ""
1048+
if operation.Name != nil {
1049+
opName = operation.Name.Value
1050+
}
10401051
if res, ok := variableNameUsed[variableName]; !ok || !res {
10411052
reportError(
10421053
context,
1043-
fmt.Sprintf(`Variable "$%v" is never used.`, variableName),
1054+
UnusedVariableMessage(variableName, opName),
10441055
[]ast.Node{variableDef},
10451056
)
10461057
}

rules_no_unused_variables_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
func TestValidate_NoUnusedVariables_UsesAllVariables(t *testing.T) {
1212
testutil.ExpectPassesRule(t, graphql.NoUnusedVariablesRule, `
13-
query Foo($a: String, $b: String, $c: String) {
13+
query ($a: String, $b: String, $c: String) {
1414
field(a: $a, b: $b, c: $c)
1515
}
1616
`)
@@ -91,11 +91,11 @@ func TestValidate_NoUnusedVariables_VariableUsedByRecursiveFragment(t *testing.T
9191
}
9292
func TestValidate_NoUnusedVariables_VariableNotUsed(t *testing.T) {
9393
testutil.ExpectFailsRule(t, graphql.NoUnusedVariablesRule, `
94-
query Foo($a: String, $b: String, $c: String) {
94+
query ($a: String, $b: String, $c: String) {
9595
field(a: $a, b: $b)
9696
}
9797
`, []gqlerrors.FormattedError{
98-
testutil.RuleError(`Variable "$c" is never used.`, 2, 41),
98+
testutil.RuleError(`Variable "$c" is never used.`, 2, 38),
9999
})
100100
}
101101
func TestValidate_NoUnusedVariables_MultipleVariablesNotUsed(t *testing.T) {
@@ -104,8 +104,8 @@ func TestValidate_NoUnusedVariables_MultipleVariablesNotUsed(t *testing.T) {
104104
field(b: $b)
105105
}
106106
`, []gqlerrors.FormattedError{
107-
testutil.RuleError(`Variable "$a" is never used.`, 2, 17),
108-
testutil.RuleError(`Variable "$c" is never used.`, 2, 41),
107+
testutil.RuleError(`Variable "$a" is never used in operation "Foo".`, 2, 17),
108+
testutil.RuleError(`Variable "$c" is never used in operation "Foo".`, 2, 41),
109109
})
110110
}
111111
func TestValidate_NoUnusedVariables_VariableNotUsedInFragments(t *testing.T) {
@@ -127,7 +127,7 @@ func TestValidate_NoUnusedVariables_VariableNotUsedInFragments(t *testing.T) {
127127
field
128128
}
129129
`, []gqlerrors.FormattedError{
130-
testutil.RuleError(`Variable "$c" is never used.`, 2, 41),
130+
testutil.RuleError(`Variable "$c" is never used in operation "Foo".`, 2, 41),
131131
})
132132
}
133133
func TestValidate_NoUnusedVariables_MultipleVariablesNotUsed2(t *testing.T) {
@@ -149,8 +149,8 @@ func TestValidate_NoUnusedVariables_MultipleVariablesNotUsed2(t *testing.T) {
149149
field
150150
}
151151
`, []gqlerrors.FormattedError{
152-
testutil.RuleError(`Variable "$a" is never used.`, 2, 17),
153-
testutil.RuleError(`Variable "$c" is never used.`, 2, 41),
152+
testutil.RuleError(`Variable "$a" is never used in operation "Foo".`, 2, 17),
153+
testutil.RuleError(`Variable "$c" is never used in operation "Foo".`, 2, 41),
154154
})
155155
}
156156
func TestValidate_NoUnusedVariables_VariableNotUsedByUnreferencedFragment(t *testing.T) {
@@ -165,7 +165,7 @@ func TestValidate_NoUnusedVariables_VariableNotUsedByUnreferencedFragment(t *tes
165165
field(b: $b)
166166
}
167167
`, []gqlerrors.FormattedError{
168-
testutil.RuleError(`Variable "$b" is never used.`, 2, 17),
168+
testutil.RuleError(`Variable "$b" is never used in operation "Foo".`, 2, 17),
169169
})
170170
}
171171
func TestValidate_NoUnusedVariables_VariableNotUsedByFragmentUsedByOtherOperation(t *testing.T) {
@@ -183,7 +183,7 @@ func TestValidate_NoUnusedVariables_VariableNotUsedByFragmentUsedByOtherOperatio
183183
field(b: $b)
184184
}
185185
`, []gqlerrors.FormattedError{
186-
testutil.RuleError(`Variable "$b" is never used.`, 2, 17),
187-
testutil.RuleError(`Variable "$a" is never used.`, 5, 17),
186+
testutil.RuleError(`Variable "$b" is never used in operation "Foo".`, 2, 17),
187+
testutil.RuleError(`Variable "$a" is never used in operation "Bar".`, 5, 17),
188188
})
189189
}

0 commit comments

Comments
 (0)