Skip to content

Commit 8ff2f49

Browse files
authored
Merge pull request #8696 from apollographql/make-cache.batch-return-options.update-result
Use `cache.batch` within `cache.updateQuery` and `cache.updateFragment`
2 parents 756ab87 + 6bf12cb commit 8ff2f49

File tree

7 files changed

+331
-40
lines changed

7 files changed

+331
-40
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
- A new nested entry point called `@apollo/client/testing/core` has been created. Importing from this entry point instead of `@apollo/client/testing` excludes any React-related dependencies. <br/>
3737
[@wassim-k](https://github.com/wassim-k) in [#8687](https://github.com/apollographql/apollo-client/pull/8687)
3838

39+
- Make `cache.batch` return the result of calling the `options.update` function. <br/>
40+
[@benjamn](https://github.com/benjamn) in [#8696](https://github.com/apollographql/apollo-client/pull/8696)
41+
3942
### React Refactoring
4043

4144
#### Bug Fixes (due to [@brainkim](https://github.com/brainkim) in [#8596](https://github.com/apollographql/apollo-client/pull/8596)):

src/cache/core/__tests__/cache.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class TestCache extends ApolloCache<unknown> {
2121
}
2222

2323
public performTransaction(transaction: <TSerialized>(c: ApolloCache<TSerialized>) => void): void {
24+
transaction(this);
2425
}
2526

2627
public read<T, TVariables = any>(query: Cache.ReadOptions<TVariables>): T | null {

src/cache/core/cache.ts

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ export type Transaction<T> = (c: ApolloCache<T>) => void;
1414
export abstract class ApolloCache<TSerialized> implements DataProxy {
1515
// required to implement
1616
// core API
17-
public abstract read<T, TVariables = any>(
18-
query: Cache.ReadOptions<TVariables, T>,
19-
): T | null;
20-
public abstract write<TResult = any, TVariables = any>(
21-
write: Cache.WriteOptions<TResult, TVariables>,
17+
public abstract read<TData = any, TVariables = any>(
18+
query: Cache.ReadOptions<TVariables, TData>,
19+
): TData | null;
20+
public abstract write<TData = any, TVariables = any>(
21+
write: Cache.WriteOptions<TData, TVariables>,
2222
): Reference | undefined;
2323
public abstract diff<T>(query: Cache.DiffOptions): Cache.DiffResult<T>;
24-
public abstract watch(watch: Cache.WatchOptions): () => void;
24+
public abstract watch<TData = any, TVariables = any>(
25+
watch: Cache.WatchOptions<TData, TVariables>,
26+
): () => void;
2527
public abstract reset(): Promise<void>;
2628

2729
// Remove whole objects from the cache by passing just options.id, or
@@ -59,11 +61,16 @@ export abstract class ApolloCache<TSerialized> implements DataProxy {
5961
// provide a default batch implementation that's just another way of calling
6062
// performTransaction. Subclasses of ApolloCache (such as InMemoryCache) can
6163
// override the batch method to do more interesting things with its options.
62-
public batch(options: Cache.BatchOptions<this>) {
64+
public batch<U>(options: Cache.BatchOptions<this, U>): U {
6365
const optimisticId =
6466
typeof options.optimistic === "string" ? options.optimistic :
6567
options.optimistic === false ? null : void 0;
66-
this.performTransaction(options.update, optimisticId);
68+
let updateResult: U;
69+
this.performTransaction(
70+
() => updateResult = options.update(this),
71+
optimisticId,
72+
);
73+
return updateResult!;
6774
}
6875

6976
public abstract performTransaction(
@@ -171,21 +178,29 @@ export abstract class ApolloCache<TSerialized> implements DataProxy {
171178
options: Cache.UpdateQueryOptions<TData, TVariables>,
172179
update: (data: TData | null) => TData | null | void,
173180
): TData | null {
174-
const value = this.readQuery<TData, TVariables>(options);
175-
const data = update(value);
176-
if (data === void 0 || data === null) return value;
177-
this.writeQuery<TData, TVariables>({ ...options, data });
178-
return data;
181+
return this.batch({
182+
update(cache) {
183+
const value = cache.readQuery<TData, TVariables>(options);
184+
const data = update(value);
185+
if (data === void 0 || data === null) return value;
186+
cache.writeQuery<TData, TVariables>({ ...options, data });
187+
return data;
188+
},
189+
});
179190
}
180191

181192
public updateFragment<TData = any, TVariables = any>(
182193
options: Cache.UpdateFragmentOptions<TData, TVariables>,
183194
update: (data: TData | null) => TData | null | void,
184195
): TData | null {
185-
const value = this.readFragment<TData, TVariables>(options);
186-
const data = update(value);
187-
if (data === void 0 || data === null) return value;
188-
this.writeFragment<TData, TVariables>({ ...options, data });
189-
return data;
196+
return this.batch({
197+
update(cache) {
198+
const value = cache.readFragment<TData, TVariables>(options);
199+
const data = update(value);
200+
if (data === void 0 || data === null) return value;
201+
cache.writeFragment<TData, TVariables>({ ...options, data });
202+
return data;
203+
},
204+
});
190205
}
191206
}

src/cache/core/types/Cache.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { Modifier, Modifiers } from './common';
33
import { ApolloCache } from '../cache';
44

55
export namespace Cache {
6-
export type WatchCallback = (
7-
diff: Cache.DiffResult<any>,
8-
lastDiff?: Cache.DiffResult<any>,
6+
export type WatchCallback<TData = any> = (
7+
diff: Cache.DiffResult<TData>,
8+
lastDiff?: Cache.DiffResult<TData>,
99
) => void;
1010

1111
export interface ReadOptions<TVariables = any, TData = any>
@@ -25,19 +25,23 @@ export namespace Cache {
2525
result: TResult;
2626
}
2727

28-
export interface DiffOptions extends ReadOptions {
28+
export interface DiffOptions<
29+
TData = any,
30+
TVariables = any,
31+
> extends ReadOptions<TVariables, TData> {
2932
// The DiffOptions interface is currently just an alias for
3033
// ReadOptions, though DiffOptions used to be responsible for
3134
// declaring the returnPartialData option.
3235
}
3336

3437
export interface WatchOptions<
35-
Watcher extends object = Record<string, any>
36-
> extends ReadOptions {
37-
watcher?: Watcher;
38+
TData = any,
39+
TVariables = any,
40+
> extends ReadOptions<TVariables, TData> {
41+
watcher?: object;
3842
immediate?: boolean;
39-
callback: WatchCallback;
40-
lastDiff?: DiffResult<any>;
43+
callback: WatchCallback<TData>;
44+
lastDiff?: DiffResult<TData>;
4145
}
4246

4347
export interface EvictOptions {
@@ -54,10 +58,13 @@ export namespace Cache {
5458
broadcast?: boolean;
5559
}
5660

57-
export interface BatchOptions<C extends ApolloCache<any>> {
61+
export interface BatchOptions<
62+
TCache extends ApolloCache<any>,
63+
TUpdateResult = void,
64+
> {
5865
// Same as the first parameter of performTransaction, except the cache
5966
// argument will have the subclass type rather than ApolloCache.
60-
update(cache: C): void;
67+
update(cache: TCache): TUpdateResult;
6168

6269
// Passing a string for this option creates a new optimistic layer, with the
6370
// given string as its layer.id, just like passing a string for the
@@ -66,7 +73,7 @@ export namespace Cache {
6673
// against the current top layer of the cache), and passing false is the
6774
// same as passing null (running the operation against root/non-optimistic
6875
// cache data).
69-
optimistic: string | boolean;
76+
optimistic?: string | boolean;
7077

7178
// If you specify the ID of an optimistic layer using this option, that
7279
// layer will be removed as part of the batch transaction, triggering at
@@ -80,7 +87,7 @@ export namespace Cache {
8087
// this batch operation, pass this optional callback function. Returning
8188
// false from the callback will prevent broadcasting this result.
8289
onWatchUpdated?: (
83-
this: C,
90+
this: TCache,
8491
watch: Cache.WatchOptions,
8592
diff: Cache.DiffResult<any>,
8693
lastDiff: Cache.DiffResult<any> | undefined,

0 commit comments

Comments
 (0)