apollo-kotlin icon indicating copy to clipboard operation
apollo-kotlin copied to clipboard

Updating the cache using data classes is tedious

Open teodorpenkov opened this issue 3 years ago • 4 comments

Background We are doing pagination via the apollo cache - The flow is basically watching an initial query for the first page and update the cache when more data is available. That way we have only 1 watcher per pageable component.

The cache update is rather tedious tho e.g. :

data.copy(
  account = data.account.copy(
    inbox = data.account.inbox.copy(
      fragments = data.account.inbox.fragments.copy(
        receiptPage = data.account.inbox.fragments.receiptPage.copy(
          nodes = listOf(node) + data.account.inbox.fragments.receiptPage.nodes,
          pageInfo = data.account.inbox.fragments.receiptPage.pageInfo
        )
      )
    )
  )
)

In the above example everything except nodes and pageInfo is unchanged. In the Swift version we can directly update them (value types 😏)

Question.

What would you recommend to do in this case? I know there are libraries like arrow which can simplify the problem but the integration with the apollo coden will be really tricky.

teodorpenkov avatar Nov 18 '21 14:11 teodorpenkov

Hi! 👋 Thanks for the question!

As of today we don't really have a satisfying answer to this. Improving the story of using pagination with caching in particular is on our roadmap, as we know this use-case is important to a lot of users.

One thing you could do to simplify the code a bit is to update a fragment in the cache instead of the whole data, something like:

val id = "QUERY_ROOT.account.inbox" // Or any other id you defined
val variables = Variables.EMPTY_VARIABLES // Or the variables used
write(fragment, CacheKey(id), variables)

We didn't know Arrow could be a potential candidate or path for this so thanks for the pointer! If you have any other ideas or feedback on this area please don't hesitate to share.

BoD avatar Nov 18 '21 15:11 BoD

Thanks for answering. I'm trying to make arrow work with the current state of the library. I basically need to write a kotlin compiler plugin that will generate the code that arrow generates when the apollo codegen is finished. I will let you know if I have something working in this issue.

teodorpenkov avatar Nov 18 '21 15:11 teodorpenkov

Interesting! Yes, don't hesitate to share your findings.

BoD avatar Nov 18 '21 15:11 BoD

Linking this forum post here since it's the same issue: https://spectrum.chat/apollo/apollo-android/update-cache-using-previous-query-data-ios-parallel~bd005192-cbf7-440b-9482-8adb028e283d

martinbonnin avatar Apr 06 '22 09:04 martinbonnin