Skip to content

Commit 6fde2e3

Browse files
authored
Add suspenseCache as a lazy hidden property on ApolloClient (#11053)
This removes `SuspenseCache` from the `Provider` options, and instead creates a `SuspenseCache` the first time it is requested by `getSuspenseCache`. It will be saved as a property on the `ApolloClient` instance, so those two instances will always be tied together. Configuration options can now be passed in as part of the `ApolloClient` configuration: ```js new ApolloClient({ react: { suspense: { autoDisposeTimeoutMs: 60_000 } } }) ```
1 parent cda4e01 commit 6fde2e3

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

src/testing/matchers/index.d.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {
44
OperationVariables,
55
} from '../../core/index.js';
66

7-
interface ApolloCustomMatchers<R = void> {
7+
interface ApolloCustomMatchers<R = void, T = {}> {
88
/**
99
* Used to determine if two GraphQL query documents are equal to each other by
1010
* comparing their printed values. The document must be parsed by `gql`.
@@ -14,18 +14,17 @@ interface ApolloCustomMatchers<R = void> {
1414
/**
1515
* Used to determine if the Suspense cache has a cache entry.
1616
*/
17-
toHaveSuspenseCacheEntryUsing(
18-
client: ApolloClient<unknown>,
17+
toHaveSuspenseCacheEntryUsing: T extends ApolloClient<any> ? ((
1918
query: DocumentNode,
2019
options?: {
2120
variables?: OperationVariables;
2221
queryKey?: string | number | any[];
2322
}
24-
): R;
23+
) => R) : { error: "matcher needs to be called on an ApolloClient instance" };
2524
}
2625

2726
declare global {
2827
namespace jest {
29-
interface Matchers<R = void> extends ApolloCustomMatchers<R> {}
28+
interface Matchers<R = void, T = {}> extends ApolloCustomMatchers<R, T> {}
3029
}
3130
}

src/testing/matchers/toHaveSuspenseCacheEntryUsing.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
import type { MatcherFunction } from 'expect';
22
import type { DocumentNode } from 'graphql';
3-
import type { ApolloClient, OperationVariables } from '../../core/index.js';
4-
import { SuspenseCache } from '../../react/index.js';
3+
import type { OperationVariables } from '../../core/index.js';
4+
import { ApolloClient } from '../../core/index.js';
55
import { canonicalStringify } from '../../cache/index.js';
6+
import { getSuspenseCache } from '../../react/cache/index.js';
7+
import type { CacheKey } from '../../react/cache/types.js';
68

79
export const toHaveSuspenseCacheEntryUsing: MatcherFunction<
810
[
9-
client: ApolloClient<unknown>,
1011
query: DocumentNode,
1112
options: {
1213
variables?: OperationVariables;
1314
queryKey?: string | number | any[];
1415
}
1516
]
1617
> = function (
17-
suspenseCache,
1818
client,
1919
query,
2020
{ variables, queryKey = [] } = Object.create(null)
2121
) {
22-
if (!(suspenseCache instanceof SuspenseCache)) {
23-
throw new Error('Actual must be an instance of `SuspenseCache`');
22+
if (!(client instanceof ApolloClient)) {
23+
throw new Error('Actual must be an instance of `ApolloClient`');
2424
}
2525

26-
const cacheKey = (
27-
[client, query, canonicalStringify(variables)] as any[]
28-
).concat(queryKey);
26+
const suspenseCache = getSuspenseCache(client);
27+
28+
const cacheKey: CacheKey = [
29+
query,
30+
canonicalStringify(variables),
31+
...([] as any[]).concat(queryKey),
32+
];
2933
const queryRef = suspenseCache['queryRefs'].lookupArray(cacheKey)?.current;
3034

3135
return {

0 commit comments

Comments
 (0)