Skip to content

Commit c2d0dff

Browse files
fix(vue-query): allow computed ref as enabled property in queryOptions (#10465)
Fixes #10458 The enabled property in QueryOptions type was incorrectly restricted to only accept getter functions (() => Enabled). This prevented using computed refs and other Vue reactive values. This change aligns QueryOptions.enabled type with UseQueryOptions.enabled, allowing: - boolean values - Ref<boolean> - ComputedRef<boolean> - () => boolean getter functions - (query) => boolean query predicates Co-authored-by: Damian Osipiuk <osipiukd+git@gmail.com>
1 parent 08a4d2b commit c2d0dff

File tree

3 files changed

+69
-3
lines changed

3 files changed

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

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assertType, describe, expectTypeOf, it } from 'vitest'
2-
import { reactive, ref } from 'vue-demi'
2+
import { computed, reactive, ref } from 'vue-demi'
33
import { dataTagSymbol } from '@tanstack/query-core'
44
import { queryKey } from '@tanstack/query-test-utils'
55
import { QueryClient } from '../queryClient'
@@ -253,4 +253,49 @@ describe('queryOptions', () => {
253253
expectTypeOf(resolvedGetter.queryFn).not.toBeUndefined()
254254
expectTypeOf(resolvedGetter.queryKey).not.toBeUndefined()
255255
})
256+
257+
it('should allow computed ref as enabled property', () => {
258+
const enabled = computed(() => true)
259+
260+
// This was broken in #10452, fixed in #10458
261+
const options = queryOptions({
262+
queryKey: ['key'],
263+
queryFn: () => Promise.resolve(1),
264+
enabled,
265+
})
266+
267+
expectTypeOf(options.queryKey).not.toBeUndefined()
268+
})
269+
270+
it('should allow ref as enabled property', () => {
271+
const enabled = ref(true)
272+
273+
const options = queryOptions({
274+
queryKey: ['key'],
275+
queryFn: () => Promise.resolve(1),
276+
enabled,
277+
})
278+
279+
expectTypeOf(options.queryKey).not.toBeUndefined()
280+
})
281+
282+
it('should allow boolean as enabled property', () => {
283+
const options = queryOptions({
284+
queryKey: ['key'],
285+
queryFn: () => Promise.resolve(1),
286+
enabled: true,
287+
})
288+
289+
expectTypeOf(options.queryKey).not.toBeUndefined()
290+
})
291+
292+
it('should allow getter function as enabled property', () => {
293+
const options = queryOptions({
294+
queryKey: ['key'],
295+
queryFn: () => Promise.resolve(1),
296+
enabled: () => true,
297+
})
298+
299+
expectTypeOf(options.queryKey).not.toBeUndefined()
300+
})
256301
})

packages/vue-query/src/queryOptions.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { DeepUnwrapRef, ShallowOption } from './types'
1+
import type { DeepUnwrapRef, MaybeRefOrGetter, ShallowOption } from './types'
22
import type {
33
DataTag,
44
DefaultError,
@@ -23,7 +23,14 @@ export type QueryOptions<
2323
TQueryData,
2424
TQueryKey
2525
>]: Property extends 'enabled'
26-
? () => Enabled<TQueryFnData, TError, TQueryData, DeepUnwrapRef<TQueryKey>>
26+
?
27+
| MaybeRefOrGetter<boolean | undefined>
28+
| (() => Enabled<
29+
TQueryFnData,
30+
TError,
31+
TQueryData,
32+
DeepUnwrapRef<TQueryKey>
33+
>)
2734
: QueryObserverOptions<
2835
TQueryFnData,
2936
TError,

0 commit comments

Comments
 (0)