Skip to content

Commit

Permalink
Handle different cache ids in useSuspenseFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Dec 17, 2024
1 parent cb0c2f8 commit 296fe26
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
15 changes: 11 additions & 4 deletions src/react/hooks/useSuspenseFragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import { canonicalStringify } from "../../cache/index.js";
import type { Cache } from "../../cache/index.js";
import { useApolloClient } from "./useApolloClient.js";
import { getSuspenseCache } from "../internal/index.js";
import type { CacheKey } from "../internal/index.js";
import React from "rehackt";
import type { FragmentKey } from "../internal/cache/types.js";
import React, { useMemo } from "rehackt";
import type { FragmentCacheKey, FragmentKey } from "../internal/cache/types.js";
import { __use } from "./internal/__use.js";
import { wrapHook } from "./internal/index.js";

Expand Down Expand Up @@ -59,8 +58,16 @@ function _useSuspenseFragment<
options: UseSuspenseFragmentOptions<TData, TVariables>
): UseSuspenseFragmentResult<TData> {
const client = useApolloClient(options.client);
const { from } = options;
const { cache } = client;

const cacheKey: CacheKey = [
const id = useMemo(
() => (typeof from === "string" ? from : cache.identify(from)),
[cache, from]
)!;

const cacheKey: FragmentCacheKey = [
id,
options.fragment,
canonicalStringify(options.variables),
];
Expand Down
4 changes: 2 additions & 2 deletions src/react/internal/cache/SuspenseCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
import type { Observable } from "../../../utilities/index.js";
import { canUseWeakMap } from "../../../utilities/index.js";
import { InternalQueryReference } from "./QueryReference.js";
import type { CacheKey } from "./types.js";
import type { CacheKey, FragmentCacheKey } from "./types.js";
import { FragmentReference } from "./FragmentReference.js";

export interface SuspenseCacheOptions {
Expand Down Expand Up @@ -58,7 +58,7 @@ export class SuspenseCache {
}

getFragmentRef<TData>(
cacheKey: CacheKey,
cacheKey: FragmentCacheKey,
createObservable: () => Observable<WatchFragmentResult<TData>>
) {
const ref = this.fragmentRefs.lookupArray(cacheKey) as {
Expand Down
6 changes: 6 additions & 0 deletions src/react/internal/cache/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ export type CacheKey = [
...queryKey: any[],
];

export type FragmentCacheKey = [
cacheId: string,
fragment: DocumentNode,
stringifiedVariables: string,
];

export interface QueryKey {
__queryKey?: string;
}
Expand Down

0 comments on commit 296fe26

Please sign in to comment.