I'm writing an app that is using anilist graphql backend. This backend provides a pagination with Page query. So the paginated query itself looks like that:
- For Anime screen:
query ($page: Int, $perPage: Int) {
Page (page: $page, perPage: $perPage) {
media (type: ANIME) {
id
title {
romaji
}
}
}
}
- For Manga screen:
query ($page: Int, $perPage: Int) {
Page (page: $page, perPage: $perPage) {
media (type: MANGA) {
id
title {
romaji
}
}
}
}
In my app there two screens Manga and Anime they both sends this query but with different type (ANIME or MANGA). And at this point I faced a problem. Page query has no id so it's impossible split in cache Page query for ANIME and Page query for MANGA. Pagination will also affect this problem as each time user reach the end of list I will send a new Page query. As I can see it should work in the next way:
- Page query for ANIME and for MANGA should be separated in cache in other words cache should somehow know that they are different objects.
- On response for a next page request I should merge media in Page of MANGA or in Page of ANIME.
Do you have any ideas how to obtain such functionality? For now I consider an option to clear cache on screen change, that will allow me to avoid a first part of problem, cache will always contain just one Page query. But then the second part of problem will stay. On each new page obtained Page in cache will be just overwritten.
So I found a way to satisfies my requirements. It includes 2 steps:
As result I got the cache like this:
So now apollo cache for Page contains the results separated by type ANIME or MANGA. The only thing is that, as I found, updateQuery is deprecated, so not sure how long this approach will be valid.