Skip to content

Commit 9825295

Browse files
authored
Adjust useReadQuery wrapper logic to work with transported objects. (#11757)
* Adjust `useReadQuery` wrapper logic to work with transported objects. * size-limit
1 parent 80d2ba5 commit 9825295

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

.changeset/hungry-bobcats-battle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": patch
3+
---
4+
5+
Adjust `useReadQuery` wrapper logic to work with transported objects.

.size-limits.json

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

src/react/hooks/useReadQuery.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import {
44
unwrapQueryRef,
55
updateWrappedQueryRef,
66
} from "../internal/index.js";
7-
import type { QueryReference } from "../internal/index.js";
7+
import type {
8+
InternalQueryReference,
9+
QueryReference,
10+
} from "../internal/index.js";
811
import { __use, wrapHook } from "./internal/index.js";
912
import { toApolloError } from "./useSuspenseQuery.js";
1013
import { useSyncExternalStore } from "./useSyncExternalStore.js";
1114
import type { ApolloError } from "../../errors/index.js";
1215
import type { NetworkStatus } from "../../core/index.js";
16+
import { useApolloClient } from "./useApolloClient.js";
1317

1418
export interface UseReadQueryResult<TData = unknown> {
1519
/**
@@ -39,10 +43,25 @@ export interface UseReadQueryResult<TData = unknown> {
3943
export function useReadQuery<TData>(
4044
queryRef: QueryReference<TData>
4145
): UseReadQueryResult<TData> {
46+
const unwrapped = unwrapQueryRef(
47+
queryRef
48+
) satisfies InternalQueryReference<TData> as /*
49+
by all rules of this codebase, this should never be undefined
50+
but if `queryRef` is a transported object, it cannot have a
51+
`QUERY_REFERENCE_SYMBOL` symbol property, so the call above
52+
will return `undefined` and we want that represented in the type
53+
*/ InternalQueryReference<TData> | undefined;
54+
4255
return wrapHook(
4356
"useReadQuery",
4457
_useReadQuery,
45-
unwrapQueryRef(queryRef)["observable"]
58+
unwrapped ?
59+
unwrapped["observable"]
60+
// in the case of a "transported" queryRef object, we need to use the
61+
// client that's available to us at the current position in the React tree
62+
// that ApolloClient will then have the job to recreate a real queryRef from
63+
// the transported object
64+
: useApolloClient()
4665
)(queryRef);
4766
}
4867

0 commit comments

Comments
 (0)