Skip to content

Commit b7388e6

Browse files
Instead of omitting the field we ensure that it is present and an empty object
1 parent f2e96a1 commit b7388e6

3 files changed

Lines changed: 19 additions & 17 deletions

File tree

lib/src/main/java/graphql/nadel/result/NadelResultMerger.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ internal object NadelResultMerger {
6262

6363
// Ensure field is present in result
6464
if (topLevelResultKey.value !in data) {
65-
data[topLevelResultKey.value] = null
65+
val topLevelField = topLevelFields.first { it.resultKey == topLevelResultKey.value }
66+
data[topLevelResultKey.value] =
67+
if (isNamespacedField(topLevelField, engineSchema) && children.isEmpty()) {
68+
mutableMapOf<String, Any?>()
69+
} else {
70+
null
71+
}
6672
}
6773

6874
if (children.isNotEmpty()) {
@@ -121,18 +127,12 @@ internal object NadelResultMerger {
121127
// NOTE: please ensure all fields are from object types and will NOT have multiple field defs
122128
// Other code in this file relies on this contract
123129
for (field in topLevelFields) {
124-
val namespaced = isNamespacedField(field, engineSchema)
125-
126-
if (namespaced && field.children.isEmpty()) {
127-
continue
128-
}
129-
130130
val requiredChildFields = requiredFields
131131
.computeIfAbsent(NadelResultKey(field.resultKey)) {
132132
mutableListOf()
133133
}
134134

135-
if (namespaced) {
135+
if (isNamespacedField(field, engineSchema)) {
136136
requiredChildFields.addAll(field.children)
137137
}
138138
}

test/src/test/kotlin/graphql/nadel/tests/next/fixtures/namespaced/EmptyNamespaceIsOmittedTest.kt renamed to test/src/test/kotlin/graphql/nadel/tests/next/fixtures/namespaced/EmptyNamespaceIsEmptyObjectTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import graphql.nadel.tests.next.NadelIntegrationTest
77
* Regression test for the HOT/bug where an empty namespace was materialised as `"jira": null`.
88
*
99
* When every child of a namespaced field is skipped (e.g. `@skip(if: true)`), the namespace has no
10-
* children, no service call is made, and it must be OMITTED from the response entirely - NOT emitted
11-
* as `jira: null`. Emitting `null` changed the response shape and tripped a downstream Relay non-null
12-
* handler, turning the whole response into `data: null`.
10+
* children and no service call is made. It must be materialised as an empty object `"jira": {}`,
11+
* NOT as `"jira": null` (which tripped a downstream Relay non-null handler, turning the whole
12+
* response into `data: null`).
1313
*
14-
* Expected result: `{ "hello": "world" }` with no `jira` key.
14+
* Expected result: `{ "hello": "world", "jira": {} }`.
1515
*/
16-
class EmptyNamespaceIsOmittedTest : NadelIntegrationTest(
16+
class EmptyNamespaceIsEmptyObjectTest : NadelIntegrationTest(
1717
query = """
1818
query {
1919
hello

test/src/test/kotlin/graphql/nadel/tests/next/fixtures/namespaced/EmptyNamespaceIsOmittedTestSnapshot.kt renamed to test/src/test/kotlin/graphql/nadel/tests/next/fixtures/namespaced/EmptyNamespaceIsEmptyObjectTestSnapshot.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import kotlin.collections.List
1010
import kotlin.collections.listOf
1111

1212
private suspend fun main() {
13-
graphql.nadel.tests.next.update<EmptyNamespaceIsOmittedTest>()
13+
graphql.nadel.tests.next.update<EmptyNamespaceIsEmptyObjectTest>()
1414
}
1515

1616
/**
@@ -19,7 +19,7 @@ private suspend fun main() {
1919
* Refer to [graphql.nadel.tests.next.UpdateTestSnapshots]
2020
*/
2121
@Suppress("unused")
22-
public class EmptyNamespaceIsOmittedTestSnapshot : TestSnapshot() {
22+
public class EmptyNamespaceIsEmptyObjectTestSnapshot : TestSnapshot() {
2323
/**
2424
* Query
2525
*
@@ -63,7 +63,8 @@ public class EmptyNamespaceIsOmittedTestSnapshot : TestSnapshot() {
6363
* ```json
6464
* {
6565
* "data": {
66-
* "hello": "world"
66+
* "hello": "world",
67+
* "jira": {}
6768
* }
6869
* }
6970
* ```
@@ -72,7 +73,8 @@ public class EmptyNamespaceIsOmittedTestSnapshot : TestSnapshot() {
7273
result = """
7374
| {
7475
| "data": {
75-
| "hello": "world"
76+
| "hello": "world",
77+
| "jira": {}
7678
| }
7779
| }
7880
""".trimMargin(),

0 commit comments

Comments
 (0)