Skip to content

Instantly share code, notes, and snippets.

@susimsek
Created August 27, 2022 09:45
Show Gist options
  • Save susimsek/c5efa0b3984558106741904ded37d953 to your computer and use it in GitHub Desktop.
Save susimsek/c5efa0b3984558106741904ded37d953 to your computer and use it in GitHub Desktop.
Graphql Review Microservice
@Controller
class ReviewController(
private val reviewService: ReviewService
) {
@BatchMapping
fun reviews(products: MutableList<Product>): Mono<Map<Product, List<Review>>> {
val productIds = products.map {it.id}.toMutableList()
val result = reviewService.getReviewsByProductIdsIn(productIds)
return result
.collectMultimap { it.productId }
.map { m ->
productIds.associate { id ->
val key = products.find { id.equals(it.id) }!!
val value = m[id]?.toMutableList() ?: emptyList()
key to value
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment