From c35375f8659260f3ea43e89b09f38c4f84545c18 Mon Sep 17 00:00:00 2001 From: Oryan Date: Fri, 2 Oct 2026 17:59:42 -0400 Subject: [PATCH 1/2] Support resolvers for supertypes of data classes A GraphQLResolver is now used for any data class extending or implementing Base. Resolvers are searched most specific first, followed by the data class itself. Fixes #497 Co-Authored-By: Claude Opus 5.5 (1M context) --- .../graphql/kickstart/tools/ResolverInfo.kt | 25 ++-- .../kickstart/tools/SchemaClassScanner.kt | 37 ++++-- .../kickstart/tools/SupertypeResolverTest.kt | 118 ++++++++++++++++++ 3 files changed, 152 insertions(+), 28 deletions(-) create mode 100644 src/test/kotlin/graphql/kickstart/tools/SupertypeResolverTest.kt diff --git a/src/main/kotlin/graphql/kickstart/tools/ResolverInfo.kt b/src/main/kotlin/graphql/kickstart/tools/ResolverInfo.kt index 63016fa1..267feb21 100644 --- a/src/main/kotlin/graphql/kickstart/tools/ResolverInfo.kt +++ b/src/main/kotlin/graphql/kickstart/tools/ResolverInfo.kt @@ -48,26 +48,19 @@ internal class NormalResolverInfo( } } -internal class MultiResolverInfo(val resolverInfoList: List) : DataClassTypeResolverInfo, ResolverInfo() { - override val dataClassType = findDataClass() - - /** - * Checks if all `ResolverInfo` instances are related to the same data type - */ - private fun findDataClass(): Class { - val dataClass = resolverInfoList.asSequence().map { it.dataClassType }.distinct().singleOrNull() - - if (dataClass == null) { - throw ResolverError("Resolvers may not use the same type.") - } else { - return dataClass - } - } +/** + * Combines all resolvers that apply to a data class, i.e. resolvers for the data class itself and for any of its supertypes. + * Resolvers are searched in the given order, followed by the data class itself. + */ +internal class MultiResolverInfo( + val resolverInfoList: List, + override val dataClassType: Class +) : DataClassTypeResolverInfo, ResolverInfo() { override fun getFieldSearches(): List { return resolverInfoList .asSequence() - .map { FieldResolverScanner.Search(it.resolverType, this, it.resolver, dataClassType) } + .map { FieldResolverScanner.Search(it.resolverType, this, it.resolver, it.dataClassType) } .plus(FieldResolverScanner.Search(dataClassType, this, null)) .toList() } diff --git a/src/main/kotlin/graphql/kickstart/tools/SchemaClassScanner.kt b/src/main/kotlin/graphql/kickstart/tools/SchemaClassScanner.kt index ccfc23ae..edd9123f 100644 --- a/src/main/kotlin/graphql/kickstart/tools/SchemaClassScanner.kt +++ b/src/main/kotlin/graphql/kickstart/tools/SchemaClassScanner.kt @@ -6,6 +6,7 @@ import graphql.kickstart.tools.util.* import graphql.language.* import graphql.schema.GraphQLScalarType import graphql.schema.idl.ScalarInfo +import org.apache.commons.lang3.ClassUtils import org.slf4j.LoggerFactory import java.lang.reflect.Method @@ -28,7 +29,6 @@ internal class SchemaClassScanner( private val subscriptionResolvers = resolvers.filterIsInstance() private val resolverInfos = resolvers.asSequence().minus(queryResolvers).minus(mutationResolvers).minus(subscriptionResolvers).map { NormalResolverInfo(it, options) }.toList() - private val resolverInfosByDataClass = this.resolverInfos.associateBy { it.dataClassType } private val initialDictionary = initialDictionary.mapValues { InitialDictionaryEntry(it.value) } private val extensionDefinitions = allDefinitions.filterIsInstance() @@ -270,27 +270,40 @@ internal class SchemaClassScanner( private fun getResolverInfoFromTypeDictionary(typeName: String): ResolverInfo? { val dictionaryType = initialDictionary[typeName]?.get() return if (dictionaryType != null) { - resolverInfosByDataClass[dictionaryType] ?: DataClassResolverInfo(dictionaryType) + getResolverInfoFromDataClass(dictionaryType) } else { null } } + /** + * Find all resolvers for the data class or any of its supertypes, most specific first. + */ + private fun getResolverInfoFromDataClass(dataClass: JavaType): ResolverInfo { + val resolverInfoList = resolverInfos + .filter { it.dataClassType == dataClass || isResolverForSupertype(it, dataClass) } + .sortedByDescending { ClassUtils.getAllSuperclasses(it.dataClassType).size + ClassUtils.getAllInterfaces(it.dataClassType).size } + + return when { + resolverInfoList.isEmpty() -> DataClassResolverInfo(dataClass) + resolverInfoList.size == 1 && resolverInfoList.single().dataClassType == dataClass -> resolverInfoList.single() + else -> MultiResolverInfo(resolverInfoList, dataClass.unwrap()) + } + } + + private fun isResolverForSupertype(resolverInfo: NormalResolverInfo, dataClass: JavaType) = + dataClass is Class<*> && resolverInfo.dataClassType != Object::class.java && resolverInfo.dataClassType.isAssignableFrom(dataClass) + /** * Scan a new object for types that haven't been mapped yet. */ private fun scanQueueItemForPotentialMatches(item: QueueItem) { - val resolverInfoList = this.resolverInfos.filter { it.dataClassType == item.clazz } - val resolverInfo: ResolverInfo = (if (resolverInfoList.size > 1) { - MultiResolverInfo(resolverInfoList) + val resolverInfo = if (item.clazz == Object::class.java) { + getResolverInfoFromTypeDictionary(item.type.name) + ?: throw SchemaClassScannerError("The GraphQL schema type '${item.type.name}' maps to a field of type java.lang.Object however there is no matching entry for this type in the type dictionary. You may need to add this type to the dictionary before building the schema.") } else { - if (item.clazz == Object::class.java) { - getResolverInfoFromTypeDictionary(item.type.name) - } else { - resolverInfosByDataClass[item.clazz] ?: DataClassResolverInfo(item.clazz) - } - }) - ?: throw SchemaClassScannerError("The GraphQL schema type '${item.type.name}' maps to a field of type java.lang.Object however there is no matching entry for this type in the type dictionary. You may need to add this type to the dictionary before building the schema.") + getResolverInfoFromDataClass(item.clazz) + } scanResolverInfoForPotentialMatches(item.type, resolverInfo) } diff --git a/src/test/kotlin/graphql/kickstart/tools/SupertypeResolverTest.kt b/src/test/kotlin/graphql/kickstart/tools/SupertypeResolverTest.kt new file mode 100644 index 00000000..8410f7ea --- /dev/null +++ b/src/test/kotlin/graphql/kickstart/tools/SupertypeResolverTest.kt @@ -0,0 +1,118 @@ +package graphql.kickstart.tools + +import graphql.GraphQL +import graphql.execution.AsyncExecutionStrategy +import graphql.schema.GraphQLSchema +import org.junit.Test + +class SupertypeResolverTest { + + private val schema: GraphQLSchema = SchemaParser.newParser() + .schemaString( + """ + type Query { + thing: Thing! + otherThing: OtherThing! + } + + type Thing { + name: String! + active: Boolean! + label: String! + description: String! + } + + type OtherThing { + name: String! + active: Boolean! + label: String! + description: String! + } + """) + .resolvers(QueryResolver(), BaseResolver(), NamedResolver(), OtherThingResolver()) + .build() + .makeExecutableSchema() + private val gql: GraphQL = GraphQL.newGraphQL(schema) + .queryExecutionStrategy(AsyncExecutionStrategy()) + .build() + + @Test + fun `resolvers for a superclass or interface should resolve fields of subclasses`() { + val data = assertNoGraphQlErrors(gql) { + """ + query { + thing { + name + active + label + description + } + } + """ + } + + assertEquals(data["thing"], mapOf( + "name" to "thing", + "active" to true, + "label" to "base label", + "description" to "named description" + )) + } + + @Test + fun `more specific resolvers should take precedence over supertype resolvers`() { + val data = assertNoGraphQlErrors(gql) { + """ + query { + otherThing { + name + active + label + description + } + } + """ + } + + assertEquals(data["otherThing"], mapOf( + "name" to "other thing", + "active" to false, + "label" to "other label", + "description" to "named description" + )) + } + + class QueryResolver : GraphQLQueryResolver { + fun thing(): Thing = Thing() + fun otherThing(): OtherThing = OtherThing() + } + + interface Named { + val name: String + } + + abstract class Base : Named + + class Thing : Base() { + override val name = "thing" + } + + class OtherThing : Base() { + override val name = "other thing" + } + + class BaseResolver : GraphQLResolver { + fun isActive(base: Base): Boolean = true + fun label(base: Base): String = "base label" + } + + class NamedResolver : GraphQLResolver { + fun label(named: Named): String = "named label" + fun description(named: Named): String = "named description" + } + + class OtherThingResolver : GraphQLResolver { + fun isActive(otherThing: OtherThing): Boolean = false + fun label(otherThing: OtherThing): String = "other label" + } +} From 8bba338811df7543c79b68093e5618926fef496d Mon Sep 17 00:00:00 2001 From: Oryan Date: Sat, 3 Oct 2026 09:34:18 -0400 Subject: [PATCH 2/2] Add test for supertype resolver precedence over data class getters - Cover case where a GraphQLResolver method and a getter on the subclass both match a field - Move scanQueueItemForPotentialMatches next to related scan methods (no behavior change) --- .../kickstart/tools/SchemaClassScanner.kt | 28 +++++++++---------- .../kickstart/tools/SupertypeResolverTest.kt | 28 +++++++++++++++++++ 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/graphql/kickstart/tools/SchemaClassScanner.kt b/src/main/kotlin/graphql/kickstart/tools/SchemaClassScanner.kt index 1374044b..8d678863 100644 --- a/src/main/kotlin/graphql/kickstart/tools/SchemaClassScanner.kt +++ b/src/main/kotlin/graphql/kickstart/tools/SchemaClassScanner.kt @@ -281,6 +281,20 @@ internal class SchemaClassScanner( } } + /** + * Scan a new object for types that haven't been mapped yet. + */ + private fun scanQueueItemForPotentialMatches(item: QueueItem) { + val resolverInfo = if (item.clazz == Object::class.java) { + getResolverInfoFromTypeDictionary(item.type.name) + ?: throw SchemaClassScannerError("The GraphQL schema type '${item.type.name}' maps to a field of type java.lang.Object however there is no matching entry for this type in the type dictionary. You may need to add this type to the dictionary before building the schema.") + } else { + getResolverInfoFromDataClass(item.clazz) + } + + scanResolverInfoForPotentialMatches(item.type, resolverInfo) + } + private fun getResolverInfoFromTypeDictionary(typeName: String): ResolverInfo? { val dictionaryType = initialDictionary[typeName]?.get() return if (dictionaryType != null) { @@ -308,20 +322,6 @@ internal class SchemaClassScanner( private fun isResolverForSupertype(resolverInfo: NormalResolverInfo, dataClass: JavaType) = dataClass is Class<*> && resolverInfo.dataClassType != Object::class.java && resolverInfo.dataClassType.isAssignableFrom(dataClass) - /** - * Scan a new object for types that haven't been mapped yet. - */ - private fun scanQueueItemForPotentialMatches(item: QueueItem) { - val resolverInfo = if (item.clazz == Object::class.java) { - getResolverInfoFromTypeDictionary(item.type.name) - ?: throw SchemaClassScannerError("The GraphQL schema type '${item.type.name}' maps to a field of type java.lang.Object however there is no matching entry for this type in the type dictionary. You may need to add this type to the dictionary before building the schema.") - } else { - getResolverInfoFromDataClass(item.clazz) - } - - scanResolverInfoForPotentialMatches(item.type, resolverInfo) - } - private fun scanResolverInfoForPotentialMatches(type: ObjectTypeDefinition, resolverInfo: ResolverInfo) { type.getExtendedFieldDefinitions(extensionDefinitions).forEach { field -> val fieldResolver = fieldResolverScanner.findFieldResolver(field, resolverInfo) diff --git a/src/test/kotlin/graphql/kickstart/tools/SupertypeResolverTest.kt b/src/test/kotlin/graphql/kickstart/tools/SupertypeResolverTest.kt index 8410f7ea..f3d1ef1d 100644 --- a/src/test/kotlin/graphql/kickstart/tools/SupertypeResolverTest.kt +++ b/src/test/kotlin/graphql/kickstart/tools/SupertypeResolverTest.kt @@ -13,6 +13,7 @@ class SupertypeResolverTest { type Query { thing: Thing! otherThing: OtherThing! + labelledThing: LabelledThing! } type Thing { @@ -28,6 +29,10 @@ class SupertypeResolverTest { label: String! description: String! } + + type LabelledThing { + label: String! + } """) .resolvers(QueryResolver(), BaseResolver(), NamedResolver(), OtherThingResolver()) .build() @@ -82,9 +87,27 @@ class SupertypeResolverTest { )) } + @Test + fun `supertype resolvers should take precedence over getters on the data class`() { + val data = assertNoGraphQlErrors(gql) { + """ + query { + labelledThing { + label + } + } + """ + } + + assertEquals(data["labelledThing"], mapOf( + "label" to "base label" + )) + } + class QueryResolver : GraphQLQueryResolver { fun thing(): Thing = Thing() fun otherThing(): OtherThing = OtherThing() + fun labelledThing(): LabelledThing = LabelledThing() } interface Named { @@ -101,6 +124,11 @@ class SupertypeResolverTest { override val name = "other thing" } + class LabelledThing : Base() { + override val name = "labelled thing" + val label = "getter label" + } + class BaseResolver : GraphQLResolver { fun isActive(base: Base): Boolean = true fun label(base: Base): String = "base label"