Skip to content

Commit

Permalink
Merge pull request #10 from dgraph-io/jatin/fixIdBug
Browse files Browse the repository at this point in the history
fix(GraphQL): Fix bug that got introduced in last commit , which doesn't allow string value in [ID] to be coerced to list.
  • Loading branch information
JatinDev543 authored Jan 21, 2021
2 parents b48c15b + 83d89f4 commit d604941
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
10 changes: 5 additions & 5 deletions validator/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (v *varValidator) validateVarType(typ *ast.Type, val reflect.Value) (reflec
if typ.Name() == "ID" && val.Type().Name() != "string" {
val = val.Convert((reflect.ValueOf("string")).Type())
slc = append(slc, val.String())
} else if typ.Name() != "ID" {
} else {
slc = append(slc, val.Interface())
}
val = reflect.ValueOf(slc)
Expand All @@ -101,10 +101,10 @@ func (v *varValidator) validateVarType(typ *ast.Type, val reflect.Value) (reflec
field = field.Elem()
}
cval, err := v.validateVarType(typ.Elem, field)
if typ.Name() == "ID" && val.Type().Name() != "string" {
cval = cval.Convert((reflect.ValueOf("string")).Type())
slc = append(slc, cval.String())
} else if typ.Name() == "ID" {
if typ.Name() == "ID" {
if val.Type().Name() != "string" {
cval = cval.Convert((reflect.ValueOf("string")).Type())
}
slc = append(slc, cval.String())
}
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions validator/vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ func TestValidateVars(t *testing.T) {
require.EqualValues(t, expected, vars["var"])
})

t.Run("single string value should be coerced to string array when required type is [ID]", func(t *testing.T) {
q := gqlparser.MustLoadQuery(schema, `query foo($var: [ID]) { idArrayArg(i: $var) }`)
vars, gerr := validator.VariableValues(schema, q.Operations.ForName(""), map[string]interface{}{
"var": "5",
})
require.Nil(t, gerr)
expected := []interface{}{"5"}
require.EqualValues(t, expected, vars["var"])
})

t.Run("int array should be coerced to string array when required type is [ID]", func(t *testing.T) {
q := gqlparser.MustLoadQuery(schema, `query foo($var: [ID]) { idArrayArg(i: $var) }`)
vars, gerr := validator.VariableValues(schema, q.Operations.ForName(""), map[string]interface{}{
Expand Down

0 comments on commit d604941

Please sign in to comment.