I want something like apolloStore.remove() but that it notifies all watchers and recreates all my lists without this item in it.
For example:
Post with ID 100 is in the topPosts List and mostLikedPosts List.
Now I delete it and it should get removed from all the lists that have the Post with ID 100 in it.
Unfortunately this is not possible at the moment, you’ll have to update the list manually. The cache is a very simple key/value store that knows nothing about foreign keys or anything like that. We could think about supporting this but it’s a major undertaking so I wouldn’t hold my breath there.
When I add a Post item to that list and then like the post which triggers the watchers, the added post dissapears from the list again because the cached list still only has 4 items.
How can I add the item to this cached getUsersPosts list?
The easiest way is certainly to refetch your getUserPosts list avec a like but that comes at the price of an extra network request. If you want to handle everything locally, you’ll have to write the list using ApolloStore API:
val data = apolloStore.readOperation(query)
data = data.copy(
getUsersPosts = data.getUsersPosts + CacheKey(Post = 111)
)
apolloStore.writeOperation(query, data)
Right, good catch Sorry for missing this. Operations work at the Data level but CacheKey is at the Record level. Looking into it, I’ll post an update in a few minutes.
Is there a way to choose the position for the merged item?
I believe yes! The list is newUsers so instead of appending, you can prepend:
// Replace
val newUsers = (existingRecord.fields.get("users") as List<Any>) + CacheKey(key = "Post:${post.id}")
// With
val newUsers = listOf(CacheKey(key = "Post:${post.id}")) + (existingRecord.fields.get("users") as List<Any>)