From 59a9ef1cdc4ec971b178f9bba4152e9c0fe36815 Mon Sep 17 00:00:00 2001 From: Shane Myrick Date: Mon, 24 Feb 2020 13:06:14 -0800 Subject: [PATCH] Update completable future mapping for coroutines Use the handy method on coroutine GlobalScope to execute as a CompletableFuture instead of mapping the result. This shouldn't really change much but does make the code a little easier to read --- .../federation/execution/EntityResolver.kt | 6 +++--- .../graphql/execution/FunctionDataFetcher.kt | 21 ++++++++----------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/graphql-kotlin-federation/src/main/kotlin/com/expediagroup/graphql/federation/execution/EntityResolver.kt b/graphql-kotlin-federation/src/main/kotlin/com/expediagroup/graphql/federation/execution/EntityResolver.kt index 7a521e4185..aae7b6f4b6 100644 --- a/graphql-kotlin-federation/src/main/kotlin/com/expediagroup/graphql/federation/execution/EntityResolver.kt +++ b/graphql-kotlin-federation/src/main/kotlin/com/expediagroup/graphql/federation/execution/EntityResolver.kt @@ -23,7 +23,7 @@ import graphql.schema.DataFetchingEnvironment import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll -import kotlinx.coroutines.future.asCompletableFuture +import kotlinx.coroutines.future.future import java.util.concurrent.CompletableFuture /** @@ -45,7 +45,7 @@ open class EntityResolver(private val federatedTypeRegistry: FederatedTypeRegist val representations: List> = env.getArgument("representations") val indexedBatchRequestsByType = representations.withIndex().groupBy { it.value["__typename"].toString() } - return GlobalScope.async { + return GlobalScope.future { val data = mutableListOf() val errors = mutableListOf() indexedBatchRequestsByType.map { (typeName, indexedRequests) -> @@ -68,6 +68,6 @@ open class EntityResolver(private val federatedTypeRegistry: FederatedTypeRegist .data(data) .errors(errors) .build() - }.asCompletableFuture() + } } } diff --git a/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/execution/FunctionDataFetcher.kt b/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/execution/FunctionDataFetcher.kt index 8360983b12..07a14384e5 100644 --- a/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/execution/FunctionDataFetcher.kt +++ b/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/execution/FunctionDataFetcher.kt @@ -26,8 +26,7 @@ import graphql.schema.DataFetcher import graphql.schema.DataFetchingEnvironment import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.async -import kotlinx.coroutines.future.asCompletableFuture +import kotlinx.coroutines.future.future import java.lang.reflect.InvocationTargetException import java.util.concurrent.CompletableFuture import kotlin.coroutines.CoroutineContext @@ -50,13 +49,13 @@ open class FunctionDataFetcher( private val target: Any?, private val fn: KFunction<*>, private val objectMapper: ObjectMapper = jacksonObjectMapper() -) : DataFetcher { +) : DataFetcher { /** * Invoke a suspend function or blocking function, passing in the [target] if not null or default to using the source from the environment. */ override fun get(environment: DataFetchingEnvironment): Any? { - val instance = target ?: environment.getSource() + val instance = target ?: environment.getSource() return instance?.let { val parameterValues = getParameterValues(fn, environment) @@ -116,14 +115,12 @@ open class FunctionDataFetcher( parameterValues: Array, coroutineContext: CoroutineContext = EmptyCoroutineContext, coroutineStart: CoroutineStart = CoroutineStart.DEFAULT - ): CompletableFuture { - return GlobalScope.async(context = coroutineContext, start = coroutineStart) { - try { - fn.callSuspend(instance, *parameterValues) - } catch (exception: InvocationTargetException) { - throw exception.cause ?: exception - } - }.asCompletableFuture() + ): CompletableFuture = GlobalScope.future(context = coroutineContext, start = coroutineStart) { + try { + fn.callSuspend(instance, *parameterValues) + } catch (exception: InvocationTargetException) { + throw exception.cause ?: exception + } } /**