From da844c7e159987de30872c6a8303fc49cc1d9e73 Mon Sep 17 00:00:00 2001 From: Steven Barker Date: Wed, 19 Jun 2024 11:49:04 +1200 Subject: [PATCH] tests for defer queries with errors --- ...rQueryWithDifferentServiceCallsSnapshot.kt | 32 ++-- .../defer/DeferOnListItemsTestSnapshot.kt | 166 ++++++++++++++++++ .../next/fixtures/defer/DeferWithErrorTest.kt | 70 ++++++++ .../defer/DeferWithErrorTestSnapshot.kt | 106 +++++++++++ .../MultipleDeferDirectivesTestSnapshot.kt | 12 +- ...dsinMultipleDeferDirectivesTestSnapshot.kt | 22 +-- 6 files changed, 375 insertions(+), 33 deletions(-) create mode 100644 test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/DeferOnListItemsTestSnapshot.kt create mode 100644 test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/DeferWithErrorTest.kt create mode 100644 test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/DeferWithErrorTestSnapshot.kt diff --git a/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/ComprehensiveDeferQueryWithDifferentServiceCallsSnapshot.kt b/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/ComprehensiveDeferQueryWithDifferentServiceCallsSnapshot.kt index 0e368c5e2..9125924dc 100644 --- a/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/ComprehensiveDeferQueryWithDifferentServiceCallsSnapshot.kt +++ b/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/ComprehensiveDeferQueryWithDifferentServiceCallsSnapshot.kt @@ -96,13 +96,8 @@ public class ComprehensiveDeferQueryWithDifferentServiceCallsSnapshot : TestSnap | "path": [ | "user" | ], - | "label": "team-details", | "data": { - | "teamName": "The Unicorns", - | "teamMembers": [ - | "Felipe", - | "Franklin" - | ] + | "profilePicture": "https://examplesite.com/user/profile_picture.jpg" | } | } | ] @@ -116,8 +111,13 @@ public class ComprehensiveDeferQueryWithDifferentServiceCallsSnapshot : TestSnap | "path": [ | "user" | ], + | "label": "team-details", | "data": { - | "profilePicture": "https://examplesite.com/user/profile_picture.jpg" + | "teamName": "The Unicorns", + | "teamMembers": [ + | "Felipe", + | "Franklin" + | ] | } | } | ] @@ -133,12 +133,12 @@ public class ComprehensiveDeferQueryWithDifferentServiceCallsSnapshot : TestSnap * "data": { * "user": { * "name": "Steven", - * "profilePicture": "https://examplesite.com/user/profile_picture.jpg", * "teamName": "The Unicorns", * "teamMembers": [ * "Felipe", * "Franklin" - * ] + * ], + * "profilePicture": "https://examplesite.com/user/profile_picture.jpg" * }, * "product": { * "productName": "Awesome Product", @@ -188,13 +188,8 @@ public class ComprehensiveDeferQueryWithDifferentServiceCallsSnapshot : TestSnap | "path": [ | "user" | ], - | "label": "team-details", | "data": { - | "teamName": "The Unicorns", - | "teamMembers": [ - | "Felipe", - | "Franklin" - | ] + | "profilePicture": "https://examplesite.com/user/profile_picture.jpg" | } | } | ] @@ -208,8 +203,13 @@ public class ComprehensiveDeferQueryWithDifferentServiceCallsSnapshot : TestSnap | "path": [ | "user" | ], + | "label": "team-details", | "data": { - | "profilePicture": "https://examplesite.com/user/profile_picture.jpg" + | "teamName": "The Unicorns", + | "teamMembers": [ + | "Felipe", + | "Franklin" + | ] | } | } | ] diff --git a/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/DeferOnListItemsTestSnapshot.kt b/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/DeferOnListItemsTestSnapshot.kt new file mode 100644 index 000000000..ce1b5ce10 --- /dev/null +++ b/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/DeferOnListItemsTestSnapshot.kt @@ -0,0 +1,166 @@ +// @formatter:off +package graphql.nadel.tests.next.fixtures.defer + +import graphql.nadel.tests.next.ExpectedNadelResult +import graphql.nadel.tests.next.ExpectedServiceCall +import graphql.nadel.tests.next.TestSnapshot +import graphql.nadel.tests.next.listOfJsonStrings +import kotlin.Suppress +import kotlin.collections.List +import kotlin.collections.listOf + +/** + * This class is generated. Do NOT modify. + * + * Refer to [graphql.nadel.tests.next.UpdateTestSnapshots + */ +@Suppress("unused") +public class DeferOnListItemsTestSnapshot : TestSnapshot() { + override val calls: List = listOf( + ExpectedServiceCall( + service = "defer", + query = """ + | { + | defer { + | list { + | fast + | ... @defer { + | slow + | } + | } + | } + | } + """.trimMargin(), + variables = "{}", + result = """ + | { + | "data": { + | "defer": { + | "list": [ + | { + | "fast": "fastString" + | }, + | { + | "fast": "fastString" + | } + | ] + | } + | }, + | "hasNext": true + | } + """.trimMargin(), + delayedResults = listOfJsonStrings( + """ + | { + | "hasNext": false, + | "incremental": [ + | { + | "path": [ + | "defer", + | "list", + | 1 + | ], + | "data": { + | "slow": "slowString" + | } + | } + | ] + | } + """.trimMargin(), + """ + | { + | "hasNext": true, + | "incremental": [ + | { + | "path": [ + | "defer", + | "list", + | 0 + | ], + | "data": { + | "slow": "slowString" + | } + | } + | ] + | } + """.trimMargin(), + ), + ), + ) + + /** + * ```json + * { + * "data": { + * "defer": { + * "list": [ + * { + * "fast": "fastString", + * "slow": "slowString" + * }, + * { + * "fast": "fastString", + * "slow": "slowString" + * } + * ] + * } + * } + * } + * ``` + */ + override val result: ExpectedNadelResult = ExpectedNadelResult( + result = """ + | { + | "data": { + | "defer": { + | "list": [ + | { + | "fast": "fastString" + | }, + | { + | "fast": "fastString" + | } + | ] + | } + | }, + | "hasNext": true + | } + """.trimMargin(), + delayedResults = listOfJsonStrings( + """ + | { + | "hasNext": false, + | "incremental": [ + | { + | "path": [ + | "defer", + | "list", + | 1 + | ], + | "data": { + | "slow": "slowString" + | } + | } + | ] + | } + """.trimMargin(), + """ + | { + | "hasNext": true, + | "incremental": [ + | { + | "path": [ + | "defer", + | "list", + | 0 + | ], + | "data": { + | "slow": "slowString" + | } + | } + | ] + | } + """.trimMargin(), + ), + ) +} diff --git a/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/DeferWithErrorTest.kt b/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/DeferWithErrorTest.kt new file mode 100644 index 000000000..e9da236cf --- /dev/null +++ b/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/DeferWithErrorTest.kt @@ -0,0 +1,70 @@ +package graphql.nadel.tests.next.fixtures.defer + +import graphql.GraphqlErrorBuilder +import graphql.execution.DataFetcherResult +import graphql.nadel.NadelExecutionHints +import graphql.nadel.tests.next.NadelIntegrationTest + +open class DeferWithErrorTest : NadelIntegrationTest( + query = """ + query { + defer { + hello + ... @defer(label: "slow-defer") { + slow + } + } + } + """.trimIndent(), + services = listOf( + Service( + name = "defer", + overallSchema = """ + directive @defer(if: Boolean, label: String) on FRAGMENT_SPREAD | INLINE_FRAGMENT + + type Query { + defer: DeferApi + } + type DeferApi { + hello: String + slow: String + } + + """.trimIndent(), + runtimeWiring = { wiring -> + data class DeferApi( + val hello: String, + val slow: String, + ) + + wiring + .type("Query") { type -> + type + .dataFetcher("defer") { env -> + Any() + } + } + .type("DeferApi") { type -> + type + .dataFetcher("hello") { env -> + "helloString" + } + .dataFetcher("slow") { env -> + DataFetcherResult.newResult() + .error( + GraphqlErrorBuilder.newError() + .message("An error occurred while fetching 'slow'") + .build() + ) + .build() + } + } + }, + ), + ), +) { + override fun makeExecutionHints(): NadelExecutionHints.Builder { + return super.makeExecutionHints() + .deferSupport { true } + } +} diff --git a/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/DeferWithErrorTestSnapshot.kt b/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/DeferWithErrorTestSnapshot.kt new file mode 100644 index 000000000..1f4d1adf4 --- /dev/null +++ b/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/DeferWithErrorTestSnapshot.kt @@ -0,0 +1,106 @@ +// @formatter:off +package graphql.nadel.tests.next.fixtures.defer + +import graphql.nadel.tests.next.ExpectedNadelResult +import graphql.nadel.tests.next.ExpectedServiceCall +import graphql.nadel.tests.next.TestSnapshot +import graphql.nadel.tests.next.listOfJsonStrings +import kotlin.Suppress +import kotlin.collections.List +import kotlin.collections.listOf + +/** + * This class is generated. Do NOT modify. + * + * Refer to [graphql.nadel.tests.next.UpdateTestSnapshots + */ +@Suppress("unused") +public class DeferWithErrorTestSnapshot : TestSnapshot() { + override val calls: List = listOf( + ExpectedServiceCall( + service = "defer", + query = """ + | { + | defer { + | hello + | ... @defer(label: "slow-defer") { + | slow + | } + | } + | } + """.trimMargin(), + variables = "{}", + result = """ + | { + | "data": { + | "defer": { + | "hello": "helloString" + | } + | }, + | "hasNext": true + | } + """.trimMargin(), + delayedResults = listOfJsonStrings( + """ + | { + | "hasNext": false, + | "incremental": [ + | { + | "path": [ + | "defer" + | ], + | "label": "slow-defer", + | "data": { + | "slow": null + | } + | } + | ] + | } + """.trimMargin(), + ), + ), + ) + + /** + * ```json + * { + * "data": { + * "defer": { + * "hello": "helloString", + * "slow": null + * } + * } + * } + * ``` + */ + override val result: ExpectedNadelResult = ExpectedNadelResult( + result = """ + | { + | "data": { + | "defer": { + | "hello": "helloString" + | } + | }, + | "hasNext": true + | } + """.trimMargin(), + delayedResults = listOfJsonStrings( + """ + | { + | "hasNext": false, + | "incremental": [ + | { + | "path": [ + | "defer" + | ], + | "label": "slow-defer", + | "data": { + | "slow": null + | } + | } + | ] + | } + """.trimMargin(), + ), + ) +} diff --git a/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/MultipleDeferDirectivesTestSnapshot.kt b/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/MultipleDeferDirectivesTestSnapshot.kt index 2dd653088..b9d5affa2 100644 --- a/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/MultipleDeferDirectivesTestSnapshot.kt +++ b/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/MultipleDeferDirectivesTestSnapshot.kt @@ -53,7 +53,7 @@ public class MultipleDeferDirectivesTestSnapshot : TestSnapshot() { | "defer" | ], | "data": { - | "slowField": "slowString" + | "anotherSlowField": 123456789 | } | } | ] @@ -68,7 +68,7 @@ public class MultipleDeferDirectivesTestSnapshot : TestSnapshot() { | "defer" | ], | "data": { - | "anotherSlowField": 123456789 + | "slowField": "slowString" | } | } | ] @@ -84,8 +84,8 @@ public class MultipleDeferDirectivesTestSnapshot : TestSnapshot() { * "data": { * "defer": { * "fastField": "123", - * "anotherSlowField": 123456789, - * "slowField": "slowString" + * "slowField": "slowString", + * "anotherSlowField": 123456789 * } * } * } @@ -112,7 +112,7 @@ public class MultipleDeferDirectivesTestSnapshot : TestSnapshot() { | "defer" | ], | "data": { - | "slowField": "slowString" + | "anotherSlowField": 123456789 | } | } | ] @@ -127,7 +127,7 @@ public class MultipleDeferDirectivesTestSnapshot : TestSnapshot() { | "defer" | ], | "data": { - | "anotherSlowField": 123456789 + | "slowField": "slowString" | } | } | ] diff --git a/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/MultipleFieldsinMultipleDeferDirectivesTestSnapshot.kt b/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/MultipleFieldsinMultipleDeferDirectivesTestSnapshot.kt index bd58c66e4..b3d0df19e 100644 --- a/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/MultipleFieldsinMultipleDeferDirectivesTestSnapshot.kt +++ b/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/defer/MultipleFieldsinMultipleDeferDirectivesTestSnapshot.kt @@ -55,8 +55,8 @@ public class MultipleFieldsinMultipleDeferDirectivesTestSnapshot : TestSnapshot( | "defer" | ], | "data": { - | "slowField4": "slowString4", - | "slowField3": "slowString3" + | "slowField": "slowString", + | "slowField2": "slowString2" | } | } | ] @@ -71,8 +71,8 @@ public class MultipleFieldsinMultipleDeferDirectivesTestSnapshot : TestSnapshot( | "defer" | ], | "data": { - | "slowField": "slowString", - | "slowField2": "slowString2" + | "slowField4": "slowString4", + | "slowField3": "slowString3" | } | } | ] @@ -88,10 +88,10 @@ public class MultipleFieldsinMultipleDeferDirectivesTestSnapshot : TestSnapshot( * "data": { * "defer": { * "fastField": "123", - * "slowField": "slowString", - * "slowField2": "slowString2", * "slowField4": "slowString4", - * "slowField3": "slowString3" + * "slowField3": "slowString3", + * "slowField": "slowString", + * "slowField2": "slowString2" * } * } * } @@ -118,8 +118,8 @@ public class MultipleFieldsinMultipleDeferDirectivesTestSnapshot : TestSnapshot( | "defer" | ], | "data": { - | "slowField4": "slowString4", - | "slowField3": "slowString3" + | "slowField": "slowString", + | "slowField2": "slowString2" | } | } | ] @@ -134,8 +134,8 @@ public class MultipleFieldsinMultipleDeferDirectivesTestSnapshot : TestSnapshot( | "defer" | ], | "data": { - | "slowField": "slowString", - | "slowField2": "slowString2" + | "slowField4": "slowString4", + | "slowField3": "slowString3" | } | } | ]