graphql icon indicating copy to clipboard operation
graphql copied to clipboard

How to deal with integer overflow when using variables

Open gams-temark opened this issue 1 year ago • 2 comments

Hi, I have been trying a work-around when dealing with integer values for ranges above int32. I've looked at Scalars for the BigInt type but I am not sure how can I incorporate something like this in this package.

Here's what my variable looks like:

// max range for int32 is 2,147,483,647
myId := 3211136557

v := map[string]any{
    "id":  graphql.Int(myId) // the value for this will be a random int32 since it is overflowing
}

For now, what I'm doing is statically indicating the ID in my query but it would be nice if it could be dynamic.

Thanks

gams-temark avatar Feb 12 '24 02:02 gams-temark

graphql.Int is documented as being able to represent values between -(2^31) and 2^31 - 1. It corresponds to the Int scalar in the GraphQL specification: https://spec.graphql.org/October2021/#sec-Int.

Do you own the schema and can pick the type of the ID? If so, consider using https://spec.graphql.org/October2021/#sec-ID or another string scalar instead of Int:

v := map[string]any{
    "id": graphql.ID("3211136557")
}

dmitshur avatar Feb 12 '24 04:02 dmitshur

Do you own the schema and can pick the type of the ID? If so, consider using https://spec.graphql.org/October2021/#sec-ID or another string scalar instead of Int:

v := map[string]any{
    "id": graphql.ID("3211136557")
}

Unfortunately not, I've just tried this just to see what would be the result:

Type mismatch on variable $id and argument ids ([ID!]! / [Int])

So i'll just stick to hard coding it for now, thanks for the response @dmitshur and this cool package for a graphql client 💯

gams-temark avatar Feb 12 '24 05:02 gams-temark