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

TypeMismatchError if controller method returning Flow is declared without suspend keyword #988

Closed
hantsy opened this issue May 29, 2024 · 1 comment
Assignees
Labels
type: bug A general bug
Milestone

Comments

@hantsy
Copy link
Contributor

hantsy commented May 29, 2024

I tried to update my example project to use Flow, Spring Boot 3.3.0/Spring GraphQL 1.3 includes the Kotlin Flow type support.

@QueryMapping
fun allPosts(): Flow<Post> = postService.allPosts()// remove .toList() to return Flow type directly

But run the following tests in the QueryTests.

@Test
fun `get all posts`() = runTest {
    coEvery { postService.allPosts() } returns
            flowOf(
                Post(
                    id = UUID.randomUUID(),
                    title = "Post 1",
                    content = "Post 1 content",
                    status = PostStatus.DRAFT,
                    createdAt = LocalDateTime.now()
                ),
                Post(
                    id = UUID.randomUUID(),
                    title = "Post 2",
                    content = "Post 2 content",
                    status = PostStatus.DRAFT,
                    createdAt = LocalDateTime.now()
                )
            )
    val query = "{ allPosts { title content }}"
message: ${error.message}") } }
    graphQlTester.document(query)
       .execute()
       .path("data.allPosts[*].title")
       .entityList(String::class.java).hasSize(2).contains("POST 1", "POST 2")

    coVerify(exactly = 1) { postService.allPosts() }
}

And I got the error [TypeMismatchError{path=[allPosts], expectedType=[Post!]}].

The example project here: https://github.com/hantsy/spring-graphql-sample/tree/master/spring-graphql-rsocket-kotlin-co

#954 resolved the Flow return type, it should support Flow in @QueryMapping also.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 29, 2024
@hantsy hantsy changed the title Support @QueryMapping return a Flow Support @QueryMapping returns Flow type result May 29, 2024
@rstoyanchev
Copy link
Contributor

It works when the method is declared with suspend, but not without.

@rstoyanchev rstoyanchev changed the title Support @QueryMapping returns Flow type result TypeMismatchError if controller method returning Flow is declared without suspend keyword Jun 5, 2024
@rstoyanchev rstoyanchev added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 5, 2024
@rstoyanchev rstoyanchev added this to the 1.3.1 milestone Jun 5, 2024
@rstoyanchev rstoyanchev self-assigned this Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants