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 coercion is misleading #69

Closed
zerkms opened this issue Jul 16, 2015 · 3 comments
Closed

Integer coercion is misleading #69

zerkms opened this issue Jul 16, 2015 · 3 comments

Comments

@zerkms
Copy link

zerkms commented Jul 16, 2015

The coerce function for the integers is defined as

export var GraphQLInt = new GraphQLScalarType({
  name: 'Int',
  coerce(value) {
    var num = +value;
    return num === num && num <= MAX_INT && num >= MIN_INT ? num | 0 : null;
  },
  coerceLiteral(ast) {
    if (ast.kind === Kind.INT) {
      var num = parseInt(ast.value, 10);
      if (num <= MAX_INT && num >= MIN_INT) {
        return num;
      }
    }
  }
});

Which means that if a number does not fit the 32 bits, then the significant bits will be truncated.

Which is generally a not expected behaviour, since the coercion must either produce a valid result, or a null.

References:

@leebyron
Copy link
Contributor

Thanks for the report, I'll add tests

@zerkms
Copy link
Author

zerkms commented Jul 17, 2015

@leebyron sorry for disturbing, but in es6/2015 there is a Math.trunc() that could help eliminate the entire (num < 0 ? Math.ceil : Math.floor) expression

@leebyron
Copy link
Contributor

I'm aware of this, but decided to not require a global object polyfill since GraphQL.js should be included as a side-effect-free library.

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

2 participants