Skip to content

Commit aa1578e

Browse files
committed
Make coroutine scope configurable via context
1 parent 0ee4f39 commit aa1578e

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/main/kotlin/com/coxautodev/graphql/tools/MethodFieldResolver.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import graphql.language.TypeName
1212
import graphql.schema.DataFetcher
1313
import graphql.schema.DataFetchingEnvironment
1414
import graphql.schema.GraphQLTypeUtil.isScalar
15-
import kotlinx.coroutines.GlobalScope
1615
import kotlinx.coroutines.future.future
1716
import java.lang.reflect.Method
1817
import java.util.Comparator
@@ -194,7 +193,7 @@ open class MethodFieldResolverDataFetcher(private val sourceResolver: SourceReso
194193
val args = this.args.map { it(environment) }.toTypedArray()
195194

196195
return if (isSuspendFunction) {
197-
GlobalScope.future(options.coroutineContextProvider.provide()) {
196+
environment.coroutineScope().future(options.coroutineContextProvider.provide()) {
198197
methodAccess.invokeSuspend(source, methodIndex, args)?.transformWithGenericWrapper(environment)
199198
}
200199
} else {

src/main/kotlin/com/coxautodev/graphql/tools/SchemaParserBuilder.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import graphql.schema.idl.RuntimeWiring
1414
import graphql.schema.idl.SchemaDirectiveWiring
1515
import kotlinx.coroutines.Dispatchers
1616
import kotlinx.coroutines.ExperimentalCoroutinesApi
17-
import kotlinx.coroutines.GlobalScope
1817
import kotlinx.coroutines.channels.ReceiveChannel
1918
import kotlinx.coroutines.reactive.publish
2019
import org.antlr.v4.runtime.RecognitionException
@@ -383,8 +382,8 @@ data class SchemaParserOptions internal constructor(
383382
GenericWrapper(CompletableFuture::class, 0),
384383
GenericWrapper(CompletionStage::class, 0),
385384
GenericWrapper(Publisher::class, 0),
386-
GenericWrapper.withTransformer(ReceiveChannel::class, 0, { receiveChannel ->
387-
GlobalScope.publish(coroutineContextProvider.provide()) {
385+
GenericWrapper.withTransformer(ReceiveChannel::class, 0, { receiveChannel, environment ->
386+
environment.coroutineScope().publish(coroutineContextProvider.provide()) {
388387
try {
389388
for (item in receiveChannel) {
390389
send(item)

src/main/kotlin/com/coxautodev/graphql/tools/Utils.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import graphql.language.NonNullType
66
import graphql.language.ObjectTypeDefinition
77
import graphql.language.ObjectTypeExtensionDefinition
88
import graphql.language.Type
9+
import graphql.schema.DataFetchingEnvironment
10+
import kotlinx.coroutines.CoroutineScope
11+
import kotlinx.coroutines.GlobalScope
912
import java.lang.reflect.ParameterizedType
1013

1114
/**
@@ -33,3 +36,8 @@ internal fun JavaType.unwrap(): Class<out Any> =
3336
} else {
3437
this as Class<*>
3538
}
39+
40+
internal fun DataFetchingEnvironment.coroutineScope(): CoroutineScope {
41+
val context: Any = this.getContext()
42+
return if (context is CoroutineScope) context else GlobalScope
43+
}

0 commit comments

Comments
 (0)