Skip to content

Commit 5b41269

Browse files
authored
fix(validator): allow nullable variables for nonnull args with default (#396)
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. Given the tests are imported from JS, I'm not sure how to add a new one there. Here's an example schema and query to reproduce this: ``` type Query { user(username: String! = "foo"): User } type User { username: String! } ``` ``` query Foo($username: String) { user(username: $username) { username } } ```
1 parent fecb70c commit 5b41269

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

ast/value.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ type Value struct {
2929
Comment *CommentGroup
3030

3131
// Require validation
32-
Definition *Definition
33-
VariableDefinition *VariableDefinition
34-
ExpectedType *Type
32+
Definition *Definition
33+
VariableDefinition *VariableDefinition
34+
ExpectedType *Type
35+
ExpectedTypeHasDefault bool
3536
}
3637

3738
type ChildValue struct {

validator/core/walk.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ func (w *Walker) walkValue(value *ast.Value) {
182182
fieldDef := value.Definition.Fields.ForName(child.Name)
183183
if fieldDef != nil {
184184
child.Value.ExpectedType = fieldDef.Type
185+
child.Value.ExpectedTypeHasDefault = fieldDef.DefaultValue != nil && fieldDef.DefaultValue.Kind != ast.NullValue
185186
child.Value.Definition = w.Schema.Types[fieldDef.Type.Name()]
186187
}
187188
}
@@ -208,6 +209,7 @@ func (w *Walker) walkValue(value *ast.Value) {
208209
func (w *Walker) walkArgument(argDef *ast.ArgumentDefinition, arg *ast.Argument) {
209210
if argDef != nil {
210211
arg.Value.ExpectedType = argDef.Type
212+
arg.Value.ExpectedTypeHasDefault = argDef.DefaultValue != nil && argDef.DefaultValue.Kind != ast.NullValue
211213
arg.Value.Definition = w.Schema.Types[argDef.Type.Name()]
212214
}
213215

validator/rules/variables_in_allowed_position.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ var VariablesInAllowedPositionRule = Rule{
2525
}
2626
}
2727

28+
// If the expected type has a default, the given variable can be null
29+
if value.ExpectedTypeHasDefault {
30+
tmp.NonNull = false
31+
}
32+
2833
if !value.VariableDefinition.Type.IsCompatible(&tmp) {
2934
addError(
3035
Message(

0 commit comments

Comments
 (0)