Skip to content

Commit

Permalink
Wrap useQueryRefHandlers in wrapHook. (#11771)
Browse files Browse the repository at this point in the history
* Wrap `useQueryRefHandlers` in `wrapHook`.

* Clean up Prettier, Size-limit, and Api-Extractor

---------

Co-authored-by: phryneas <phryneas@users.noreply.github.com>
  • Loading branch information
phryneas and phryneas committed Apr 11, 2024
1 parent d90787d commit e72cbba
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
15 changes: 15 additions & 0 deletions .api-reports/api-report-react_internal.md
Expand Up @@ -1936,6 +1936,17 @@ type UseFragmentResult<TData> = {
// @public
function useQuery<TData = any, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: QueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>): QueryResult<TData, TVariables>;

// Warning: (ae-forgotten-export) The symbol "UseQueryRefHandlersResult" needs to be exported by the entry point index.d.ts
//
// @public
function useQueryRefHandlers<TData = unknown, TVariables extends OperationVariables = OperationVariables>(queryRef: QueryReference<TData, TVariables>): UseQueryRefHandlersResult<TData, TVariables>;

// @public (undocumented)
interface UseQueryRefHandlersResult<TData = unknown, TVariables extends OperationVariables = OperationVariables> {
fetchMore: FetchMoreFunction<TData, TVariables>;
refetch: RefetchFunction<TData, TVariables>;
}

// Warning: (ae-forgotten-export) The symbol "UseReadQueryResult" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
Expand Down Expand Up @@ -2056,6 +2067,10 @@ interface WrappableHooks {
//
// (undocumented)
useQuery: typeof useQuery;
// Warning: (ae-forgotten-export) The symbol "useQueryRefHandlers" needs to be exported by the entry point index.d.ts
//
// (undocumented)
useQueryRefHandlers: typeof useQueryRefHandlers;
// Warning: (ae-forgotten-export) The symbol "useReadQuery" needs to be exported by the entry point index.d.ts
//
// (undocumented)
Expand Down
5 changes: 5 additions & 0 deletions .changeset/kind-foxes-float.md
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Wrap `useQueryRefHandlers` in `wrapHook`.
2 changes: 1 addition & 1 deletion .size-limits.json
@@ -1,4 +1,4 @@
{
"dist/apollo-client.min.cjs": 39530,
"dist/apollo-client.min.cjs": 39538,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32809
}
2 changes: 2 additions & 0 deletions src/react/hooks/internal/wrapHook.ts
Expand Up @@ -4,6 +4,7 @@ import type {
useBackgroundQuery,
useReadQuery,
useFragment,
useQueryRefHandlers,
} 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 {
useBackgroundQuery: typeof useBackgroundQuery;
useReadQuery: typeof useReadQuery;
useFragment: typeof useFragment;
useQueryRefHandlers: typeof useQueryRefHandlers;
}

/**
Expand Down
35 changes: 34 additions & 1 deletion src/react/hooks/useQueryRefHandlers.ts
Expand Up @@ -5,10 +5,15 @@ import {
updateWrappedQueryRef,
wrapQueryRef,
} from "../internal/index.js";
import type { QueryReference } from "../internal/index.js";
import type {
InternalQueryReference,
QueryReference,
} from "../internal/index.js";
import type { OperationVariables } from "../../core/types.js";
import type { RefetchFunction, FetchMoreFunction } from "./useSuspenseQuery.js";
import type { FetchMoreQueryOptions } from "../../core/watchQueryOptions.js";
import { useApolloClient } from "./useApolloClient.js";
import { wrapHook } from "./internal/index.js";

export interface UseQueryRefHandlersResult<
TData = unknown,
Expand Down Expand Up @@ -44,6 +49,34 @@ export function useQueryRefHandlers<
TVariables extends OperationVariables = OperationVariables,
>(
queryRef: QueryReference<TData, TVariables>
): UseQueryRefHandlersResult<TData, TVariables> {
const unwrapped = unwrapQueryRef(
queryRef
) satisfies InternalQueryReference<TData> as /*
by all rules of this codebase, this should never be undefined
but if `queryRef` is a transported object, it cannot have a
`QUERY_REFERENCE_SYMBOL` symbol property, so the call above
will return `undefined` and we want that represented in the type
*/ InternalQueryReference<TData> | undefined;

return wrapHook(
"useQueryRefHandlers",
_useQueryRefHandlers,
unwrapped ?
unwrapped["observable"]
// in the case of a "transported" queryRef object, we need to use the
// client that's available to us at the current position in the React tree
// that ApolloClient will then have the job to recreate a real queryRef from
// the transported object
: useApolloClient()
)(queryRef);
}

function _useQueryRefHandlers<
TData = unknown,
TVariables extends OperationVariables = OperationVariables,
>(
queryRef: QueryReference<TData, TVariables>
): UseQueryRefHandlersResult<TData, TVariables> {
const [previousQueryRef, setPreviousQueryRef] = React.useState(queryRef);
const [wrappedQueryRef, setWrappedQueryRef] = React.useState(queryRef);
Expand Down

0 comments on commit e72cbba

Please sign in to comment.