Skip to content

fix: register getter on runtime when field is of a parent type #2091

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

Merged
merged 1 commit into from
Apr 23, 2025
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
Expand Up @@ -66,10 +66,12 @@ open class SimpleKotlinDataFetcherFactoryProvider : KotlinDataFetcherFactoryProv
}

/**
* [SimpleSingletonKotlinDataFetcherFactoryProvider] is a specialization of [SimpleKotlinDataFetcherFactoryProvider] that will provide a
* [SimpleSingletonKotlinDataFetcherFactoryProvider] is a specialization of [SimpleKotlinDataFetcherFactoryProvider] that will provide
* a [SingletonPropertyDataFetcher] that should be used to target property resolutions without allocating a DataFetcher per property
*/
open class SimpleSingletonKotlinDataFetcherFactoryProvider : SimpleKotlinDataFetcherFactoryProvider() {
override fun propertyDataFetcherFactory(kClass: KClass<*>, kProperty: KProperty<*>): DataFetcherFactory<Any?> =
SingletonPropertyDataFetcher.getFactoryAndRegister(kClass, kProperty)
SingletonPropertyDataFetcher.factory.also {
SingletonPropertyDataFetcher.register(kClass, kProperty)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@ import graphql.schema.DataFetcherFactory
import graphql.schema.DataFetchingEnvironment
import graphql.schema.GraphQLFieldDefinition
import graphql.schema.LightDataFetcher
import org.slf4j.LoggerFactory
import java.util.concurrent.ConcurrentHashMap
import java.util.function.Supplier
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
import kotlin.reflect.full.memberProperties

/**
* Singleton Property [DataFetcher] that stores references to underlying properties getters.
*/
internal object SingletonPropertyDataFetcher : LightDataFetcher<Any?> {

private val factory: DataFetcherFactory<Any?> = DataFetcherFactory<Any?> { SingletonPropertyDataFetcher }

private val logger = LoggerFactory.getLogger(SingletonPropertyDataFetcher::class.java)
val factory: DataFetcherFactory<Any?> = DataFetcherFactory<Any?> { SingletonPropertyDataFetcher }
private val getters: ConcurrentHashMap<String, KProperty.Getter<*>> = ConcurrentHashMap()

fun getFactoryAndRegister(kClass: KClass<*>, kProperty: KProperty<*>): DataFetcherFactory<Any?> {
fun register(kClass: KClass<*>, kProperty: KProperty<*>) {
getters.computeIfAbsent("${kClass.java.name}.${kProperty.name}") {
kProperty.getter
}
return factory
}

override fun get(
Expand All @@ -32,7 +33,17 @@ internal object SingletonPropertyDataFetcher : LightDataFetcher<Any?> {
environmentSupplier: Supplier<DataFetchingEnvironment>
): Any? =
sourceObject?.let {
getters["${sourceObject.javaClass.name}.${fieldDefinition.name}"]?.call(sourceObject)
getters["${sourceObject.javaClass.name}.${fieldDefinition.name}"]?.call(sourceObject) ?: run {
sourceObject::class.memberProperties
.find { it.name == fieldDefinition.name }
?.let { kProperty ->
kProperty.getter.call(sourceObject).also {
register(sourceObject::class, kProperty)
}
}
} ?: run {
logger.error("getter method not found: ${sourceObject.javaClass.name}.${fieldDefinition.name}")
}
}

override fun get(environment: DataFetchingEnvironment): Any? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ class ToSchemaTest {
assertEquals(1, geo?.get("query")?.get("id"))
}

@ParameterizedTest(name = "{index} ==> {1}")
@MethodSource("toSchemaTestArguments")
fun `SchemaGenerator generates resolvers for parent classes`(provider: KotlinDataFetcherFactoryProvider, name: String) {
val schema = toSchema(
queries = listOf(TopLevelObject(QueryObject())),
mutations = listOf(TopLevelObject(MutationObject())),
config = testSchemaConfig(provider)
)
val graphQL = GraphQL.newGraphQL(schema).build()

val result = graphQL.execute(" { range { start { day } end { day } } }")
val data: Map<String, Map<String, Map<String, Any>>>? = result.getData()
assertEquals(30, data?.get("range")?.get("start")?.get("day"))
assertEquals(14, data?.get("range")?.get("end")?.get("day"))
}

@ParameterizedTest(name = "{index} ==> {1}")
@MethodSource("toSchemaTestArguments")
fun `SchemaGenerator generates a simple GraphQL schema with default builder`(provider: KotlinDataFetcherFactoryProvider, name: String) {
Expand Down Expand Up @@ -405,9 +421,21 @@ class ToSchemaTest {
fun foo(): String = "bar"
}

open class ParentDate(val day: Int, val month: Int, val year: Int)

data class DateRange(val start: ParentDate, val end: ParentDate)

class ChildDate(day: Int, month: Int, year: Int) : ParentDate(day, month, year)

class QueryObject {
@GraphQLDescription("A GraphQL query method")
fun query(@GraphQLDescription("A GraphQL value") value: Int): Geography = Geography(value, GeoType.CITY, listOf())
fun range(): DateRange {
return DateRange(
ChildDate(30, 5, 1992),
ChildDate(14, 6, 1992),
)
}
}

class QueryWithIgnored {
Expand Down
Loading