Skip to content

Commit cbda68d

Browse files
fix(vue-query): Make query options watchers sync (#5929)
* fix: make watcher for queryOptions sync to reflect changes to them immediately * fix: make option watchers sync for other composibles * test: improve test for dependent queries * test: update test to check for dependent query to be called with the correct key * docs: fix example to use queryKey instead of reactive variable * test: resolve mock queryFn * test: keep fetch status checks in test for dependent queries --------- Co-authored-by: Damian Osipiuk <osipiukd+git@gmail.com>
1 parent 744e5ea commit cbda68d

File tree

7 files changed

+74
-25
lines changed

7 files changed

+74
-25
lines changed

examples/vue/dependent-queries/src/Post.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ export default defineComponent({
3636
3737
const { data: author } = useQuery(
3838
['author', authorId],
39-
() => fetchAuthor(authorId.value),
39+
({ queryKey: [, id] }) => fetchAuthor(id),
4040
{
41-
enabled: computed(() => !!post.value?.userId),
41+
enabled: computed(() => !!authorId.value),
4242
},
4343
)
4444

packages/vue-query/src/__tests__/useQuery.test.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ describe('useQuery', () => {
146146
})
147147

148148
secondKeyRef.value = 'key8'
149-
await flushPromises()
150-
151149
expect(query).toMatchObject({
152150
status: { value: 'loading' },
153151
data: { value: undefined },
@@ -172,9 +170,6 @@ describe('useQuery', () => {
172170
})
173171

174172
enabled.value = true
175-
176-
await flushPromises()
177-
178173
expect(query).toMatchObject({
179174
fetchStatus: { value: 'fetching' },
180175
data: { value: undefined },
@@ -192,16 +187,18 @@ describe('useQuery', () => {
192187

193188
const enabled = computed(() => !!data.value)
194189

190+
const dependentQueryFn = jest.fn().mockImplementation(simpleFetcher)
195191
const { fetchStatus, status } = useQuery(
196192
['dependant2'],
197-
simpleFetcher,
193+
dependentQueryFn,
198194
reactive({
199195
enabled,
200196
}),
201197
)
202198

203199
expect(data.value).toStrictEqual(undefined)
204200
expect(fetchStatus.value).toStrictEqual('idle')
201+
expect(dependentQueryFn).not.toHaveBeenCalled()
205202

206203
await flushPromises()
207204

@@ -212,6 +209,10 @@ describe('useQuery', () => {
212209

213210
expect(fetchStatus.value).toStrictEqual('idle')
214211
expect(status.value).toStrictEqual('success')
212+
expect(dependentQueryFn).toHaveBeenCalledTimes(1)
213+
expect(dependentQueryFn).toHaveBeenCalledWith(
214+
expect.objectContaining({ queryKey: ['dependant2'] }),
215+
)
215216
})
216217

217218
test('should stop listening to changes on onScopeDispose', async () => {
@@ -233,6 +234,34 @@ describe('useQuery', () => {
233234
expect(status.value).toStrictEqual('loading')
234235
})
235236

237+
test('should use the current value for the queryKey when refetch is called', async () => {
238+
const fetchFn = jest.fn()
239+
const keyRef = ref('key11')
240+
const query = useQuery({
241+
queryKey: ['key10', keyRef],
242+
queryFn: fetchFn,
243+
enabled: false,
244+
})
245+
246+
expect(fetchFn).not.toHaveBeenCalled()
247+
await query.refetch()
248+
expect(fetchFn).toHaveBeenCalledTimes(1)
249+
expect(fetchFn).toHaveBeenCalledWith(
250+
expect.objectContaining({
251+
queryKey: ['key10', 'key11'],
252+
}),
253+
)
254+
255+
keyRef.value = 'key12'
256+
await query.refetch()
257+
expect(fetchFn).toHaveBeenCalledTimes(2)
258+
expect(fetchFn).toHaveBeenCalledWith(
259+
expect.objectContaining({
260+
queryKey: ['key10', 'key12'],
261+
}),
262+
)
263+
})
264+
236265
describe('errorBoundary', () => {
237266
test('should evaluate useErrorBoundary when query is expected to throw', async () => {
238267
const boundaryFn = jest.fn()

packages/vue-query/src/useBaseQuery.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,14 @@ export function useBaseQuery<
102102
{ immediate: true },
103103
)
104104

105-
watch(defaultedOptions, () => {
106-
observer.setOptions(defaultedOptions.value)
107-
updateState(state, observer.getCurrentResult())
108-
})
105+
watch(
106+
defaultedOptions,
107+
() => {
108+
observer.setOptions(defaultedOptions.value)
109+
updateState(state, observer.getCurrentResult())
110+
},
111+
{ flush: 'sync' },
112+
)
109113

110114
onScopeDispose(() => {
111115
unsubscribe()

packages/vue-query/src/useIsFetching.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ export function useIsFetching(
4242
isFetching.value = queryClient.isFetching(filters)
4343
})
4444

45-
watch(filters, () => {
46-
isFetching.value = queryClient.isFetching(filters)
47-
})
45+
watch(
46+
filters,
47+
() => {
48+
isFetching.value = queryClient.isFetching(filters)
49+
},
50+
{ flush: 'sync' },
51+
)
4852

4953
onScopeDispose(() => {
5054
unsubscribe()

packages/vue-query/src/useIsMutating.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ export function useIsMutating(
4242
isMutating.value = queryClient.isMutating(filters)
4343
})
4444

45-
watch(filters, () => {
46-
isMutating.value = queryClient.isMutating(filters)
47-
})
45+
watch(
46+
filters,
47+
() => {
48+
isMutating.value = queryClient.isMutating(filters)
49+
},
50+
{ flush: 'sync' },
51+
)
4852

4953
onScopeDispose(() => {
5054
unsubscribe()

packages/vue-query/src/useMutation.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,13 @@ export function useMutation<
181181
})
182182
}
183183

184-
watch(options, () => {
185-
observer.setOptions(queryClient.defaultMutationOptions(options.value))
186-
})
184+
watch(
185+
options,
186+
() => {
187+
observer.setOptions(queryClient.defaultMutationOptions(options.value))
188+
},
189+
{ flush: 'sync' },
190+
)
187191

188192
onScopeDispose(() => {
189193
unsubscribe()

packages/vue-query/src/useQueries.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,14 @@ export function useQueries<T extends any[]>({
206206
{ immediate: true },
207207
)
208208

209-
watch(defaultedQueries, () => {
210-
observer.setQueries(defaultedQueries.value)
211-
state.splice(0, state.length, ...observer.getCurrentResult())
212-
})
209+
watch(
210+
defaultedQueries,
211+
() => {
212+
observer.setQueries(defaultedQueries.value)
213+
state.splice(0, state.length, ...observer.getCurrentResult())
214+
},
215+
{ flush: 'sync' },
216+
)
213217

214218
onScopeDispose(() => {
215219
unsubscribe()

0 commit comments

Comments
 (0)