-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
benjamn
merged 2 commits into
master
from
avoid-network-for-incomplete-optimistic-cache-diffs
Jun 10, 2020
Merged
Avoid network after incomplete optimistic cache results. #6419
benjamn
merged 2 commits into
master
from
avoid-network-for-incomplete-optimistic-cache-diffs
Jun 10, 2020
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
commented
Jun 9, 2020
benjamn
commented
Jun 9, 2020
benjamn
commented
Jun 9, 2020
hwillson
approved these changes
Jun 10, 2020
There was a problem hiding this 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!
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().
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 bothcache-and-network
andnetwork-only
turn intocache-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.