...

Source file src/github.com/vektah/gqlparser/v2/validator/rules/unique_variable_names.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("UniqueVariableNames", func(observers *Events, addError AddErrFunc) {
    12  		observers.OnOperation(func(walker *Walker, operation *ast.OperationDefinition) {
    13  			seen := map[string]int{}
    14  			for _, def := range operation.VariableDefinitions {
    15  				// add the same error only once per a variable.
    16  				if seen[def.Variable] == 1 {
    17  					addError(
    18  						Message(`There can be only one variable named "$%s".`, def.Variable),
    19  						At(def.Position),
    20  					)
    21  				}
    22  				seen[def.Variable]++
    23  			}
    24  		})
    25  	})
    26  }
    27  

View as plain text