Last active
May 17, 2020 16:09
-
-
Save svenhakvoort/c72dae3074b182344d8e059b810c7239 to your computer and use it in GitHub Desktop.
CustomDataFetcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demographql.config | |
import org.springframework.context.annotation.Bean | |
import org.springframework.context.annotation.Configuration | |
@Configuration | |
class CustomDataFetcherConfig { | |
@Bean | |
fun dataFetcherFactoryProvider() = CustomDataFetcherFactoryProvider() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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