Skip to content

Commit

Permalink
Merge pull request #8 from dgraph-io/jatin/GRAPHQL-745
Browse files Browse the repository at this point in the history
fix(GraphQL): This PR change the coerced slice to type interface.
  • Loading branch information
JatinDev543 authored Dec 14, 2020
2 parents c23012d + b296ca2 commit af5c357
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
9 changes: 4 additions & 5 deletions validator/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/dgraph-io/gqlparser/v2/gqlerror"
)

var UnexpectedType = fmt.Errorf("Unexpected Type")

// VariableValues coerces and validates variable values
func VariableValues(schema *ast.Schema, op *ast.OperationDefinition, variables map[string]interface{}) (map[string]interface{}, *gqlerror.Error) {
coercedVars := map[string]interface{}{}
Expand Down Expand Up @@ -82,9 +80,10 @@ func (v *varValidator) validateVarType(typ *ast.Type, val reflect.Value) (reflec
if val.Kind() != reflect.Slice {
// GraphQL spec says that non-null values should be coerced to an array when possible.
// Hence if the value is not a slice, we create a slice and add val to it.
slc := reflect.MakeSlice(reflect.SliceOf(val.Type()), 0, 0)
slc = reflect.Append(slc, val)
val = slc
slc := make([]interface{}, 0)
slc = append(slc, val.Interface())
val = reflect.ValueOf(slc)
val.Convert(val.Type())
}
for i := 0; i < val.Len(); i++ {
resetPath()
Expand Down
6 changes: 3 additions & 3 deletions validator/vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestValidateVars(t *testing.T) {
"var": map[string]interface{}{"name": "hello"},
})
require.Nil(t, gerr)
require.EqualValues(t, []map[string]interface{}{{"name": "hello"}}, vars["var"])
require.EqualValues(t, []interface{}{map[string]interface{}{"name": "hello"}}, vars["var"])
})

t.Run("non-null int value should be coerced to an array", func(t *testing.T) {
Expand All @@ -166,7 +166,7 @@ func TestValidateVars(t *testing.T) {
"var": 5,
})
require.Nil(t, gerr)
expected := []int{5}
expected := []interface{}{5}
require.EqualValues(t, expected, vars["var"])
})

Expand All @@ -176,7 +176,7 @@ func TestValidateVars(t *testing.T) {
"var": []map[string]interface{}{{"and": 5}},
})
require.Nil(t, gerr)
expected := []map[string]interface{}{{"and": []int{5}}}
expected := []map[string]interface{}{{"and": []interface{}{5}}}
require.EqualValues(t, expected, vars["var"])
})

Expand Down

0 comments on commit af5c357

Please sign in to comment.