diff --git a/validator/core/walk.go b/validator/core/walk.go index 4e4c9b30..8a8bc4c0 100644 --- a/validator/core/walk.go +++ b/validator/core/walk.go @@ -295,6 +295,7 @@ func (w *Walker) walkSelection(parentDef *ast.Definition, it ast.Selection) { if def != nil && !w.validatedFragmentSpreads[def.Name] { // prevent infinite recursion w.validatedFragmentSpreads[def.Name] = true + w.walkDirectives(nextParentDef, def.Directives, ast.LocationFragmentDefinition) w.walkSelectionSet(nextParentDef, def.SelectionSet) } diff --git a/validator/validator_test.go b/validator/validator_test.go index 8e1cc82f..39088e48 100644 --- a/validator/validator_test.go +++ b/validator/validator_test.go @@ -223,6 +223,31 @@ func TestNoUnusedVariablesWithRules(t *testing.T) { require.NoError(t, err) require.Nil(t, validator.ValidateWithRules(s, q, nil)) }) + + t.Run("variable used in fragment definition directive", func(t *testing.T) { + s := gqlparser.MustLoadSchema( + &ast.Source{Name: "graph/schema.graphqls", Input: ` + directive @testDirective(x: Int) on FRAGMENT_DEFINITION + + type Query { + bar: String! + } + `, BuiltIn: false}, + ) + + q, err := parser.ParseQuery(&ast.Source{Name: "fragmentDefinitionDirective", Input: ` + query Foo($x: Int) { + ...Bar + } + + fragment Bar on Query @testDirective(x: $x) { + bar + } + `}) + require.NoError(t, err) + + require.Nil(t, validator.ValidateWithRules(s, q, nil)) + }) } func TestCustomRuleSet(t *testing.T) {