Skip to content

Make coroutine scope configurable via context #350

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
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 @@ -202,7 +202,7 @@ open class MethodFieldResolverDataFetcher(private val sourceResolver: SourceReso
val args = this.args.map { it(environment) }.toTypedArray()

return if (isSuspendFunction) {
GlobalScope.future(options.coroutineContextProvider.provide()) {
environment.coroutineScope().future(options.coroutineContextProvider.provide()) {
methodAccess.invokeSuspend(source, methodIndex, args)?.transformWithGenericWrapper(environment)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import graphql.schema.idl.RuntimeWiring
import graphql.schema.idl.SchemaDirectiveWiring
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.reactive.publish
import org.antlr.v4.runtime.RecognitionException
Expand Down Expand Up @@ -383,8 +382,8 @@ data class SchemaParserOptions internal constructor(
GenericWrapper(CompletableFuture::class, 0),
GenericWrapper(CompletionStage::class, 0),
GenericWrapper(Publisher::class, 0),
GenericWrapper.withTransformer(ReceiveChannel::class, 0, { receiveChannel ->
GlobalScope.publish(coroutineContextProvider.provide()) {
GenericWrapper.withTransformer(ReceiveChannel::class, 0, { receiveChannel, environment ->
environment.coroutineScope().publish(coroutineContextProvider.provide()) {
try {
for (item in receiveChannel) {
send(item)
Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/com/coxautodev/graphql/tools/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import graphql.language.NonNullType
import graphql.language.ObjectTypeDefinition
import graphql.language.ObjectTypeExtensionDefinition
import graphql.language.Type
import graphql.schema.DataFetchingEnvironment
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import java.lang.reflect.Method
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Proxy
Expand Down Expand Up @@ -38,6 +41,12 @@ internal fun JavaType.unwrap(): Class<out Any> =
}



internal fun DataFetchingEnvironment.coroutineScope(): CoroutineScope {
val context: Any? = this.getContext()
return if (context is CoroutineScope) context else GlobalScope
}

internal val Class<*>.declaredNonProxyMethods: List<JavaMethod>
get() {
return when {
Expand All @@ -46,6 +55,7 @@ internal val Class<*>.declaredNonProxyMethods: List<JavaMethod>
}
}


/**
* Simple heuristic to check is a method is a trivial data fetcher.
*
Expand Down