From 5de375280463e19e6947597e1c9bffdf1ae8b85e Mon Sep 17 00:00:00 2001 From: Franklin Wang Date: Thu, 7 Dec 2023 16:43:56 +1300 Subject: [PATCH] Add test for abstract type --- .../nadel/tests/ServiceValidationException.kt | 2 +- ...-conditional-hydration-in-abstract-type.kt | 37 +++ ...conditional-hydration-in-abstract-type.yml | 307 ++++++++++++++++++ 3 files changed, 345 insertions(+), 1 deletion(-) create mode 100644 test/src/test/kotlin/graphql/nadel/tests/hooks/new-batching-conditional-hydration-in-abstract-type.kt create mode 100644 test/src/test/resources/fixtures/new hydration/new batching/new-batching-conditional-hydration-in-abstract-type.yml diff --git a/test/src/test/kotlin/graphql/nadel/tests/ServiceValidationException.kt b/test/src/test/kotlin/graphql/nadel/tests/ServiceValidationException.kt index f16f901ec..32935d928 100644 --- a/test/src/test/kotlin/graphql/nadel/tests/ServiceValidationException.kt +++ b/test/src/test/kotlin/graphql/nadel/tests/ServiceValidationException.kt @@ -2,7 +2,7 @@ package graphql.nadel.tests import graphql.nadel.validation.NadelSchemaValidationError -data class ValidationException constructor( +data class ValidationException( override val message: String, val errors: Set, ) : RuntimeException(message) diff --git a/test/src/test/kotlin/graphql/nadel/tests/hooks/new-batching-conditional-hydration-in-abstract-type.kt b/test/src/test/kotlin/graphql/nadel/tests/hooks/new-batching-conditional-hydration-in-abstract-type.kt new file mode 100644 index 000000000..e4e34a9a4 --- /dev/null +++ b/test/src/test/kotlin/graphql/nadel/tests/hooks/new-batching-conditional-hydration-in-abstract-type.kt @@ -0,0 +1,37 @@ +package graphql.nadel.tests.hooks + +import graphql.nadel.Nadel +import graphql.nadel.NadelExecutionHints +import graphql.nadel.engine.blueprint.NadelGenericHydrationInstruction +import graphql.nadel.engine.transform.result.json.JsonNode +import graphql.nadel.hooks.NadelExecutionHooks +import graphql.nadel.tests.EngineTestHook +import graphql.nadel.tests.UseHook + +@UseHook +class `new-batching-conditional-hydration-in-abstract-type` : EngineTestHook { + override fun makeExecutionHints(builder: NadelExecutionHints.Builder): NadelExecutionHints.Builder { + return super.makeExecutionHints(builder) + .newBatchHydrationGrouping { true } + } + + override fun makeNadel(builder: Nadel.Builder): Nadel.Builder { + return super.makeNadel(builder) + .executionHooks( + object : NadelExecutionHooks { + override fun getHydrationInstruction( + instructions: List, + sourceId: JsonNode, + userContext: Any?, + ): T { + val type = (sourceId.value as String).substringBefore("/") + + return instructions + .first { + it.actorFieldDef.name.startsWith(type, ignoreCase = true) + } + } + }, + ) + } +} diff --git a/test/src/test/resources/fixtures/new hydration/new batching/new-batching-conditional-hydration-in-abstract-type.yml b/test/src/test/resources/fixtures/new hydration/new batching/new-batching-conditional-hydration-in-abstract-type.yml new file mode 100644 index 000000000..dde485678 --- /dev/null +++ b/test/src/test/resources/fixtures/new hydration/new batching/new-batching-conditional-hydration-in-abstract-type.yml @@ -0,0 +1,307 @@ +name: "new batching conditional hydration in abstract type" +enabled: true +overallSchema: + # language=GraphQL + monolith: | + type Query { + activity: [IActivity] + issuesByIds(ids: [ID!]!): [Issue!] + commentsByIds(ids: [ID!]!): [Comment!] + } + interface IActivity { + content: [ActivityContent] + } + union ActivityContent = Issue | Comment + type Activity implements IActivity { + id: ID! + content: [ActivityContent] + @hydrated( + service: "monolith" + field: "commentsByIds" + arguments: [ + {name: "ids" value: "$source.contentIds"} + ] + ) + @hydrated( + service: "monolith" + field: "issuesByIds" + arguments: [ + {name: "ids" value: "$source.contentIds"} + ] + ) + } + type SingleActivity implements IActivity { + id: ID! + content: [ActivityContent] + @hydrated( + service: "monolith" + field: "issuesByIds" + arguments: [ + {name: "ids" value: "$source.contentId"} + ] + ) + } + type Issue { + id: ID! + title: String + } + type Comment { + id: ID! + content: String + } +underlyingSchema: + # language=GraphQL + monolith: | + type Query { + activity: [IActivity] + commentsByIds(ids: [ID!]!): [Comment!] + issuesByIds(ids: [ID!]!): [Issue!] + } + interface IActivity { + content: [ActivityContent] + } + union ActivityContent = Issue | Comment + type Activity implements IActivity { + id: ID! + content: [ActivityContent] + contentIds: [ID!] + } + type SingleActivity implements IActivity { + id: ID! + content: [ActivityContent] + contentId: ID! + } + type Issue { + id: ID! + title: String + } + type Comment { + id: ID! + content: String + } +# language=GraphQL +query: | + { + activity { + content { + __typename + ... on Issue { + id + title + } + ... on Comment { + id + content + } + } + } + } +variables: { } +serviceCalls: + - serviceName: "monolith" + request: + # language=GraphQL + query: | + { + activity { + __typename__batch_hydration__content: __typename + ... on Activity { + batch_hydration__content__contentIds: contentIds + batch_hydration__content__contentIds: contentIds + } + ... on SingleActivity { + batch_hydration__content__contentId: contentId + } + } + } + variables: { } + # language=JSON + response: |- + { + "data": { + "activity": [ + { + "__typename__batch_hydration__content": "Activity", + "batch_hydration__content__contentIds": [ + "issue/4000", + "comment/5000", + "comment/6000" + ] + }, + { + "__typename__batch_hydration__content": "SingleActivity", + "batch_hydration__content__contentId": "issue/8080" + }, + { + "__typename__batch_hydration__content": "Activity", + "batch_hydration__content__contentIds": [ + "comment/1234", + "comment/9001" + ] + }, + { + "__typename__batch_hydration__content": "SingleActivity", + "batch_hydration__content__contentId": "issue/7496" + } + ] + }, + "extensions": {} + } + - serviceName: "monolith" + request: + # language=GraphQL + query: | + { + issuesByIds(ids: ["issue/4000"]) { + __typename + id + batch_hydration__content__id: id + title + } + } + variables: { } + # language=JSON + response: |- + { + "data": { + "issuesByIds": [ + { + "__typename": "Issue", + "id": "issue/4000", + "batch_hydration__content__id": "issue/4000", + "title": "Four Thousand" + } + ] + }, + "extensions": {} + } + - serviceName: "monolith" + request: + # language=GraphQL + query: | + { + issuesByIds(ids: ["issue/8080", "issue/7496"]) { + __typename + id + batch_hydration__content__id: id + title + } + } + variables: { } + # language=JSON + response: |- + { + "data": { + "issuesByIds": [ + { + "__typename": "Issue", + "id": "issue/7496", + "batch_hydration__content__id": "issue/7496", + "title": "Seven Four Nine Six" + } + ] + }, + "extensions": {} + } + - serviceName: "monolith" + request: + # language=GraphQL + query: | + { + commentsByIds(ids: ["comment/5000", "comment/6000", "comment/1234", "comment/9001"]) { + __typename + content + id + batch_hydration__content__id: id + } + } + variables: { } + # language=JSON + response: |- + { + "data": { + "commentsByIds": [ + { + "__typename": "Comment", + "id": "comment/5000", + "batch_hydration__content__id": "comment/5000", + "content": "Five Thousand" + }, + { + "__typename": "Comment", + "id": "comment/6000", + "batch_hydration__content__id": "comment/6000", + "content": "Six Thousand" + }, + { + "__typename": "Comment", + "id": "comment/9001", + "batch_hydration__content__id": "comment/9001", + "content": "It's over 9000" + }, + { + "__typename": "Comment", + "id": "comment/1234", + "batch_hydration__content__id": "comment/1234", + "content": "One Two Three Four" + } + ] + }, + "extensions": {} + } +# language=JSON +response: | + { + "data": { + "activity": [ + { + "content": [ + { + "__typename": "Issue", + "id": "issue/4000", + "title": "Four Thousand" + }, + { + "__typename": "Comment", + "id": "comment/5000", + "content": "Five Thousand" + }, + { + "__typename": "Comment", + "id": "comment/6000", + "content": "Six Thousand" + } + ] + }, + { + "content": [ + null + ] + }, + { + "content": [ + { + "__typename": "Comment", + "id": "comment/1234", + "content": "One Two Three Four" + }, + { + "__typename": "Comment", + "id": "comment/9001", + "content": "It's over 9000" + } + ] + }, + { + "content": [ + { + "__typename": "Issue", + "id": "issue/7496", + "title": "Seven Four Nine Six" + } + ] + } + ] + }, + "errors": [] + }