Skip to content

Commit 49243c8

Browse files
hriday330autofix-ci[bot]DamianOsipiuk
authored
fix(vue-query): Support infiniteQueryOptions for MaybeRef argument (#9634)
* test(vue-query): add type-check test for useInfiniteQuery with infiniteQueryOptions * fix(vue-query): wrap useInfiniteQuery options in MaybeRefOrGetter * remove type problem example * test(vue-query): add test for plain options getter * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Damian Osipiuk <osipiukd+git@gmail.com>
1 parent cd4ef5c commit 49243c8

File tree

3 files changed

+61
-26
lines changed

3 files changed

+61
-26
lines changed

packages/vue-query/src/__tests__/useInfiniteQuery.test-d.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expectTypeOf, it } from 'vitest'
22
import { computed, reactive } from 'vue-demi'
33
import { sleep } from '@tanstack/query-test-utils'
44
import { useInfiniteQuery } from '../useInfiniteQuery'
5+
import { infiniteQueryOptions } from '../infiniteQueryOptions'
56
import type { InfiniteData } from '@tanstack/query-core'
67

78
describe('Discriminated union return type', () => {
@@ -95,4 +96,35 @@ describe('Discriminated union return type', () => {
9596
expectTypeOf(query.data).toEqualTypeOf<InfiniteData<string, unknown>>()
9697
}
9798
})
99+
100+
it('should accept computed options using infiniteQueryOptions', () => {
101+
const options = computed(() =>
102+
infiniteQueryOptions({
103+
queryKey: ['infiniteQuery'],
104+
queryFn: () => sleep(0).then(() => 'Some data'),
105+
getNextPageParam: () => undefined,
106+
initialPageParam: 0,
107+
}),
108+
)
109+
const query = reactive(useInfiniteQuery(options))
110+
111+
if (query.isSuccess) {
112+
expectTypeOf(query.data).toEqualTypeOf<InfiniteData<string, unknown>>()
113+
}
114+
})
115+
116+
it('should accept plain options using infiniteQueryOptions', () => {
117+
const options = () =>
118+
infiniteQueryOptions({
119+
queryKey: ['infiniteQuery'],
120+
queryFn: () => sleep(0).then(() => 'Some data'),
121+
getNextPageParam: () => undefined,
122+
initialPageParam: 0,
123+
})
124+
const query = reactive(useInfiniteQuery(options))
125+
126+
if (query.isSuccess) {
127+
expectTypeOf(query.data).toEqualTypeOf<InfiniteData<string, unknown>>()
128+
}
129+
})
98130
})

packages/vue-query/src/useBaseQuery.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type {
2222
import type { QueryClient } from './queryClient'
2323
import type { UseQueryOptions } from './useQuery'
2424
import type { UseInfiniteQueryOptions } from './useInfiniteQuery'
25+
import type { MaybeRefOrGetter } from './types'
2526

2627
export type UseBaseQueryReturnType<
2728
TData,
@@ -58,13 +59,15 @@ export function useBaseQuery<
5859
TPageParam,
5960
>(
6061
Observer: typeof QueryObserver,
61-
options: UseQueryOptionsGeneric<
62-
TQueryFnData,
63-
TError,
64-
TData,
65-
TQueryData,
66-
TQueryKey,
67-
TPageParam
62+
options: MaybeRefOrGetter<
63+
UseQueryOptionsGeneric<
64+
TQueryFnData,
65+
TError,
66+
TData,
67+
TQueryData,
68+
TQueryKey,
69+
TPageParam
70+
>
6871
>,
6972
queryClient?: QueryClient,
7073
): UseBaseQueryReturnType<TData, TError> {

packages/vue-query/src/useInfiniteQuery.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,14 @@ export function useInfiniteQuery<
7272
TQueryKey extends QueryKey = QueryKey,
7373
TPageParam = unknown,
7474
>(
75-
options: DefinedInitialDataInfiniteOptions<
76-
TQueryFnData,
77-
TError,
78-
TData,
79-
TQueryKey,
80-
TPageParam
75+
options: MaybeRefOrGetter<
76+
DefinedInitialDataInfiniteOptions<
77+
TQueryFnData,
78+
TError,
79+
TData,
80+
TQueryKey,
81+
TPageParam
82+
>
8183
>,
8284
queryClient?: QueryClient,
8385
): UseInfiniteQueryReturnType<TData, TError>
@@ -89,12 +91,14 @@ export function useInfiniteQuery<
8991
TQueryKey extends QueryKey = QueryKey,
9092
TPageParam = unknown,
9193
>(
92-
options: UndefinedInitialDataInfiniteOptions<
93-
TQueryFnData,
94-
TError,
95-
TData,
96-
TQueryKey,
97-
TPageParam
94+
options: MaybeRefOrGetter<
95+
UndefinedInitialDataInfiniteOptions<
96+
TQueryFnData,
97+
TError,
98+
TData,
99+
TQueryKey,
100+
TPageParam
101+
>
98102
>,
99103
queryClient?: QueryClient,
100104
): UseInfiniteQueryReturnType<TData, TError>
@@ -106,18 +110,14 @@ export function useInfiniteQuery<
106110
TQueryKey extends QueryKey = QueryKey,
107111
TPageParam = unknown,
108112
>(
109-
options: UseInfiniteQueryOptions<
110-
TQueryFnData,
111-
TError,
112-
TData,
113-
TQueryKey,
114-
TPageParam
113+
options: MaybeRefOrGetter<
114+
UseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>
115115
>,
116116
queryClient?: QueryClient,
117117
): UseInfiniteQueryReturnType<TData, TError>
118118

119119
export function useInfiniteQuery(
120-
options: UseInfiniteQueryOptions,
120+
options: MaybeRefOrGetter<UseInfiniteQueryOptions>,
121121
queryClient?: QueryClient,
122122
) {
123123
return useBaseQuery(

0 commit comments

Comments
 (0)