Skip to content

Commit b9d4b12

Browse files
authored
Merge pull request #350 from martinraison/environment-coroutine-scope
Make coroutine scope configurable via context
2 parents f973984 + 08d2561 commit b9d4b12

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ open class MethodFieldResolverDataFetcher(private val sourceResolver: SourceReso
202202
val args = this.args.map { it(environment) }.toTypedArray()
203203

204204
return if (isSuspendFunction) {
205-
GlobalScope.future(options.coroutineContextProvider.provide()) {
205+
environment.coroutineScope().future(options.coroutineContextProvider.provide()) {
206206
methodAccess.invokeSuspend(source, methodIndex, args)?.transformWithGenericWrapper(environment)
207207
}
208208
} 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: 10 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.Method
1013
import java.lang.reflect.ParameterizedType
1114
import java.lang.reflect.Proxy
@@ -38,6 +41,12 @@ internal fun JavaType.unwrap(): Class<out Any> =
3841
}
3942

4043

44+
45+
internal fun DataFetchingEnvironment.coroutineScope(): CoroutineScope {
46+
val context: Any? = this.getContext()
47+
return if (context is CoroutineScope) context else GlobalScope
48+
}
49+
4150
internal val Class<*>.declaredNonProxyMethods: List<JavaMethod>
4251
get() {
4352
return when {
@@ -46,6 +55,7 @@ internal val Class<*>.declaredNonProxyMethods: List<JavaMethod>
4655
}
4756
}
4857

58+
4959
/**
5060
* Simple heuristic to check is a method is a trivial data fetcher.
5161
*

0 commit comments

Comments
 (0)