Skip to content

Commit f097786

Browse files
ddebrunnerDan Debrunner
andcommitted
Allow omitted non-null arguments when a default value exists (#7)
Co-authored-by: Dan Debrunner <[email protected]>
1 parent 1a9db88 commit f097786

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

definition.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,13 @@ func (st *Argument) Error() error {
656656
return nil
657657
}
658658

659+
// IsRequired returns if an argument is required,
660+
// i.e. cannot be omitted.
661+
func (st *Argument) IsRequired() bool {
662+
_, isOfTypeNonNull := st.Type.(*NonNull)
663+
return isOfTypeNonNull && st.DefaultValue == nil
664+
}
665+
659666
// Interface Type Definition
660667
//
661668
// When a field can return one of a heterogeneous set of types, a Interface type

rules.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,15 +1271,15 @@ func ProvidedNonNullArgumentsRule(context *ValidationContext) *ValidationRuleIns
12711271
for _, argDef := range fieldDef.Args {
12721272
argAST, _ := argASTMap[argDef.Name()]
12731273
if argAST == nil {
1274-
if argDefType, ok := argDef.Type.(*NonNull); ok {
1274+
if argDef.IsRequired() {
12751275
fieldName := ""
12761276
if fieldAST.Name != nil {
12771277
fieldName = fieldAST.Name.Value
12781278
}
12791279
reportError(
12801280
context,
12811281
fmt.Sprintf(`Field "%v" argument "%v" of type "%v" `+
1282-
`is required but not provided.`, fieldName, argDef.Name(), argDefType),
1282+
`is required but not provided.`, fieldName, argDef.Name(), argDef.Type),
12831283
[]ast.Node{fieldAST},
12841284
)
12851285
}
@@ -1312,15 +1312,15 @@ func ProvidedNonNullArgumentsRule(context *ValidationContext) *ValidationRuleIns
13121312
for _, argDef := range directiveDef.Args {
13131313
argAST, _ := argASTMap[argDef.Name()]
13141314
if argAST == nil {
1315-
if argDefType, ok := argDef.Type.(*NonNull); ok {
1315+
if argDef.IsRequired() {
13161316
directiveName := ""
13171317
if directiveAST.Name != nil {
13181318
directiveName = directiveAST.Name.Value
13191319
}
13201320
reportError(
13211321
context,
13221322
fmt.Sprintf(`Directive "@%v" argument "%v" of type `+
1323-
`"%v" is required but not provided.`, directiveName, argDef.Name(), argDefType),
1323+
`"%v" is required but not provided.`, directiveName, argDef.Name(), argDef.Type),
13241324
[]ast.Node{directiveAST},
13251325
)
13261326
}

rules_provided_non_null_arguments_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ func TestValidate_ProvidedNonNullArguments_ValidNonNullableValue_NoArgOnOptional
3636
}
3737
`)
3838
}
39+
func TestValidate_ProvidedNonNullArguments_ValidNonNullableValue_WithDefault(t *testing.T) {
40+
testutil.ExpectPassesRule(t, graphql.ProvidedNonNullArgumentsRule, `
41+
{
42+
complicatedArgs {
43+
nonNullFieldWithDefault
44+
}
45+
}
46+
`)
47+
}
48+
func TestValidate_ProvidedNonNullArguments_ValidNonNullableValue_WithDefaultAndValue(t *testing.T) {
49+
testutil.ExpectPassesRule(t, graphql.ProvidedNonNullArgumentsRule, `
50+
{
51+
complicatedArgs {
52+
nonNullFieldWithDefault(arg:1)
53+
}
54+
}
55+
`)
56+
}
3957
func TestValidate_ProvidedNonNullArguments_ValidNonNullableValue_MultipleArgs(t *testing.T) {
4058
testutil.ExpectPassesRule(t, graphql.ProvidedNonNullArgumentsRule, `
4159
{

testutil/rules_test_harness.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,15 @@ func init() {
383383
},
384384
},
385385
},
386+
"nonNullFieldWithDefault": &graphql.Field{
387+
Type: graphql.String,
388+
Args: graphql.FieldConfigArgument{
389+
"arg": &graphql.ArgumentConfig{
390+
Type: graphql.NewNonNull(graphql.Int),
391+
DefaultValue: 0,
392+
},
393+
},
394+
},
386395
"multipleOpts": &graphql.Field{
387396
Type: graphql.String,
388397
Args: graphql.FieldConfigArgument{

0 commit comments

Comments
 (0)