Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vue-query): ensure that invalidateQueries trigger queryFn with updated ref values #6561

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/vue-query/src/__tests__/queryClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, expect, test, vi } from 'vitest'
import { ref } from 'vue-demi'
import { QueryClient as QueryClientOrigin } from '@tanstack/query-core'
import { QueryClient } from '../queryClient'
import { flushPromises } from './test-utils'

vi.mock('@tanstack/query-core')

Expand Down Expand Up @@ -196,6 +197,8 @@ describe('QueryCache', () => {
{ cancelRefetch: ref(false) },
)

await flushPromises()

expect(QueryClientOrigin.prototype.invalidateQueries).toBeCalledWith(
{
queryKey: queryKeyUnref,
Expand Down
15 changes: 11 additions & 4 deletions packages/vue-query/src/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,17 @@ export class QueryClient extends QC {
filters: MaybeRefDeep<InvalidateQueryFilters> = {},
options: MaybeRefDeep<InvalidateOptions> = {},
): Promise<void> {
return super.invalidateQueries(
cloneDeepUnref(filters),
cloneDeepUnref(options),
)
// (dosipiuk): We need to delay `invalidate` execution to next macro task for all reactive values to be updated.
// This ensures that `context` in `queryFn` while `invalidating` along reactive variable change has correct value.
return new Promise((resolve) => {
setTimeout(async () => {
await super.invalidateQueries(
cloneDeepUnref(filters),
cloneDeepUnref(options),
)
resolve()
}, 0)
})
}

refetchQueries(
Expand Down