Skip to content
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

Avoid network after incomplete optimistic cache results. #6419

Merged
merged 2 commits into from
Jun 10, 2020

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented Jun 9, 2020

This fixes an issue found by @darkbasic while working with optimistic updates: #6183 (comment)

The cache-first FetchPolicy is important not just because it's the default policy, but also because both cache-and-network and network-only turn into cache-first after the first network request (#6353).

Once the cache-first policy is in effect for a query, any changes to the cache that cause the query to begin reading incomplete data will generally trigger a network request, thanks to (#6221).

However, if the source of the changes is an optimistic update for a mutation, it seems reasonable to avoid the network request during the mutation, since there's a good chance the incompleteness of the optimistic data is only temporary, and the client might read a complete result after the optimistic update is rolled back, removing the need to do a network request. I wouldn't say this logic is iron-clad, exactly, but it matches my intuition.

Of course, if the non-optimistic read following the rollback is incomplete, a network request will be triggered, so skipping the network request during optimistic updates does not mean ignoring legitimate incompleteness forever.

Note: we already avoid sending network requests for queries that are currently in flight, but in this case it's the mutation that's in flight, so this commit introduces a way to prevent other affected queries (which are not currently in flight, themselves) from hitting the network.

This fixes an issue found by @darkbasic while working with optimistic
updates: #6183 (comment)

The cache-first FetchPolicy is important not just because it's the default
policy, but also because both cache-and-network and network-only turn into
cache-first after the first network request (#6353).

Once the cache-first policy is in effect for a query, any changes to the
cache that cause the query to begin reading incomplete data will generally
trigger a network request.

However, if the source of the changes is an optimistic update for a
mutation, it seems reasonable to avoid the network request during the
mutation, since there's a good chance the incompleteness of the optimistic
data is only temporary, and the client might read a complete result after
the optimistic update is rolled back, removing the need to do a network
request.

Of course, if the non-optimistic read following the rollback is
incomplete, a network request will be triggered, so skipping the network
request during optimistic updates does not mean ignoring legitimate
incompleteness forever.

Note: we already avoid sending network requests for queries that are
currently in flight, but in this case it's the mutation that's in flight,
so this commit introduces a way to prevent other affected queries (which
are not currently in flight, themselves) from hitting the network.
@benjamn benjamn added this to the Release 3.0 milestone Jun 9, 2020
@benjamn benjamn self-assigned this Jun 9, 2020
@benjamn benjamn requested review from hwillson and jcreighton June 9, 2020 20:54
Copy link
Member

@hwillson hwillson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great @benjamn - thanks!

src/cache/inmemory/readFromStore.ts Show resolved Hide resolved
@benjamn benjamn merged commit 9450938 into master Jun 10, 2020
@benjamn benjamn deleted the avoid-network-for-incomplete-optimistic-cache-diffs branch June 10, 2020 00:25
benjamn added a commit that referenced this pull request Jun 25, 2020
…)"

This reverts commit 9450938.

The logic of diff.optimistic needs rethinking. Any query could read from
the cache at a time when optimistic updates are in progress, and that
possibility should not affect the network behavior of the query.

I had hoped that the dependency tracking system would serve to protect
queries whose field dependencies were unrelated to the optimistic updates,
but I have now observed at least one case where this hope was badly
mistaken, while debugging an endless loading spinner issue with the latest
versions of @apollo/client (rc3-rc.8) in the studio.apollographql.com
application.

As I mentioned in #6419, I was not able to reproduce the original scenario
using an ordinary optimistic mutation, so I think we should wait until
someone can provide a more realistic reproduction of the problem:
#6419 (comment)

I have left the test in place, disabled with itAsync.skip, so that we can
revisit this functionality in the future.
benjamn added a commit that referenced this pull request Jun 25, 2020
…)" (#6493)

This reverts commit 9450938.

The logic of diff.optimistic needs rethinking. Any query could read from
the cache at a time when optimistic updates are in progress, and that
possibility should not affect the network behavior of the query.

I had hoped that the dependency tracking system would serve to protect
queries whose field dependencies were unrelated to the optimistic updates,
but I have now observed at least one case where this hope was badly
mistaken, while debugging an endless loading spinner issue with the latest
versions of @apollo/client (rc3-rc.8) in the studio.apollographql.com
application.

As I mentioned in #6419, I was not able to reproduce the original scenario
using an ordinary optimistic mutation, so I think we should wait until
someone can provide a more realistic reproduction of the problem:
#6419 (comment)

I have left the test in place, disabled with itAsync.skip, so that we can
revisit this functionality in the future.
benjamn added a commit that referenced this pull request Aug 17, 2020
I first attempted to solve this bug in #6419, but that approach was
flawed, and we ultimately reverted it in #6493. Both of these changes
happened shortly before the AC3 launch (rc.3 and rc.9, respectively).

The key to this solution is that diff.fromOptimisticTransaction is only
ever set by the InMemoryCache broadcast code, when we know that we've just
performed an optimistic transaction, and we're broadcasting to a query
watcher that requested optimistic data. The QueryInfo class receives this
broadcast, and uses diff.fromOptimisticTransaction to decide whether to do
a full reapplication of the chosen fetch policy by calling oq.reobserve(),
or simply to deliver a single cache result by calling oq.observe().
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants