Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions validator/unique_directives_per_location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package validator

import (
"github.com/vektah/gqlparser"
)

func init() {
directiveListVisitors = append(directiveListVisitors, uniqueDirectivesPerLocation)
}

// A GraphQL document is only valid if all directives at a given location are uniquely named.
func uniqueDirectivesPerLocation(ctx *vctx, parentDef *gqlparser.Definition, directives []gqlparser.Directive, location gqlparser.DirectiveLocation) {
seen := map[string]bool{}

for _, dir := range directives {
if seen[dir.Name] {
ctx.errors = append(ctx.errors, Error(
Rule("UniqueDirectivesPerLocation"),
Message(`The directive "%s" can only be used once at this location.`, dir.Name),
))
}
seen[dir.Name] = true
}
}
1 change: 1 addition & 0 deletions validator/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestSpec(t *testing.T) {
t.Run("ScalarLeafs", runSpec(schemas, deviations, "../spec/validation/ScalarLeafs.yml"))
t.Run("SingleFieldSubscriptions", runSpec(schemas, deviations, "../spec/validation/SingleFieldSubscriptions.yml"))
t.Run("UniqueArgumentNames", runSpec(schemas, deviations, "../spec/validation/UniqueArgumentNames.yml"))
t.Run("UniqueDirectivesPerLocation", runSpec(schemas, deviations, "../spec/validation/UniqueDirectivesPerLocation.yml"))
}

func runSpec(schemas []*gqlparser.Schema, deviations map[string]*Spec, filename string) func(t *testing.T) {
Expand Down
36 changes: 17 additions & 19 deletions validator/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var fieldVisitors []func(vctx *vctx, parentDef *gqlparser.Definition, fieldDef *
var fragmentVisitors []func(vctx *vctx, parentDef *gqlparser.Definition, fragment *gqlparser.FragmentDefinition)
var inlineFragmentVisitors []func(vctx *vctx, parentDef *gqlparser.Definition, inlineFragment *gqlparser.InlineFragment)
var directiveVisitors []func(vctx *vctx, parentDef *gqlparser.Definition, directiveDef *gqlparser.DirectiveDefinition, directive *gqlparser.Directive, location gqlparser.DirectiveLocation)
var directiveListVisitors []func(vctx *vctx, parentDef *gqlparser.Definition, directives []gqlparser.Directive, location gqlparser.DirectiveLocation)

func init() {
//fieldVisitors = append(fieldVisitors, func(vctx *vctx, parentDef *gqlparser.Definition, fieldDef *gqlparser.FieldDefinition, field *gqlparser.Field) {
Expand Down Expand Up @@ -53,9 +54,7 @@ func (c *vctx) walkOperation(operation *gqlparser.OperationDefinition) {
loc = gqlparser.LocationSubscription
}

for _, dir := range operation.Directives {
c.walkDirective(def, &dir, loc)
}
c.walkDirectives(def, operation.Directives, loc)

for _, v := range operation.SelectionSet {
c.walkSelection(def, v)
Expand All @@ -64,9 +63,8 @@ func (c *vctx) walkOperation(operation *gqlparser.OperationDefinition) {

func (c *vctx) walkFragment(it *gqlparser.FragmentDefinition) {
parentDef := c.schema.Types[it.TypeCondition.Name()]
if parentDef == nil {
return
}

c.walkDirectives(parentDef, it.Directives, gqlparser.LocationFragmentDefinition)

for _, v := range fragmentVisitors {
v(c, parentDef, it)
Expand All @@ -77,10 +75,16 @@ func (c *vctx) walkFragment(it *gqlparser.FragmentDefinition) {
}
}

func (c *vctx) walkDirective(parentDef *gqlparser.Definition, directive *gqlparser.Directive, location gqlparser.DirectiveLocation) {
def := c.schema.Directives[directive.Name]
for _, v := range directiveVisitors {
v(c, parentDef, def, directive, location)
func (c *vctx) walkDirectives(parentDef *gqlparser.Definition, directives []gqlparser.Directive, location gqlparser.DirectiveLocation) {
for _, v := range directiveListVisitors {
v(c, parentDef, directives, location)
}

for _, dir := range directives {
def := c.schema.Directives[dir.Name]
for _, v := range directiveVisitors {
v(c, parentDef, def, &dir, location)
}
}
}

Expand Down Expand Up @@ -110,9 +114,7 @@ func (c *vctx) walkSelection(parentDef *gqlparser.Definition, it gqlparser.Selec
c.walkSelection(nextParentDef, sel)
}

for _, dir := range it.Directives {
c.walkDirective(nextParentDef, &dir, gqlparser.LocationField)
}
c.walkDirectives(nextParentDef, it.Directives, gqlparser.LocationField)

case gqlparser.InlineFragment:
for _, v := range inlineFragmentVisitors {
Expand All @@ -124,9 +126,7 @@ func (c *vctx) walkSelection(parentDef *gqlparser.Definition, it gqlparser.Selec
nextParentDef = c.schema.Types[it.TypeCondition.Name()]
}

for _, dir := range it.Directives {
c.walkDirective(nextParentDef, &dir, gqlparser.LocationInlineFragment)
}
c.walkDirectives(nextParentDef, it.Directives, gqlparser.LocationInlineFragment)

for _, sel := range it.SelectionSet {
c.walkSelection(nextParentDef, sel)
Expand All @@ -140,9 +140,7 @@ func (c *vctx) walkSelection(parentDef *gqlparser.Definition, it gqlparser.Selec
nextParentDef = c.schema.Types[def.TypeCondition.Name()]
}

for _, dir := range it.Directives {
c.walkDirective(nextParentDef, &dir, gqlparser.LocationFragmentSpread)
}
c.walkDirectives(nextParentDef, it.Directives, gqlparser.LocationFragmentSpread)

if def != nil {
for _, sel := range def.SelectionSet {
Expand Down