diff --git a/.api-reports/api-report-react.md b/.api-reports/api-report-react.md index 8a6f21665bd..267adc1a330 100644 --- a/.api-reports/api-report-react.md +++ b/.api-reports/api-report-react.md @@ -2101,11 +2101,17 @@ export function useQuery(rv: ReactiveVar): T; // @public (undocumented) -export function useReadQuery(queryRef: QueryReference): { +export function useReadQuery(queryRef: QueryReference): UseReadQueryResult; + +// @public (undocumented) +export interface UseReadQueryResult { + // (undocumented) data: TData; - networkStatus: NetworkStatus; + // (undocumented) error: ApolloError | undefined; -}; + // (undocumented) + networkStatus: NetworkStatus; +} // @public (undocumented) export function useSubscription(subscription: DocumentNode | TypedDocumentNode, options?: SubscriptionHookOptions, NoInfer>): SubscriptionResult; diff --git a/.api-reports/api-report-react_hooks.md b/.api-reports/api-report-react_hooks.md index cf0f649ed18..110739c8df9 100644 --- a/.api-reports/api-report-react_hooks.md +++ b/.api-reports/api-report-react_hooks.md @@ -1988,11 +1988,17 @@ export function useQuery(rv: ReactiveVar): T; // @public (undocumented) -export function useReadQuery(queryRef: QueryReference): { +export function useReadQuery(queryRef: QueryReference): UseReadQueryResult; + +// @public (undocumented) +export interface UseReadQueryResult { + // (undocumented) data: TData; - networkStatus: NetworkStatus; + // (undocumented) error: ApolloError | undefined; -}; + // (undocumented) + networkStatus: NetworkStatus; +} // Warning: (ae-forgotten-export) The symbol "SubscriptionHookOptions" needs to be exported by the entry point index.d.ts // diff --git a/.api-reports/api-report.md b/.api-reports/api-report.md index e3f49b900c0..3004669da51 100644 --- a/.api-reports/api-report.md +++ b/.api-reports/api-report.md @@ -2742,11 +2742,17 @@ export function useQuery(rv: ReactiveVar): T; // @public (undocumented) -export function useReadQuery(queryRef: QueryReference): { +export function useReadQuery(queryRef: QueryReference): UseReadQueryResult; + +// @public (undocumented) +export interface UseReadQueryResult { + // (undocumented) data: TData; - networkStatus: NetworkStatus; + // (undocumented) error: ApolloError | undefined; -}; + // (undocumented) + networkStatus: NetworkStatus; +} // @public (undocumented) export function useSubscription(subscription: DocumentNode | TypedDocumentNode, options?: SubscriptionHookOptions, NoInfer>): SubscriptionResult; diff --git a/.changeset/good-experts-repair.md b/.changeset/good-experts-repair.md new file mode 100644 index 00000000000..37aef92f934 --- /dev/null +++ b/.changeset/good-experts-repair.md @@ -0,0 +1,5 @@ +--- +"@apollo/client": patch +--- + +Add an explicit return type for the `useReadQuery` hook called `UseReadQueryResult`. Previously the return type of this hook was inferred from the return value. diff --git a/src/react/hooks/index.ts b/src/react/hooks/index.ts index 3aace0d3e9d..61d50665cac 100644 --- a/src/react/hooks/index.ts +++ b/src/react/hooks/index.ts @@ -11,6 +11,7 @@ export type { UseSuspenseQueryResult } from "./useSuspenseQuery.js"; export { useSuspenseQuery } from "./useSuspenseQuery.js"; export type { UseBackgroundQueryResult } from "./useBackgroundQuery.js"; export { useBackgroundQuery } from "./useBackgroundQuery.js"; +export type { UseReadQueryResult } from "./useReadQuery.js"; export { useReadQuery } from "./useReadQuery.js"; export { skipToken } from "./constants.js"; export type { SkipToken } from "./constants.js"; diff --git a/src/react/hooks/useReadQuery.ts b/src/react/hooks/useReadQuery.ts index 85d4b3026f1..e6a97e1446f 100644 --- a/src/react/hooks/useReadQuery.ts +++ b/src/react/hooks/useReadQuery.ts @@ -5,8 +5,37 @@ import { __use } from "./internal/index.js"; import { toApolloError } from "./useSuspenseQuery.js"; import { invariant } from "../../utilities/globals/index.js"; import { useSyncExternalStore } from "./useSyncExternalStore.js"; +import type { ApolloError } from "../../errors/index.js"; +import type { NetworkStatus } from "../../core/index.js"; -export function useReadQuery(queryRef: QueryReference) { +export interface UseReadQueryResult { + /** + * An object containing the result of your GraphQL query after it completes. + * + * This value might be `undefined` if a query results in one or more errors + * (depending on the query's `errorPolicy`). + */ + data: TData; + /** + * If the query produces one or more errors, this object contains either an + * array of `graphQLErrors` or a single `networkError`. Otherwise, this value + * is `undefined`. + * + * This property can be ignored when using the default `errorPolicy` or an + * `errorPolicy` of `none`. The hook will throw the error instead of setting + * this property. + */ + error: ApolloError | undefined; + /** + * A number indicating the current network state of the query's associated + * request. {@link https://github.com/apollographql/apollo-client/blob/d96f4578f89b933c281bb775a39503f6cdb59ee8/src/core/networkStatus.ts#L4 | See possible values}. + */ + networkStatus: NetworkStatus; +} + +export function useReadQuery( + queryRef: QueryReference +): UseReadQueryResult { const internalQueryRef = unwrapQueryRef(queryRef); invariant( internalQueryRef.promiseCache,