How to make DataLoaders fun in Schema codes works in Spring #1534
-
| In the  class User(val id: ID) {
    @GraphQLDescription("Get the users friends using data loader")
    fun getFriends(dataFetchingEnvironment: DataFetchingEnvironment): CompletableFuture<List<User>> {
        return dataFetchingEnvironment.getValueFromDataLoader("FriendsDataLoader", id)
    }
}But how to make it work in a Spring boot application? I tried to define the Post schema like this. data class Post(
    val id: UUID?,
    @UpperCase
    val title: String,
    val content: String?,
    val status: String? = null,
    val createdAt: LocalDateTime?,
    val authorId: UUID? = null,
) {
    lateinit var author: Author
    lateinit var comments: List<Comment>
    fun getComments(environment: DataFetchingEnvironment): CompletableFuture<List<Comment>> {
        return environment.getValueFromDataLoader(CommentsDataLoader.name, id)
    }
    fun getAuthor(environment: DataFetchingEnvironment): CompletableFuture<Author> {
        return environment.getValueFromDataLoader(AuthorsDataLoader.name, authorId)
    }
}And define the DataLoaders like this. @Component
class AuthorsDataLoader(val authorService: AuthorService) : KotlinDataLoader<UUID, Author> {
    companion object {
        const val name = "AuthorsDataLoader"
    }
    override val dataLoaderName = name
    override fun getDataLoader(): DataLoader<UUID, Author> {
        return DataLoaderFactory.newDataLoader { keys, environment ->
            loaderScope.future {
                authorService.getAuthorByIdIn(keys).toList()
            }
        }
    }
}
@Component
class CommentsDataLoader(val postService: PostService) : KotlinDataLoader<UUID, List<Comment>> {
    companion object {
        private val log = LoggerFactory.getLogger(CommentsDataLoader::class.java)
        const val name = "CommentsDataLoader"
    }
    override val dataLoaderName = AuthorsDataLoader.name
    override fun getDataLoader(): DataLoader<UUID, List<Comment>> {
        return DataLoaderFactory.newMappedDataLoader { keys, environment ->
            loaderScope.future {
                val comments = postService.getCommentsByPostIdIn(keys).toList()
                val mappedComments: MutableMap<UUID, List<Comment>> = mutableMapOf()
                keys.forEach { mappedComments[it] = comments.filter { c -> c.postId == it } }
                log.info("mapped comments: {}", mappedComments)
                mappedComments
            }
        }
    }
}The above codes does not works. | 
Beta Was this translation helpful? Give feedback.
      
      
          Answered by
          
            dariuszkuc
          
      
      
        Sep 6, 2022 
      
    
    Replies: 1 comment
-
| For completeness -> this seems like a duplicate of #1533 | 
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            
      Answer selected by
        samuelAndalon
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
For completeness -> this seems like a duplicate of #1533