Skip to content

Commit 139e67c

Browse files
KimHyeongRae0TkDodoDamianOsipiuk
authored
fix(vue-query): allow computed ref as queryKey property in queryOptions (#10530)
* fix(vue-query): allow computed ref as queryKey property in queryOptions Closes #10525 * fix(vue-query): widen queryKey to MaybeRefOrGetter to match enabled toValueDeep already calls `() => T` at runtime, so the queryKey type should accept the same reactive forms as enabled: Ref, ComputedRef, and getters. Adds a getter test and updates the changeset. --------- Co-authored-by: Dominik Dorfmeister 🔮 <office@dorfmeister.cc> Co-authored-by: Damian Osipiuk <osipiukd+git@gmail.com>
1 parent 299447b commit 139e67c

3 files changed

Lines changed: 56 additions & 7 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
'@tanstack/vue-query': patch
3+
---
4+
5+
fix(vue-query): allow reactive and getter 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 getter functions. The related fix in #10465 only covered the `enabled` property.
8+
9+
Now the `queryKey` property in `queryOptions` accepts the same reactive forms as `enabled`:
10+
- Plain `QueryKey` arrays
11+
- `Ref<QueryKey>`
12+
- `ComputedRef<QueryKey>`
13+
- `() => QueryKey` (getter)

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,38 @@ 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+
})
324+
325+
it('should allow getter function as queryKey', () => {
326+
const id = ref<string | null>('1')
327+
328+
const options = queryOptions({
329+
queryKey: () => ['foo', id.value] as const,
330+
queryFn: () => Promise.resolve({ id: '1' }),
331+
})
332+
333+
expectTypeOf(options.queryKey).not.toBeUndefined()
334+
})
301335
})

packages/vue-query/src/queryOptions.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ export type QueryOptions<
3131
TQueryData,
3232
DeepUnwrapRef<TQueryKey>
3333
>)
34-
: QueryObserverOptions<
35-
TQueryFnData,
36-
TError,
37-
TData,
38-
TQueryData,
39-
DeepUnwrapRef<TQueryKey>
40-
>[Property]
34+
: Property extends 'queryKey'
35+
? MaybeRefOrGetter<TQueryKey>
36+
: QueryObserverOptions<
37+
TQueryFnData,
38+
TError,
39+
TData,
40+
TQueryData,
41+
DeepUnwrapRef<TQueryKey>
42+
>[Property]
4143
} & ShallowOption
4244

4345
export type UndefinedInitialQueryOptions<

0 commit comments

Comments
 (0)