Apollo Kotlin: Is it possible to invalidate only a portion of the cache?

The project I’m working on is using the MemoryCacheFactory and I was wondering if it would be possible to invalidate all the cache entries that belongs to a specific query.

The use case is that, due to the current client side pagination logic of a query, I can’t simply use the FetchPolicy.NetworkFirst since, once I send the refresh event, it will only work for the current page and not for the previous or following pages that would still be cached, having a different page value. The easiest solution I came up it would be to invalidate everything that belongs to the specific query, no matter the page value.

Is it possible to do it using the ApolloStore?

@mbonnin any suggestions?

Wooops, sorry I missed that one. Thanks for the ping :pray:

possible to invalidate all the cache entries that belongs to a specific query.

It’s not really possible in a generic way because that would mean knowing all the parameters that your query has been called with. That being said, there are some low level APIs that could help, especially NormalizedCache.remove(pattern: String). Given a paginated query like this:

{
   author {
     id
     books(first: 50, after: "someCursor")
   }
}

I think you could do something like this:

apollostore.accessCache {
  it.remove("Author%book%")
}

The actual pattern to use will depend your cache keys, field names, etc… but I think that’d work to remove all the pages.

Moving forward, we’re looking at better paginations APIs. It’s still incubating so there are very little docs about it but you can get an idea of the current state by looking at the pagination integration tests. Do not hesitate to try them out and let us know any feedback you have about the API/feature.

1 Like