graphql-js icon indicating copy to clipboard operation
graphql-js copied to clipboard

AST: `IntValueNode` and `FloatValueNode` store values as string

Open SeppahBaws opened this issue 1 year ago • 2 comments

Using the AST provided by the library, I'm doing some introspection on the queries to extract the default values passed in to a query.

I noticed that the IntValueNode and FloatValueNode types both use string to store the value instead of number.

https://github.com/graphql/graphql-js/blob/2aedf25e157d1d1c8fdfeaa4c0d2f3d9d3457dba/src/language/ast.ts#L478-L482 https://github.com/graphql/graphql-js/blob/2aedf25e157d1d1c8fdfeaa4c0d2f3d9d3457dba/src/language/ast.ts#L484-L488

This seems a bit counter-intuitive to me, and would require me to write some custom parsing code on top of the existing parsing that graphql-js does. Is this an intentional decision to store the values in a string instead of a number, or is it something that should be changed?

SeppahBaws avatar Jan 24 '24 09:01 SeppahBaws

The parser will always parse the raw-value as a string and hence put it in the AST that way, you can use valueFromAST or valueFromASTUntyped to correctly have this coerced for you.

It also looks like the GraphQL specification defines all literal values, including integers, as strings. This enables reliably parsing and serializing integers and floats across different programming languages and environments.

JoviDeCroock avatar Jan 30 '24 10:01 JoviDeCroock

Ah okay thanks for the insight, I did not know about that! 🙂

SeppahBaws avatar Jan 30 '24 11:01 SeppahBaws