Skip to content

Commit

Permalink
Merge pull request #11 from dgraph-io/jatin/GRAPHQL-1006
Browse files Browse the repository at this point in the history
Fixes GRAPHQL-1006
In this PR we have added input coercion for Integers which are passed in variables.
Previously this code was part of dgraph.
  • Loading branch information
JatinDev543 authored Feb 12, 2021
2 parents d604941 + 3a9b1f5 commit 147d6c6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
3 changes: 3 additions & 0 deletions validator/testdata/vars.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
type Query {
optionalIntArg(i: Int): Boolean!
intArg(i: Int!): Boolean!
int64Arg(i: Int64!): Boolean!
stringArg(i: String): Boolean!
boolArg(i: Boolean!): Boolean!
floatArg(i: Float!): Boolean!
Expand All @@ -16,6 +17,8 @@ type Query {
typeArrayArg(i: [CustomType]): Boolean!
}

scalar Int64

input InputType {
name: String!
nullName: String
Expand Down
25 changes: 24 additions & 1 deletion validator/vars.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package validator

import (
"errors"
"reflect"
"strconv"
"strings"

"fmt"
Expand Down Expand Up @@ -145,10 +147,31 @@ func (v *varValidator) validateVarType(typ *ast.Type, val reflect.Value) (reflec
case ast.Scalar:
kind := val.Type().Kind()
switch typ.NamedType {
case "Int":
case "Int", "Int64":
if kind == reflect.String || kind == reflect.Int || kind == reflect.Int32 || kind == reflect.Int64 {
var errIntCoerce error
var valString string
if kind == reflect.String {
valString = val.String()
} else {
valString = strconv.FormatInt(val.Int(), 10)
}
if typ.NamedType == "Int" {
_, errIntCoerce = strconv.ParseInt(valString, 10, 32)
} else {
_, errIntCoerce = strconv.ParseInt(valString, 10, 64)
}
if errIntCoerce != nil {
if errors.Is(errIntCoerce, strconv.ErrRange) {
return val, gqlerror.ErrorPathf(v.path, "Out of range value '%s', for type `%s`", valString, typ.NamedType)

} else {
return val, gqlerror.ErrorPathf(v.path, "Type mismatched for Value `%s`, expected:`%s`", valString, typ.NamedType)
}
}
return val, nil
}

case "Float":
if kind == reflect.String || kind == reflect.Float32 || kind == reflect.Float64 || kind == reflect.Int || kind == reflect.Int32 || kind == reflect.Int64 {
return val, nil
Expand Down
25 changes: 24 additions & 1 deletion validator/vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,31 @@ func TestValidateVars(t *testing.T) {
})
require.EqualError(t, gerr, "input: variable.var cannot use bool as Int")
})
})

t.Run("error for invalid coercing like Float -> 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.0,
})
require.EqualError(t, gerr, "input: variable.var cannot use float64 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{}{
"var": 2147483648,
})
require.EqualError(t, gerr, "input: variable.var Out of range value '2147483648', for type `Int`")
})

t.Run("out of range error for Int64", func(t *testing.T) {
q := gqlparser.MustLoadQuery(schema, `query foo($var: Int64!) { int64Arg(i: $var) }`)
_, gerr := validator.VariableValues(schema, q.Operations.ForName(""), map[string]interface{}{
"var": "9223372036854775808",
})
require.EqualError(t, gerr, "input: variable.var Out of range value '9223372036854775808', for type `Int64`")
})
})
t.Run("Int Array", func(t *testing.T) {
t.Run("Array with null", func(t *testing.T) {
q := gqlparser.MustLoadQuery(schema, `query foo($var: [Int]) { intArrayArg(i: $var) }`)
Expand Down

0 comments on commit 147d6c6

Please sign in to comment.