Skip to content

Commit c8c76a5

Browse files
authored
Add an explicit return type for useReadQuery (#11297)
1 parent 89374ca commit c8c76a5

6 files changed

Lines changed: 63 additions & 10 deletions

File tree

.api-reports/api-report-react.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,11 +2101,17 @@ export function useQuery<TData = any, TVariables extends OperationVariables = Op
21012101
export function useReactiveVar<T>(rv: ReactiveVar<T>): T;
21022102

21032103
// @public (undocumented)
2104-
export function useReadQuery<TData>(queryRef: QueryReference<TData>): {
2104+
export function useReadQuery<TData>(queryRef: QueryReference<TData>): UseReadQueryResult<TData>;
2105+
2106+
// @public (undocumented)
2107+
export interface UseReadQueryResult<TData = unknown> {
2108+
// (undocumented)
21052109
data: TData;
2106-
networkStatus: NetworkStatus;
2110+
// (undocumented)
21072111
error: ApolloError | undefined;
2108-
};
2112+
// (undocumented)
2113+
networkStatus: NetworkStatus;
2114+
}
21092115

21102116
// @public (undocumented)
21112117
export function useSubscription<TData = any, TVariables extends OperationVariables = OperationVariables>(subscription: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: SubscriptionHookOptions<NoInfer<TData>, NoInfer<TVariables>>): SubscriptionResult<TData, TVariables>;

.api-reports/api-report-react_hooks.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,11 +1988,17 @@ export function useQuery<TData = any, TVariables extends OperationVariables = Op
19881988
export function useReactiveVar<T>(rv: ReactiveVar<T>): T;
19891989

19901990
// @public (undocumented)
1991-
export function useReadQuery<TData>(queryRef: QueryReference<TData>): {
1991+
export function useReadQuery<TData>(queryRef: QueryReference<TData>): UseReadQueryResult<TData>;
1992+
1993+
// @public (undocumented)
1994+
export interface UseReadQueryResult<TData = unknown> {
1995+
// (undocumented)
19921996
data: TData;
1993-
networkStatus: NetworkStatus;
1997+
// (undocumented)
19941998
error: ApolloError | undefined;
1995-
};
1999+
// (undocumented)
2000+
networkStatus: NetworkStatus;
2001+
}
19962002

19972003
// Warning: (ae-forgotten-export) The symbol "SubscriptionHookOptions" needs to be exported by the entry point index.d.ts
19982004
//

.api-reports/api-report.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2742,11 +2742,17 @@ export function useQuery<TData = any, TVariables extends OperationVariables = Op
27422742
export function useReactiveVar<T>(rv: ReactiveVar<T>): T;
27432743

27442744
// @public (undocumented)
2745-
export function useReadQuery<TData>(queryRef: QueryReference<TData>): {
2745+
export function useReadQuery<TData>(queryRef: QueryReference<TData>): UseReadQueryResult<TData>;
2746+
2747+
// @public (undocumented)
2748+
export interface UseReadQueryResult<TData = unknown> {
2749+
// (undocumented)
27462750
data: TData;
2747-
networkStatus: NetworkStatus;
2751+
// (undocumented)
27482752
error: ApolloError | undefined;
2749-
};
2753+
// (undocumented)
2754+
networkStatus: NetworkStatus;
2755+
}
27502756

27512757
// @public (undocumented)
27522758
export function useSubscription<TData = any, TVariables extends OperationVariables = OperationVariables>(subscription: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: SubscriptionHookOptions<NoInfer<TData>, NoInfer<TVariables>>): SubscriptionResult<TData, TVariables>;

.changeset/good-experts-repair.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+
Add an explicit return type for the `useReadQuery` hook called `UseReadQueryResult`. Previously the return type of this hook was inferred from the return value.

src/react/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type { UseSuspenseQueryResult } from "./useSuspenseQuery.js";
1111
export { useSuspenseQuery } from "./useSuspenseQuery.js";
1212
export type { UseBackgroundQueryResult } from "./useBackgroundQuery.js";
1313
export { useBackgroundQuery } from "./useBackgroundQuery.js";
14+
export type { UseReadQueryResult } from "./useReadQuery.js";
1415
export { useReadQuery } from "./useReadQuery.js";
1516
export { skipToken } from "./constants.js";
1617
export type { SkipToken } from "./constants.js";

src/react/hooks/useReadQuery.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,37 @@ import { __use } from "./internal/index.js";
55
import { toApolloError } from "./useSuspenseQuery.js";
66
import { invariant } from "../../utilities/globals/index.js";
77
import { useSyncExternalStore } from "./useSyncExternalStore.js";
8+
import type { ApolloError } from "../../errors/index.js";
9+
import type { NetworkStatus } from "../../core/index.js";
810

9-
export function useReadQuery<TData>(queryRef: QueryReference<TData>) {
11+
export interface UseReadQueryResult<TData = unknown> {
12+
/**
13+
* An object containing the result of your GraphQL query after it completes.
14+
*
15+
* This value might be `undefined` if a query results in one or more errors
16+
* (depending on the query's `errorPolicy`).
17+
*/
18+
data: TData;
19+
/**
20+
* If the query produces one or more errors, this object contains either an
21+
* array of `graphQLErrors` or a single `networkError`. Otherwise, this value
22+
* is `undefined`.
23+
*
24+
* This property can be ignored when using the default `errorPolicy` or an
25+
* `errorPolicy` of `none`. The hook will throw the error instead of setting
26+
* this property.
27+
*/
28+
error: ApolloError | undefined;
29+
/**
30+
* A number indicating the current network state of the query's associated
31+
* request. {@link https://github.com/apollographql/apollo-client/blob/d96f4578f89b933c281bb775a39503f6cdb59ee8/src/core/networkStatus.ts#L4 | See possible values}.
32+
*/
33+
networkStatus: NetworkStatus;
34+
}
35+
36+
export function useReadQuery<TData>(
37+
queryRef: QueryReference<TData>
38+
): UseReadQueryResult<TData> {
1039
const internalQueryRef = unwrapQueryRef(queryRef);
1140
invariant(
1241
internalQueryRef.promiseCache,

0 commit comments

Comments
 (0)