From b21bb996796b487d7f369f7e162a0d3096ffaae4 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Thu, 9 Nov 2023 14:19:05 +0000 Subject: [PATCH 1/6] Add note about nullable variables with default values --- spec/Section 6 -- Execution.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spec/Section 6 -- Execution.md b/spec/Section 6 -- Execution.md index 8efd401a4..9d3fcaee5 100644 --- a/spec/Section 6 -- Execution.md +++ b/spec/Section 6 -- Execution.md @@ -645,6 +645,13 @@ Note: Variable values are not coerced because they are expected to be coerced before executing the operation in {CoerceVariableValues()}, and valid operations must only allow usage of variables of appropriate types. +Note: When a default value exists for a variable definition, the type of the +variable is allowed to be nullable even if it is used in a non-nullable +position, see +[Allowing Optional Variables When Default Values Exist](#sec-All-Variable-Usages-Are-Allowed.Allowing-Optional-Variables-When-Default-Values-Exist) +in Validation. If the value for a variable is explicitly {null} and is used in a +non-nullable position, a _field error_ will be raised. + ### Value Resolution While nearly all of GraphQL execution can be described generically, ultimately From a91cdba0dfedd0c13655f03cc3ac3910da388686 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Thu, 9 Nov 2023 15:07:33 +0000 Subject: [PATCH 2/6] Add variables to table --- spec/Section 3 -- Type System.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/spec/Section 3 -- Type System.md b/spec/Section 3 -- Type System.md index e52519fb7..74f598399 100644 --- a/spec/Section 3 -- Type System.md +++ b/spec/Section 3 -- Type System.md @@ -1773,18 +1773,18 @@ constructing the list. Following are examples of input coercion with various list types and values: -| Expected Type | Provided Value | Coerced Value | -| ------------- | ---------------- | --------------------------- | -| `[Int]` | `[1, 2, 3]` | `[1, 2, 3]` | -| `[Int]` | `[1, "b", true]` | Error: Incorrect item value | -| `[Int]` | `1` | `[1]` | -| `[Int]` | `null` | `null` | -| `[[Int]]` | `[[1], [2, 3]]` | `[[1], [2, 3]]` | -| `[[Int]]` | `[1, 2, 3]` | `[[1], [2], [3]]` | -| `[[Int]]` | `[1, null, 3]` | `[[1], null, [3]]` | -| `[[Int]]` | `[[1], ["b"]]` | Error: Incorrect item value | -| `[[Int]]` | `1` | `[[1]]` | -| `[[Int]]` | `null` | `null` | +| Expected Type | Literal Value | Variable Values | Coerced Value | +| ------------- | ---------------- | --------------- | --------------------------- | +| `[Int]` | `[1, 2, 3]` | `{}` | `[1, 2, 3]` | +| `[Int]` | `[1, "b", true]` | `{}` | Error: Incorrect item value | +| `[Int]` | `1` | `{}` | `[1]` | +| `[Int]` | `null` | `{}` | `null` | +| `[[Int]]` | `[[1], [2, 3]]` | `{}` | `[[1], [2, 3]]` | +| `[[Int]]` | `[1, 2, 3]` | `{}` | `[[1], [2], [3]]` | +| `[[Int]]` | `[1, null, 3]` | `{}` | `[[1], null, [3]]` | +| `[[Int]]` | `[[1], ["b"]]` | `{}` | Error: Incorrect item value | +| `[[Int]]` | `1` | `{}` | `[[1]]` | +| `[[Int]]` | `null` | `{}` | `null` | ## Non-Null From 4ca2023992ed5bb2aef2c58d4f532859fb580acd Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Thu, 9 Nov 2023 16:59:23 +0000 Subject: [PATCH 3/6] Algorithm for coercing list values --- spec/Section 3 -- Type System.md | 73 ++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/spec/Section 3 -- Type System.md b/spec/Section 3 -- Type System.md index 74f598399..c147257e2 100644 --- a/spec/Section 3 -- Type System.md +++ b/spec/Section 3 -- Type System.md @@ -1771,20 +1771,69 @@ This allows inputs which accept one or many arguments (sometimes referred to as single value, a client can just pass that value directly rather than constructing the list. +The result of coercion of a value {value} to a list type {listType} is +{CoerceListValue(value, listType)}. + +CoerceListValue(value, listType): + +- If {value} is {null}, return {null}. +- Let {itemType} be the inner type of {listType}. +- Let {coercedList} be an empty list. +- If {value} is a list: + - For each {itemValue} in {value}: + - Let {coercedItemValue} be {CoerceListItemValue(itemValue, itemType)}. + - Append {coercedItemValue} to {coercedList}. +- Otherwise: + - Let {coercedItemValue} be {CoerceListItemValue(value, itemType)}. + - Append {coercedItemValue} to {coercedList}. +- Return {coercedList}. + +CoerceListItemValue(itemValue, itemType): + +- If {itemValue} is {null}, return {null}. +- Otherwise, if {itemValue} is a Variable: + - Let {runtimeValue} be the runtime value of that variable, or {null} if no + runtime value is provided. + - If {runtimeValue} is {null} and {itemType} is a non-null type, a _field + error_ must be raised. + - Return {runtimeValue}. +- Otherwise, return the result of coercing {itemValue} according to the input + coercion rules for {itemType}. + Following are examples of input coercion with various list types and values: -| Expected Type | Literal Value | Variable Values | Coerced Value | -| ------------- | ---------------- | --------------- | --------------------------- | -| `[Int]` | `[1, 2, 3]` | `{}` | `[1, 2, 3]` | -| `[Int]` | `[1, "b", true]` | `{}` | Error: Incorrect item value | -| `[Int]` | `1` | `{}` | `[1]` | -| `[Int]` | `null` | `{}` | `null` | -| `[[Int]]` | `[[1], [2, 3]]` | `{}` | `[[1], [2, 3]]` | -| `[[Int]]` | `[1, 2, 3]` | `{}` | `[[1], [2], [3]]` | -| `[[Int]]` | `[1, null, 3]` | `{}` | `[[1], null, [3]]` | -| `[[Int]]` | `[[1], ["b"]]` | `{}` | Error: Incorrect item value | -| `[[Int]]` | `1` | `{}` | `[[1]]` | -| `[[Int]]` | `null` | `{}` | `null` | +| Expected Type | Literal Value | Variable Values | Coerced Value | +| ------------- | ---------------- | --------------- | ---------------------------- | +| `[Int]` | `[1, 2, 3]` | `{}` | `[1, 2, 3]` | +| `[Int]` | `[1, null]` | `{}` | `[1, null]` | +| `[Int]` | `[1, "b", true]` | `{}` | Error: Incorrect item value | +| `[Int]` | `1` | `{}` | `[1]` | +| `[Int]` | `null` | `{}` | `null` | +| `[Int]` | `[1, $b]` | `{}` | `[1, null]` | +| `[Int]` | `[1, $b]` | `{"b": 2}` | `[1, 2]` | +| `[Int]` | `[1, $b]` | `{"b": null}` | `[1, null]` | +| `[Int]!` | `[null]` | `{}` | `[null]` | +| `[Int]!` | `null` | `{}` | Error: Must be non-null | +| `[Int!]` | `[1, 2, 3]` | `{}` | `[1, 2, 3]` | +| `[Int!]` | `[1, null]` | `{}` | Error: Item must be non-null | +| `[Int!]` | `[1, "b", true]` | `{}` | Error: Incorrect item value | +| `[Int!]` | `1` | `{}` | `[1]` | +| `[Int!]` | `null` | `{}` | `null` | +| `[Int!]` | `[1, $b]` | `{}` | Error: Item must be non-null | +| `[Int!]` | `[1, $b]` | `{"b": 2}` | `[1, 2]` | +| `[Int!]` | `[1, $b]` | `{"b": null}` | Error: Item must be non-null | +| `[[Int]]` | `[[1], [2, 3]]` | `{}` | `[[1], [2, 3]]` | +| `[[Int]]` | `[1, 2, 3]` | `{}` | `[[1], [2], [3]]` | +| `[[Int]]` | `[1, null, 3]` | `{}` | `[[1], null, [3]]` | +| `[[Int]]` | `[[1], ["b"]]` | `{}` | Error: Incorrect item value | +| `[[Int]]` | `1` | `{}` | `[[1]]` | +| `[[Int]]` | `null` | `{}` | `null` | +| `[[Int]]` | `[1, [$b]]` | `{}` | `[[1],[null]]` | +| `[[Int]]` | `[1, [$b]]` | `{"b": null}` | `[[1],[null]]` | +| `[[Int]]` | `[1, [$b]]` | `{"b": 2}` | `[[1],[2]]` | +| `[[Int]]` | `[1, $b]` | `{"b": [2]}` | `[[1],[2]]` | +| `[[Int]]` | `[1, $b]` | `{"b": 2}` | `[[1],[2]]` | +| `[[Int]]` | `[1, $b]` | `{"b": null}` | `[[1],null]` | ## Non-Null From ec3d50a664f02a3218c81bb8c3202c91765fa952 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Thu, 9 Nov 2023 17:14:38 +0000 Subject: [PATCH 4/6] Move note and clarify algorithm --- spec/Section 3 -- Type System.md | 20 ++++++++++++++++---- spec/Section 6 -- Execution.md | 7 ------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/spec/Section 3 -- Type System.md b/spec/Section 3 -- Type System.md index c147257e2..2a1f783fd 100644 --- a/spec/Section 3 -- Type System.md +++ b/spec/Section 3 -- Type System.md @@ -1792,14 +1792,26 @@ CoerceListItemValue(itemValue, itemType): - If {itemValue} is {null}, return {null}. - Otherwise, if {itemValue} is a Variable: - - Let {runtimeValue} be the runtime value of that variable, or {null} if no - runtime value is provided. - - If {runtimeValue} is {null} and {itemType} is a non-null type, a _field + - If the variable provides a runtime value: + - Let {coercedItemValue} be the runtime value of the variable. + - Otherwise, if the variable definition provides a default value: + - Let {coercedItemValue} be this default value. + - Otherwise: + - Let {coercedItemValue} be {null}. + - If {coercedItemValue} is {null} and {itemType} is a non-null type, a _field error_ must be raised. - - Return {runtimeValue}. + - Return {coercedItemValue}. - Otherwise, return the result of coercing {itemValue} according to the input coercion rules for {itemType}. +Note: When a default value exists for a variable definition, the type of the +variable is allowed to be nullable even if it is used in a non-nullable +position, see +[Allowing Optional Variables When Default Values Exist](#sec-All-Variable-Usages-Are-Allowed.Allowing-Optional-Variables-When-Default-Values-Exist) +in Validation. If the value for such a variable is explicitly {null} and is used +as the value for a list item of non-nullable type then a _field error_ will be +raised. + Following are examples of input coercion with various list types and values: | Expected Type | Literal Value | Variable Values | Coerced Value | diff --git a/spec/Section 6 -- Execution.md b/spec/Section 6 -- Execution.md index 9d3fcaee5..8efd401a4 100644 --- a/spec/Section 6 -- Execution.md +++ b/spec/Section 6 -- Execution.md @@ -645,13 +645,6 @@ Note: Variable values are not coerced because they are expected to be coerced before executing the operation in {CoerceVariableValues()}, and valid operations must only allow usage of variables of appropriate types. -Note: When a default value exists for a variable definition, the type of the -variable is allowed to be nullable even if it is used in a non-nullable -position, see -[Allowing Optional Variables When Default Values Exist](#sec-All-Variable-Usages-Are-Allowed.Allowing-Optional-Variables-When-Default-Values-Exist) -in Validation. If the value for a variable is explicitly {null} and is used in a -non-nullable position, a _field error_ will be raised. - ### Value Resolution While nearly all of GraphQL execution can be described generically, ultimately From 6aed5a9653a92ada8849fdf6e289623997977436 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Mon, 13 Nov 2023 09:26:27 +0000 Subject: [PATCH 5/6] Add another example --- spec/Section 3 -- Type System.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/Section 3 -- Type System.md b/spec/Section 3 -- Type System.md index 2a1f783fd..7e9663946 100644 --- a/spec/Section 3 -- Type System.md +++ b/spec/Section 3 -- Type System.md @@ -1836,6 +1836,7 @@ Following are examples of input coercion with various list types and values: | `[Int!]` | `[1, $b]` | `{"b": null}` | Error: Item must be non-null | | `[[Int]]` | `[[1], [2, 3]]` | `{}` | `[[1], [2, 3]]` | | `[[Int]]` | `[1, 2, 3]` | `{}` | `[[1], [2], [3]]` | +| `[[Int]]` | `[1, [2], 3]` | `{}` | `[[1], [2], [3]]` | | `[[Int]]` | `[1, null, 3]` | `{}` | `[[1], null, [3]]` | | `[[Int]]` | `[[1], ["b"]]` | `{}` | Error: Incorrect item value | | `[[Int]]` | `1` | `{}` | `[[1]]` | From fba35d54523692c5b112c8fc3c773f50ef402b43 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Thu, 28 Nov 2024 18:14:59 +0000 Subject: [PATCH 6/6] Fix bug in null handling --- spec/Section 3 -- Type System.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/Section 3 -- Type System.md b/spec/Section 3 -- Type System.md index 7e9663946..7c07ae64d 100644 --- a/spec/Section 3 -- Type System.md +++ b/spec/Section 3 -- Type System.md @@ -1790,8 +1790,7 @@ CoerceListValue(value, listType): CoerceListItemValue(itemValue, itemType): -- If {itemValue} is {null}, return {null}. -- Otherwise, if {itemValue} is a Variable: +- If {itemValue} is a Variable: - If the variable provides a runtime value: - Let {coercedItemValue} be the runtime value of the variable. - Otherwise, if the variable definition provides a default value: