...

Source file src/github.com/vektah/gqlparser/v2/validator/rules/known_directives.go

Documentation: github.com/vektah/gqlparser/v2/validator/rules

     1  package validator
     2  
     3  import (
     4  	"github.com/vektah/gqlparser/v2/ast"
     5  
     6  	//nolint:revive // Validator rules each use dot imports for convenience.
     7  	. "github.com/vektah/gqlparser/v2/validator"
     8  )
     9  
    10  func init() {
    11  	AddRule("KnownDirectives", func(observers *Events, addError AddErrFunc) {
    12  		type mayNotBeUsedDirective struct {
    13  			Name   string
    14  			Line   int
    15  			Column int
    16  		}
    17  		var seen = map[mayNotBeUsedDirective]bool{}
    18  		observers.OnDirective(func(walker *Walker, directive *ast.Directive) {
    19  			if directive.Definition == nil {
    20  				addError(
    21  					Message(`Unknown directive "@%s".`, directive.Name),
    22  					At(directive.Position),
    23  				)
    24  				return
    25  			}
    26  
    27  			for _, loc := range directive.Definition.Locations {
    28  				if loc == directive.Location {
    29  					return
    30  				}
    31  			}
    32  
    33  			// position must be exists if directive.Definition != nil
    34  			tmp := mayNotBeUsedDirective{
    35  				Name:   directive.Name,
    36  				Line:   directive.Position.Line,
    37  				Column: directive.Position.Column,
    38  			}
    39  
    40  			if !seen[tmp] {
    41  				addError(
    42  					Message(`Directive "@%s" may not be used on %s.`, directive.Name, directive.Location),
    43  					At(directive.Position),
    44  				)
    45  				seen[tmp] = true
    46  			}
    47  		})
    48  	})
    49  }
    50  

View as plain text