Skip to content

Commit

Permalink
properly coerce Int input values (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Nov 6, 2016
1 parent 0dd3874 commit fce75a5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ func TestMutation(t *testing.T) {
Variables: map[string]interface{}{
"ep": "JEDI",
"review": map[string]interface{}{
"stars": 5,
"stars": float64(5),
"commentary": "This is a great movie!",
},
},
Expand Down
20 changes: 16 additions & 4 deletions internal/exec/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,23 @@ var intScalar = &scalar{
name: "Int",
reflectType: reflect.TypeOf(int32(0)),
coerceInput: func(input interface{}) (interface{}, error) {
i := input.(int)
if i < math.MinInt32 || i > math.MaxInt32 {
return nil, fmt.Errorf("not a 32-bit integer: %d", i)
switch input := input.(type) {
case int32:
return input, nil
case int:
if input < math.MinInt32 || input > math.MaxInt32 {
return nil, fmt.Errorf("not a 32-bit integer")
}
return int32(input), nil
case float64:
coerced := int32(input)
if input < math.MinInt32 || input > math.MaxInt32 || float64(coerced) != input {
return nil, fmt.Errorf("not a 32-bit integer")
}
return coerced, nil
default:
return nil, fmt.Errorf("wrong type")
}
return int32(i), nil
},
}
var floatScalar = &scalar{
Expand Down

0 comments on commit fce75a5

Please sign in to comment.