Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow omitted non-null arguments when a default value exists (#7) #602

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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