From aa1578e48dce29b950a750e00933b0c1cbc35258 Mon Sep 17 00:00:00 2001 From: Martin Raison Date: Sat, 4 Jan 2020 21:06:24 +0100 Subject: [PATCH 1/2] Make coroutine scope configurable via context --- .../com/coxautodev/graphql/tools/MethodFieldResolver.kt | 3 +-- .../com/coxautodev/graphql/tools/SchemaParserBuilder.kt | 5 ++--- src/main/kotlin/com/coxautodev/graphql/tools/Utils.kt | 8 ++++++++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/com/coxautodev/graphql/tools/MethodFieldResolver.kt b/src/main/kotlin/com/coxautodev/graphql/tools/MethodFieldResolver.kt index 89465f33..2c4c0bbb 100644 --- a/src/main/kotlin/com/coxautodev/graphql/tools/MethodFieldResolver.kt +++ b/src/main/kotlin/com/coxautodev/graphql/tools/MethodFieldResolver.kt @@ -12,7 +12,6 @@ import graphql.language.TypeName import graphql.schema.DataFetcher import graphql.schema.DataFetchingEnvironment import graphql.schema.GraphQLTypeUtil.isScalar -import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.future.future import java.lang.reflect.Method import java.util.Comparator @@ -194,7 +193,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 { diff --git a/src/main/kotlin/com/coxautodev/graphql/tools/SchemaParserBuilder.kt b/src/main/kotlin/com/coxautodev/graphql/tools/SchemaParserBuilder.kt index b1a9c33f..645437b0 100644 --- a/src/main/kotlin/com/coxautodev/graphql/tools/SchemaParserBuilder.kt +++ b/src/main/kotlin/com/coxautodev/graphql/tools/SchemaParserBuilder.kt @@ -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 @@ -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) diff --git a/src/main/kotlin/com/coxautodev/graphql/tools/Utils.kt b/src/main/kotlin/com/coxautodev/graphql/tools/Utils.kt index ab51f3b3..f76a08f3 100644 --- a/src/main/kotlin/com/coxautodev/graphql/tools/Utils.kt +++ b/src/main/kotlin/com/coxautodev/graphql/tools/Utils.kt @@ -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.ParameterizedType /** @@ -33,3 +36,8 @@ internal fun JavaType.unwrap(): Class = } else { this as Class<*> } + +internal fun DataFetchingEnvironment.coroutineScope(): CoroutineScope { + val context: Any = this.getContext() + return if (context is CoroutineScope) context else GlobalScope +} From de09f345f49d068efe9bba389dc8c500ed76c890 Mon Sep 17 00:00:00 2001 From: Martin Raison Date: Sat, 4 Jan 2020 21:47:52 +0100 Subject: [PATCH 2/2] Any -> Any? --- src/main/kotlin/com/coxautodev/graphql/tools/Utils.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/coxautodev/graphql/tools/Utils.kt b/src/main/kotlin/com/coxautodev/graphql/tools/Utils.kt index f76a08f3..b8cef88d 100644 --- a/src/main/kotlin/com/coxautodev/graphql/tools/Utils.kt +++ b/src/main/kotlin/com/coxautodev/graphql/tools/Utils.kt @@ -38,6 +38,6 @@ internal fun JavaType.unwrap(): Class = } internal fun DataFetchingEnvironment.coroutineScope(): CoroutineScope { - val context: Any = this.getContext() + val context: Any? = this.getContext() return if (context is CoroutineScope) context else GlobalScope }