-
Notifications
You must be signed in to change notification settings - Fork 646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrating CACHE_ONLY
queries to v3
#3689
Comments
Thanks for sending this! I'll dig into this a bit more later but a few initial thoughts:
Indeed, that's a difference with To watch the cache, you should be able to do something like below: apolloClient.query(query).fetchPolicy(FetchPolicy.CacheOnly).watch() There is a special case to not throw on errors if the query is watched so I think it should work? I don't think I have access to https://github.com/lwasyl/apollo-playground/blob/apollo3_cache_only ? But will definitely look if you can give access to user martinbonnin. |
Thanks for reply! Indeed I used Using |
So, I was initially wrong by saying Anyway, I was able to get my project compile again on Apollo 3.0.0-rc02, but I'm still having a few tests failing. I was able to debug 2 of them. One is related to Here's a repro project (I made sure it's public this time 😅 ) It's basically the same as #2798, but the challenge in |
Hi 👋 Indeed, that's something that's not possible to do in Apollo Android 3. We're looking at adding it back |
#3743 introduces I think the interceptors from the test does what you're looking for? private val refetchPolicyInterceptor = object : ApolloInterceptor {
var hasSeenValidResponse: Boolean = false
override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> {
return if (!hasSeenValidResponse) {
CacheOnlyInterceptor.intercept(request, chain).onEach {
if (it.data != null) {
// We have valid data, we can now use the network
hasSeenValidResponse = true
}
}
} else {
// If for some reason we have a cache miss, get fresh data from the network
CacheFirstInterceptor.intercept(request, chain)
}
}
} It's a pretty significant change to the watcher and cache pipeline so didn't want to include that last minute in the 3.0 release but let us know if that'd work for you and we'll include it in the next release. |
Wow! That looks great 🚀 The test you linked describes the scenario I had in mind when reporting this issue. Most probably I'll want to add additional logging when
Sure, that wasn't a problem at all. I really appreciate you're willing to include such feature only because I asked for it 😄 |
This should now be in the snapshots |
I confirm this works an intented in one of snapshot builds I tried 👍 Thank you 🙏 |
Fixed in 3.1.0 |
Context.
The whole codebase of my project relies on apollo cache having a valid state. I treat cache as source of truth. I either watch the cache or mutate it, never together. Translating that to Apollo-specific language I've been using either
CACHE_ONLY
orNETWORK_ONLY
response fetcher (+CACHE_FIRST
refetch response fetcher, just in case, but the assumption is it should never reach the network).The only customization, in addition to classes provided by Apollo was a workaround for #2798 where I ended up with custom fetcher that only listens cache until
dependentKeys
are known for given query. In other words, it usesCACHE_ONLY
refetch fetcher and after first successful refresh call withNETWORK_ONLY
it switches back to defaultCACHE_FIRST
refetch fetcher.Repro steps
I created playground project: https://github.com/lwasyl/apollo-playground/blob/apollo3_cache_only/src/test/kotlin/com/example/Test.kt#L19 where I'm trying to understand v3.0.0 changes and how to migrate from response fetchers to fetch policies.
I noticed it's not possible to provide custom fetch policy anymore as
FetchPolicy
is an enum (which brings back issue described in #2798), but I noticed that I'm basically unable to watch the cache anymore. Queried called withFetchPolicy.CacheOnly
on an empty cache kills the flow with:so calling the same query with
FetchPolicy.NetworkOnly
will never notify collectors ofCacheOnly
Flow, as it is already disposed 🤔Question
What's the proper way of observing query without making network calls as a side effect? Especially in a scenario where the cache is empty, before
dependentKeys
(not sure if they are still called that way) are known?Is there a way to provide custom
FetchPolicy
to work around the issue described in: #2798?The text was updated successfully, but these errors were encountered: