Skip to content

Commit f579962

Browse files
fix(angular-query-experimental): allow returning undefined in initialData function
Fix incorrect types that disallowed that. This is necessary because the initialData function can typically read cache synchronously and need to be able to signal to the query if there is a cache miss and an actual fetch needs to be deployed. No breaking changes, any code that worked before should work now as well.
1 parent 7368bd0 commit f579962

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ describe('queryOptions', () => {
2525
},
2626
})
2727
})
28+
29+
test('should allow undefined response in initialData', () => {
30+
return (id: string | null) =>
31+
queryOptions({
32+
queryKey: ['todo', id],
33+
queryFn: () =>
34+
Promise.resolve({
35+
id: '1',
36+
title: 'Do Laundry',
37+
}),
38+
initialData: () =>
39+
!id
40+
? undefined
41+
: {
42+
id,
43+
title: 'Initial Data',
44+
},
45+
})
46+
})
2847
})
2948

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

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

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

49
export type UndefinedInitialDataOptions<
@@ -20,7 +25,7 @@ export type DefinedInitialDataOptions<
2025
> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
2126
initialData:
2227
| NonUndefinedGuard<TQueryFnData>
23-
| (() => NonUndefinedGuard<TQueryFnData>)
28+
| InitialDataFunction<NonUndefinedGuard<TQueryFnData>>
2429
}
2530

2631
export function queryOptions<

0 commit comments

Comments
 (0)