Skip to content

Instantly share code, notes, and snippets.

@svenhakvoort
Last active May 17, 2020 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svenhakvoort/c72dae3074b182344d8e059b810c7239 to your computer and use it in GitHub Desktop.
Save svenhakvoort/c72dae3074b182344d8e059b810c7239 to your computer and use it in GitHub Desktop.
CustomDataFetcher
package com.example.demographql.config
import com.expediagroup.graphql.execution.FunctionDataFetcher
import graphql.schema.DataFetchingEnvironment
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import kotlin.reflect.KFunction
class CustomFunctionDataFetcher(target: Any?, fn: KFunction<*>) : FunctionDataFetcher(target, fn) {
private fun convert(environment: DataFetchingEnvironment): Any? {
return when (val result = super.get(environment)) {
is Mono<*> -> result.toFuture()
is Flux<*> -> result.collectList().toFuture()
else -> result
}
}
override fun get(environment: DataFetchingEnvironment): Any? {
return convert(environment)
}
}
package com.example.demographql.config
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
@Configuration
class CustomDataFetcherConfig {
@Bean
fun dataFetcherFactoryProvider() = CustomDataFetcherFactoryProvider()
}
package com.example.demographql.config
import com.expediagroup.graphql.execution.SimpleKotlinDataFetcherFactoryProvider
import graphql.schema.DataFetcherFactory
import kotlin.reflect.KFunction
class CustomDataFetcherFactoryProvider(
) : SimpleKotlinDataFetcherFactoryProvider() {
override fun functionDataFetcherFactory(target: Any?, kFunction: KFunction<*>): DataFetcherFactory<Any?> = DataFetcherFactory {
CustomFunctionDataFetcher(
target = target,
fn = kFunction)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment