Skip to content

Commit af5c357

Browse files
authored
Merge pull request #8 from dgraph-io/jatin/GRAPHQL-745
fix(GraphQL): This PR change the coerced slice to type interface.
2 parents c23012d + b296ca2 commit af5c357

2 files changed

Lines changed: 7 additions & 8 deletions

File tree

validator/vars.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"github.com/dgraph-io/gqlparser/v2/gqlerror"
1111
)
1212

13-
var UnexpectedType = fmt.Errorf("Unexpected Type")
14-
1513
// VariableValues coerces and validates variable values
1614
func VariableValues(schema *ast.Schema, op *ast.OperationDefinition, variables map[string]interface{}) (map[string]interface{}, *gqlerror.Error) {
1715
coercedVars := map[string]interface{}{}
@@ -82,9 +80,10 @@ func (v *varValidator) validateVarType(typ *ast.Type, val reflect.Value) (reflec
8280
if val.Kind() != reflect.Slice {
8381
// GraphQL spec says that non-null values should be coerced to an array when possible.
8482
// Hence if the value is not a slice, we create a slice and add val to it.
85-
slc := reflect.MakeSlice(reflect.SliceOf(val.Type()), 0, 0)
86-
slc = reflect.Append(slc, val)
87-
val = slc
83+
slc := make([]interface{}, 0)
84+
slc = append(slc, val.Interface())
85+
val = reflect.ValueOf(slc)
86+
val.Convert(val.Type())
8887
}
8988
for i := 0; i < val.Len(); i++ {
9089
resetPath()

validator/vars_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func TestValidateVars(t *testing.T) {
157157
"var": map[string]interface{}{"name": "hello"},
158158
})
159159
require.Nil(t, gerr)
160-
require.EqualValues(t, []map[string]interface{}{{"name": "hello"}}, vars["var"])
160+
require.EqualValues(t, []interface{}{map[string]interface{}{"name": "hello"}}, vars["var"])
161161
})
162162

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

@@ -176,7 +176,7 @@ func TestValidateVars(t *testing.T) {
176176
"var": []map[string]interface{}{{"and": 5}},
177177
})
178178
require.Nil(t, gerr)
179-
expected := []map[string]interface{}{{"and": []int{5}}}
179+
expected := []map[string]interface{}{{"and": []interface{}{5}}}
180180
require.EqualValues(t, expected, vars["var"])
181181
})
182182

0 commit comments

Comments
 (0)