Skip to content

Commit

Permalink
fix(query-options): allow returning undefined in initialData function (
Browse files Browse the repository at this point in the history
…#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>
  • Loading branch information
3 people committed Aug 1, 2024
1 parent 5ab13b9 commit fdb8ce1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
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 type {
DataTag,
DefaultError,
InitialDataFunction,
QueryKey,
} from '@tanstack/query-core'
import type { CreateQueryOptions, NonUndefinedGuard } from './types'

/**
Expand All @@ -10,7 +15,7 @@ export type UndefinedInitialDataOptions<
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
initialData?: undefined
initialData?: undefined | InitialDataFunction<NonUndefinedGuard<TQueryFnData>>
}

/**
Expand Down
19 changes: 19 additions & 0 deletions packages/react-query/src/__tests__/queryOptions.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,23 @@ describe('queryOptions', () => {
QueriesObserver<Array<QueryObserverResult>>
>()
})

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',
},
})
})
})
9 changes: 7 additions & 2 deletions packages/react-query/src/queryOptions.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 type {
DataTag,
DefaultError,
InitialDataFunction,
QueryKey,
} from '@tanstack/query-core'
import type { UseQueryOptions } from './types'

export type UndefinedInitialDataOptions<
Expand All @@ -7,7 +12,7 @@ export type UndefinedInitialDataOptions<
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> = UseQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
initialData?: undefined
initialData?: undefined | InitialDataFunction<NonUndefinedGuard<TQueryFnData>>
}

type NonUndefinedGuard<T> = T extends undefined ? never : T
Expand Down

0 comments on commit fdb8ce1

Please sign in to comment.