...

Source file src/github.com/vektah/gqlparser/v2/validator/rules/provided_required_arguments.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("ProvidedRequiredArguments", func(observers *Events, addError AddErrFunc) {
    12  		observers.OnField(func(walker *Walker, field *ast.Field) {
    13  			if field.Definition == nil {
    14  				return
    15  			}
    16  
    17  		argDef:
    18  			for _, argDef := range field.Definition.Arguments {
    19  				if !argDef.Type.NonNull {
    20  					continue
    21  				}
    22  				if argDef.DefaultValue != nil {
    23  					continue
    24  				}
    25  				for _, arg := range field.Arguments {
    26  					if arg.Name == argDef.Name {
    27  						continue argDef
    28  					}
    29  				}
    30  
    31  				addError(
    32  					Message(`Field "%s" argument "%s" of type "%s" is required, but it was not provided.`, field.Name, argDef.Name, argDef.Type.String()),
    33  					At(field.Position),
    34  				)
    35  			}
    36  		})
    37  
    38  		observers.OnDirective(func(walker *Walker, directive *ast.Directive) {
    39  			if directive.Definition == nil {
    40  				return
    41  			}
    42  
    43  		argDef:
    44  			for _, argDef := range directive.Definition.Arguments {
    45  				if !argDef.Type.NonNull {
    46  					continue
    47  				}
    48  				if argDef.DefaultValue != nil {
    49  					continue
    50  				}
    51  				for _, arg := range directive.Arguments {
    52  					if arg.Name == argDef.Name {
    53  						continue argDef
    54  					}
    55  				}
    56  
    57  				addError(
    58  					Message(`Directive "@%s" argument "%s" of type "%s" is required, but it was not provided.`, directive.Definition.Name, argDef.Name, argDef.Type.String()),
    59  					At(directive.Position),
    60  				)
    61  			}
    62  		})
    63  	})
    64  }
    65  

View as plain text