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(query-core): fix to show type error when queryKey is a wrong type in invalidateQueries #8691

Closed
wants to merge 2 commits into from
Closed
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
20 changes: 20 additions & 0 deletions packages/query-core/src/__tests__/queryClient.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -496,4 +496,24 @@ describe('fully typed usage', () => {
queryClient.setQueryDefaults(queryKey, {} as any)
queryClient.getMutationDefaults(mutationKey)
})

it('shows type error when queryKey is a wrong type in invalidateQueries', () => {
const queryClient = new QueryClient()

queryClient.invalidateQueries()

queryClient.invalidateQueries({
queryKey: ['1'],
})

queryClient.invalidateQueries({
// @ts-expect-error
queryKey: '1',
})

queryClient.invalidateQueries({
// @ts-expect-error
queryKey: {},
})
})
Comment on lines +500 to +518
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve added this test to:

where I’m also re-writing the types for invalidateQueries and other methods.

the test passes there as well, so I’ll close this PR to avoid the conflicts.

})
21 changes: 12 additions & 9 deletions packages/query-core/src/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,25 +336,28 @@ export class QueryClient {
}

invalidateQueries<
TInvalidateQueryFilters extends InvalidateQueryFilters<
any,
any,
any,
any
> = InvalidateQueryFilters,
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
filters?: TInvalidateQueryFilters,
filters?: InvalidateQueryFilters<TQueryFnData, TError, TData, TQueryKey>,
options: InvalidateOptions = {},
): Promise<void> {
return notifyManager.batch(() => {
this.#queryCache.findAll(filters).forEach((query) => {
this.#queryCache.findAll(filters as QueryFilters).forEach((query) => {
query.invalidate()
})

if (filters?.refetchType === 'none') {
return Promise.resolve()
}
const refetchFilters: RefetchQueryFilters = {
const refetchFilters: RefetchQueryFilters<
TQueryFnData,
TError,
TData,
TQueryKey
> = {
...filters,
type: filters?.refetchType ?? filters?.type ?? 'active',
}
Expand Down
40 changes: 37 additions & 3 deletions packages/vue-query/src/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,37 @@
return super.cancelQueries(cloneDeepUnref(filters), cloneDeepUnref(options))
}

invalidateQueries(
filters: MaybeRefDeep<InvalidateQueryFilters> = {},
invalidateQueries<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
filters?: InvalidateQueryFilters<TQueryFnData, TError, TData, TQueryKey>,
options?: InvalidateOptions,
): Promise<void>

invalidateQueries<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
filters?: MaybeRefDeep<
InvalidateQueryFilters<TQueryFnData, TError, TData, TQueryKey>
>,
options?: MaybeRefDeep<InvalidateOptions>,
): Promise<void>

invalidateQueries<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
filters: MaybeRefDeep<
InvalidateQueryFilters<TQueryFnData, TError, TData, TQueryKey>
> = {},

Check warning on line 204 in packages/vue-query/src/queryClient.ts

View check run for this annotation

Codecov / codecov/patch

packages/vue-query/src/queryClient.ts#L204

Added line #L204 was not covered by tests
options: MaybeRefDeep<InvalidateOptions> = {},
): Promise<void> {
const filtersCloned = cloneDeepUnref(filters)
Expand All @@ -187,7 +216,12 @@
return Promise.resolve()
}

const refetchFilters: RefetchQueryFilters = {
const refetchFilters: RefetchQueryFilters<
TQueryFnData,
TError,
TData,
TQueryKey
> = {
...filtersCloned,
type: filtersCloned.refetchType ?? filtersCloned.type ?? 'active',
}
Expand Down
Loading