Skip to content

astFromValue - JavaScript BigInt support#4088

Open
ardatan wants to merge 5 commits intographql:nextfrom
ardatan:bigint-support
Open

astFromValue - JavaScript BigInt support#4088
ardatan wants to merge 5 commits intographql:nextfrom
ardatan:bigint-support

Conversation

@ardatan
Copy link
Copy Markdown
Member

@ardatan ardatan commented May 8, 2024

This PR adds support for BigInt values to astFromValue.
It coerces whenever possible instead of failing.
This will also allow custom scalars like https://the-guild.dev/graphql/scalars/docs/scalars/big-int to get a BigInt as a default value. Similar to #4085

@netlify
Copy link
Copy Markdown

netlify bot commented May 8, 2024

Deploy Preview for compassionate-pike-271cb3 ready!

Name Link
🔨 Latest commit 62f92fe
🔍 Latest deploy log https://app.netlify.com/sites/compassionate-pike-271cb3/deploys/670bb7bed4d9de000852eb12
😎 Deploy Preview https://deploy-preview-4088--compassionate-pike-271cb3.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@github-actions
Copy link
Copy Markdown

github-actions bot commented May 8, 2024

Hi @ardatan, I'm @github-actions bot happy to help you with this PR 👋

Supported commands

Please post this commands in separate comments and only one per comment:

  • @github-actions run-benchmark - Run benchmark comparing base and merge commits for this PR
  • @github-actions publish-pr-on-npm - Build package from this PR and publish it on NPM

Copy link
Copy Markdown
Member

@saihaj saihaj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ardatan
Copy link
Copy Markdown
Member Author

ardatan commented May 9, 2024

@saihaj Isn't it for Int scalar type? However, this PR doesn't change that behavior. Int still doesn't support larger integers. This PR just allows to use bigint type of JavaScript just like number and string. So it is not the size of the number but the type of it.
If Int supports 32-bit numeric values in string format, it should allow the same values in bigint format as well right?
Same for ID and `Float but without 32-bit limit.

@ardatan ardatan requested a review from saihaj May 9, 2024 17:16
@saihaj
Copy link
Copy Markdown
Member

saihaj commented May 9, 2024

@saihaj Isn't it for Int scalar type? However, this PR doesn't change that behavior. Int still doesn't support larger integers. This PR just allows to use bigint type of JavaScript just like number and string. So it is not the size of the number but the type of it. If Int supports 32-bit numeric values in string format, it should allow the same values in bigint format as well right? Same for ID and `Float but without 32-bit limit.

Oh I see what you mean, its like we are coercing 2147483646n as 2147483646 since it is a valid GraphQL Int after all

@saihaj saihaj requested review from a team and removed request for a team June 26, 2024 17:11
@yaacovCR yaacovCR mentioned this pull request Sep 27, 2024
17 tasks
@yaacovCR
Copy link
Copy Markdown
Contributor

yaacovCR commented Oct 1, 2024

In terms of default values, that use case should probably be re-examined after #3814 lands, which should hopefully be soon! We are halfway-through the stack.

I suppose we will need to support conversion from BigInt to Int/Float when possible if the received JSON is parsed with a BigInt supporting parser. I think it probably would require some discussion at the WG in terms of whether to include it, but I would be in favor.

@yaacovCR
Copy link
Copy Markdown
Contributor

yaacovCR commented Oct 1, 2024

In terms of output, I am of two minds in terms of whether we should be supporting conversion from bigint to int, because that means that a custom scalar probably should have been used, in case the value was greater than the int threshold. Maybe it's better to fail overall?

For conversion to string or id, I imagine we would want to support it, I can't see why not?

See #3973 and #3109.

value: '123',
});

expect(astFromValue(9007199254740993n, GraphQLFloat)).to.deep.equal({
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change in type of this test is because astFromValue is not safe, set to be replaced by valueToLiteral() for all remaining uses in #3814

But a bigint of sufficient size, -- as far as I know -- cannot be converted to a JS float.

We also in this PR do not properly handle GraphQLFloat scalar parsing and serialization of small BigInts, I will push a failing test to demonstrate what I mean.

We also probably should update the logic within the new valueToLiteral() utility. It's possible that overall we can just delay landing this PR until after #3814

@yaacovCR yaacovCR mentioned this pull request Feb 4, 2026
yaacovCR added a commit that referenced this pull request Feb 11, 2026
building on prior work:

- #3109
- #3973
- #4088
- #4223 
- #4276 

This PR adds bigint support to:
- GraphQLInt, including input/output coercion, throwing if out of
bounds, and new default value programmatic default value to literal
- GraphQLFloat, including input/output coercion, throwing if cannot be
represented, including with any loss of precision.
- GraphQLString, output coercion only, like number
- GraphQLBoolean, output coercion only, like number
- GraphQLID, output and input coercion, in parallel to number, and new
default value programmatic default value to literal

Also, for custom scalars:
- updates the `defaultScalarValueToLiteral` to convert a `bigint` to an
Int literal
- updates for deprecated `astFromValue` for passthrough custom scalars,
i.e. scalars that return `bigint` from coerceOutputValue/serialize, to
convert `bigint` to an Int literal.

cf. also #3913

Co-authored-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
Co-authored-by: jdecroock <decroockjovi@gmail.com>
Co-authored-by: Hkmu <3169251+hkmu@users.noreply.github.com>
Co-authored-by: xonx <119700621+xonx4l@users.noreply.github.com>
yaacovCR added a commit that referenced this pull request Feb 22, 2026
building on prior work:

- #3109
- #3973
- #4088
- #4223 
- #4276 

This PR adds bigint support to:
- GraphQLInt, including input/output coercion, throwing if out of
bounds, and new default value programmatic default value to literal
- GraphQLFloat, including input/output coercion, throwing if cannot be
represented, including with any loss of precision.
- GraphQLString, output coercion only, like number
- GraphQLBoolean, output coercion only, like number
- GraphQLID, output and input coercion, in parallel to number, and new
default value programmatic default value to literal

Also, for custom scalars:
- updates the `defaultScalarValueToLiteral` to convert a `bigint` to an
Int literal
- updates for deprecated `astFromValue` for passthrough custom scalars,
i.e. scalars that return `bigint` from coerceOutputValue/serialize, to
convert `bigint` to an Int literal.

cf. also #3913

Co-authored-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
Co-authored-by: jdecroock <decroockjovi@gmail.com>
Co-authored-by: Hkmu <3169251+hkmu@users.noreply.github.com>
Co-authored-by: xonx <119700621+xonx4l@users.noreply.github.com>
yaacovCR added a commit that referenced this pull request Feb 22, 2026
building on prior work:

- #3109
- #3973
- #4088
- #4223 
- #4276 

This PR adds bigint support to:
- GraphQLInt, including input/output coercion, throwing if out of
bounds, and new default value programmatic default value to literal
- GraphQLFloat, including input/output coercion, throwing if cannot be
represented, including with any loss of precision.
- GraphQLString, output coercion only, like number
- GraphQLBoolean, output coercion only, like number
- GraphQLID, output and input coercion, in parallel to number, and new
default value programmatic default value to literal

Also, for custom scalars:
- updates the `defaultScalarValueToLiteral` to convert a `bigint` to an
Int literal
- updates for deprecated `astFromValue` for passthrough custom scalars,
i.e. scalars that return `bigint` from coerceOutputValue/serialize, to
convert `bigint` to an Int literal.

cf. also #3913

Co-authored-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
Co-authored-by: jdecroock <decroockjovi@gmail.com>
Co-authored-by: Hkmu <3169251+hkmu@users.noreply.github.com>
Co-authored-by: xonx <119700621+xonx4l@users.noreply.github.com>
yaacovCR added a commit that referenced this pull request Feb 23, 2026
building on prior work:

- #3109
- #3973
- #4088
- #4223 
- #4276 

This PR adds bigint support to:
- GraphQLInt, including input/output coercion, throwing if out of
bounds, and new default value programmatic default value to literal
- GraphQLFloat, including input/output coercion, throwing if cannot be
represented, including with any loss of precision.
- GraphQLString, output coercion only, like number
- GraphQLBoolean, output coercion only, like number
- GraphQLID, output and input coercion, in parallel to number, and new
default value programmatic default value to literal

Also, for custom scalars:
- updates the `defaultScalarValueToLiteral` to convert a `bigint` to an
Int literal
- updates for deprecated `astFromValue` for passthrough custom scalars,
i.e. scalars that return `bigint` from coerceOutputValue/serialize, to
convert `bigint` to an Int literal.

cf. also #3913

Co-authored-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
Co-authored-by: jdecroock <decroockjovi@gmail.com>
Co-authored-by: Hkmu <3169251+hkmu@users.noreply.github.com>
Co-authored-by: xonx <119700621+xonx4l@users.noreply.github.com>
yaacovCR added a commit that referenced this pull request Feb 23, 2026
building on prior work:

- #3109
- #3973
- #4088
- #4223 
- #4276 

This PR adds bigint support to:
- GraphQLInt, including input/output coercion, throwing if out of
bounds, and new default value programmatic default value to literal
- GraphQLFloat, including input/output coercion, throwing if cannot be
represented, including with any loss of precision.
- GraphQLString, output coercion only, like number
- GraphQLBoolean, output coercion only, like number
- GraphQLID, output and input coercion, in parallel to number, and new
default value programmatic default value to literal

Also, for custom scalars:
- updates the `defaultScalarValueToLiteral` to convert a `bigint` to an
Int literal
- updates for deprecated `astFromValue` for passthrough custom scalars,
i.e. scalars that return `bigint` from coerceOutputValue/serialize, to
convert `bigint` to an Int literal.

cf. also #3913

Co-authored-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
Co-authored-by: jdecroock <decroockjovi@gmail.com>
Co-authored-by: Hkmu <3169251+hkmu@users.noreply.github.com>
Co-authored-by: xonx <119700621+xonx4l@users.noreply.github.com>
yaacovCR added a commit that referenced this pull request Feb 24, 2026
building on prior work:

- #3109
- #3973
- #4088
- #4223 
- #4276 

This PR adds bigint support to:
- GraphQLInt, including input/output coercion, throwing if out of
bounds, and new default value programmatic default value to literal
- GraphQLFloat, including input/output coercion, throwing if cannot be
represented, including with any loss of precision.
- GraphQLString, output coercion only, like number
- GraphQLBoolean, output coercion only, like number
- GraphQLID, output and input coercion, in parallel to number, and new
default value programmatic default value to literal

Also, for custom scalars:
- updates the `defaultScalarValueToLiteral` to convert a `bigint` to an
Int literal
- updates for deprecated `astFromValue` for passthrough custom scalars,
i.e. scalars that return `bigint` from coerceOutputValue/serialize, to
convert `bigint` to an Int literal.

cf. also #3913

Co-authored-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
Co-authored-by: jdecroock <decroockjovi@gmail.com>
Co-authored-by: Hkmu <3169251+hkmu@users.noreply.github.com>
Co-authored-by: xonx <119700621+xonx4l@users.noreply.github.com>
yaacovCR added a commit that referenced this pull request Feb 24, 2026
building on prior work:

- #3109
- #3973
- #4088
- #4223 
- #4276 

This PR adds bigint support to:
- GraphQLInt, including input/output coercion, throwing if out of
bounds, and new default value programmatic default value to literal
- GraphQLFloat, including input/output coercion, throwing if cannot be
represented, including with any loss of precision.
- GraphQLString, output coercion only, like number
- GraphQLBoolean, output coercion only, like number
- GraphQLID, output and input coercion, in parallel to number, and new
default value programmatic default value to literal

Also, for custom scalars:
- updates the `defaultScalarValueToLiteral` to convert a `bigint` to an
Int literal
- updates for deprecated `astFromValue` for passthrough custom scalars,
i.e. scalars that return `bigint` from coerceOutputValue/serialize, to
convert `bigint` to an Int literal.

cf. also #3913

Co-authored-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
Co-authored-by: jdecroock <decroockjovi@gmail.com>
Co-authored-by: Hkmu <3169251+hkmu@users.noreply.github.com>
Co-authored-by: xonx <119700621+xonx4l@users.noreply.github.com>
yaacovCR added a commit that referenced this pull request Feb 24, 2026
building on prior work:

- #3109
- #3973
- #4088
- #4223 
- #4276 

This PR adds bigint support to:
- GraphQLInt, including input/output coercion, throwing if out of
bounds, and new default value programmatic default value to literal
- GraphQLFloat, including input/output coercion, throwing if cannot be
represented, including with any loss of precision.
- GraphQLString, output coercion only, like number
- GraphQLBoolean, output coercion only, like number
- GraphQLID, output and input coercion, in parallel to number, and new
default value programmatic default value to literal

Also, for custom scalars:
- updates the `defaultScalarValueToLiteral` to convert a `bigint` to an
Int literal
- updates for deprecated `astFromValue` for passthrough custom scalars,
i.e. scalars that return `bigint` from coerceOutputValue/serialize, to
convert `bigint` to an Int literal.

cf. also #3913

Co-authored-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
Co-authored-by: jdecroock <decroockjovi@gmail.com>
Co-authored-by: Hkmu <3169251+hkmu@users.noreply.github.com>
Co-authored-by: xonx <119700621+xonx4l@users.noreply.github.com>
yaacovCR added a commit that referenced this pull request Feb 24, 2026
building on prior work:

- #3109
- #3973
- #4088
- #4223 
- #4276 

This PR adds bigint support to:
- GraphQLInt, including input/output coercion, throwing if out of
bounds, and new default value programmatic default value to literal
- GraphQLFloat, including input/output coercion, throwing if cannot be
represented, including with any loss of precision.
- GraphQLString, output coercion only, like number
- GraphQLBoolean, output coercion only, like number
- GraphQLID, output and input coercion, in parallel to number, and new
default value programmatic default value to literal

Also, for custom scalars:
- updates the `defaultScalarValueToLiteral` to convert a `bigint` to an
Int literal
- updates for deprecated `astFromValue` for passthrough custom scalars,
i.e. scalars that return `bigint` from coerceOutputValue/serialize, to
convert `bigint` to an Int literal.

cf. also #3913

Co-authored-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
Co-authored-by: jdecroock <decroockjovi@gmail.com>
Co-authored-by: Hkmu <3169251+hkmu@users.noreply.github.com>
Co-authored-by: xonx <119700621+xonx4l@users.noreply.github.com>
@yaacovCR
Copy link
Copy Markdown
Contributor

yaacovCR commented Mar 5, 2026

Thank you @ardatan so much for your work on this! bigint support belatedly made it into v17 as reworked in #4550

@yaacovCR yaacovCR closed this Mar 5, 2026
@ardatan
Copy link
Copy Markdown
Member Author

ardatan commented Mar 6, 2026

Thanks @yaacovCR ! But like we discussed here earlier -> #4085
v17 seems far from being released any time soon. So I think a patch-fix in v16 would be better to have.

@yaacovCR yaacovCR reopened this Mar 6, 2026
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 6, 2026

@yaacovCR is attempting to deploy a commit to the The GraphQL Foundation Team on Vercel.

A member of the Team first needs to authorize it.

@yaacovCR
Copy link
Copy Markdown
Contributor

yaacovCR commented Mar 6, 2026

Thanks @yaacovCR ! But like we discussed here earlier -> #4085 v17 seems far from being released any time soon. So I think a patch-fix in v16 would be better to have.

I'm hopeful to (finally!) move v17 to beta over the weekend.

Reopening to help track, however, as I think now that we have this merged into v17 and we know directionally where the code is headed with the new valueToLiteral() functionality, etc, perhaps we can backport more of the new changes to v16.

Ideally this would be a non-change for anyone not using the new functionality.

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

Successfully merging this pull request may close these issues.

4 participants