From 4e51082d50e234ea1b0bc73e570f9465cfacbd73 Mon Sep 17 00:00:00 2001 From: Franklin Wang Date: Fri, 15 Nov 2024 16:03:10 +1100 Subject: [PATCH] Add proper virtual type definition --- .../virtualType/NadelVirtualTypeDefinition.kt | 26 +++++++++++++++++++ .../virtualType/NadelVirtualTypeDirective.kt | 13 ---------- .../graphql/nadel/engine/util/GraphQLUtil.kt | 6 ++++- .../graphql/nadel/schema/NadelDirectives.kt | 3 +++ .../nadel/schema/OverallSchemaGenerator.kt | 1 + .../validation/util/NadelBuiltInTypes.kt | 5 +++- ...piesFieldAndHasPolymorphicHydrationTest.kt | 1 - .../copy/HydrationCopiesFieldHintOffTest.kt | 1 - .../copy/HydrationCopiesFieldTest.kt | 1 - 9 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 lib/src/main/java/graphql/nadel/definition/virtualType/NadelVirtualTypeDefinition.kt delete mode 100644 lib/src/main/java/graphql/nadel/definition/virtualType/NadelVirtualTypeDirective.kt diff --git a/lib/src/main/java/graphql/nadel/definition/virtualType/NadelVirtualTypeDefinition.kt b/lib/src/main/java/graphql/nadel/definition/virtualType/NadelVirtualTypeDefinition.kt new file mode 100644 index 000000000..c8557921b --- /dev/null +++ b/lib/src/main/java/graphql/nadel/definition/virtualType/NadelVirtualTypeDefinition.kt @@ -0,0 +1,26 @@ +package graphql.nadel.definition.virtualType + +import graphql.language.DirectiveDefinition +import graphql.nadel.engine.util.parseDefinition +import graphql.schema.GraphQLDirectiveContainer +import graphql.schema.GraphQLSchemaElement + +internal fun GraphQLSchemaElement.isVirtualType(): Boolean { + return (this as? GraphQLDirectiveContainer) + ?.hasAppliedDirective(NadelVirtualTypeDefinition.directiveDefinition.name) == true +} + +internal class NadelVirtualTypeDefinition { + companion object { + val directiveDefinition = parseDefinition( + // language=GraphQL + """ + directive @virtualType on OBJECT + """.trimIndent(), + ) + } + + object Keyword { + const val virtualType = "virtualType" + } +} diff --git a/lib/src/main/java/graphql/nadel/definition/virtualType/NadelVirtualTypeDirective.kt b/lib/src/main/java/graphql/nadel/definition/virtualType/NadelVirtualTypeDirective.kt deleted file mode 100644 index c3fe1adfa..000000000 --- a/lib/src/main/java/graphql/nadel/definition/virtualType/NadelVirtualTypeDirective.kt +++ /dev/null @@ -1,13 +0,0 @@ -package graphql.nadel.definition.virtualType - -import graphql.schema.GraphQLDirectiveContainer - -internal fun GraphQLDirectiveContainer.isVirtualType(): Boolean { - return hasAppliedDirective(NadelVirtualTypeDirective.Keyword.virtualType) -} - -internal class NadelVirtualTypeDirective { - object Keyword { - const val virtualType = "virtualType" - } -} diff --git a/lib/src/main/java/graphql/nadel/engine/util/GraphQLUtil.kt b/lib/src/main/java/graphql/nadel/engine/util/GraphQLUtil.kt index a95fe2748..5705441b8 100644 --- a/lib/src/main/java/graphql/nadel/engine/util/GraphQLUtil.kt +++ b/lib/src/main/java/graphql/nadel/engine/util/GraphQLUtil.kt @@ -69,6 +69,7 @@ import graphql.schema.GraphQLUnionType import graphql.schema.GraphQLUnmodifiedType import graphql.schema.idl.TypeUtil import kotlinx.coroutines.future.asDeferred +import org.intellij.lang.annotations.Language internal typealias AnyAstValue = Value<*> internal typealias AnyAstNode = Node<*> @@ -649,6 +650,9 @@ internal fun ExecutableNormalizedField.getFieldDefinitionSequence( } } -internal inline fun > parseDefinition(sdl: String): T { +internal inline fun > parseDefinition( + @Language("GraphQL") + sdl: String, +): T { return Parser.parse(sdl).definitions.singleOfType() } diff --git a/lib/src/main/java/graphql/nadel/schema/NadelDirectives.kt b/lib/src/main/java/graphql/nadel/schema/NadelDirectives.kt index 1b150dbac..5818bf837 100644 --- a/lib/src/main/java/graphql/nadel/schema/NadelDirectives.kt +++ b/lib/src/main/java/graphql/nadel/schema/NadelDirectives.kt @@ -10,6 +10,7 @@ import graphql.nadel.definition.hydration.NadelHydrationResultConditionDefinitio import graphql.nadel.definition.hydration.NadelHydrationResultFieldPredicateDefinition import graphql.nadel.definition.partition.NadelPartitionDefinition import graphql.nadel.definition.renamed.NadelRenamedDefinition +import graphql.nadel.definition.virtualType.NadelVirtualTypeDefinition import graphql.nadel.engine.util.singleOfType import graphql.parser.Parser @@ -71,6 +72,8 @@ object NadelDirectives { val partitionDirectiveDefinition = NadelPartitionDefinition.directiveDefinition + val virtualTypeDirectiveDefinition = NadelVirtualTypeDefinition.directiveDefinition + private inline fun > parseDefinition(sdl: String): T { return Parser.parse(sdl).definitions.singleOfType() } diff --git a/lib/src/main/java/graphql/nadel/schema/OverallSchemaGenerator.kt b/lib/src/main/java/graphql/nadel/schema/OverallSchemaGenerator.kt index 20339e3ba..a5b56be66 100644 --- a/lib/src/main/java/graphql/nadel/schema/OverallSchemaGenerator.kt +++ b/lib/src/main/java/graphql/nadel/schema/OverallSchemaGenerator.kt @@ -63,6 +63,7 @@ internal class OverallSchemaGenerator { // add our custom directives if they are not present addIfNotPresent(overallRegistry, allDefinitions, NadelDirectives.nadelHydrationArgumentDefinition) addIfNotPresent(overallRegistry, allDefinitions, NadelDirectives.hydratedDirectiveDefinition) + addIfNotPresent(overallRegistry, allDefinitions, NadelDirectives.virtualTypeDirectiveDefinition) addIfNotPresent(overallRegistry, allDefinitions, NadelDirectives.renamedDirectiveDefinition) addIfNotPresent(overallRegistry, allDefinitions, NadelDirectives.hiddenDirectiveDefinition) addIfNotPresent(overallRegistry, allDefinitions, NadelDirectives.nadelBatchObjectIdentifiedByDefinition) diff --git a/lib/src/main/java/graphql/nadel/validation/util/NadelBuiltInTypes.kt b/lib/src/main/java/graphql/nadel/validation/util/NadelBuiltInTypes.kt index df9f7535c..3bf0e04f8 100644 --- a/lib/src/main/java/graphql/nadel/validation/util/NadelBuiltInTypes.kt +++ b/lib/src/main/java/graphql/nadel/validation/util/NadelBuiltInTypes.kt @@ -10,15 +10,16 @@ import graphql.nadel.schema.NadelDirectives.deferDirectiveDefinition import graphql.nadel.schema.NadelDirectives.dynamicServiceDirectiveDefinition import graphql.nadel.schema.NadelDirectives.hiddenDirectiveDefinition import graphql.nadel.schema.NadelDirectives.hydratedDirectiveDefinition -import graphql.nadel.schema.NadelDirectives.nadelHydrationRemainingArguments import graphql.nadel.schema.NadelDirectives.nadelBatchObjectIdentifiedByDefinition import graphql.nadel.schema.NadelDirectives.nadelHydrationArgumentDefinition import graphql.nadel.schema.NadelDirectives.nadelHydrationConditionDefinition +import graphql.nadel.schema.NadelDirectives.nadelHydrationRemainingArguments import graphql.nadel.schema.NadelDirectives.nadelHydrationResultConditionDefinition import graphql.nadel.schema.NadelDirectives.nadelHydrationResultFieldPredicateDefinition import graphql.nadel.schema.NadelDirectives.namespacedDirectiveDefinition import graphql.nadel.schema.NadelDirectives.partitionDirectiveDefinition import graphql.nadel.schema.NadelDirectives.renamedDirectiveDefinition +import graphql.nadel.schema.NadelDirectives.virtualTypeDirectiveDefinition object NadelBuiltInTypes { val builtInScalars = setOf( @@ -50,6 +51,8 @@ object NadelBuiltInTypes { nadelHydrationResultConditionDefinition, nadelHydrationConditionDefinition, nadelHydrationRemainingArguments, + + virtualTypeDirectiveDefinition, ).mapTo(LinkedHashSet()) { it.name } diff --git a/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/hydration/copy/HydrationCopiesFieldAndHasPolymorphicHydrationTest.kt b/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/hydration/copy/HydrationCopiesFieldAndHasPolymorphicHydrationTest.kt index 69b8d6c95..11c51848b 100644 --- a/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/hydration/copy/HydrationCopiesFieldAndHasPolymorphicHydrationTest.kt +++ b/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/hydration/copy/HydrationCopiesFieldAndHasPolymorphicHydrationTest.kt @@ -228,7 +228,6 @@ class HydrationCopiesFieldAndHasPolymorphicHydrationTest : NadelIntegrationTest( ] ) } - directive @virtualType on OBJECT type WorkConnection @virtualType { edges: [WorkEdge] pageInfo: PageInfo diff --git a/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/hydration/copy/HydrationCopiesFieldHintOffTest.kt b/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/hydration/copy/HydrationCopiesFieldHintOffTest.kt index 6c76fe000..423d824b1 100644 --- a/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/hydration/copy/HydrationCopiesFieldHintOffTest.kt +++ b/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/hydration/copy/HydrationCopiesFieldHintOffTest.kt @@ -168,7 +168,6 @@ class HydrationCopiesFieldHintOffTest : NadelIntegrationTest( ] ) } - directive @virtualType on OBJECT type WorkConnection @virtualType { edges: [WorkEdge] pageInfo: PageInfo diff --git a/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/hydration/copy/HydrationCopiesFieldTest.kt b/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/hydration/copy/HydrationCopiesFieldTest.kt index 853113c7a..935140cb4 100644 --- a/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/hydration/copy/HydrationCopiesFieldTest.kt +++ b/test/src/test/kotlin/graphql/nadel/tests/next/fixtures/hydration/copy/HydrationCopiesFieldTest.kt @@ -168,7 +168,6 @@ class HydrationCopiesFieldTest : NadelIntegrationTest( ] ) } - directive @virtualType on OBJECT type WorkConnection @virtualType { edges: [WorkEdge] pageInfo: PageInfo