Skip to content

Commit

Permalink
Merge pull request #15 from dgraph-io/jatin/GRAPHQL-1081
Browse files Browse the repository at this point in the history
fix(GRAPHQL): Remove support of String --> int32 coercion in variables.
  • Loading branch information
JatinDev543 authored Mar 16, 2021
2 parents 6adf0a9 + 06de469 commit eb159c9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion validator/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (v *varValidator) validateVarType(typ *ast.Type, val reflect.Value) (reflec
namedType := val.Type().Name()
switch typ.NamedType {
case "Int", "Int64":
if kind == reflect.String || kind == reflect.Int || kind == reflect.Int32 || kind == reflect.Int64 {
if kind == reflect.Int || kind == reflect.Int32 || kind == reflect.Int64 || (namedType == "string" && typ.NamedType == "Int64") || namedType == "Number" {
var errIntCoerce error
var valString string
if kind == reflect.String {
Expand Down
8 changes: 8 additions & 0 deletions validator/vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,14 @@ func TestValidateVars(t *testing.T) {
require.EqualError(t, gerr, "input: variable.var cannot use float64 as Int")
})

t.Run("error for invalid coercing, String -> Int ", func(t *testing.T) {
q := gqlparser.MustLoadQuery(schema, `query foo($var: Int!) { intArg(i: $var) }`)
_, gerr := validator.VariableValues(schema, q.Operations.ForName(""), map[string]interface{}{
"var": "18",
})
require.EqualError(t, gerr, "input: variable.var cannot use string as Int")
})

t.Run("out of range error for Int", func(t *testing.T) {
q := gqlparser.MustLoadQuery(schema, `query foo($var: Int!) { intArg(i: $var) }`)
_, gerr := validator.VariableValues(schema, q.Operations.ForName(""), map[string]interface{}{
Expand Down

0 comments on commit eb159c9

Please sign in to comment.