Skip to content

Commit

Permalink
chore: clean codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hantsy committed May 28, 2024
1 parent fe65db2 commit 0a04c19
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Post> = postService.allPosts().toList()
fun allPosts(): Flow<Post> = postService.allPosts()

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

@BatchMapping
suspend fun author(posts: List<Post>): List<Author?> {
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<Post>): Flow<Author?> = flow {
posts.forEach { post ->
val author = runCatching {
post.authorId?.let { authorService.getAuthorById(it) }
}.getOrNull()
emit(author)
}
}

@MutationMapping
Expand All @@ -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<Comment> {
return postService.commentAdded().asPublisher()
fun commentAdded(): Flow<Comment> {
return postService.commentAdded()//.asPublisher()
}
}
Original file line number Diff line number Diff line change
@@ -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<UUID, String> {
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")
}
}

0 comments on commit 0a04c19

Please sign in to comment.