From fdb8ce1c38bc30fec9e984218b2fdc7802aef695 Mon Sep 17 00:00:00 2001 From: Shahar Har-Shuv Date: Thu, 1 Aug 2024 09:33:13 -0400 Subject: [PATCH] fix(query-options): allow returning undefined in initialData function (#7351) * Fix PR * Fix for react adapter * ci: apply automated fixes --------- Co-authored-by: Dominik Dorfmeister Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- .../src/__tests__/query-options.test-d.ts | 19 +++++++++++++++++++ .../src/query-options.ts | 9 +++++++-- .../src/__tests__/queryOptions.test-d.tsx | 19 +++++++++++++++++++ packages/react-query/src/queryOptions.ts | 9 +++++++-- 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/packages/angular-query-experimental/src/__tests__/query-options.test-d.ts b/packages/angular-query-experimental/src/__tests__/query-options.test-d.ts index f6c63fdce9..0d49180b4a 100644 --- a/packages/angular-query-experimental/src/__tests__/query-options.test-d.ts +++ b/packages/angular-query-experimental/src/__tests__/query-options.test-d.ts @@ -22,6 +22,25 @@ describe('queryOptions', () => { }, }) }) + + test('should allow undefined response in initialData', () => { + return (id: string | null) => + queryOptions({ + queryKey: ['todo', id], + queryFn: () => + Promise.resolve({ + id: '1', + title: 'Do Laundry', + }), + initialData: () => + !id + ? undefined + : { + id, + title: 'Initial Data', + }, + }) + }) }) test('should work when passed to injectQuery', () => { diff --git a/packages/angular-query-experimental/src/query-options.ts b/packages/angular-query-experimental/src/query-options.ts index 6f89bea03a..4bc4233ba2 100644 --- a/packages/angular-query-experimental/src/query-options.ts +++ b/packages/angular-query-experimental/src/query-options.ts @@ -1,4 +1,9 @@ -import type { DataTag, DefaultError, QueryKey } from '@tanstack/query-core' +import type { + DataTag, + DefaultError, + InitialDataFunction, + QueryKey, +} from '@tanstack/query-core' import type { CreateQueryOptions, NonUndefinedGuard } from './types' /** @@ -10,7 +15,7 @@ export type UndefinedInitialDataOptions< TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, > = CreateQueryOptions & { - initialData?: undefined + initialData?: undefined | InitialDataFunction> } /** diff --git a/packages/react-query/src/__tests__/queryOptions.test-d.tsx b/packages/react-query/src/__tests__/queryOptions.test-d.tsx index f019d883b3..03958a63a0 100644 --- a/packages/react-query/src/__tests__/queryOptions.test-d.tsx +++ b/packages/react-query/src/__tests__/queryOptions.test-d.tsx @@ -186,4 +186,23 @@ describe('queryOptions', () => { QueriesObserver> >() }) + + it('should allow undefined response in initialData', () => { + return (id: string | null) => + queryOptions({ + queryKey: ['todo', id], + queryFn: () => + Promise.resolve({ + id: '1', + title: 'Do Laundry', + }), + initialData: () => + !id + ? undefined + : { + id, + title: 'Initial Data', + }, + }) + }) }) diff --git a/packages/react-query/src/queryOptions.ts b/packages/react-query/src/queryOptions.ts index 5eae0e4c23..ec1c11aa2c 100644 --- a/packages/react-query/src/queryOptions.ts +++ b/packages/react-query/src/queryOptions.ts @@ -1,4 +1,9 @@ -import type { DataTag, DefaultError, QueryKey } from '@tanstack/query-core' +import type { + DataTag, + DefaultError, + InitialDataFunction, + QueryKey, +} from '@tanstack/query-core' import type { UseQueryOptions } from './types' export type UndefinedInitialDataOptions< @@ -7,7 +12,7 @@ export type UndefinedInitialDataOptions< TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, > = UseQueryOptions & { - initialData?: undefined + initialData?: undefined | InitialDataFunction> } type NonUndefinedGuard = T extends undefined ? never : T