Replies: 8 comments 3 replies
-
For example, how to convert the following @DgsDataLoader(name = "authorsLoader")
class AuthorsDataLoader(val authorService: AuthorService) : BatchLoader<String, Author> {
// there is no a Kotlin Co variant for `BatchLoader`???
override fun load(keys: List<String>): CompletionStage<List<Author>> = supplyAsync {
authorService.getAuthorByIdIn(keys) // assume it returns a Flow instead.
}
}
@DgsDataLoader(name = "comments")
class CommentsDataLoader(val postService: PostService) : MappedBatchLoader<String, List<Comment>> {
override fun load(keys: Set<String>): CompletionStage<Map<String, List<Comment>>> {
val comments = postService.getCommentsByPostIdIn(keys) // assume it returns a Flow instead.
val mappedComments: MutableMap<String, List<Comment>> = mutableMapOf()
keys.forEach {
mappedComments[it] = comments.filter { c -> c.postId == it }
}
log.info("mapped comments: {}", mappedComments)
return supplyAsync { mappedComments }
}
companion object {
val log: Logger = LoggerFactory.getLogger(CommentsDataLoader::class.java)
}
} |
Beta Was this translation helpful? Give feedback.
-
If possible add a new contract for Kotlin Coroutines like this. class AuthorsDataLoader(val authorService: AuthorService) : CoBatchLoader<String, Author> {
override fun load(keys: List<String>): Flow<Author> = authorService.getAuthorByIdIn(keys)
}
class CommentsDataLoader(val postService: PostService) : CoMappedBatchLoader<String, List<Comment>> {
suspend override fun load(keys: Set<String>): Map<String, List<Comment>> {}
} |
Beta Was this translation helpful? Give feedback.
-
Add @DgsDataLoader(name = "authorsLoader")
class AuthorsDataLoader(val authorService: AuthorService) : BatchLoader<String, Author> {
override fun load(keys: List<String>): CompletionStage<List<Author>> = GlobalScope.future {
authorService.getAuthorByIdIn(keys).toList()
}
}
@DgsDataLoader(name = "comments")
class CommentsDataLoader(val postService: PostService) : MappedBatchLoader<String, List<Comment>> {
override fun load(keys: Set<String>): CompletionStage<Map<String, List<Comment>>> = GlobalScope.future {
val comments = postService.getCommentsByPostIdIn(keys).toList()
val mappedComments: MutableMap<String, List<Comment>> = mutableMapOf()
keys.forEach {
mappedComments[it] = comments.filter { c -> c.postId == it }
}
log.info("mapped comments: {}", mappedComments)
mappedComments
}
companion object {
val log: Logger = LoggerFactory.getLogger(CommentsDataLoader::class.java)
}
} |
Beta Was this translation helpful? Give feedback.
-
The difficulty with this is that the data loader API comes from this library: https://github.com/graphql-java/java-dataloader, which So although I like the suggestion in theory, I don't see a practical way to implement it. |
Beta Was this translation helpful? Give feedback.
-
Bump Has there been thoughts or updates on how to implement this? Or perhaps a work around? Currently working on my own Kotlin coroutines implementation and this is a pretty signification problem as we are using data loaders quite frequently. @hantsy seems to go against the Kotlin documentation found here https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-global-scope/ I noticed you are using |
Beta Was this translation helpful? Give feedback.
-
The problem is still that it's not something we can fix in DGS - it sits a level deeper in graphql-java. Not much we can do about it in the framework. @hantsy @BCantos17 definitely do correct me if I'm wrong about this. |
Beta Was this translation helpful? Give feedback.
-
@paulbakker Spring GraphQL provides Reactor DataLoader variants, if Dgs add similar features, it is easy to convert Reactor to Kotlin Coroutines by reactor Kotlin extensions. |
Beta Was this translation helpful? Give feedback.
-
While working at Carbon Health I built a small library to support this our custom context. @muratakbal was nice enough to give me permission to extract and publish it. I have done that here. We used these in prod without seeing any issues, but our scale was much smaller than Netflix would have to deal with. Feedback or suggestions for improvement welcome! |
Beta Was this translation helpful? Give feedback.
-
I noticed the issue #413 is closed, and I have tried to create a sample project to experience the Kotlin Coroutines support.
But I still can not find a way to declare a Kotlin Coroutines version of
DataLoader
s.Is there a Kotlin Coroutines based
DataLoader
?I hope there is a Kotlin Coroutines variant instead of the original
BatchLoader
/MappedBatchLoader
which returns aCompeletionStage
.#413 (comment)
Beta Was this translation helpful? Give feedback.
All reactions