Apollo v3 HTTP Cache isn't working! Not able to debug the caching process after migrating from 2.x.x to 3.x.x

Hi, I have been working with Apollo for last 3 months (almost daily) and started from version 2.x.x to latest (3.7.1) one. Previously there were some logger to log the cache request and responses as well. I can’t find those in v3 so far.
I have implemented the way apollo doc mentioned but the HTTP Cache is not working as expected. I would like to ask for expert’s opinions and little help to get rid of it. I am just doing the following thing to achieve my goal, so please let me know if I have missed anything else. Thank you

        apolloClient = ApolloClient.Builder()
            .serverUrl(serverUrl)
            .httpCache(
                directory = File(cacheDir, "mvApolloCache"),
                maxSize = cacheStoreApollo()
            )
            .httpExpireTimeout(3600 * 1000) // can be updated
            .okHttpClient(buildOkHttp())
            .addCustomScalarAdapter(UUID4.type, stringTypeAdapter)
            .addCustomScalarAdapter(ParsedDateTime.type, stringTypeAdapter)
            .build()

FYI, My client is Android app with kotlin (.kts) support.

Hi!

In 3.x, there is no equivalent of the logger that was available in 2.x to get information about the HTTP cache. That being said, your code looks correct to me. Can you tell us what is not working as expected?

@Benoit_Lubek Hi, thanks for your response.! Really appreciate it.
I have tried the similar config for http caching in the sample project from apollo-android. The issue here that I’m facing that is, When I get response while internet is connected, then I turn off the internet connection to check if the http cache works properly or not. So, when I open the app after that (presumably cached by apollo http cache), I see nothing, means no response/data at all after once. Sometimes, initially it has data on first offline load but when I come back from detail page it’s gone. If I failed to make you understand this, please let me know I will create a video representation of it. Thanks again

It’s hard to tell what could be the issue here. To investigate, you could try to use the HttpFetchPolicy.CacheOnly policy, this will throw in case of a cache miss, and this will tell you if this is an issue with caching or something else. More broadly, you could add breakpoints in CachingHttpInterceptor and run your app in debug mode to see what’s happening.

Hi @Benoit_Lubek thanks for your response. I have already tried that and it’s confirmed that, HTTP cache is missing for some reason, but if my implementation is correct then why am I getting Caused by: java.lang.IllegalStateException: HTTP cache: no snapshot. Am I still missing something.

This is the exception you see when there is a cache miss. Is there a way you could share a project that we can have a look at, so we can reproduce the issue and try to understand its cause?

1 Like

Yeah sure, man. Here it is, you can download my update from your tutorial project. Let me know your findings. Thanks
Project link: https://filetransfer.io/data-package/3Fi8cm1I#link

Thanks for sharing the project!
I reproduced the issue. The reason this isn’t working is because you are defining the http cache interceptor twice:

  1. .httpCache(...)
  2. .addHttpInterceptor(CachingHttpInterceptor(

This 2 conflict and break cache writing.

Instead you should use 1. only and that fixes the issue.

About that, I added this after you asked me to add it. But even before that, it wasn’t caching my responses either. Did it work expectedly after removing this line?
When I tried the issue was like caching the responses a single time, once after turning off the network for the first time. But after that, it was showing an empty screen instead of list.

// Using this configuration
instance = ApolloClient.Builder()
    .serverUrl("https://apollo-fullstack-tutorial.herokuapp.com/graphql")
    .webSocketServerUrl("wss://apollo-fullstack-tutorial.herokuapp.com/graphql")
    .okHttpClient(okHttpClient)
    .httpCache(
        directory = File(context.cacheDir.absolute path, "apolloCache"),
        maxSize = 50 * 1024 * 1024
    )
    .httpExpireTimeout(1000 * 1000) // can be updated
    .build()

And getting the following result: Data package from November 28th. - FileTransfer.io
Here is my github forked branch: GitHub - sam43/apollo-kotlin-tutorial at apollo-v3-http-cache-test

Thanks, I was able to reproduce the issue, and it appears you have discovered a bug!

This was a case where a failure in an unrelated Subscription (which happens when there is no network connectivity) actually causes the cached response for a Query to be removed from the cache.

A fix was made here and it should be present in the next release of the library.

Thanks again for bringing this to our attention and for the reproducer project!

1 Like

Hey @Benoit_Lubek thanks a lot man. It’s my pleasure to me that I become bit useful to you guys. Let me know if there is anything where I can contribute on. Nice meeting you.

1 Like