Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/exec/packer/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ func UnmarshalInput(typ reflect.Type, input any) (any, error) {
return float64(input), nil
case int:
return float64(input), nil
case int64:
return float64(input), nil
}

case reflect.String:
Expand Down
71 changes: 66 additions & 5 deletions issue_305_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ type int64Scalar struct {
func (u *int64Scalar) ImplementsGraphQLType(name string) bool { return name == "Int64" }

func (u *int64Scalar) UnmarshalGraphQL(input any) error {
value, ok := input.(int64)
if !ok {
return fmt.Errorf("Int64 expects int64 got %T", input)
switch value := input.(type) {
case int64:
u.Value = value
return nil
case int32:
u.Value = int64(value)
return nil
default:
return fmt.Errorf("Int64 expects int32 or int64 got %T", input)
}
u.Value = value
return nil
}

type issue305Resolver struct{}
Expand All @@ -35,23 +39,80 @@ func (r *issue305Resolver) Regular(args struct{ X int32 }) int32 {
return args.X
}

func (r *issue305Resolver) Floaty(args struct{ X float64 }) float64 {
return args.X
}

func TestIssue305IntegerLiteralBehavior(t *testing.T) {
schema := graphql.MustParseSchema(`
scalar Int64
type Query {
custom(hash: Int64!): String!
regular(x: Int!): Int!
floaty(x: Float!): Float!
}
`, &issue305Resolver{})

const large = "3626262620"
const largeInt int64 = 3626262620
const negLargeInt int64 = -largeInt

gqltesting.RunTests(t, []*gqltesting.Test{
{
Schema: schema,
Query: `{ custom(hash: 123) }`,
ExpectedResult: `{"custom":"123"}`,
},
{
Schema: schema,
Query: fmt.Sprintf(`{ custom(hash: -%s) }`, large),
ExpectedResult: fmt.Sprintf(`{"custom":%q}`, fmt.Sprintf("-%s", large)),
},
{
Schema: schema,
Query: fmt.Sprintf(`{ custom(hash: %s) }`, large),
ExpectedResult: fmt.Sprintf(`{"custom":%q}`, large),
},
{
Schema: schema,
Query: `query($hash: Int64!) { custom(hash: $hash) }`,
Variables: map[string]any{"hash": int32(123)},
ExpectedResult: `{"custom":"123"}`,
},
{
Schema: schema,
Query: `query($hash: Int64!) { custom(hash: $hash) }`,
Variables: map[string]any{"hash": largeInt},
ExpectedResult: fmt.Sprintf(`{"custom":%q}`, large),
},
{
Schema: schema,
Query: `query($hash: Int64!) { custom(hash: $hash) }`,
Variables: map[string]any{"hash": negLargeInt},
ExpectedResult: fmt.Sprintf(`{"custom":%q}`, fmt.Sprintf("-%s", large)),
},
{
Schema: schema,
Query: fmt.Sprintf(`{ floaty(x: %s) }`, large),
ExpectedResult: fmt.Sprintf(`{"floaty":%s}`, large),
},
{
Schema: schema,
Query: fmt.Sprintf(`{ floaty(x: -%s) }`, large),
ExpectedResult: fmt.Sprintf(`{"floaty":%s}`, fmt.Sprintf("-%s", large)),
},
{
Schema: schema,
Query: `query($x: Float!) { floaty(x: $x) }`,
Variables: map[string]any{"x": largeInt},
ExpectedResult: fmt.Sprintf(`{"floaty":%s}`, large),
},
{
Schema: schema,
Query: `query($x: Float!) { floaty(x: $x) }`,
Variables: map[string]any{"x": negLargeInt},
ExpectedResult: fmt.Sprintf(`{"floaty":%s}`, fmt.Sprintf("-%s", large)),
},
{
Schema: schema,
Query: fmt.Sprintf(`{ regular(x: %d) }`, math.MaxInt32),
Expand Down
4 changes: 4 additions & 0 deletions nullable_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ func (s *NullFloat) UnmarshalGraphQL(input any) error {
coerced := float64(v)
s.Value = &coerced
return nil
case int64:
coerced := float64(v)
s.Value = &coerced
return nil
default:
return fmt.Errorf("wrong type for Float: %T", v)
}
Expand Down
7 changes: 7 additions & 0 deletions nullable_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,13 @@ func TestNullFloat_UnmarshalGraphQL(t *testing.T) {
},
wantEq: ref,
},
{
name: "int64",
args: args{
input: int64(good),
},
wantEq: ref,
},
{
name: "float64",
args: args{
Expand Down
Loading