Skip to content

Commit 8ff7de8

Browse files
authored
Update completable future mapping for coroutines (#613)
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
1 parent 64e0387 commit 8ff7de8

File tree

2 files changed

+12
-15
lines changed
  • graphql-kotlin-federation/src/main/kotlin/com/expediagroup/graphql/federation/execution
  • graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/execution

2 files changed

+12
-15
lines changed

graphql-kotlin-federation/src/main/kotlin/com/expediagroup/graphql/federation/execution/EntityResolver.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import graphql.schema.DataFetchingEnvironment
2323
import kotlinx.coroutines.GlobalScope
2424
import kotlinx.coroutines.async
2525
import kotlinx.coroutines.awaitAll
26-
import kotlinx.coroutines.future.asCompletableFuture
26+
import kotlinx.coroutines.future.future
2727
import java.util.concurrent.CompletableFuture
2828

2929
/**
@@ -45,7 +45,7 @@ open class EntityResolver(private val federatedTypeRegistry: FederatedTypeRegist
4545
val representations: List<Map<String, Any>> = env.getArgument("representations")
4646

4747
val indexedBatchRequestsByType = representations.withIndex().groupBy { it.value["__typename"].toString() }
48-
return GlobalScope.async {
48+
return GlobalScope.future {
4949
val data = mutableListOf<Any?>()
5050
val errors = mutableListOf<GraphQLError>()
5151
indexedBatchRequestsByType.map { (typeName, indexedRequests) ->
@@ -68,6 +68,6 @@ open class EntityResolver(private val federatedTypeRegistry: FederatedTypeRegist
6868
.data(data)
6969
.errors(errors)
7070
.build()
71-
}.asCompletableFuture()
71+
}
7272
}
7373
}

graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/execution/FunctionDataFetcher.kt

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ import graphql.schema.DataFetcher
2626
import graphql.schema.DataFetchingEnvironment
2727
import kotlinx.coroutines.CoroutineStart
2828
import kotlinx.coroutines.GlobalScope
29-
import kotlinx.coroutines.async
30-
import kotlinx.coroutines.future.asCompletableFuture
29+
import kotlinx.coroutines.future.future
3130
import java.lang.reflect.InvocationTargetException
3231
import java.util.concurrent.CompletableFuture
3332
import kotlin.coroutines.CoroutineContext
@@ -50,13 +49,13 @@ open class FunctionDataFetcher(
5049
private val target: Any?,
5150
private val fn: KFunction<*>,
5251
private val objectMapper: ObjectMapper = jacksonObjectMapper()
53-
) : DataFetcher<Any> {
52+
) : DataFetcher<Any?> {
5453

5554
/**
5655
* Invoke a suspend function or blocking function, passing in the [target] if not null or default to using the source from the environment.
5756
*/
5857
override fun get(environment: DataFetchingEnvironment): Any? {
59-
val instance = target ?: environment.getSource<Any>()
58+
val instance = target ?: environment.getSource<Any?>()
6059

6160
return instance?.let {
6261
val parameterValues = getParameterValues(fn, environment)
@@ -116,14 +115,12 @@ open class FunctionDataFetcher(
116115
parameterValues: Array<Any?>,
117116
coroutineContext: CoroutineContext = EmptyCoroutineContext,
118117
coroutineStart: CoroutineStart = CoroutineStart.DEFAULT
119-
): CompletableFuture<Any?> {
120-
return GlobalScope.async(context = coroutineContext, start = coroutineStart) {
121-
try {
122-
fn.callSuspend(instance, *parameterValues)
123-
} catch (exception: InvocationTargetException) {
124-
throw exception.cause ?: exception
125-
}
126-
}.asCompletableFuture()
118+
): CompletableFuture<Any?> = GlobalScope.future(context = coroutineContext, start = coroutineStart) {
119+
try {
120+
fn.callSuspend(instance, *parameterValues)
121+
} catch (exception: InvocationTargetException) {
122+
throw exception.cause ?: exception
123+
}
127124
}
128125

129126
/**

0 commit comments

Comments
 (0)