Skip to content

Commit

Permalink
Wrap useSuspenseFragment hook for streaming package
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Dec 17, 2024
1 parent 2a130cd commit cb0c2f8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/react/hooks/internal/wrapHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
useReadQuery,
useFragment,
useQueryRefHandlers,
useSuspenseFragment,
} from "../index.js";
import type { QueryManager } from "../../../core/QueryManager.js";
import type { ApolloClient } from "../../../core/ApolloClient.js";
Expand All @@ -17,6 +18,7 @@ interface WrappableHooks {
createQueryPreloader: typeof createQueryPreloader;
useQuery: typeof useQuery;
useSuspenseQuery: typeof useSuspenseQuery;
useSuspenseFragment: typeof useSuspenseFragment;
useBackgroundQuery: typeof useBackgroundQuery;
useReadQuery: typeof useReadQuery;
useFragment: typeof useFragment;
Expand Down
26 changes: 24 additions & 2 deletions src/react/hooks/useSuspenseFragment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { ApolloClient, Reference, StoreObject } from "../../core/index.js";
import type {
ApolloClient,
OperationVariables,
Reference,
StoreObject,
} from "../../core/index.js";
import { canonicalStringify } from "../../cache/index.js";
import type { Cache } from "../../cache/index.js";
import { useApolloClient } from "./useApolloClient.js";
Expand All @@ -7,6 +12,7 @@ import type { CacheKey } from "../internal/index.js";
import React from "rehackt";
import type { FragmentKey } from "../internal/cache/types.js";
import { __use } from "./internal/__use.js";
import { wrapHook } from "./internal/index.js";

export interface UseSuspenseFragmentOptions<TData, TVars>
extends Omit<
Expand All @@ -33,7 +39,23 @@ export interface UseSuspenseFragmentOptions<TData, TVars>

export type UseSuspenseFragmentResult<TData> = { data: TData };

export function useSuspenseFragment<TData, TVariables>(
export function useSuspenseFragment<
TData = unknown,
TVariables extends OperationVariables = OperationVariables,
>(
options: UseSuspenseFragmentOptions<TData, TVariables>
): UseSuspenseFragmentResult<TData> {
return wrapHook(
"useSuspenseFragment",
_useSuspenseFragment,
useApolloClient(typeof options === "object" ? options.client : undefined)
)(options);
}

function _useSuspenseFragment<
TData = unknown,
TVariables extends OperationVariables = OperationVariables,
>(
options: UseSuspenseFragmentOptions<TData, TVariables>
): UseSuspenseFragmentResult<TData> {
const client = useApolloClient(options.client);
Expand Down

0 comments on commit cb0c2f8

Please sign in to comment.