Skip to content

Commit

Permalink
Added support for [Int] to have nil(null) values, same for String and…
Browse files Browse the repository at this point in the history
… Boolean
  • Loading branch information
Arun Poudel committed Jul 1, 2019
1 parent 05741cd commit 1ab3e4f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions validator/testdata/vars.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ type Query {
structArg(i: InputType!): Boolean!
defaultStructArg(i: InputType! = {name: "foo"}): Boolean!
arrayArg(i: [InputType!]): Boolean!
intArrayArg(i: [Int]): Boolean!
stringArrayArg(i: [String]): Boolean!
boolArrayArg(i: [Boolean]): Boolean!
}

input InputType {
Expand Down
5 changes: 5 additions & 0 deletions validator/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ func (v *varValidator) validateVarType(typ *ast.Type, val reflect.Value) *gqlerr
panic(fmt.Errorf("missing def for %s", typ.NamedType))
}

if !typ.NonNull && !val.IsValid() {
// If the type is not null and we got a invalid value namely null/nil, then it's valid
return nil
}

switch def.Kind {
case ast.Enum:
kind := val.Type().Kind()
Expand Down
39 changes: 39 additions & 0 deletions validator/vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,45 @@ func TestValidateVars(t *testing.T) {
require.EqualError(t, gerr, "input: variable.var cannot use bool as Int")
})
})

t.Run("Int Array", func(t *testing.T) {
t.Run("Array with null", func(t *testing.T) {
q := gqlparser.MustLoadQuery(schema, `query foo($var: [Int]) { intArrayArg(i: $var) }`)
a := 1
b := 2

_, gerr := validator.VariableValues(schema, q.Operations.ForName(""), map[string]interface{}{
"var": []*int{&a, &b, nil},
})
require.Nil(t, gerr)
})
})

t.Run("String Array", func(t *testing.T) {
t.Run("Array with null", func(t *testing.T) {
q := gqlparser.MustLoadQuery(schema, `query foo($var: [String]) { stringArrayArg(i: $var) }`)
a := "1"
b := "2"

_, gerr := validator.VariableValues(schema, q.Operations.ForName(""), map[string]interface{}{
"var": []*string{&a, &b, nil},
})
require.Nil(t, gerr)
})
})

t.Run("Boolean Array", func(t *testing.T) {
t.Run("Array with null", func(t *testing.T) {
q := gqlparser.MustLoadQuery(schema, `query foo($var: [Boolean]) { boolArrayArg(i: $var) }`)
a := true
b := false

_, gerr := validator.VariableValues(schema, q.Operations.ForName(""), map[string]interface{}{
"var": []*bool{&a, &b, nil},
})
require.Nil(t, gerr)
})
})
}

func mustReadFile(name string) string {
Expand Down

0 comments on commit 1ab3e4f

Please sign in to comment.