-
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
Use cache.batch
within cache.updateQuery
and cache.updateFragment
#8696
Use cache.batch
within cache.updateQuery
and cache.updateFragment
#8696
Conversation
optimistic: string | boolean; | ||
optimistic?: string | boolean; |
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.
Side fix: since optimistic
defaults to true
, it should not be a required property.
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.
Should we add | undefined
here? There’s a lot of TypeScript stuff which distinguishes missing and undefined these days (microsoft/TypeScript#43947)
@@ -472,6 +473,8 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> { | |||
// options.onWatchUpdated. | |||
this.broadcastWatches(options); | |||
} | |||
|
|||
return updateResult!; |
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.
The !
assertion is warranted as long as perform
is called earlier in the function (which it is).
77756c9
to
fea2404
Compare
9c3cb9b
to
184d6af
Compare
adc9b88
to
1b944e6
Compare
cache.batch
return the result of calling options.update
cache.batch
within cache.updateQuery
and cache.updateFragment
1b944e6
to
5c957cb
Compare
5c957cb
to
cfb2d88
Compare
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.
Some nits but I’m okay to merge. I’m always worried about TypeScript stuff because we aren’t testing that we aren’t breaking TypeScript inference for codegen users (yet), but it doesn’t look like there are any breaking type changes.
Since
cache.updateQuery
andcache.updateFragment
(#8382) run user-provided code (theupdate
function), there could end up being multiple cache write operations within a single update, which would triggerbroadcastWatches
separately, unless we apply batching.Fortunately, the
ApolloCache
class already has a tool forbroadcastWatches
batching:cache.batch
(introduced by #7819 in AC3.4). However, the exercise of usingcache.batch
for this purpose led me a useful improvement for that API, which I describe below.The
cache.batch
API followed its predecessorcache.performTransaction(cache => ...)
in not returning the result of the transaction function (theoptions.update
function forcache.batch
), which makes the following code harder to write:Since the
options.update
function is required inCache.BatchOptions
, andcache.batch
always calls it once (synchronously) before returning, I think it makes sense forcache.batch({ update(cache) { ... }})
to return whateveroptions.update
returns, enabling the shorter style of code above.With these improvements to
cache.batch
, the changes withincache.updateQuery
andcache.updateFragment
are shorter/cleaner than they otherwise would have been.