Skip to content

Commit 0a04c19

Browse files
committed
chore: clean codes
1 parent fe65db2 commit 0a04c19

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

spring-graphql-rsocket-kotlin-co/src/main/kotlin/com/example/demo/gql/datafetchers/PostController.kt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import com.example.demo.AuthorService
44
import com.example.demo.PostService
55
import com.example.demo.gql.types.*
66
import jakarta.validation.Valid
7+
import kotlinx.coroutines.flow.Flow
8+
import kotlinx.coroutines.flow.flow
79
import kotlinx.coroutines.flow.toList
8-
import kotlinx.coroutines.reactive.asPublisher
9-
import org.reactivestreams.Publisher
1010
import org.springframework.graphql.data.method.annotation.*
1111
import org.springframework.stereotype.Controller
1212
import org.springframework.validation.annotation.Validated
@@ -21,7 +21,7 @@ class PostController(
2121
// Flow is not supported as return type.
2222
// see: https://github.com/spring-projects/spring-graphql/issues/393
2323
@QueryMapping
24-
suspend fun allPosts(): List<Post> = postService.allPosts().toList()
24+
fun allPosts(): Flow<Post> = postService.allPosts()
2525

2626
@QueryMapping
2727
suspend fun postById(@Argument postId: UUID) = postService.getPostById(postId)
@@ -39,10 +39,13 @@ class PostController(
3939
}
4040

4141
@BatchMapping
42-
suspend fun author(posts: List<Post>): List<Author?> {
43-
val keys = posts.map { it.authorId!! }.toList()
44-
val authorByIds = authorService.getAuthorByIdIn(keys).toList()
45-
return keys.map { k -> authorByIds.firstOrNull { author: Author -> author.id == k } }
42+
suspend fun author(posts: List<Post>): Flow<Author?> = flow {
43+
posts.forEach { post ->
44+
val author = runCatching {
45+
post.authorId?.let { authorService.getAuthorById(it) }
46+
}.getOrNull()
47+
emit(author)
48+
}
4649
}
4750

4851
@MutationMapping
@@ -57,8 +60,10 @@ class PostController(
5760

5861
// subscription return type does not support Kotlin Flow
5962
// see: https://github.com/spring-projects/spring-graphql/issues/393
63+
// Flow type is supported since Spring for GraphQL 1.3
64+
// and https://github.com/spring-projects/spring-graphql/issues/954
6065
@SubscriptionMapping
61-
fun commentAdded(): Publisher<Comment> {
62-
return postService.commentAdded().asPublisher()
66+
fun commentAdded(): Flow<Comment> {
67+
return postService.commentAdded()//.asPublisher()
6368
}
6469
}
Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
11
package com.example.demo.gql.scalars
22

3+
import graphql.GraphQLContext
4+
import graphql.execution.CoercedVariables
35
import graphql.language.StringValue
6+
import graphql.language.Value
47
import graphql.schema.Coercing
58
import graphql.schema.CoercingParseLiteralException
69
import graphql.schema.CoercingSerializeException
710
import java.util.*
811

912
class UUIDScalar : Coercing<UUID, String> {
10-
override fun serialize(o: Any): String {
11-
return when (o) {
12-
is UUID -> o.toString()
13-
else -> throw CoercingSerializeException("Not a valid UUID")
13+
14+
override fun parseLiteral(
15+
input: Value<*>,
16+
variables: CoercedVariables,
17+
graphQLContext: GraphQLContext,
18+
locale: Locale
19+
): UUID? {
20+
if (input is StringValue) {
21+
return UUID.fromString(input.value)
1422
}
23+
throw CoercingParseLiteralException("Value is not a valid UUID string")
1524
}
1625

17-
override fun parseValue(o: Any): UUID {
18-
return UUID.fromString(o.toString())
26+
override fun valueToLiteral(input: Any, graphQLContext: GraphQLContext, locale: Locale): Value<*> {
27+
return StringValue(input.toString())
1928
}
2029

21-
override fun parseLiteral(input: Any): UUID {
22-
if (input is StringValue) {
23-
return UUID.fromString(input.value)
30+
override fun parseValue(input: Any, graphQLContext: GraphQLContext, locale: Locale): UUID? {
31+
return UUID.fromString(input.toString())
32+
}
33+
34+
override fun serialize(dataFetcherResult: Any, graphQLContext: GraphQLContext, locale: Locale): String? {
35+
return when (dataFetcherResult) {
36+
is UUID -> dataFetcherResult.toString()
37+
else -> throw CoercingSerializeException("Not a valid UUID")
2438
}
25-
throw CoercingParseLiteralException("Value is not a valid UUID string")
2639
}
2740
}

0 commit comments

Comments
 (0)