Skip to content

Commit 296fe26

Browse files
committed
Handle different cache ids in useSuspenseFragment
1 parent cb0c2f8 commit 296fe26

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

src/react/hooks/useSuspenseFragment.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import { canonicalStringify } from "../../cache/index.js";
88
import type { Cache } from "../../cache/index.js";
99
import { useApolloClient } from "./useApolloClient.js";
1010
import { getSuspenseCache } from "../internal/index.js";
11-
import type { CacheKey } from "../internal/index.js";
12-
import React from "rehackt";
13-
import type { FragmentKey } from "../internal/cache/types.js";
11+
import React, { useMemo } from "rehackt";
12+
import type { FragmentCacheKey, FragmentKey } from "../internal/cache/types.js";
1413
import { __use } from "./internal/__use.js";
1514
import { wrapHook } from "./internal/index.js";
1615

@@ -59,8 +58,16 @@ function _useSuspenseFragment<
5958
options: UseSuspenseFragmentOptions<TData, TVariables>
6059
): UseSuspenseFragmentResult<TData> {
6160
const client = useApolloClient(options.client);
61+
const { from } = options;
62+
const { cache } = client;
6263

63-
const cacheKey: CacheKey = [
64+
const id = useMemo(
65+
() => (typeof from === "string" ? from : cache.identify(from)),
66+
[cache, from]
67+
)!;
68+
69+
const cacheKey: FragmentCacheKey = [
70+
id,
6471
options.fragment,
6572
canonicalStringify(options.variables),
6673
];

src/react/internal/cache/SuspenseCache.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
import type { Observable } from "../../../utilities/index.js";
77
import { canUseWeakMap } from "../../../utilities/index.js";
88
import { InternalQueryReference } from "./QueryReference.js";
9-
import type { CacheKey } from "./types.js";
9+
import type { CacheKey, FragmentCacheKey } from "./types.js";
1010
import { FragmentReference } from "./FragmentReference.js";
1111

1212
export interface SuspenseCacheOptions {
@@ -58,7 +58,7 @@ export class SuspenseCache {
5858
}
5959

6060
getFragmentRef<TData>(
61-
cacheKey: CacheKey,
61+
cacheKey: FragmentCacheKey,
6262
createObservable: () => Observable<WatchFragmentResult<TData>>
6363
) {
6464
const ref = this.fragmentRefs.lookupArray(cacheKey) as {

src/react/internal/cache/types.ts

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ export type CacheKey = [
66
...queryKey: any[],
77
];
88

9+
export type FragmentCacheKey = [
10+
cacheId: string,
11+
fragment: DocumentNode,
12+
stringifiedVariables: string,
13+
];
14+
915
export interface QueryKey {
1016
__queryKey?: string;
1117
}

0 commit comments

Comments
 (0)