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

Clarification: scalar coercion rules and default input values #868

Closed
tonyghita opened this issue May 11, 2021 · 4 comments
Closed

Clarification: scalar coercion rules and default input values #868

tonyghita opened this issue May 11, 2021 · 4 comments

Comments

@tonyghita
Copy link

tonyghita commented May 11, 2021

I'm hoping to get some clarification around the Float portion of the specification.

Specifically, I'm considering the input coercion section and how this relates to default values.

When expected as an input type, both integer and float input values are accepted. Integer input values are coerced to Float by adding an empty fractional part, for example 1.0 for the integer input value 1.

Say we have the schema

input Example {
  f: Float = 1
 }

and we do an introspection query for the default value of Example.f

{
  __type(name: "Example") {
    inputFields {
      defaultValue
    }
  }
}

Should the defaultValue that comes back on the introspection query response be "1" (as written) or "1.0" (according to the input coercion rules)?

@benjie
Copy link
Member

benjie commented May 11, 2021

No, the 1.0 is just an example of a number with an empty fractional part; what you do in your programming language of choice is implementation specific (for example in C you'd cast from int to float, in JS ints and floats are both just numbers, etc). The float values 1, 1.0 and 1.00 are all equivalent and GraphQL does not expect (or want) you to distinguish them.

When it comes to output, the spec states:

Response formats that support an appropriate double-precision number type should use that type to represent this scalar.

For example if you were using protobuf as your response format then you'd want to use the double type.

Mostly with GraphQL we use JSON (but that's not required) which doesn't explicitly differentiate between int and float, so representing the float in the simplest format (1) is perfectly fine.

TL;DR: no, just returning the float 1 is fine.

@tonyghita
Copy link
Author

tonyghita commented May 11, 2021

@benjie that seems reasonable. What does this sentence of the specification refer to?

Integer input values are coerced to Float by adding an empty fractional part, for example 1.0 for the integer input value 1.

If the coercion of Float is implementation specific, why specify this behavior? Where would this transformation be reflected?

@benjie
Copy link
Member

benjie commented May 11, 2021

If the coercion of Float is implementation specific, why specify this behavior?

That's a programming-language-agnostic description of how to convert an int to a float.

Where would this transformation be reflected?

During coercion; i.e. if you receive an int where a float was expected, convert it to a float that has the same integer part and an empty fractional part.

@tonyghita
Copy link
Author

Thank you for the clarification!

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