At a glance
- Project
- GraphQL Spec
- Identifier
- #1058
- Stage
- RFC 2 / Draft
- Champion
- @benjie
- Updated
- 2025-07-04
- Latest activity
- Advanced to RFC 2 on
- Source
- Pull request
Pull request description
Fixes #1002.
Previously, list coercion does not detail what to do with variables at all, and that could lead to either a null pointer exception, or to double-coercion of the variable value if you're only following the spec.
Consider the following valid schema:
type Query {
sum(numbers:[Int!]!): Int
}
and the query that is valid against this schema:
query Q ($number: Int = 3) {
sum(numbers: [1, $number, 3])
}
NOTE: We're using the variable in a list item position!
If you issue this to the GraphQL server with variables {"number": null} then CoerceVariableValues will give you {"number": null} and when you fast-forward to CoerceArgumentValues you'll go in to 5.j.iii.1:
https://spec.graphql.org/draft/#sel-NANTHHCJFTDFBBCAACGB0yS
- Let {coercedValues} be an empty unordered Map.
coercedValues = {} - Let {argumentValues} be the argument values provided in {field}.
argumentValues = { numbers: [1, $number, 3] } - Let {fieldName} be the name of {field}.
fieldName = 'sum' - Let {argumentDefinitions} be the arguments defined by {objectType} for the
field named {fieldName}.
argumentDefinitions = { numbers: ... } - For each {argumentDefinition} in {argumentDefinitions}:
- Let {argumentName} be the name of {argumentDefinition}.
argumentName = 'numbers' - Let {argumentType} be the expected type of {argumentDefinition}.
argumentType = [Int!]! - Let {defaultValue} be the default value for {argumentDefinition}.
defaultValue = undefined - Let {hasValue} be {true} if {argumentValues} provides a value for the name
{argumentName}.
hasValue = true - Let {argumentValue} be the value provided in {argumentValues} for the name
{argumentName}.
argumentValue = [1, $number, 3] - If {argumentValue} is a {Variable}:
NOPE- Let {variableName} be the name of {argumentValue}.
- Let {hasValue} be {true} if {variableValues} provides a value for the name {variableName}.
- Let {value} be the value provided in {variableValues} for the name {variableName}.
- Otherwise, let {value} be {argumentValue}.
value = [1, $number, 3] - If {hasValue} is not {true} and {defaultValue} exists (including {null}):
NOT TRIGGERED- Add an entry to {coercedValues} named {argumentName} with the value {defaultValue}.
- Otherwise if {argumentType} is a Non-Nullable type, and either {hasValue} is
not {true} or {value} is {null}, raise a field error.
NOT TRIGGERED - Otherwise if {hasValue} is true:
Yes, it is- If {value} is {null}:
It is not, it is a list- Add an entry to {coercedValues} named {argumentName} with the value {null}.
- Otherwise, if {argumentValue} is a {Variable}:
It is not, it is a list- Add an entry to {coercedValues} named {argumentName} with the value {value}.
- Otherwise:
YES- If {value} cannot be coerced according to the input coercion rules of {argumentType}, raise a field error. TIME TO VISIT LIST COERCION
- Let {coercedValue} be the result of coercing {value} according to the input coercion rules of {argumentType}.
- Add an entry to {coercedValues} named {argumentName} with the value {coercedValue}.
- If {value} is {null}:
- Let {argumentName} be the name of {argumentDefinition}.
- Return {coercedValues}.
Time to visit list coercion
We need to coerce the value [1, $number, 3] to the non-nullable type [Int!]!.
Step 1: handle the non-null. It's not null. Great!
Now we need to coerce the value [1, $number, 3] to the list type [Int!].
Here's what the spec says about input coercion for lists:
When expected as an input, list values are accepted only when each item in the list can be accepted by the list’s item type.
If the value passed as an input to a list type is not a list and not the null value, then the result of input coercion is a list of size one, where the single item value is the result of input coercion for the list’s item type on the provided value (note this may apply recursively for nested lists).
This allows inputs which accept one or many arguments (sometimes referred to as “var args”) to declare their input type as a list while for the common case of a single value, a client can just pass that value directly rather than constructing the list.
We have a list, so we only care about the bold line.
This line seems to miss a bunch of situations.
For example: if we were coercing to [Int] the value [1, $number, 3] with variables {} then is $number (which is undefined, since it wasn't provided in the variables) "accepted by the list's item type"? Really we must coerce this to null, but that doesn't seem to be detailed. In fact this entire section doesn't mention variables at all.
We're actually coercing to [Int!], so the question is: is $number accepted by the list's item type? $number itself is a variable, so...
I've attempted to solve this problem by being much more explicit about the input coercion for lists, inspired by the input coercion for input objects. I've also added a non-normative note highlighting the risk of a null variable being fed through into a non-nullable position, why that can occur (validation) and what we do about it (field error). I've also expanded the table with both variables and many more examples to cover many more edge cases.
Timeline
July 2025
- Advanced to RFC 2 on by leebyron
- Discussed in GraphQL WG
The GraphQL Working Group meeting focused on reviewing recent specification updates and editorial changes, including discussions about request errors, operation execution, and meta fields implementation. The team evaluated various RFC proposals related to coercion algorithms, schema coordinates, and the handling of meta fields in GraphQL.js, with several members providing feedback on potential improvements and clarifications. The conversation ended with discussions about the implementation of experimental features like executeIncrementally and the handling of asynchronous iterators, with the team agreeing to maintain a general specification while allowing for implementation-specific details in GraphQL.js.
Zoom summary · GraphQL WG · 2025-07-03The GraphQL Working Group meeting began with Lee noting the thinner attendance due to the holiday week and introduced the agenda, which included updates on the spec, editorial changes, and several RFCs. Benjie highlighted the upcoming discount period for GraphQL Conference tickets and provided a summary of the previous meeting's progress, including merged items like executable descriptions and deprecation discussions. The group planned to discuss schema coordinates, argument values, list coercion algorithms, and incorporating Meta fields into execute collected fields, among other topics. Lee encouraged attendees to review the previous meeting's outcomes and emphasized the importance of highlighting upcoming dates to avoid scheduling conflicts.
Zoom summary · GraphQL WG · 2025-07-03The meeting focused on discussing schema coordinates and meta fields, with Leebyron explaining that meta fields are not part of the schema and can be implementation-specific. Benjie highlighted the distinction between schema elements and introspection types, noting that introspection queries may include both schema and implementation-specific types. The group also discussed two RFCs related to argument value coercion in GraphQL, with Leebyron proposing a more significant rewrite of the algorithm to avoid relying on the "hasValue" concept. Benjie presented a coercion table for arrays and input objects, which aligns with GraphQL.js behavior and addresses underspecified areas in the current specification.
Zoom summary · GraphQL WG · 2025-07-03GraphQL Input Coercion DiscussionBenjie and Lee discussed advancing a GraphQL specification for input coercion, particularly focusing on list coercion. Lee suggested not advancing to Stage 2 due to incomplete definitions for input object coercion, proposing instead to address the entire input coercion domain holistically. Benjie argued for prioritizing a more specific solution for list coercion to help implementations, with the possibility of a later refactor to address the broader input coercion problem. They agreed to proceed with the current draft for list coercion while exploring a more comprehensive solution in parallel.
Zoom summary · GraphQL WG · 2025-07-03
June 2025
- Marked as Ready for Next Stage on by benjie
- Discussed in GraphQL WGNext Steps
Lee: Review Benjie's list coercion PR for algorithmic clarity and variable handling
Zoom summary · GraphQL WG · 2025-06-26The group discusses two issues in the GraphQL specification. Benjie presents a longstanding bug in the spec that needs fixing, providing detailed spec text for Lee to review. They also address list coercion, which Benjie has rewritten to use a more algorithmic system and to better handle variables. Lee agrees to carefully review both issues, emphasizing the importance of aligning the spec with the reference implementation, GraphQL.js. The meeting concludes with Lee acknowledging the productivity of the session and the need to finalize the discussed changes.
Zoom summary · GraphQL WG · 2025-06-26
January 2025
- 2 commits pushed on
- @benjie committed "Reflect that variable values should already be coerced, and thus appl…"
- @benjie committed "'Including null' (copied from section 6)"
- Discussed in GraphQL WG
December 2024
- Discussed in GraphQL WG
RFC - promote to RFC2? Merge as an editorial fix?
Meeting notes · GraphQL WG · 2024-12-05
November 2024
- @benjie committed "Fix bug in null handling" on
December 2023
- Advanced to RFC 1 on by michaelstaib
- Discussed in GraphQL WG
November 2023
- @benjie committed "Add another example" on
- Pull request created on by benjie
- 4 commits pushed on
- @benjie committed "Add note about nullable variables with default values"
- @benjie committed "Add variables to table"
- @benjie committed "Algorithm for coercing list values"
- @benjie committed "Move note and clarify algorithm"
- Advanced to RFC 0 on by benjie