Skip to content

Commit fdb8ce1

Browse files
ShacharHarshuvTkDodoautofix-ci[bot]
authored
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 <office@dorfmeister.cc> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 5ab13b9 commit fdb8ce1

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

packages/angular-query-experimental/src/__tests__/query-options.test-d.ts

+19
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ describe('queryOptions', () => {
2222
},
2323
})
2424
})
25+
26+
test('should allow undefined response in initialData', () => {
27+
return (id: string | null) =>
28+
queryOptions({
29+
queryKey: ['todo', id],
30+
queryFn: () =>
31+
Promise.resolve({
32+
id: '1',
33+
title: 'Do Laundry',
34+
}),
35+
initialData: () =>
36+
!id
37+
? undefined
38+
: {
39+
id,
40+
title: 'Initial Data',
41+
},
42+
})
43+
})
2544
})
2645

2746
test('should work when passed to injectQuery', () => {

packages/angular-query-experimental/src/query-options.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import type { DataTag, DefaultError, QueryKey } from '@tanstack/query-core'
1+
import type {
2+
DataTag,
3+
DefaultError,
4+
InitialDataFunction,
5+
QueryKey,
6+
} from '@tanstack/query-core'
27
import type { CreateQueryOptions, NonUndefinedGuard } from './types'
38

49
/**
@@ -10,7 +15,7 @@ export type UndefinedInitialDataOptions<
1015
TData = TQueryFnData,
1116
TQueryKey extends QueryKey = QueryKey,
1217
> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
13-
initialData?: undefined
18+
initialData?: undefined | InitialDataFunction<NonUndefinedGuard<TQueryFnData>>
1419
}
1520

1621
/**

packages/react-query/src/__tests__/queryOptions.test-d.tsx

+19
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,23 @@ describe('queryOptions', () => {
186186
QueriesObserver<Array<QueryObserverResult>>
187187
>()
188188
})
189+
190+
it('should allow undefined response in initialData', () => {
191+
return (id: string | null) =>
192+
queryOptions({
193+
queryKey: ['todo', id],
194+
queryFn: () =>
195+
Promise.resolve({
196+
id: '1',
197+
title: 'Do Laundry',
198+
}),
199+
initialData: () =>
200+
!id
201+
? undefined
202+
: {
203+
id,
204+
title: 'Initial Data',
205+
},
206+
})
207+
})
189208
})

packages/react-query/src/queryOptions.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import type { DataTag, DefaultError, QueryKey } from '@tanstack/query-core'
1+
import type {
2+
DataTag,
3+
DefaultError,
4+
InitialDataFunction,
5+
QueryKey,
6+
} from '@tanstack/query-core'
27
import type { UseQueryOptions } from './types'
38

49
export type UndefinedInitialDataOptions<
@@ -7,7 +12,7 @@ export type UndefinedInitialDataOptions<
712
TData = TQueryFnData,
813
TQueryKey extends QueryKey = QueryKey,
914
> = UseQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
10-
initialData?: undefined
15+
initialData?: undefined | InitialDataFunction<NonUndefinedGuard<TQueryFnData>>
1116
}
1217

1318
type NonUndefinedGuard<T> = T extends undefined ? never : T

0 commit comments

Comments
 (0)