1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
package validator
import (
"github.com/vektah/gqlparser/v2/ast"
//nolint:revive // Validator rules each use dot imports for convenience.
. "github.com/vektah/gqlparser/v2/validator"
)
func init() {
AddRule("NoUndefinedVariables", func(observers *Events, addError AddErrFunc) {
observers.OnValue(func(walker *Walker, value *ast.Value) {
if walker.CurrentOperation == nil || value.Kind != ast.Variable || value.VariableDefinition != nil {
return
}
if walker.CurrentOperation.Name != "" {
addError(
Message(`Variable "%s" is not defined by operation "%s".`, value, walker.CurrentOperation.Name),
At(value.Position),
)
} else {
addError(
Message(`Variable "%s" is not defined.`, value),
At(value.Position),
)
}
})
})
}
|