Skip to content

Commit

Permalink
Implement KnownFragmentNames validator
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame authored and vektah committed Jul 11, 2018
1 parent e01bb21 commit fb3fc1d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
20 changes: 20 additions & 0 deletions validator/known_fragment_names.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package validator

import (
"fmt"

"github.com/vektah/gqlparser"
)

func init() {
addRule("KnownFragmentNames", func(observers *Events, addError addErrFunc) {
observers.OnFragmentSpread(func(walker *Walker, parentDef *gqlparser.Definition, fragmentDef *gqlparser.FragmentDefinition, fragmentSpread *gqlparser.FragmentSpread) {
if fragmentDef != nil {
return
}

message := fmt.Sprintf(`Unknown fragment "%s".`, fragmentSpread.Name)
addError(Message(message))
})
})
}
1 change: 1 addition & 0 deletions validator/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestSpec(t *testing.T) {
t.Run("FragmentsOnCompositeTypes", runSpec(schemas, deviations, "../spec/validation/FragmentsOnCompositeTypes.yml"))
t.Run("KnownArgumentNames", runSpec(schemas, deviations, "../spec/validation/KnownArgumentNames.yml"))
t.Run("KnownDirectives", runSpec(schemas, deviations, "../spec/validation/KnownDirectives.yml"))
t.Run("KnownFragmentNames", runSpec(schemas, deviations, "../spec/validation/KnownFragmentNames.yml"))

t.Run("LoneAnonymousOperation", runSpec(schemas, deviations, "../spec/validation/LoneAnonymousOperation.yml"))

Expand Down
8 changes: 8 additions & 0 deletions validator/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Events struct {
field []func(walker *Walker, parentDef *gqlparser.Definition, fieldDef *gqlparser.FieldDefinition, field *gqlparser.Field)
fragment []func(walker *Walker, parentDef *gqlparser.Definition, fragment *gqlparser.FragmentDefinition)
inlineFragment []func(walker *Walker, parentDef *gqlparser.Definition, inlineFragment *gqlparser.InlineFragment)
fragmentSpread []func(walker *Walker, parentDef *gqlparser.Definition, fragmentDef *gqlparser.FragmentDefinition, fragmentSpread *gqlparser.FragmentSpread)
directive []func(walker *Walker, parentDef *gqlparser.Definition, directiveDef *gqlparser.DirectiveDefinition, directive *gqlparser.Directive, location gqlparser.DirectiveLocation)
directiveList []func(walker *Walker, parentDef *gqlparser.Definition, directives []gqlparser.Directive, location gqlparser.DirectiveLocation)
value []func(walker *Walker, value gqlparser.Value)
Expand All @@ -31,6 +32,9 @@ func (o *Events) OnFragment(f func(walker *Walker, parentDef *gqlparser.Definiti
func (o *Events) OnInlineFragment(f func(walker *Walker, parentDef *gqlparser.Definition, inlineFragment *gqlparser.InlineFragment)) {
o.inlineFragment = append(o.inlineFragment, f)
}
func (o *Events) OnFragmentSpread(f func(walker *Walker, parentDef *gqlparser.Definition, fragmentDef *gqlparser.FragmentDefinition, fragmentSpread *gqlparser.FragmentSpread)) {
o.fragmentSpread = append(o.fragmentSpread, f)
}
func (o *Events) OnDirective(f func(walker *Walker, parentDef *gqlparser.Definition, directiveDef *gqlparser.DirectiveDefinition, directive *gqlparser.Directive, location gqlparser.DirectiveLocation)) {
o.directive = append(o.directive, f)
}
Expand Down Expand Up @@ -189,6 +193,10 @@ func (w *Walker) walkSelection(parentDef *gqlparser.Definition, it gqlparser.Sel
case gqlparser.FragmentSpread:
def := w.Document.GetFragment(it.Name)

for _, v := range w.Observers.fragmentSpread {
v(w, parentDef, def, &it)
}

var nextParentDef *gqlparser.Definition
if def != nil {
nextParentDef = w.Schema.Types[def.TypeCondition.Name()]
Expand Down

0 comments on commit fb3fc1d

Please sign in to comment.