Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for passing in client into useFragment #11525

Merged
merged 14 commits into from
Feb 1, 2024
5 changes: 5 additions & 0 deletions .changeset/brave-cougars-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Allows passing in client via options to useFragment
6 changes: 4 additions & 2 deletions src/react/hooks/useFragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {

import { useApolloClient } from "./useApolloClient.js";
import { useSyncExternalStore } from "./useSyncExternalStore.js";
import type { OperationVariables } from "../../core/index.js";
import type { ApolloClient, OperationVariables } from "../../core/index.js";
import type { NoInfer } from "../types/types.js";
import { useDeepMemo, useLazyRef } from "./internal/index.js";

Expand All @@ -28,6 +28,7 @@ export interface UseFragmentOptions<TData, TVars>
from: StoreObject | Reference | string;
// Override this field to make it optional (default: true).
optimistic?: boolean;
client?: ApolloClient;
vezaynk marked this conversation as resolved.
Show resolved Hide resolved
}

export type UseFragmentResult<TData> =
Expand All @@ -45,7 +46,7 @@ export type UseFragmentResult<TData> =
export function useFragment<TData = any, TVars = OperationVariables>(
options: UseFragmentOptions<TData, TVars>
): UseFragmentResult<TData> {
const { cache } = useApolloClient();
const { cache } = useApolloClient(options.client);

const diffOptions = useDeepMemo<Cache.DiffOptions<TData, TVars>>(() => {
const {
Expand Down Expand Up @@ -117,3 +118,4 @@ function diffToResult<TData>(

return result;
}

vezaynk marked this conversation as resolved.
Show resolved Hide resolved