Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin Coroutines Support #393

Closed
hantsy opened this issue May 25, 2022 · 7 comments
Closed

Kotlin Coroutines Support #393

hantsy opened this issue May 25, 2022 · 7 comments
Assignees
Labels
status: superseded Issue is superseded by another type: enhancement A general enhancement

Comments

@hantsy
Copy link
Contributor

hantsy commented May 25, 2022

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 25, 2022
@rstoyanchev
Copy link
Contributor

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
Copy link
Contributor Author

hantsy commented Jun 8, 2022

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

@rstoyanchev rstoyanchev added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 26, 2022
@rstoyanchev rstoyanchev added this to the 1.1 Backlog milestone Oct 26, 2022
@xmlking
Copy link

xmlking commented Feb 14, 2023

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
Copy link
Contributor Author

hantsy commented Feb 14, 2023

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

@xmlking
Copy link

xmlking commented Feb 19, 2023

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
Copy link
Contributor Author

hantsy commented Feb 20, 2023

@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 rstoyanchev modified the milestones: 1.2 Backlog, 1.x Backlog May 15, 2023
@rstoyanchev rstoyanchev modified the milestones: 1.2 Backlog, 1.3 Backlog Oct 13, 2023
@rstoyanchev rstoyanchev self-assigned this Apr 18, 2024
@rstoyanchev
Copy link
Contributor

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 rstoyanchev closed this as not planned Won't fix, can't repro, duplicate, stale Apr 18, 2024
@rstoyanchev rstoyanchev removed this from the 1.3 Backlog milestone Apr 18, 2024
@rstoyanchev rstoyanchev added the status: superseded Issue is superseded by another label Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: superseded Issue is superseded by another type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants