Skip to content

Add spec for Long custom scalar type#26

Merged
martinbonnin merged 8 commits intographql:mainfrom
jakobmerrild:add-spec-for-long-scalar
Dec 18, 2025
Merged

Add spec for Long custom scalar type#26
martinbonnin merged 8 commits intographql:mainfrom
jakobmerrild:add-spec-for-long-scalar

Conversation

@jakobmerrild
Copy link
Contributor

The Long data type is commonly used in many applications because the largest value that can be held in an Int value is 2^31-1. See: graphql/graphql-spec#73

I'm honestly torn about whether or not output strings should be allowed 👀 . As discussed in the linked issue common clients (JavaScript) does not read JSON numbers above 2^53-1 into number without loss of precision.

Also accepting String inputs without accepting String outputs seem inconsistent.

The `Long` data type is commonly used in many applications because
the largest value that can be held in an `Int` value is `2^31-1`.
See: graphql/graphql-spec#73
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Dec 16, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

Copy link
Contributor

@martinbonnin martinbonnin left a comment

Choose a reason for hiding this comment

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

Thanks for looking into this.

I left a few comments mainly FYI. Feel free to use them or reject them, this repo is made so that we don't have all to agree. Just resolve the comments you reject so we can know when to merge this.

jakobmerrild and others added 3 commits November 30, 2025 14:23
Co-authored-by: Martin Bonnin <martin@mbonnin.net>
Co-authored-by: Martin Bonnin <martin@mbonnin.net>
This makes the wording more consistent and removes all references
to JSON.
There's a separate suggestion to update the guide lines to not
mention being liberal in what is accepted for inputs.
See: graphql#40
@benjie
Copy link
Member

benjie commented Dec 4, 2025

Though it makes sense to use IntValue when parsing literals, for serialization I think you should definitely use strings, at least in JSON. For example, if you use to_json on a bigint in Postgres it will output a long number, which JavaScript can then not process, causing tremendous headaches. JavaScript will NOT change this, doing so would be a breaking change which is antithetical to JavaScript's design goals, so this is something that we have to live with.

Using a numeric format when serializing to JSON would make programming languages which don't have native support for parsing arbitrarily long numbers in JSON have to use a custom JSON parser for all payloads sent using the most common HTTP encoding. That's not really viable, and will result in few people adopting Long.

Instead, I'd recommend that you require string in both directions, at least for JSON.

Note this text from the spec:

a GraphQL service could define a scalar called UUID which, while serialized as a string, conforms to RFC 4122. When querying a field of type UUID, you can then rely on the ability to parse the result with an RFC 4122 compliant parser. Another example of a potentially useful custom scalar is URL, which serializes as a string, but is guaranteed by the service to be a valid URL.

Serializing to string is perfectly valid, and is a safe fallback for basically everything. Even binary can serialize as string via base64/etc.

@martinbonnin
Copy link
Contributor

martinbonnin commented Dec 17, 2025

@jakobmerrild did you want to update this to use Strings in both directions? (or limit the range to something that JS can parse?)

@jakobmerrild
Copy link
Contributor Author

@jakobmerrild did you want to update this to use Strings in both directions? (or limit the range to something that JS can parse?)

Yes! Sorry, I've been busy with other work. I'll try to get to this later today

Ultimately, not every language can easily parse arbitrarily large
JSON numbers. As such we are better off using a string serialization
scheme which then allows the adaptors to use whichever methods they
see fit to turn the string into a different representation after
coercion, e.g. Long or BigInt
The recent changes represent a significant departure from the original
spec, so we update the date :)
@jakobmerrild
Copy link
Contributor Author

Aaaand, I got distracted again. Have updated the spec to:

  1. Conform to the template-string template
  2. Specify that serialisation is supposed to be to/from string values

@martinbonnin
Copy link
Contributor

@jakobmerrild thanks for updating this PR. We have a few CI failures. Mind looking into those? I think we can merge after that.

@martinbonnin
Copy link
Contributor

All good ✅ @jakobmerrild is it ok to merge this pull request?

@jakobmerrild
Copy link
Contributor Author

All good from my end 👍

@martinbonnin
Copy link
Contributor

All good from my end

It's the important end :)

As detailed in the README, feedbacks may not block the merging of a new scalar specification. The authors has final say. Merging this.

@martinbonnin martinbonnin merged commit 040b3e9 into graphql:main Dec 18, 2025
5 checks passed
@martinbonnin
Copy link
Contributor

Thanks for the contribution!

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