Nick
11/03/2021, 7:05 PMmbonnin
11/03/2021, 7:11 PMCacheAndNetwork
emits 2 items so it's not included in the possible FetchPolicy
mbonnin
11/03/2021, 7:12 PMfetchPolicy(CacheOnly).refetchPolicy(NetworkFirst)
But that will trigger a network request on each watched itemmbonnin
11/03/2021, 7:15 PMCacheOnly
watcher and run a NetworkOnly
query out of the band to update the watcher if neededNick
11/03/2021, 7:20 PMNick
11/03/2021, 7:20 PMmbonnin
11/03/2021, 7:20 PMmbonnin
11/03/2021, 7:21 PMmbonnin
11/03/2021, 7:21 PMrefetch
is what happens when a cache change has been seen by a watcherNick
11/03/2021, 7:22 PMmbonnin
11/03/2021, 7:22 PMCacheOnly
on refetchPolicy()
mbonnin
11/03/2021, 7:23 PMNick
11/03/2021, 7:24 PMNick
11/03/2021, 7:29 PMonStart
be the best spot you think?
apollo.query(ProjectMediaNotesQuery(projectId))
.fetchPolicy(FetchPolicy.CacheOnly)
.watch()
.onStart {
apollo.query(ProjectMediaNotesQuery(projectId))
.fetchPolicy(FetchPolicy.NetworkOnly)
.execute()
}
mbonnin
11/03/2021, 7:34 PMNetworkFirst
watcherNick
11/03/2021, 7:35 PMmbonnin
11/03/2021, 7:36 PMmbonnin
11/03/2021, 7:37 PM// Get from the cache
apollo.query(myQuery).fetchPolicy(CacheOnly).execute()
// Update from network and watch
apollo.query(myQuery).fetchPolicy(NetworkFirst).watch().collect {
}
Nick
11/03/2021, 7:40 PMmbonnin
11/03/2021, 7:42 PMFlow
if needed:
fun <D : Query.Data> ApolloQueryCall<D>.watchCacheAndNetwork(): Flow<ApolloResponse<D>> {
return flow {
try {
emit(copy().fetchPolicy(FetchPolicy.CacheOnly).execute())
} catch (e: ApolloException) {
}
copy().fetchPolicy(FetchPolicy.NetworkOnly).watch().collect {
emit(it)
}
}
}
Nick
11/03/2021, 7:44 PMfun <T : Query.Data> Query<T>.autoUpdate(client: ApolloClient) = client.query(this)
.fetchPolicy(FetchPolicy.NetworkFirst)
.watch()
.onStart {
emit(client.query(this@autoUpdate).fetchPolicy(FetchPolicy.CacheOnly).execute())
}
Nick
11/03/2021, 7:47 PMmbonnin
11/03/2021, 7:50 PMNick
11/03/2021, 7:50 PMmbonnin
11/03/2021, 7:50 PMNick
11/03/2021, 7:51 PMmbonnin
11/03/2021, 7:51 PMmbonnin
11/03/2021, 7:51 PMNick
11/03/2021, 7:51 PMwasyl
11/04/2021, 7:06 AMTBH, I’m not sure why would anyone use anything else thanMy understanding is that sometimes a query knows it’s outdated and should emit, but there isn’t enough data in the cache to do so. For example, aonCacheOnly
refetchPolicy()
query Items { items { id, name } }
will be observing the cache, and another query query Other { items { id, quantity } }
is fetched from the network. If the second query contains new or different items ids, then query Items
won’t be able to emit its data, because there are no `name`s in the cache for the new items (but it knows that items
have changed). So with a CacheOnly
refetch policy, query Items
would emit a cache miss. Allowing the query to refetch from network ensures that the data in the app is consistent between different queries, even if they are disjoint like that
*I’m still on Apollo 2 so something might be different in 3mbonnin
11/04/2021, 9:23 AM