Skip to content

Commit

Permalink
Create a separate BackgroundQueryHookOptions type and use it for `u…
Browse files Browse the repository at this point in the history
…seBackgroundQuery` (#11018)
  • Loading branch information
jerelmiller authored Jun 28, 2023
1 parent 879ee31 commit 5618953
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-pans-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@apollo/client': patch
---

`useBackgroundQuery` now uses its own options type called `BackgroundQueryHookOptions` rather than reusing `SuspenseQueryHookOptions`.
43 changes: 11 additions & 32 deletions src/react/hooks/useBackgroundQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -31,16 +31,10 @@ export type UseBackgroundQueryResult<
export function useBackgroundQuery<
TData,
TVariables extends OperationVariables,
TOptions extends Omit<
SuspenseQueryHookOptions<TData>,
'variables' | 'returnPartialData' | 'refetchWritePolicy'
>
TOptions extends Omit<BackgroundQueryHookOptions<TData>, 'variables'>
>(
query: DocumentNode | TypedDocumentNode<TData, TVariables>,
options?: Omit<
SuspenseQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>,
'returnPartialData' | 'refetchWritePolicy'
> &
options?: BackgroundQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>> &
TOptions
): UseBackgroundQueryResult<
TOptions['errorPolicy'] extends 'ignore' | 'all'
Expand All @@ -56,10 +50,7 @@ export function useBackgroundQuery<
TVariables extends OperationVariables = OperationVariables
>(
query: DocumentNode | TypedDocumentNode<TData, TVariables>,
options: Omit<
SuspenseQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>,
'returnPartialData' | 'refetchWritePolicy'
> & {
options: BackgroundQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>> & {
returnPartialData: true;
errorPolicy: 'ignore' | 'all';
}
Expand All @@ -70,10 +61,7 @@ export function useBackgroundQuery<
TVariables extends OperationVariables = OperationVariables
>(
query: DocumentNode | TypedDocumentNode<TData, TVariables>,
options: Omit<
SuspenseQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>,
'returnPartialData' | 'refetchWritePolicy'
> & {
options: BackgroundQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>> & {
errorPolicy: 'ignore' | 'all';
}
): UseBackgroundQueryResult<TData | undefined, TVariables>;
Expand All @@ -83,10 +71,7 @@ export function useBackgroundQuery<
TVariables extends OperationVariables = OperationVariables
>(
query: DocumentNode | TypedDocumentNode<TData, TVariables>,
options: Omit<
SuspenseQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>,
'returnPartialData' | 'refetchWritePolicy'
> & {
options: BackgroundQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>> & {
skip: boolean;
}
): UseBackgroundQueryResult<TData | undefined, TVariables>;
Expand All @@ -99,10 +84,7 @@ export function useBackgroundQuery<
// TVariables extends OperationVariables = OperationVariables
// >(
// query: DocumentNode | TypedDocumentNode<TData, TVariables>,
// options: Omit<
// SuspenseQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>,
// 'returnPartialData' | 'refetchWritePolicy'
// > & {
// options: SuspenseQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>> & {
// returnPartialData: true;
// }
// ): UseBackgroundQueryResult<DeepPartial<TData>, TVariables>;
Expand All @@ -112,20 +94,17 @@ export function useBackgroundQuery<
TVariables extends OperationVariables = OperationVariables
>(
query: DocumentNode | TypedDocumentNode<TData, TVariables>,
options?: Omit<
SuspenseQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>,
'returnPartialData' | 'refetchWritePolicy'
>
options?: BackgroundQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>
): UseBackgroundQueryResult<TData, TVariables>;

export function useBackgroundQuery<
TData = unknown,
TVariables extends OperationVariables = OperationVariables
>(
query: DocumentNode | TypedDocumentNode<TData, TVariables>,
options: Omit<
SuspenseQueryHookOptions<TData, TVariables>,
'returnPartialData' | 'refetchWritePolicy'
options: BackgroundQueryHookOptions<
NoInfer<TData>,
NoInfer<TVariables>
> = Object.create(null)
): UseBackgroundQueryResult<TData> {
const suspenseCache = useSuspenseCache(options.suspenseCache);
Expand Down
25 changes: 25 additions & 0 deletions src/react/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TData, TVariables>,
| 'client'
| 'variables'
| 'errorPolicy'
| 'context'
| 'canonizeResults'
| 'skip'
> {
fetchPolicy?: BackgroundQueryHookFetchPolicy;
suspenseCache?: SuspenseCache;
queryKey?: string | number | any[];
}

/**
* @deprecated TODO Delete this unused interface.
*/
Expand Down

0 comments on commit 5618953

Please sign in to comment.