Skip to content

Update completable future mapping for coroutines #613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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