Skip to content

Commit d05ad5d

Browse files
committed
fix(rules): match spec for defaulted required args
This adds a new test, that fails on master, alongside with the correct changes for it to work as expected. The validation was failing for arguments that are defined as required, but have a default value, preventing a non-required argument to override their value, while it should work as expected.
1 parent fd79992 commit d05ad5d

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

rules_variables_in_allowed_position_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,13 @@ func TestValidate_VariablesInAllowedPosition_StringToNonNullableBooleanInDirecti
244244
`expecting type "Boolean!".`, 2, 19, 3, 26),
245245
})
246246
}
247+
func TestValidate_VariablesInAllowedPosition_IntToNonNullableIntArgDefaulted(t *testing.T) {
248+
testutil.ExpectPassesRule(t, graphql.VariablesInAllowedPositionRule, `
249+
query Query($intArg: Int)
250+
{
251+
complicatedArgs {
252+
nonNullIntArgFieldWithDefault(nonNullIntArgWithDefault: $intArg)
253+
}
254+
}
255+
`)
256+
}

testutil/rules_test_harness.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,15 @@ func init() {
316316
},
317317
},
318318
},
319+
"nonNullIntArgFieldWithDefault": &graphql.Field{
320+
Type: graphql.String,
321+
Args: graphql.FieldConfigArgument{
322+
"nonNullIntArgWithDefault": &graphql.ArgumentConfig{
323+
Type: graphql.NewNonNull(graphql.Int),
324+
DefaultValue: 1,
325+
},
326+
},
327+
},
319328
"stringArgField": &graphql.Field{
320329
Type: graphql.String,
321330
Args: graphql.FieldConfigArgument{

validator.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,13 @@ func (ctx *ValidationContext) VariableUsages(node HasSelectionSet) []*VariableUs
238238
kinds.Variable: {
239239
Kind: func(p visitor.VisitFuncParams) (string, interface{}) {
240240
if node, ok := p.Node.(*ast.Variable); ok && node != nil {
241+
inputType := typeInfo.InputType()
242+
if nonNull, ok := inputType.(*NonNull); ok && typeInfo.argument != nil && typeInfo.argument.DefaultValue != nil {
243+
inputType = nonNull.OfType
244+
}
241245
usages = append(usages, &VariableUsage{
242246
Node: node,
243-
Type: typeInfo.InputType(),
247+
Type: inputType,
244248
})
245249
}
246250
return visitor.ActionNoChange, nil

0 commit comments

Comments
 (0)