Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer parsed as float64 when passed in as a variable #514

Closed
s1na opened this issue Apr 13, 2022 · 5 comments
Closed

Integer parsed as float64 when passed in as a variable #514

s1na opened this issue Apr 13, 2022 · 5 comments

Comments

@s1na
Copy link

s1na commented Apr 13, 2022

Hi. We have a simple custom scalar Long which represents a int64 number. I.e.:

type Long int64

// UnmarshalGraphQL unmarshals the provided GraphQL query data.
func (b *Long) UnmarshalGraphQL(input interface{}) error {
        var err error
        switch input := input.(type) {
        case string:
                value, err := strconv.ParseInt(input, 10, 64)
                *b = Long(value)
                return err
        case int32:
                *b = Long(input)
        case int64:
                *b = Long(input)
        default:
                err = fmt.Errorf("unexpected type %T for Long", input)
        }
        return err
}

A long literal in the query works ok, i.e. we can do block(number: 1000000).
But passing it as a variable, i.e. query test($num: Long!) { block(number: $num) } and then "variables": { "num": 1000000 } fails with "unexpected type float64 for Long".

@pavelnikolov
Copy link
Member

@s1na This does seem like a problem with your custom type rather than a problem with the GraphQL library. Have you tried adding float64 to the list of possible types? For example:

type Long int64

// UnmarshalGraphQL unmarshals the provided GraphQL query data.
func (b *Long) UnmarshalGraphQL(input interface{}) error {
        var err error
        switch input := input.(type) {
        case string:
                value, err := strconv.ParseInt(input, 10, 64)
                *b = Long(value)
                return err
        case int32:
                *b = Long(input)
        case int64:
                *b = Long(input)
        case float64:
                *b = Long(input)
        default:
                err = fmt.Errorf("unexpected type %T for Long", input)
        }
        return err
}

@pavelnikolov
Copy link
Member

Let me know if the above fixes the problem. If you still have issues feel free to give me a failing unit test and I'll try to make it pass.

@s1na
Copy link
Author

s1na commented Apr 13, 2022

The reason I opened this issue is the difference in behaviour when the number is passed as part of the query vs. it being passed as a variable. In the latter case it's being parsed as a float64 which causes the error in our custom type. But I don't see why it should be parsed as float64?

@holiman
Copy link

holiman commented May 12, 2022

block(number: 1000000) is graphql, whereas "variables": { "num": 1000000 } is json.
In the golang json library parser, an unquoted numeric literal is always parsed as float64. Hence the difference. I don't think there's an issue with the graphql-go library.

@pavelnikolov
Copy link
Member

I am closing this issue as I don't think that it is related directly to the GraphQL library. I don't think it is worth investing time in making JSON and GraphQL parsing the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants