diff --git a/.changeset/olive-pans-report.md b/.changeset/olive-pans-report.md new file mode 100644 index 00000000000..489155ee529 --- /dev/null +++ b/.changeset/olive-pans-report.md @@ -0,0 +1,5 @@ +--- +'@apollo/client': patch +--- + +`useBackgroundQuery` now uses its own options type called `BackgroundQueryHookOptions` rather than reusing `SuspenseQueryHookOptions`. diff --git a/src/react/hooks/useBackgroundQuery.ts b/src/react/hooks/useBackgroundQuery.ts index c90723a8fbb..1b5afd4f537 100644 --- a/src/react/hooks/useBackgroundQuery.ts +++ b/src/react/hooks/useBackgroundQuery.ts @@ -9,7 +9,7 @@ import { QUERY_REFERENCE_SYMBOL, type QueryReference, } from '../cache/QueryReference'; -import type { SuspenseQueryHookOptions, NoInfer } from '../types/types'; +import type { BackgroundQueryHookOptions, NoInfer } from '../types/types'; import { __use } from './internal'; import { useSuspenseCache } from './useSuspenseCache'; import { useTrackedQueryRefs, useWatchQueryOptions } from './useSuspenseQuery'; @@ -31,16 +31,10 @@ export type UseBackgroundQueryResult< export function useBackgroundQuery< TData, TVariables extends OperationVariables, - TOptions extends Omit< - SuspenseQueryHookOptions, - 'variables' | 'returnPartialData' | 'refetchWritePolicy' - > + TOptions extends Omit, 'variables'> >( query: DocumentNode | TypedDocumentNode, - options?: Omit< - SuspenseQueryHookOptions, NoInfer>, - 'returnPartialData' | 'refetchWritePolicy' - > & + options?: BackgroundQueryHookOptions, NoInfer> & TOptions ): UseBackgroundQueryResult< TOptions['errorPolicy'] extends 'ignore' | 'all' @@ -56,10 +50,7 @@ export function useBackgroundQuery< TVariables extends OperationVariables = OperationVariables >( query: DocumentNode | TypedDocumentNode, - options: Omit< - SuspenseQueryHookOptions, NoInfer>, - 'returnPartialData' | 'refetchWritePolicy' - > & { + options: BackgroundQueryHookOptions, NoInfer> & { returnPartialData: true; errorPolicy: 'ignore' | 'all'; } @@ -70,10 +61,7 @@ export function useBackgroundQuery< TVariables extends OperationVariables = OperationVariables >( query: DocumentNode | TypedDocumentNode, - options: Omit< - SuspenseQueryHookOptions, NoInfer>, - 'returnPartialData' | 'refetchWritePolicy' - > & { + options: BackgroundQueryHookOptions, NoInfer> & { errorPolicy: 'ignore' | 'all'; } ): UseBackgroundQueryResult; @@ -83,10 +71,7 @@ export function useBackgroundQuery< TVariables extends OperationVariables = OperationVariables >( query: DocumentNode | TypedDocumentNode, - options: Omit< - SuspenseQueryHookOptions, NoInfer>, - 'returnPartialData' | 'refetchWritePolicy' - > & { + options: BackgroundQueryHookOptions, NoInfer> & { skip: boolean; } ): UseBackgroundQueryResult; @@ -99,10 +84,7 @@ export function useBackgroundQuery< // TVariables extends OperationVariables = OperationVariables // >( // query: DocumentNode | TypedDocumentNode, -// options: Omit< -// SuspenseQueryHookOptions, NoInfer>, -// 'returnPartialData' | 'refetchWritePolicy' -// > & { +// options: SuspenseQueryHookOptions, NoInfer> & { // returnPartialData: true; // } // ): UseBackgroundQueryResult, TVariables>; @@ -112,10 +94,7 @@ export function useBackgroundQuery< TVariables extends OperationVariables = OperationVariables >( query: DocumentNode | TypedDocumentNode, - options?: Omit< - SuspenseQueryHookOptions, NoInfer>, - 'returnPartialData' | 'refetchWritePolicy' - > + options?: BackgroundQueryHookOptions, NoInfer> ): UseBackgroundQueryResult; export function useBackgroundQuery< @@ -123,9 +102,9 @@ export function useBackgroundQuery< TVariables extends OperationVariables = OperationVariables >( query: DocumentNode | TypedDocumentNode, - options: Omit< - SuspenseQueryHookOptions, - 'returnPartialData' | 'refetchWritePolicy' + options: BackgroundQueryHookOptions< + NoInfer, + NoInfer > = Object.create(null) ): UseBackgroundQueryResult { const suspenseCache = useSuspenseCache(options.suspenseCache); diff --git a/src/react/types/types.ts b/src/react/types/types.ts index 90b7107c628..c988a842857 100644 --- a/src/react/types/types.ts +++ b/src/react/types/types.ts @@ -131,6 +131,31 @@ export interface SuspenseQueryHookOptions< queryKey?: string | number | any[]; } +export type BackgroundQueryHookFetchPolicy = Extract< + WatchQueryFetchPolicy, + | 'cache-first' + | 'network-only' + | 'no-cache' + | 'cache-and-network' +>; + +export interface BackgroundQueryHookOptions< + TData = unknown, + TVariables extends OperationVariables = OperationVariables +> extends Pick< + QueryHookOptions, + | 'client' + | 'variables' + | 'errorPolicy' + | 'context' + | 'canonizeResults' + | 'skip' +> { + fetchPolicy?: BackgroundQueryHookFetchPolicy; + suspenseCache?: SuspenseCache; + queryKey?: string | number | any[]; +} + /** * @deprecated TODO Delete this unused interface. */