From 0a04c195620789b51f6965f5c9d734e43b1eb10f Mon Sep 17 00:00:00 2001 From: hantsy Date: Tue, 28 May 2024 15:38:54 +0800 Subject: [PATCH] chore: clean codes --- .../demo/gql/datafetchers/PostController.kt | 23 ++++++++----- .../example/demo/gql/scalars/UUIDScalar.kt | 33 +++++++++++++------ 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/spring-graphql-rsocket-kotlin-co/src/main/kotlin/com/example/demo/gql/datafetchers/PostController.kt b/spring-graphql-rsocket-kotlin-co/src/main/kotlin/com/example/demo/gql/datafetchers/PostController.kt index d9ad2481b..8544610b3 100644 --- a/spring-graphql-rsocket-kotlin-co/src/main/kotlin/com/example/demo/gql/datafetchers/PostController.kt +++ b/spring-graphql-rsocket-kotlin-co/src/main/kotlin/com/example/demo/gql/datafetchers/PostController.kt @@ -4,9 +4,9 @@ import com.example.demo.AuthorService import com.example.demo.PostService import com.example.demo.gql.types.* import jakarta.validation.Valid +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.toList -import kotlinx.coroutines.reactive.asPublisher -import org.reactivestreams.Publisher import org.springframework.graphql.data.method.annotation.* import org.springframework.stereotype.Controller import org.springframework.validation.annotation.Validated @@ -21,7 +21,7 @@ class PostController( // Flow is not supported as return type. // see: https://github.com/spring-projects/spring-graphql/issues/393 @QueryMapping - suspend fun allPosts(): List = postService.allPosts().toList() + fun allPosts(): Flow = postService.allPosts() @QueryMapping suspend fun postById(@Argument postId: UUID) = postService.getPostById(postId) @@ -39,10 +39,13 @@ class PostController( } @BatchMapping - suspend fun author(posts: List): List { - val keys = posts.map { it.authorId!! }.toList() - val authorByIds = authorService.getAuthorByIdIn(keys).toList() - return keys.map { k -> authorByIds.firstOrNull { author: Author -> author.id == k } } + suspend fun author(posts: List): Flow = flow { + posts.forEach { post -> + val author = runCatching { + post.authorId?.let { authorService.getAuthorById(it) } + }.getOrNull() + emit(author) + } } @MutationMapping @@ -57,8 +60,10 @@ class PostController( // subscription return type does not support Kotlin Flow // see: https://github.com/spring-projects/spring-graphql/issues/393 + // Flow type is supported since Spring for GraphQL 1.3 + // and https://github.com/spring-projects/spring-graphql/issues/954 @SubscriptionMapping - fun commentAdded(): Publisher { - return postService.commentAdded().asPublisher() + fun commentAdded(): Flow { + return postService.commentAdded()//.asPublisher() } } \ No newline at end of file diff --git a/spring-graphql-rsocket-kotlin-co/src/main/kotlin/com/example/demo/gql/scalars/UUIDScalar.kt b/spring-graphql-rsocket-kotlin-co/src/main/kotlin/com/example/demo/gql/scalars/UUIDScalar.kt index 89aa80a7e..2cdf73e61 100644 --- a/spring-graphql-rsocket-kotlin-co/src/main/kotlin/com/example/demo/gql/scalars/UUIDScalar.kt +++ b/spring-graphql-rsocket-kotlin-co/src/main/kotlin/com/example/demo/gql/scalars/UUIDScalar.kt @@ -1,27 +1,40 @@ package com.example.demo.gql.scalars +import graphql.GraphQLContext +import graphql.execution.CoercedVariables import graphql.language.StringValue +import graphql.language.Value import graphql.schema.Coercing import graphql.schema.CoercingParseLiteralException import graphql.schema.CoercingSerializeException import java.util.* class UUIDScalar : Coercing { - override fun serialize(o: Any): String { - return when (o) { - is UUID -> o.toString() - else -> throw CoercingSerializeException("Not a valid UUID") + + override fun parseLiteral( + input: Value<*>, + variables: CoercedVariables, + graphQLContext: GraphQLContext, + locale: Locale + ): UUID? { + if (input is StringValue) { + return UUID.fromString(input.value) } + throw CoercingParseLiteralException("Value is not a valid UUID string") } - override fun parseValue(o: Any): UUID { - return UUID.fromString(o.toString()) + override fun valueToLiteral(input: Any, graphQLContext: GraphQLContext, locale: Locale): Value<*> { + return StringValue(input.toString()) } - override fun parseLiteral(input: Any): UUID { - if (input is StringValue) { - return UUID.fromString(input.value) + override fun parseValue(input: Any, graphQLContext: GraphQLContext, locale: Locale): UUID? { + return UUID.fromString(input.toString()) + } + + override fun serialize(dataFetcherResult: Any, graphQLContext: GraphQLContext, locale: Locale): String? { + return when (dataFetcherResult) { + is UUID -> dataFetcherResult.toString() + else -> throw CoercingSerializeException("Not a valid UUID") } - throw CoercingParseLiteralException("Value is not a valid UUID string") } } \ No newline at end of file