spring-graphql icon indicating copy to clipboard operation
spring-graphql copied to clipboard

Kotlin Coroutines Support

Open hantsy opened this issue 3 years ago • 6 comments

Add Kotlin Coroutines specific support.

Kotlin Coroutines support as alternative type of the CompletableFuture in DataLoader

Similar to the existing ReactorDataLoader(internal class in the Spring GraphQl) etc.

  1. Create CoroutinesBatchLoader and CoroutinesMappedBatchLoader variants.
  2. Create a Kotlin Coroutines variant of DataLoader.

All Controller(Data Fetchers) fun supports Kotlin Coroutines

Currently I encountered an issue when returning a Flow in a query data fetching. For example,

Define the following Graphql schema.

type Query {
    allPosts: [Post!]!
}

When executing the following query.

@QueryMapping
fun allPosts(): Flow<Post> = postService.allPosts()

I got the exception like this.

error message: Can't resolve value (/allPosts) : type mismatch error, expected type LIST

Reproducing the issue: https://github.com/hantsy/spring-graphql-sample/blob/master/spring-graphql-rsocket-kotlin-co

Support Flow as Subscription return type

In a Kotlin Coroutines project, I would like use a Flow instead of Flux/Publisher as Subscription return type, that make my codes look smooth, no mixes of Reactor API and Kotlin Coroutines API.

val flow=MutableSharedFlow<Event>()

//emit event
flow.emit(event)

//subscribe in a Subscription
fun onEventOccured() = flow (instead of a Flux/Publisher)

Currently I have to convert it back to a Reactor Flux to make it work.

override fun commentAdded(): Flux<Comment> = flow.asFlux()

The example codes: https://github.com/hantsy/spring-graphql-sample/blob/master/spring-graphql-rsocket-kotlin-co/src/main/kotlin/com/example/demo/Services.kt#L92

Spring GraphQl has built-in ReactorBatchLoader internally. I think it is possible to use the same approach to get Flow support as a Subscription type.

Kotlin Coroutine variant of DataFetcherExceptionResolver

Provides a variant to return suspend or Flow instead of the Reactor APIs.

hantsy avatar May 25 '22 16:05 hantsy

I'm not sure that's how the implementation would work out. In general, it is more helpful to describe the ask rather than a solution. Also this overlaps with #406 and #407 while the title here is broad enough to cover all three.

If you could please, edit the description to describe all the places where you'd like to see Kotlin support. I'm gong to close the other two issues as duplicates. If we need to create sub-tasks we will, but for the requirements I see no reason to have multiple issues.

rstoyanchev avatar Jun 08 '22 11:06 rstoyanchev

OK, I listed all of my thought and issues I found when creating the Spring GraphQl/Kotlin Coroutines example project: https://github.com/hantsy/spring-graphql-sample/blob/master/spring-graphql-rsocket-kotlin-co

hantsy avatar Jun 08 '22 15:06 hantsy

I am also facing Can't resolve value (/listItems) : type mismatch error, expected type LIST got class kotlinx.coroutines.reactive.PublisherAsFlow error any workaround ? https://github.com/xmlking/micro-apps/tree/main/services/spring-graphql-r2dbc

xmlking avatar Feb 14 '23 04:02 xmlking

@xmlking Call yourflow.toList() to convert it to a List now.

hantsy avatar Feb 14 '23 04:02 hantsy

checking graphql-kotlin-spring-server spring project, looks like they support Flow but this project compatible with official spring graphql project.

https://opensource.expediagroup.com/graphql-kotlin/docs/server/spring-server/spring-subscriptions

xmlking avatar Feb 19 '23 23:02 xmlking

@xmlking I have tried it in my example project, but it has some limitations, the generic flow in in Query/Mutation and flow in Subscription can't be used at the same time.

hantsy avatar Feb 20 '23 01:02 hantsy

I've created #954 to address the above. Note that since this issue was created, we've added annotated exception handler methods. Those are the preferred way to handle exceptions now, and would be covered by the change.

rstoyanchev avatar Apr 18 '24 15:04 rstoyanchev