1
class UserEntity(val id: UUID, val name: String, val email: String){
   val occupation: String? =null
}

class AddressEntity(val id:UUID){

 val line1: String, 
 val line2 : String,
 val city: City,
 val state: State,
 val zipcode: ZipCode

    @ManyToOne
    @JoinColumn(name = "user_id", referencedColumnName = "id")
    var user: UserEntity

}

class UserResolver(private val userService: UserService) {

 @DgsQuery(field = DgsConstants.QUERY.GetUsers)
    fun getUsersByLocation(
        dfe: DataFetchingEnvironment
    ): UsersPagedResult {
       return userService.getAllUsersByLocation(getLocation(dfe))
    }

    @DgsData(parentType = "User", field = "addressList")
    fun getAddressList(dfe: DgsDataFetchingEnvironment): CompletableFuture<Address>? {
        val dataLoader: DataLoader<UUID, Address> = dfe.getDataLoader<UUID, Address>(
            AddressDataLoader::class.java
        )
        val user = dfe.getSource<User>()
        return dataLoader.load(user.id)
    }
}

I have a resolver for getUserList(). I also have a dataLoader for addresses. I want to return users only if the user has an address. Basically, even if the parent resolver returns 10 users, but the address dataloader returns addresses for only 8 users, I want to remove the 2 users who do not have address, from the Parent resultset. How can we achieve this? I wanted to try SchemaDerivative but not sure, if the filed will have access to the children.

Note: I have given an example usecase. I know for the above usecase, its easier to achieve this with the DB query. My original usecase is complicated, where I have to join multiple tables to achieve this and the addresses can only be loaded as data loaders.

user2716913
  • 565
  • 1
  • 4
  • 7

0 Answers0