-
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
Introduce new cache.batch(options) method for InMemoryCache. #7819
Merged
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
hwillson
approved these changes
Mar 11, 2021
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.
This looks great @benjamn!
Side note - I think taking a long look at our continued support of the ApolloCache
abstraction is something we should add to our 4.0 planning.
This new method is similar to performTransaction, but takes an extensible options object rather than positional arguments. Among the new options is an onDirty callback function, which will be invoked for each cache watcher affected by the batch transaction.
benjamn
force-pushed
the
new-InMemoryCache-batch-method
branch
from
March 11, 2021 16:38
2a75510
to
4c0d58f
Compare
benjamn
added a commit
that referenced
this pull request
Mar 26, 2021
When an options.onDirty callback is provided to cache.batch (#7819), we want to call it with only the Cache.WatchOptions objects that were directly affected by options.transaction, so it's important to broadcast watches before the transaction, to flush out any pending watches waiting to be broadcast. See provided tests for an example where this matters.
benjamn
added a commit
that referenced
this pull request
Mar 26, 2021
When an `options.onDirty` callback is provided to `cache.batch` (#7819), we want to call `onDirty` with only the `Cache.WatchOptions` objects that were directly affected by `options.transaction`, so it's important to broadcast watches _before_ the transaction, to flush out any pending watches waiting to be broadcast.
This was referenced Aug 3, 2022
Closed
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.
As new methods have been added to
ApolloCache
andInMemoryCache
, we have found it convenient in many cases (e.g.cache.evict
andcache.modify
) to use a method signature that accepts named options, rather than positional parameters. Named options are easier to extend in the future, and less cumbersome when some of the options might be omitted.This awkwardness of positional parameters affects the two existing transaction-related methods of
InMemoryCache
:Instead of adding new functionality to these methods by introducing new positional parameters, I would very much prefer to switch to named options, but I was previously concerned that introducing a new transaction method for the
ApolloCache
superclass would be a breaking change for anyone not usingInMemoryCache
.As this PR demonstrates, it is possible to introduce an
ApolloCache
method calledbatch
with a default implementation that just callsthis.performTransaction
, so any custom cache implementation (not based onInMemoryCache
) immediately inherits a usable version of thebatch
method. The subclass may choose to overridebatch
to take advantage of its named options, but that's not mandatory. Either way,ApolloClient
(specifically,QueryManager#markMutationResult
) can safely start callingcache.batch
instead of callingcache.performTransaction
as it does now.Of course,
InMemoryCache
overridesbatch
, and relocates most of the code that used to live inperformTransaction
into the newbatch
method. It's a much more powerful and ergonomic API, already allowing new functionality like theonDirty
callback, and anoptions.transaction
function whose first parameter has the subclass type (e.g.InMemoryCache
) rather than theApolloCache
superclass type.This PR is a stepping stone to implementing the following item from the Apollo Client v3.4
ROADMAP.md
section:updateQueries
,refetchQueries
, andawaitRefetchQueries
in a lot of cases.I will finish implementing those features in subsequent PRs, but you might already be able to guess how
onDirty
will help.