Apollo Kotlin Normalized Cache Help

Managing pagination manually

Using the ApolloStore APIs, you can update the cache manually whenever you fetch a new page of data.

Here's a general outline of how you can do this:

suspend fun fetchAndMergePage(nextPage: Int) { // 1. Get the current list from the cache val listQuery = UsersPageQuery(page = 1) val cacheResponse = apolloClient.query(listQuery).fetchPolicy(FetchPolicy.CacheOnly).execute() // 2. Fetch the next page from the network (don't update the cache yet) val networkResponse = apolloClient.query(UsersPageQuery(page = nextPage)).fetchPolicy(FetchPolicy.NetworkOnly).execute() // 3. Merge the next page with the current list val mergedList = cacheResponse.data.usersPage.items + networkResponse.data.usersPage.items val dataWithMergedList = networkResponse.data.copy( usersPage = networkResponse.data.usersPage.copy( items = mergedList ) ) // 4. Update the cache with the merged list val keys = apolloClient.apolloStore.writeOperation(operation = listQuery, operationData = dataWithMergedList) apolloClient.apolloStore.publish(keys) }

Note that in this simple example, we need to remember the last fetched page, so we can know which page to fetch next. This can be stored in shared preferences for instance. However in most cases the API can return a "page info" object containing the information needed to fetch the next page, and this can be stored in the cache with the rest of the data.

An example of doing this is available here.

Last modified: 25 June 2025