Skip to content

ResolverError when using @Cacheable #190

Open
@JoniDS

Description

@JoniDS

I am unable to use @Caching with GraphQL on Spring Boot 2.
It works fines when cache hasn't been created, but when cache exists and I request it, I get this error:

com.coxautodev.graphql.tools.ResolverError: Expected source object to be an instance of 'com.xpt.frf.model.Entry' but instead got 'com.xpt.frf.model.Entry'
at com.coxautodev.graphql.tools.FieldResolver$getSourceResolver$2.invoke(FieldResolver.kt:27) ~[graphql-java-tools-5.3.3.jar:na]
at com.coxautodev.graphql.tools.FieldResolver$getSourceResolver$2.invoke(FieldResolver.kt:10) ~[graphql-java-tools-5.3.3.jar:na]
at com.coxautodev.graphql.tools.MethodFieldResolverDataFetcher.get(MethodFieldResolver.kt:150) ~[graphql-java-tools-5.3.3.jar:na]

My service is as follows:

 @Cacheable("entries")
    override fun findAll(pageable: Pageable): Page<Entry> {
        return entryRepository.findAll(pageable)
    }

Schema:

type EntryPagedResponse implements PagedResult {
    content: [Entry]
    totalPages: Int
    totalElements: Int
    page: Int
    itemsPerPage: Int
}

interface PagedResult{
    totalPages: Int
    totalElements: Int
    page: Int
    itemsPerPage: Int
}

type Query {
    #List all available entries
    entries(
    page: Int, limit: Int,
    sort: Sort
): EntryPagedResponse
@Component
class EntriesResolver : GraphQLQueryResolver {

    @Autowired
    lateinit var entryService: EntryService

    fun entries(page: Int?, limit: Int?, sort: com.xpt.frf.model.Sort?, env: DataFetchingEnvironment): EntryPagedResponse {
       
        val pageable = if (sort != null) PageRequest.of(page ?: 0, limit
                ?: 20, Sort.by(if (sort.desc) Sort.Direction.DESC else Sort.Direction.ASC, sort.field))
        else PageRequest.of(page ?: 0, limit ?: 20)
        val results = entryService.findAll(pageable)
        return EntryPagedResponse(results.content, pageable.pageNumber, pageable.pageSize, results.totalPages, results.totalElements)
    }
}
open class EntryPagedResponse(content: List<Entry>, page: Int, itemsPerPage: Int, totalPages: Int?, totalElements: Long?) :
        PagedResponse<List<Entry>>(content, page, itemsPerPage, totalPages, totalElements)

open class PagedResponse<out T>(val content: T?, val page: Int, val itemsPerPage: Int, val totalPages: Int?, val totalElements: Long?) : Serializable

It works fine without the @Cacheable annotation.

Anyone knows how to fix this error? Many thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions