Skip to content

Commit e72cbba

Browse files
authored
Wrap useQueryRefHandlers in wrapHook. (#11771)
* Wrap `useQueryRefHandlers` in `wrapHook`. * Clean up Prettier, Size-limit, and Api-Extractor --------- Co-authored-by: phryneas <[email protected]>
1 parent d90787d commit e72cbba

File tree

5 files changed

+57
-2
lines changed

5 files changed

+57
-2
lines changed

.api-reports/api-report-react_internal.md

+15
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,17 @@ type UseFragmentResult<TData> = {
19361936
// @public
19371937
function useQuery<TData = any, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: QueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>): QueryResult<TData, TVariables>;
19381938

1939+
// Warning: (ae-forgotten-export) The symbol "UseQueryRefHandlersResult" needs to be exported by the entry point index.d.ts
1940+
//
1941+
// @public
1942+
function useQueryRefHandlers<TData = unknown, TVariables extends OperationVariables = OperationVariables>(queryRef: QueryReference<TData, TVariables>): UseQueryRefHandlersResult<TData, TVariables>;
1943+
1944+
// @public (undocumented)
1945+
interface UseQueryRefHandlersResult<TData = unknown, TVariables extends OperationVariables = OperationVariables> {
1946+
fetchMore: FetchMoreFunction<TData, TVariables>;
1947+
refetch: RefetchFunction<TData, TVariables>;
1948+
}
1949+
19391950
// Warning: (ae-forgotten-export) The symbol "UseReadQueryResult" needs to be exported by the entry point index.d.ts
19401951
//
19411952
// @public (undocumented)
@@ -2056,6 +2067,10 @@ interface WrappableHooks {
20562067
//
20572068
// (undocumented)
20582069
useQuery: typeof useQuery;
2070+
// Warning: (ae-forgotten-export) The symbol "useQueryRefHandlers" needs to be exported by the entry point index.d.ts
2071+
//
2072+
// (undocumented)
2073+
useQueryRefHandlers: typeof useQueryRefHandlers;
20592074
// Warning: (ae-forgotten-export) The symbol "useReadQuery" needs to be exported by the entry point index.d.ts
20602075
//
20612076
// (undocumented)

.changeset/kind-foxes-float.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": patch
3+
---
4+
5+
Wrap `useQueryRefHandlers` in `wrapHook`.

.size-limits.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"dist/apollo-client.min.cjs": 39530,
2+
"dist/apollo-client.min.cjs": 39538,
33
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32809
44
}

src/react/hooks/internal/wrapHook.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
useBackgroundQuery,
55
useReadQuery,
66
useFragment,
7+
useQueryRefHandlers,
78
} from "../index.js";
89
import type { QueryManager } from "../../../core/QueryManager.js";
910
import type { ApolloClient } from "../../../core/ApolloClient.js";
@@ -17,6 +18,7 @@ interface WrappableHooks {
1718
useBackgroundQuery: typeof useBackgroundQuery;
1819
useReadQuery: typeof useReadQuery;
1920
useFragment: typeof useFragment;
21+
useQueryRefHandlers: typeof useQueryRefHandlers;
2022
}
2123

2224
/**

src/react/hooks/useQueryRefHandlers.ts

+34-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ import {
55
updateWrappedQueryRef,
66
wrapQueryRef,
77
} from "../internal/index.js";
8-
import type { QueryReference } from "../internal/index.js";
8+
import type {
9+
InternalQueryReference,
10+
QueryReference,
11+
} from "../internal/index.js";
912
import type { OperationVariables } from "../../core/types.js";
1013
import type { RefetchFunction, FetchMoreFunction } from "./useSuspenseQuery.js";
1114
import type { FetchMoreQueryOptions } from "../../core/watchQueryOptions.js";
15+
import { useApolloClient } from "./useApolloClient.js";
16+
import { wrapHook } from "./internal/index.js";
1217

1318
export interface UseQueryRefHandlersResult<
1419
TData = unknown,
@@ -44,6 +49,34 @@ export function useQueryRefHandlers<
4449
TVariables extends OperationVariables = OperationVariables,
4550
>(
4651
queryRef: QueryReference<TData, TVariables>
52+
): UseQueryRefHandlersResult<TData, TVariables> {
53+
const unwrapped = unwrapQueryRef(
54+
queryRef
55+
) satisfies InternalQueryReference<TData> as /*
56+
by all rules of this codebase, this should never be undefined
57+
but if `queryRef` is a transported object, it cannot have a
58+
`QUERY_REFERENCE_SYMBOL` symbol property, so the call above
59+
will return `undefined` and we want that represented in the type
60+
*/ InternalQueryReference<TData> | undefined;
61+
62+
return wrapHook(
63+
"useQueryRefHandlers",
64+
_useQueryRefHandlers,
65+
unwrapped ?
66+
unwrapped["observable"]
67+
// in the case of a "transported" queryRef object, we need to use the
68+
// client that's available to us at the current position in the React tree
69+
// that ApolloClient will then have the job to recreate a real queryRef from
70+
// the transported object
71+
: useApolloClient()
72+
)(queryRef);
73+
}
74+
75+
function _useQueryRefHandlers<
76+
TData = unknown,
77+
TVariables extends OperationVariables = OperationVariables,
78+
>(
79+
queryRef: QueryReference<TData, TVariables>
4780
): UseQueryRefHandlersResult<TData, TVariables> {
4881
const [previousQueryRef, setPreviousQueryRef] = React.useState(queryRef);
4982
const [wrappedQueryRef, setWrappedQueryRef] = React.useState(queryRef);

0 commit comments

Comments
 (0)