Skip to content

feat: SingletonPropertyDataFetcher, avoid allocating a PropertyDataFetcher per property per graphql operation #2079

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 3 commits into from
Apr 11, 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
4 changes: 2 additions & 2 deletions generator/graphql-kotlin-schema-generator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ tasks {
limit {
counter = "INSTRUCTION"
value = "COVEREDRATIO"
minimum = "0.96".toBigDecimal()
minimum = "0.95".toBigDecimal()
}
limit {
counter = "BRANCH"
value = "COVEREDRATIO"
minimum = "0.92".toBigDecimal()
minimum = "0.91".toBigDecimal()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Expedia, Inc
* Copyright 2025 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -64,3 +64,12 @@ open class SimpleKotlinDataFetcherFactoryProvider : KotlinDataFetcherFactoryProv
PropertyDataFetcher(kProperty.getter)
}
}

/**
* [SimpleSingletonKotlinDataFetcherFactoryProvider] is a specialization of [SimpleKotlinDataFetcherFactoryProvider] that will provide a
* 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)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.expediagroup.graphql.generator.execution

import graphql.schema.DataFetcher
import graphql.schema.DataFetcherFactory
import graphql.schema.DataFetchingEnvironment
import graphql.schema.GraphQLFieldDefinition
import graphql.schema.LightDataFetcher
import java.util.concurrent.ConcurrentHashMap
import java.util.function.Supplier
import kotlin.reflect.KClass
import kotlin.reflect.KProperty

/**
* 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 getters: ConcurrentHashMap<String, KProperty.Getter<*>> = ConcurrentHashMap()

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

override fun get(
fieldDefinition: GraphQLFieldDefinition,
sourceObject: Any?,
environmentSupplier: Supplier<DataFetchingEnvironment>
): Any? =
sourceObject?.let {
getters["${sourceObject.javaClass.name}.${fieldDefinition.name}"]?.call(sourceObject)
}

override fun get(environment: DataFetchingEnvironment): Any? =
environment.getSource<Any?>()?.let { sourceObject ->
getters["${sourceObject.javaClass.name}.${environment.fieldDefinition.name}"]?.call(sourceObject)
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ package com.expediagroup.graphql.generator

import com.expediagroup.graphql.generator.directives.KotlinDirectiveWiringFactory
import com.expediagroup.graphql.generator.directives.KotlinSchemaDirectiveWiring
import com.expediagroup.graphql.generator.execution.KotlinDataFetcherFactoryProvider
import com.expediagroup.graphql.generator.execution.SimpleKotlinDataFetcherFactoryProvider
import com.expediagroup.graphql.generator.hooks.SchemaGeneratorHooks
import io.mockk.every
import io.mockk.spyk

val defaultSupportedPackages = listOf("com.expediagroup.graphql.generator")
fun testSchemaConfig() = SchemaGeneratorConfig(defaultSupportedPackages)
fun testSchemaConfig(
dataFetcherFactoryProvider: KotlinDataFetcherFactoryProvider = SimpleKotlinDataFetcherFactoryProvider()
) = SchemaGeneratorConfig(
defaultSupportedPackages,
dataFetcherFactoryProvider = dataFetcherFactoryProvider
)

fun getTestSchemaConfigWithHooks(hooks: SchemaGeneratorHooks) = SchemaGeneratorConfig(defaultSupportedPackages, hooks = hooks)

Expand Down
2 changes: 1 addition & 1 deletion servers/graphql-kotlin-spring-server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ tasks {
limit {
counter = "INSTRUCTION"
value = "COVEREDRATIO"
minimum = "0.86".toBigDecimal()
minimum = "0.85".toBigDecimal()
}
limit {
counter = "BRANCH"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Expedia, Inc
* Copyright 2025 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,18 +17,30 @@
package com.expediagroup.graphql.server.spring.execution

import com.expediagroup.graphql.generator.execution.SimpleKotlinDataFetcherFactoryProvider
import com.expediagroup.graphql.generator.execution.SimpleSingletonKotlinDataFetcherFactoryProvider
import graphql.schema.DataFetcherFactory
import org.springframework.context.ApplicationContext
import kotlin.reflect.KClass
import kotlin.reflect.KFunction

/**
* This provides a wrapper around the [SimpleKotlinDataFetcherFactoryProvider] to call the [SpringDataFetcher] on functions.
* This allows you to use Spring beans as function arugments and they will be populated by the data fetcher.
* This allows you to use Spring beans as function arguments, and they will be populated by the data fetcher.
*/
open class SpringKotlinDataFetcherFactoryProvider(
private val applicationContext: ApplicationContext
) : SimpleKotlinDataFetcherFactoryProvider() {
override fun functionDataFetcherFactory(target: Any?, kClass: KClass<*>, kFunction: KFunction<*>): DataFetcherFactory<Any?> =
DataFetcherFactory { SpringDataFetcher(target, kFunction, applicationContext) }
}

/**
* This provides a wrapper around the [SimpleSingletonKotlinDataFetcherFactoryProvider] to call the [SpringDataFetcher] on functions.
* This allows you to use Spring beans as function arguments, and they will be populated by the data fetcher.
*/
open class SpringSingletonKotlinDataFetcherFactoryProvider(
private val applicationContext: ApplicationContext
) : SimpleSingletonKotlinDataFetcherFactoryProvider() {
override fun functionDataFetcherFactory(target: Any?, kClass: KClass<*>, kFunction: KFunction<*>): DataFetcherFactory<Any?> =
DataFetcherFactory { SpringDataFetcher(target, kFunction, applicationContext) }
}
16 changes: 16 additions & 0 deletions website/docs/schema-generator/execution/fetching-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,19 @@ You can provide your own custom data fetchers to resolve functions and propertie
to your [SchemaGeneratorConfig](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/SchemaGeneratorConfig.kt).

See our [spring example app](https://github.com/ExpediaGroup/graphql-kotlin/tree/master/examples/server/spring-server) for an example of `CustomDataFetcherFactoryProvider`.

:::info

Currently, graphql-kotlin, through the [KotlinDataFetcherFactoryProvider](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/execution/KotlinDataFetcherFactoryProvider.kt#L62)
creates a [PropertyDataFetcher](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/execution/PropertyDataFetcher.kt)
per source's property. This instance is created [every single time](https://github.com/graphql-java/graphql-java/blob/master/src/main/java/graphql/schema/GraphQLCodeRegistry.java#L100)
the graphql-java [DataFetcherFactory](https://github.com/graphql-java/graphql-java/blob/master/src/main/java/graphql/schema/DataFetcherFactory.java) is invoked,
which happens to be on runtime per property per GraphQL operation.

If you want to avoid that, use or extend the [SimpleSingletonKotlinDataFetcherFactoryProvider](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/execution/KotlinDataFetcherFactoryProvider.kt#L72) which will provide a
[SingletonPropertyDataFetcher](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/execution/SingletonPropertyDataFetcher.kt) that will host its own singleton factory, and it will store
all `KProperty.Getter<*>`s in a `ConcurrentHashMap`.

This is inspired by this [graphql-java's PR](https://github.com/graphql-java/graphql-java/pull/3754)

:::
Loading