From 1b944e66c623a8f40c767d89cf19e75eab5e85ce Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Thu, 26 Aug 2021 18:37:01 -0400 Subject: [PATCH] Use `cache.batch` in `cache.update{Query,Fragment}`. --- src/cache/core/cache.ts | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/cache/core/cache.ts b/src/cache/core/cache.ts index 8f65cbd26a1..eef9d6a2ba0 100644 --- a/src/cache/core/cache.ts +++ b/src/cache/core/cache.ts @@ -176,21 +176,29 @@ export abstract class ApolloCache implements DataProxy { options: Cache.UpdateQueryOptions, update: (data: TData | null) => TData | null | void, ): TData | null { - const value = this.readQuery(options); - const data = update(value); - if (data === void 0 || data === null) return value; - this.writeQuery({ ...options, data }); - return data; + return this.batch({ + update(cache) { + const value = cache.readQuery(options); + const data = update(value); + if (data === void 0 || data === null) return value; + cache.writeQuery({ ...options, data }); + return data; + }, + }); } public updateFragment( options: Cache.UpdateFragmentOptions, update: (data: TData | null) => TData | null | void, ): TData | null { - const value = this.readFragment(options); - const data = update(value); - if (data === void 0 || data === null) return value; - this.writeFragment({ ...options, data }); - return data; + return this.batch({ + update(cache) { + const value = cache.readFragment(options); + const data = update(value); + if (data === void 0 || data === null) return value; + cache.writeFragment({ ...options, data }); + return data; + }, + }); } }