Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore same types in virtual type context #639

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String>,
underlyingSchema: Map<String, String> = emptyMap(),
): NadelValidationTestFixture {
return NadelValidationTestFixture(
overallSchema = overallSchema,
underlyingSchema = overallSchema
.mapValues { (service, schema) ->
underlyingSchema[service] ?: makeUnderlyingSchema(schema)
},
)
}
}