Quick lazy-Google question: let's say `FooQuery` f...
# apollo-kotlin
w
Quick lazy-Google question: let's say
FooQuery
fetches list of items
[a, b, c]
(with `ID`s normalized, so the items have their own entries in the cache). If I manually remove an item from the cache (using
apolloClient.apolloStore.remove(CacheKey(itemId), cascade = false)
), should I expect
FooQuery
to a) trigger refetcher b) emit
null
while
b
is missing in the cache and re-emit non-null value only when
b
is fetched, either by refetcher or some other query ?
m
remove()
doesn't publish by default so you'd at least have to call
publish(setOf(a, b, c))
If you call
publish()
, the refetcher will be called and try to fetch according to refetchPolicy
Whether it emits
null
or
throws
depends a bunch of factors like the
refetchThrows
parameter you've passed and/or whether you're on 3.x or 4.x
w
ah perfect, yeah the emission is less of an issue, I was surprised about the publishing
Though I'm not sure how I can publish what I want 🤔 In the method to remove an item I don't know which queries depend on that item. Does
Copy code
apolloClient.apolloStore.remove(CacheKey(contentId), cascade = false)
apolloClient.apolloStore.publish(setOf(contentId))
make sense or the
publish
wouldn't do anything in such case?
m
As long as
cascade
is
false
that should be alright. But there's definitely a blind spot if
cascade
is
true
w
Right, because we'd need to publish all cascaded deletions too 🧠 Perfect, that clears things up, thank you 🙂
m
Thanks for raising this.
w
🙇 thanks, just for clarity this is not an issue for us (we use
cascade=false
), and frankly I'd love to get rid of manipulating cache manually in the first place, only for reasons we can't do that. But the issue looks valid anyway, as the current behavior is just surprising because observing cache for the same query twice can yield different results (if you do observe — delete item — observe)
🧟 I just stubled upon this again, this time with
cascade = false
where I think I also can't reliably publish cache changes 🤔 Specifically when I have a query with nested objects and I remove one of the inner objects, it's not (I think) part of the cache key for the encapsulating query, so I can't publish that query's changes, right? For example
query Items
would return
foo1 = Foo(items = [ bar1 ])
where
bar1 = Bar(items = [ baz1 ]
. If I remove
baz1
, then neither
bar1
or
foo1
can be satisfied, but the cache key for
query Items
doesn't have
baz1
anywhere, so I can't publish the deletion properly, is that right? Let me know if the example isn't clear