ExpediaGroup / graphql-kotlin

Libraries for running GraphQL in Kotlin

Home Page:https://opensource.expediagroup.com/graphql-kotlin/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Combination of value classes and coroutines leads to "argument type mismatch" error

glasser opened this issue · comments

Library Version

6.4.1 or 7.0.0-alpha.5

Describe the bug

Combining value classes such as ID with withContext(Dispatchers.IO) { ... } in fields throws a weird IllegalArgumentException argument type mismatch, and sometimes throws an internal coroutines error as well.

To Reproduce

git clone https://github.com/glasser/graphql-kotlin-value-class-problem
cd graphql-kotlin-value-class-problem
./gradlew run

Expected behavior

Both GraphQL executions should show data without errors.

But instead, the GraphQL execution which runs the field suspend fun dispatchedId(): ID = withContext(Dispatchers.IO) { ID("x") } ends up printing

ExecutionResultImpl{errors=[ExceptionWhileDataFetching{path=[dispatchedId], exception=java.lang.IllegalArgumentException: argument type mismatch, locations=[SourceLocation{line=1, column=3}]}], data=null, dataPresent=true, extensions=null}

In addition, on some but not all executions, something like the following gets printed as well:

Exception in thread "DefaultDispatcher-worker-1" kotlinx.coroutines.CoroutinesInternalError: Fatal exception in coroutines machinery for DispatchedContinuation[Dispatchers.Default, Continuation at graphqlkotlinvalueclassproblem.Query.dispatchedId-WgVpxXo(App.kt:33)@4b60775d]. Please read KDoc to 'handleFatalException' method and report this incident to maintainers
        at kotlinx.coroutines.DispatchedTask.handleFatalException(DispatchedTask.kt:144)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:115)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
        Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [CompletableFutureCoroutine{Cancelled}@d5349bc, Dispatchers.Default]
Caused by: java.lang.ClassCastException: class kotlin.coroutines.jvm.internal.CompletedContinuation cannot be cast to class kotlinx.coroutines.internal.DispatchedContinuation (kotlin.coroutines.jvm.internal.CompletedContinuation and kotlinx.coroutines.internal.DispatchedContinuation are in unnamed module of loader 'app')
        at kotlinx.coroutines.CoroutineDispatcher.releaseInterceptedContinuation(CoroutineDispatcher.kt:166)

This looks like a bug in either kotlin-reflect or kotlinx.coroutines or both. While it's not the fault of graphql-kotlin, it is something that it can trigger; maybe there's some sort of workaround. I filed these issues:

Kotlin/kotlinx.coroutines#3761
https://youtrack.jetbrains.com/issue/KT-58887

Per KT-58887 this is fixed in Kotlin 1.9.20