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

Add proper virtual type definition #620

Merged
merged 1 commit into from
Nov 18, 2024
Merged
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
@@ -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<DirectiveDefinition>(
// language=GraphQL
"""
directive @virtualType on OBJECT
""".trimIndent(),
)
}

object Keyword {
const val virtualType = "virtualType"
}
}

This file was deleted.

6 changes: 5 additions & 1 deletion lib/src/main/java/graphql/nadel/engine/util/GraphQLUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<*>
Expand Down Expand Up @@ -649,6 +650,9 @@ internal fun ExecutableNormalizedField.getFieldDefinitionSequence(
}
}

internal inline fun <reified T : SDLDefinition<*>> parseDefinition(sdl: String): T {
internal inline fun <reified T : SDLDefinition<*>> parseDefinition(
@Language("GraphQL")
sdl: String,
): T {
return Parser.parse(sdl).definitions.singleOfType()
}
3 changes: 3 additions & 0 deletions lib/src/main/java/graphql/nadel/schema/NadelDirectives.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -71,6 +72,8 @@ object NadelDirectives {

val partitionDirectiveDefinition = NadelPartitionDefinition.directiveDefinition

val virtualTypeDirectiveDefinition = NadelVirtualTypeDefinition.directiveDefinition

private inline fun <reified T : SDLDefinition<*>> parseDefinition(sdl: String): T {
return Parser.parse(sdl).definitions.singleOfType()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -50,6 +51,8 @@ object NadelBuiltInTypes {
nadelHydrationResultConditionDefinition,
nadelHydrationConditionDefinition,
nadelHydrationRemainingArguments,

virtualTypeDirectiveDefinition,
).mapTo(LinkedHashSet()) {
it.name
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ class HydrationCopiesFieldAndHasPolymorphicHydrationTest : NadelIntegrationTest(
]
)
}
directive @virtualType on OBJECT
type WorkConnection @virtualType {
edges: [WorkEdge]
pageInfo: PageInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ class HydrationCopiesFieldHintOffTest : NadelIntegrationTest(
]
)
}
directive @virtualType on OBJECT
type WorkConnection @virtualType {
edges: [WorkEdge]
pageInfo: PageInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ class HydrationCopiesFieldTest : NadelIntegrationTest(
]
)
}
directive @virtualType on OBJECT
type WorkConnection @virtualType {
edges: [WorkEdge]
pageInfo: PageInfo
Expand Down
Loading