Skip to content

Commit d604941

Browse files
authored
Merge pull request #10 from dgraph-io/jatin/fixIdBug
fix(GraphQL): Fix bug that got introduced in last commit , which doesn't allow string value in [ID] to be coerced to list.
2 parents b48c15b + 83d89f4 commit d604941

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

validator/vars.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (v *varValidator) validateVarType(typ *ast.Type, val reflect.Value) (reflec
8484
if typ.Name() == "ID" && val.Type().Name() != "string" {
8585
val = val.Convert((reflect.ValueOf("string")).Type())
8686
slc = append(slc, val.String())
87-
} else if typ.Name() != "ID" {
87+
} else {
8888
slc = append(slc, val.Interface())
8989
}
9090
val = reflect.ValueOf(slc)
@@ -101,10 +101,10 @@ func (v *varValidator) validateVarType(typ *ast.Type, val reflect.Value) (reflec
101101
field = field.Elem()
102102
}
103103
cval, err := v.validateVarType(typ.Elem, field)
104-
if typ.Name() == "ID" && val.Type().Name() != "string" {
105-
cval = cval.Convert((reflect.ValueOf("string")).Type())
106-
slc = append(slc, cval.String())
107-
} else if typ.Name() == "ID" {
104+
if typ.Name() == "ID" {
105+
if val.Type().Name() != "string" {
106+
cval = cval.Convert((reflect.ValueOf("string")).Type())
107+
}
108108
slc = append(slc, cval.String())
109109
}
110110
if err != nil {

validator/vars_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,16 @@ func TestValidateVars(t *testing.T) {
180180
require.EqualValues(t, expected, vars["var"])
181181
})
182182

183+
t.Run("single string value should be coerced to string array when required type is [ID]", func(t *testing.T) {
184+
q := gqlparser.MustLoadQuery(schema, `query foo($var: [ID]) { idArrayArg(i: $var) }`)
185+
vars, gerr := validator.VariableValues(schema, q.Operations.ForName(""), map[string]interface{}{
186+
"var": "5",
187+
})
188+
require.Nil(t, gerr)
189+
expected := []interface{}{"5"}
190+
require.EqualValues(t, expected, vars["var"])
191+
})
192+
183193
t.Run("int array should be coerced to string array when required type is [ID]", func(t *testing.T) {
184194
q := gqlparser.MustLoadQuery(schema, `query foo($var: [ID]) { idArrayArg(i: $var) }`)
185195
vars, gerr := validator.VariableValues(schema, q.Operations.ForName(""), map[string]interface{}{

0 commit comments

Comments
 (0)