Skip to content

Commit

Permalink
Update completable future mapping for coroutines (ExpediaGroup#613)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
smyrick authored Feb 24, 2020
1 parent 64e0387 commit 8ff7de8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

val indexedBatchRequestsByType = representations.withIndex().groupBy { it.value["__typename"].toString() }
return GlobalScope.async {
return GlobalScope.future {
val data = mutableListOf<Any?>()
val errors = mutableListOf<GraphQLError>()
indexedBatchRequestsByType.map { (typeName, indexedRequests) ->
Expand All @@ -68,6 +68,6 @@ open class EntityResolver(private val federatedTypeRegistry: FederatedTypeRegist
.data(data)
.errors(errors)
.build()
}.asCompletableFuture()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,13 +49,13 @@ open class FunctionDataFetcher(
private val target: Any?,
private val fn: KFunction<*>,
private val objectMapper: ObjectMapper = jacksonObjectMapper()
) : DataFetcher<Any> {
) : DataFetcher<Any?> {

/**
* 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<Any>()
val instance = target ?: environment.getSource<Any?>()

return instance?.let {
val parameterValues = getParameterValues(fn, environment)
Expand Down Expand Up @@ -116,14 +115,12 @@ open class FunctionDataFetcher(
parameterValues: Array<Any?>,
coroutineContext: CoroutineContext = EmptyCoroutineContext,
coroutineStart: CoroutineStart = CoroutineStart.DEFAULT
): CompletableFuture<Any?> {
return GlobalScope.async(context = coroutineContext, start = coroutineStart) {
try {
fn.callSuspend(instance, *parameterValues)
} catch (exception: InvocationTargetException) {
throw exception.cause ?: exception
}
}.asCompletableFuture()
): CompletableFuture<Any?> = GlobalScope.future(context = coroutineContext, start = coroutineStart) {
try {
fn.callSuspend(instance, *parameterValues)
} catch (exception: InvocationTargetException) {
throw exception.cause ?: exception
}
}

/**
Expand Down

0 comments on commit 8ff7de8

Please sign in to comment.