Skip to content

Commit

Permalink
fix(angular-query-experimental): allow returning undefined in initial…
Browse files Browse the repository at this point in the history
…Data 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.
  • Loading branch information
ShacharHarshuv committed Apr 29, 2024
1 parent 7368bd0 commit f579962
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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', () => {
Expand Down
9 changes: 7 additions & 2 deletions packages/angular-query-experimental/src/query-options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { DataTag, DefaultError, QueryKey } from '@tanstack/query-core'
import {
DataTag,
DefaultError,
QueryKey,
InitialDataFunction,
} from '@tanstack/query-core'
import type { CreateQueryOptions } from './types'

export type UndefinedInitialDataOptions<
Expand All @@ -20,7 +25,7 @@ export type DefinedInitialDataOptions<
> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
initialData:
| NonUndefinedGuard<TQueryFnData>
| (() => NonUndefinedGuard<TQueryFnData>)
| InitialDataFunction<NonUndefinedGuard<TQueryFnData>>
}

export function queryOptions<
Expand Down

0 comments on commit f579962

Please sign in to comment.