...

Source file src/github.com/vektah/gqlparser/v2/validator/rules/no_unused_variables.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("NoUnusedVariables", func(observers *Events, addError AddErrFunc) {
    12  		observers.OnOperation(func(walker *Walker, operation *ast.OperationDefinition) {
    13  			for _, varDef := range operation.VariableDefinitions {
    14  				if varDef.Used {
    15  					continue
    16  				}
    17  
    18  				if operation.Name != "" {
    19  					addError(
    20  						Message(`Variable "$%s" is never used in operation "%s".`, varDef.Variable, operation.Name),
    21  						At(varDef.Position),
    22  					)
    23  				} else {
    24  					addError(
    25  						Message(`Variable "$%s" is never used.`, varDef.Variable),
    26  						At(varDef.Position),
    27  					)
    28  				}
    29  			}
    30  		})
    31  	})
    32  }
    33  

View as plain text