ExpediaGroup / graphql-kotlin

Libraries for running GraphQL in Kotlin
https://opensource.expediagroup.com/graphql-kotlin/
Apache License 2.0
1.73k stars 345 forks source link

ktor-server: Which coroutine context should I use to start a coroutine #2015

Closed fpetwo closed 2 months ago

fpetwo commented 2 months ago

I have the resolver shown below. It can access its current CoroutineContext object but also another CoroutineContext object via the DataFetchingEnvironment.

class MyResolver : Query {
    suspend fun myQuery(dfe: DataFetchingEnvironment): String {
        val currentCoroutineContext = kotlin.coroutines.coroutineContext
        val otherCoroutineContext = dfe.graphQlContext.get(CoroutineScope::class)
    }
}

These two CoroutineContext objects are not the same.

The documentation states:

In order to preserve coroutine context, we need to populate GraphQLContext map with a CoroutineScope that should be used to execute any suspendable functions

(https://opensource.expediagroup.com/graphql-kotlin/docs/schema-generator/execution/async-models#structured-concurrency)

Do I understand it correct that I should use the CoroutineContext from the DataFetchingEnvironment to start a new coroutine and NOT the given current CoroutineContext object?

Which coroutine context or better coroutine scope should I use, if I would like to start a coroutine in parallel so that the response goes back immediately without waiting for the result? Does it still make sense to use this scope: https://api.ktor.io/ktor-server/ktor-server-core/io.ktor.server.application/-application/index.html?

fpetwo commented 2 months ago

sorry, wrong issue type