-
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
Feature to not emit cache misses? #5791
Comments
Hi! Thanks for the feedback. The origin for this change was to stop throwing exceptions, as a means to simplify error handling overall. In v3, cache misses would throw by default, with the possibility to emit empty data instead with With this change in place, we figured filtering the Flow is a simple and familiar way to ignore certain elements. For extra legibility it can be an extension: fun <T : Operation.Data> Flow<ApolloResponse<T>>.ignoreCacheMisses(): Flow<ApolloResponse<T>> = filterNot {
it.exception is CacheMissException
}
// ...
apolloClient.query(...)
.watch()
.ignoreCacheMisses() |
yeah, I guess I mean that I would like some way to do this globally (at the ApolloClient.Builder step?). Although I'm not sure how that would work if you did a query with |
That's good points! An API to do it globally might be something we want to add, but it would need to play nice with |
@edenman you can restore the global "ignore cache misses" using the below extension functions on fun ApolloClient.Builder.ignoreCacheMisses() = apply {
addInterceptor(object : ApolloInterceptor {
override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> {
return chain.proceed(request).filter { it.exception !is CacheMissException }
}
})
} It's not 100% clear given the As it's always easier to add than remove, I'd prefer not adding a new API at this point. If you end up using the extension function and it works well for you, feel free to leave a comment and we'll revisit. |
For the record, we just bumped into a use case for always emitting cache misses. Having the initial cache result allows to skip displaying a loading indicator while fetching the cache result. I'm going to close this one for now. Anyone wanting a global fun ApolloClient.Builder.ignoreCacheMisses() = apply {
addInterceptor(object : ApolloInterceptor {
override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> {
return chain.proceed(request).filter { it.exception !is CacheMissException }
}
})
} If this is proven to work well, we could add it as a first party extension but I'd rather wait for a bit more feedback before doing so. |
Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Kotlin usage and allow us to serve you better. |
Question
I see that there used to be a
emitCacheMisses(Boolean)
but it was removed and I don't see why.Usecase: I'm doing a query with the default
CacheFirst
fetch policy +watch()
so I can get updates if there are any. I'd like to have the flow emit when there's a valid value, or a network/server error. I don't care about cache misses. I can obviously filter the cache miss out myself with amapNotNull
orfilter
but I'm hoping there's a cleaner way to do that.The text was updated successfully, but these errors were encountered: