Skip to content

Commit

Permalink
Adjust useReadQuery wrapper logic to work with transported objects. (#…
Browse files Browse the repository at this point in the history
…11757)

* Adjust `useReadQuery` wrapper logic to work with transported objects.

* size-limit
  • Loading branch information
phryneas authored Apr 8, 2024
1 parent 80d2ba5 commit 9825295
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-bobcats-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Adjust `useReadQuery` wrapper logic to work with transported objects.
2 changes: 1 addition & 1 deletion .size-limits.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"dist/apollo-client.min.cjs": 39518,
"dist/apollo-client.min.cjs": 39523,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32809
}
23 changes: 21 additions & 2 deletions src/react/hooks/useReadQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import {
unwrapQueryRef,
updateWrappedQueryRef,
} from "../internal/index.js";
import type { QueryReference } from "../internal/index.js";
import type {
InternalQueryReference,
QueryReference,
} from "../internal/index.js";
import { __use, wrapHook } from "./internal/index.js";
import { toApolloError } from "./useSuspenseQuery.js";
import { useSyncExternalStore } from "./useSyncExternalStore.js";
import type { ApolloError } from "../../errors/index.js";
import type { NetworkStatus } from "../../core/index.js";
import { useApolloClient } from "./useApolloClient.js";

export interface UseReadQueryResult<TData = unknown> {
/**
Expand Down Expand Up @@ -39,10 +43,25 @@ export interface UseReadQueryResult<TData = unknown> {
export function useReadQuery<TData>(
queryRef: QueryReference<TData>
): UseReadQueryResult<TData> {
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(
"useReadQuery",
_useReadQuery,
unwrapQueryRef(queryRef)["observable"]
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);
}

Expand Down

0 comments on commit 9825295

Please sign in to comment.