Skip to content

Commit

Permalink
Use cache.batch in cache.update{Query,Fragment}.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Aug 26, 2021
1 parent 25b6ec9 commit 1b944e6
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/cache/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,29 @@ export abstract class ApolloCache<TSerialized> implements DataProxy {
options: Cache.UpdateQueryOptions<TData, TVariables>,
update: (data: TData | null) => TData | null | void,
): TData | null {
const value = this.readQuery<TData, TVariables>(options);
const data = update(value);
if (data === void 0 || data === null) return value;
this.writeQuery<TData, TVariables>({ ...options, data });
return data;
return this.batch({
update(cache) {
const value = cache.readQuery<TData, TVariables>(options);
const data = update(value);
if (data === void 0 || data === null) return value;
cache.writeQuery<TData, TVariables>({ ...options, data });
return data;
},
});
}

public updateFragment<TData = any, TVariables = any>(
options: Cache.UpdateFragmentOptions<TData, TVariables>,
update: (data: TData | null) => TData | null | void,
): TData | null {
const value = this.readFragment<TData, TVariables>(options);
const data = update(value);
if (data === void 0 || data === null) return value;
this.writeFragment<TData, TVariables>({ ...options, data });
return data;
return this.batch({
update(cache) {
const value = cache.readFragment<TData, TVariables>(options);
const data = update(value);
if (data === void 0 || data === null) return value;
cache.writeFragment<TData, TVariables>({ ...options, data });
return data;
},
});
}
}

0 comments on commit 1b944e6

Please sign in to comment.