Skip to content

Commit

Permalink
Allow omitted non-null arguments when a default value exists (#7)
Browse files Browse the repository at this point in the history
Co-authored-by: Dan Debrunner <dan@stepzen.com>
  • Loading branch information
ddebrunner and Dan Debrunner committed Mar 23, 2021
1 parent 1a9db88 commit f097786
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
7 changes: 7 additions & 0 deletions definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,13 @@ func (st *Argument) Error() error {
return nil
}

// IsRequired returns if an argument is required,
// i.e. cannot be omitted.
func (st *Argument) IsRequired() bool {
_, isOfTypeNonNull := st.Type.(*NonNull)
return isOfTypeNonNull && st.DefaultValue == nil
}

// Interface Type Definition
//
// When a field can return one of a heterogeneous set of types, a Interface type
Expand Down
8 changes: 4 additions & 4 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -1271,15 +1271,15 @@ func ProvidedNonNullArgumentsRule(context *ValidationContext) *ValidationRuleIns
for _, argDef := range fieldDef.Args {
argAST, _ := argASTMap[argDef.Name()]
if argAST == nil {
if argDefType, ok := argDef.Type.(*NonNull); ok {
if argDef.IsRequired() {
fieldName := ""
if fieldAST.Name != nil {
fieldName = fieldAST.Name.Value
}
reportError(
context,
fmt.Sprintf(`Field "%v" argument "%v" of type "%v" `+
`is required but not provided.`, fieldName, argDef.Name(), argDefType),
`is required but not provided.`, fieldName, argDef.Name(), argDef.Type),
[]ast.Node{fieldAST},
)
}
Expand Down Expand Up @@ -1312,15 +1312,15 @@ func ProvidedNonNullArgumentsRule(context *ValidationContext) *ValidationRuleIns
for _, argDef := range directiveDef.Args {
argAST, _ := argASTMap[argDef.Name()]
if argAST == nil {
if argDefType, ok := argDef.Type.(*NonNull); ok {
if argDef.IsRequired() {
directiveName := ""
if directiveAST.Name != nil {
directiveName = directiveAST.Name.Value
}
reportError(
context,
fmt.Sprintf(`Directive "@%v" argument "%v" of type `+
`"%v" is required but not provided.`, directiveName, argDef.Name(), argDefType),
`"%v" is required but not provided.`, directiveName, argDef.Name(), argDef.Type),
[]ast.Node{directiveAST},
)
}
Expand Down
18 changes: 18 additions & 0 deletions rules_provided_non_null_arguments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ func TestValidate_ProvidedNonNullArguments_ValidNonNullableValue_NoArgOnOptional
}
`)
}
func TestValidate_ProvidedNonNullArguments_ValidNonNullableValue_WithDefault(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.ProvidedNonNullArgumentsRule, `
{
complicatedArgs {
nonNullFieldWithDefault
}
}
`)
}
func TestValidate_ProvidedNonNullArguments_ValidNonNullableValue_WithDefaultAndValue(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.ProvidedNonNullArgumentsRule, `
{
complicatedArgs {
nonNullFieldWithDefault(arg:1)
}
}
`)
}
func TestValidate_ProvidedNonNullArguments_ValidNonNullableValue_MultipleArgs(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.ProvidedNonNullArgumentsRule, `
{
Expand Down
9 changes: 9 additions & 0 deletions testutil/rules_test_harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,15 @@ func init() {
},
},
},
"nonNullFieldWithDefault": &graphql.Field{
Type: graphql.String,
Args: graphql.FieldConfigArgument{
"arg": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.Int),
DefaultValue: 0,
},
},
},
"multipleOpts": &graphql.Field{
Type: graphql.String,
Args: graphql.FieldConfigArgument{
Expand Down

0 comments on commit f097786

Please sign in to comment.