Skip to content

Commit 181b904

Browse files
committed
fix(vue-query): allow computed ref as queryKey property in queryOptions
Closes #10525
1 parent 5b5d1db commit 181b904

3 files changed

Lines changed: 50 additions & 8 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@tanstack/vue-query': patch
3+
---
4+
5+
fix(vue-query): allow computed ref and other reactive values as `queryKey` property in `queryOptions`
6+
7+
This fixes a regression introduced in #10452 where `queryOptions` only accepted plain arrays for the `queryKey` property, but not `computed` refs, `Ref` values, or other reactive values. The related fix in #10465 only covered the `enabled` property.
8+
9+
Now the `queryKey` property in `queryOptions` correctly accepts:
10+
- Plain `QueryKey` arrays
11+
- `Ref<QueryKey>`
12+
- `ComputedRef<QueryKey>`

packages/vue-query/src/__tests__/queryOptions.test-d.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,27 @@ describe('queryOptions', () => {
298298

299299
expectTypeOf(options.queryKey).not.toBeUndefined()
300300
})
301+
302+
it('should allow computed ref as queryKey', () => {
303+
const id = ref<string | null>('1')
304+
305+
// This was broken in #10452, the #10465 fix only covered `enabled`
306+
const options = queryOptions({
307+
queryKey: computed(() => ['foo', id.value] as const),
308+
queryFn: () => Promise.resolve({ id: '1' }),
309+
})
310+
311+
expectTypeOf(options.queryKey).not.toBeUndefined()
312+
})
313+
314+
it('should allow ref as queryKey', () => {
315+
const keyRef = ref(['foo', '1'] as const)
316+
317+
const options = queryOptions({
318+
queryKey: keyRef,
319+
queryFn: () => Promise.resolve({ id: '1' }),
320+
})
321+
322+
expectTypeOf(options.queryKey).not.toBeUndefined()
323+
})
301324
})

packages/vue-query/src/queryOptions.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import type { DeepUnwrapRef, MaybeRefOrGetter, ShallowOption } from './types'
1+
import type {
2+
DeepUnwrapRef,
3+
MaybeRef,
4+
MaybeRefOrGetter,
5+
ShallowOption,
6+
} from './types'
27
import type {
38
DataTag,
49
DefaultError,
@@ -31,13 +36,15 @@ export type QueryOptions<
3136
TQueryData,
3237
DeepUnwrapRef<TQueryKey>
3338
>)
34-
: QueryObserverOptions<
35-
TQueryFnData,
36-
TError,
37-
TData,
38-
TQueryData,
39-
DeepUnwrapRef<TQueryKey>
40-
>[Property]
39+
: Property extends 'queryKey'
40+
? MaybeRef<TQueryKey>
41+
: QueryObserverOptions<
42+
TQueryFnData,
43+
TError,
44+
TData,
45+
TQueryData,
46+
DeepUnwrapRef<TQueryKey>
47+
>[Property]
4148
} & ShallowOption
4249

4350
export type UndefinedInitialQueryOptions<

0 commit comments

Comments
 (0)