spring-projects / spring-graphql

Spring Integration for GraphQL
https://spring.io/projects/spring-graphql
Apache License 2.0
1.53k stars 304 forks source link

Kotlin Coroutines Support #393

Closed hantsy closed 6 months ago

hantsy commented 2 years ago

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.

rstoyanchev commented 2 years ago

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.

hantsy commented 2 years ago

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

xmlking commented 1 year ago

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

hantsy commented 1 year ago

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

xmlking commented 1 year ago

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

hantsy commented 1 year ago

@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.

rstoyanchev commented 6 months ago

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.