From 7e13d5c2cb8a38229d602a7a7e37d81fbbb84c00 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Thu, 9 Nov 2023 15:32:30 +0000 Subject: [PATCH 1/6] Fix coercion table for list --- spec/Section 3 -- Type System.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/Section 3 -- Type System.md b/spec/Section 3 -- Type System.md index d32b08566..08a38a244 100644 --- a/spec/Section 3 -- Type System.md +++ b/spec/Section 3 -- Type System.md @@ -1780,7 +1780,9 @@ Following are examples of input coercion with various list types and values: | `[Int]` | `1` | `[1]` | | `[Int]` | `null` | `null` | | `[[Int]]` | `[[1], [2, 3]]` | `[[1], [2, 3]]` | -| `[[Int]]` | `[1, 2, 3]` | Error: Incorrect item value | +| `[[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` | From cf0f3ee03b3a232167ee6f40be985c1077d24723 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Thu, 9 Nov 2023 14:19:05 +0000 Subject: [PATCH 2/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 f357069f9..febadf884 100644 --- a/spec/Section 6 -- Execution.md +++ b/spec/Section 6 -- Execution.md @@ -639,6 +639,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 b29453720b9eb24385ec1c05a267a18b8bb00751 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Thu, 9 Nov 2023 15:07:33 +0000 Subject: [PATCH 3/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 08a38a244..3c7e0e151 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 07d936ee601ef9ca44390f13d9856e5ba1d3af6b Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Thu, 9 Nov 2023 16:59:23 +0000 Subject: [PATCH 4/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 3c7e0e151..6c4399af1 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 ef705688d952fdc097c8a76aa4e1417a958d687a Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Thu, 9 Nov 2023 17:14:38 +0000 Subject: [PATCH 5/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 6c4399af1..fd2ac2663 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 febadf884..f357069f9 100644 --- a/spec/Section 6 -- Execution.md +++ b/spec/Section 6 -- Execution.md @@ -639,13 +639,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 76bec1c14ebc4c5bfffaacd2026beb2812ff1c86 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Mon, 13 Nov 2023 09:26:27 +0000 Subject: [PATCH 6/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 fd2ac2663..4827e773f 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]]` |