Skip to content

AST: IntValueNode and FloatValueNode store values as string #4014

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

Closed
SeppahBaws opened this issue Jan 24, 2024 · 2 comments
Closed

AST: IntValueNode and FloatValueNode store values as string #4014

SeppahBaws opened this issue Jan 24, 2024 · 2 comments

Comments

@SeppahBaws
Copy link

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.

export interface IntValueNode {
readonly kind: Kind.INT;
readonly loc?: Location | undefined;
readonly value: string;
}

export interface FloatValueNode {
readonly kind: Kind.FLOAT;
readonly loc?: Location | undefined;
readonly value: string;
}

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?

@JoviDeCroock
Copy link
Member

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.

@SeppahBaws
Copy link
Author

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

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