From f83f6bfadd6c5c07dd8706b63565c16864c6fee7 Mon Sep 17 00:00:00 2001 From: Franklin Wang Date: Thu, 28 Nov 2024 13:48:01 +1300 Subject: [PATCH] Ignore same types in virtual type context --- .../NadelHydrationVirtualTypeValidation.kt | 4 + ...NadelHydrationVirtualTypeValidationTest.kt | 101 ++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 lib/src/test/kotlin/graphql/nadel/validation/hydration/NadelHydrationVirtualTypeValidationTest.kt diff --git a/lib/src/main/java/graphql/nadel/validation/hydration/NadelHydrationVirtualTypeValidation.kt b/lib/src/main/java/graphql/nadel/validation/hydration/NadelHydrationVirtualTypeValidation.kt index 9dad73b55..0e49b7168 100644 --- a/lib/src/main/java/graphql/nadel/validation/hydration/NadelHydrationVirtualTypeValidation.kt +++ b/lib/src/main/java/graphql/nadel/validation/hydration/NadelHydrationVirtualTypeValidation.kt @@ -73,6 +73,10 @@ internal class NadelHydrationVirtualTypeValidation { val virtualFieldOutputType = virtualField.type.unwrapAll() val backingFieldOutputType = backingField.type.unwrapAll() + if (virtualFieldOutputType.name == backingFieldOutputType.name) { + return emptyMapping.asInterimSuccess() + } + val mapping = NadelVirtualTypeMapping( virtualType = virtualFieldOutputType, backingType = backingFieldOutputType, diff --git a/lib/src/test/kotlin/graphql/nadel/validation/hydration/NadelHydrationVirtualTypeValidationTest.kt b/lib/src/test/kotlin/graphql/nadel/validation/hydration/NadelHydrationVirtualTypeValidationTest.kt new file mode 100644 index 000000000..af94e4a1f --- /dev/null +++ b/lib/src/test/kotlin/graphql/nadel/validation/hydration/NadelHydrationVirtualTypeValidationTest.kt @@ -0,0 +1,101 @@ +package graphql.nadel.validation.hydration + +import graphql.nadel.NadelSchemas.Companion.newNadelSchemas +import graphql.nadel.engine.blueprint.NadelHydrationFieldInstruction +import graphql.nadel.engine.util.makeFieldCoordinates +import graphql.nadel.util.makeUnderlyingSchema +import graphql.nadel.validation.NadelSchemaValidation +import graphql.nadel.validation.NadelValidationTestFixture +import org.junit.jupiter.api.Test +import kotlin.test.assertTrue + +class NadelHydrationVirtualTypeValidationTest { + @Test + fun `generates valid virtual type context`() { + // Given + val virtualFieldCoordinates = makeFieldCoordinates("Query", "virtualField") + val fixture = makeFixture( + overallSchema = mapOf( + "serviceA" to /*language=GraphQL*/ """ + type Query { + echo: String + virtualField: DataView + @hydrated( + field: "data" + arguments: [{name: "id", value: "1"}] + ) + } + type DataView @virtualType { + id: ID + string: String + int: Int + other: OtherDataView + } + type OtherDataView @virtualType { + boolean: Boolean + data: DataView + } + """.trimIndent(), + "serviceB" to /*language=GraphQL*/ """ + type Query { + data(id: ID!): Data + } + type Data { + id: ID + string: String + bool: Boolean + int: Int! + other: OtherData + } + type OtherData { + boolean: Boolean! + data: Data + } + """.trimIndent(), + ), + ) + + // When + val blueprintValidation = NadelSchemaValidation( + newNadelSchemas() + .overallSchemas(fixture.overallSchema) + .underlyingSchemas(fixture.underlyingSchema) + .stubServiceExecution() + .build() + ).validateAndGenerateBlueprint() + + // Then + val fieldInstructions = blueprintValidation.fieldInstructions[virtualFieldCoordinates] + val hydration = fieldInstructions!!.single() as NadelHydrationFieldInstruction + + val virtualTypeContext = hydration.virtualTypeContext + assertTrue(virtualTypeContext != null) + assertTrue(virtualTypeContext.virtualFieldContainer.name == virtualFieldCoordinates.typeName) + assertTrue(virtualTypeContext.virtualField.name == virtualFieldCoordinates.fieldName) + assertTrue( + virtualTypeContext.virtualTypeToBackingType == mapOf( + "DataView" to "Data", + "OtherDataView" to "OtherData", + ), + ) + assertTrue( + virtualTypeContext.backingTypeToVirtualType == mapOf( + "Data" to "DataView", + "OtherData" to "OtherDataView", + ), + ) + } + + private fun makeFixture( + overallSchema: Map, + underlyingSchema: Map = emptyMap(), + ): NadelValidationTestFixture { + return NadelValidationTestFixture( + overallSchema = overallSchema, + underlyingSchema = overallSchema + .mapValues { (service, schema) -> + underlyingSchema[service] ?: makeUnderlyingSchema(schema) + }, + ) + } +}