ktor-server: What coroutine context should be used?

Open
#2,016 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Documentation
Clarity
Needs clarification
Activity status
Stale
Tech stack
graphql, kotlin

Research direction

Review the structured-concurrency documentation linked in the issue alongside the resolver's DataFetchingEnvironment and CoroutineScope usage. Check how the current CoroutineContext differs from the scope stored in GraphQLContext, and clarify which scope applies to parallel work and whether the Ktor Application scope is appropriate. Done means the documentation answers these scope and lifecycle questions.

Written by the indexing model from the issue text.

Description

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) as CoroutineScope).coroutineContext
    }
}

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?

Dominant language
Kotlin
Stars
1.8k
Forks
380
Avg merge
5h 6m
Merged PRs (30d)
4

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ExpediaGroup/graphql-kotlin

All issues in ExpediaGroup/graphql-kotlin

Similar issues

More Kotlin issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.