Skip to content

feat(query-core, react-query, vue-query): backport v5 some apis to v4 #9140

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

Merged
merged 1 commit into from
May 26, 2025
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
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v16.19.0
22.12.0
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0",
"expect-type": "^1.2.1",
"git-log-parser": "^1.2.0",
"jest": "^27.5.1",
"jsonfile": "^6.1.0",
Expand Down
4 changes: 3 additions & 1 deletion packages/query-core/src/mutationObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,16 @@ export class MutationObserver<
? this.currentMutation.state
: getDefaultState<TData, TError, TVariables, TContext>()

const isLoading = state.status === 'loading'
const result: MutationObserverBaseResult<
TData,
TError,
TVariables,
TContext
> = {
...state,
isLoading: state.status === 'loading',
isLoading,
isPending: isLoading,
Comment on lines +152 to +153
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We can use isPending like v5

isSuccess: state.status === 'success',
isError: state.status === 'error',
isIdle: state.status === 'idle',
Expand Down
43 changes: 37 additions & 6 deletions packages/query-core/src/queryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { notifyManager } from './notifyManager'
import { Subscribable } from './subscribable'
import type { QueryFilters } from './utils'
import type { Action, QueryState } from './query'
import type { NotifyEvent, QueryKey, QueryOptions } from './types'
import type { NotifyEvent, OmitKeyof, QueryKey, QueryOptions } from './types'
import type { QueryClient } from './queryClient'
import type { QueryObserver } from './queryObserver'

Expand Down Expand Up @@ -166,8 +166,21 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
}

find<TQueryFnData = unknown, TError = unknown, TData = TQueryFnData>(
arg1: QueryKey,
arg2?: QueryFilters,
filters: QueryFilters,
): Query<TQueryFnData, TError, TData> | undefined
Comment on lines 168 to +170
Copy link
Collaborator Author

@manudeli manudeli May 25, 2025

Choose a reason for hiding this comment

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

I added a function overload accepting only a filters object. This could make migration to v5 easier. In v5, QueryCache.find only accepts a filters object

/**
* @deprecated This method should be used with only one object argument.
*/
find<TQueryFnData = unknown, TError = unknown, TData = TQueryFnData>(
queryKey: QueryKey,
filters?: OmitKeyof<QueryFilters, 'queryKey'>,
): Query<TQueryFnData, TError, TData> | undefined
/**
* @deprecated This method should be used with only one object argument.
*/
find<TQueryFnData = unknown, TError = unknown, TData = TQueryFnData>(
arg1: QueryKey | QueryFilters,
arg2?: OmitKeyof<QueryFilters, 'queryKey'>,
Comment on lines +171 to +183
Copy link
Collaborator Author

@manudeli manudeli May 25, 2025

Choose a reason for hiding this comment

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

I tagged jsdoc @deprecated on unnecessary type overriding of QueryCache's methods

): Query<TQueryFnData, TError, TData> | undefined {
const [filters] = parseFilterArgs(arg1, arg2)

Expand All @@ -178,10 +191,28 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
return this.queries.find((query) => matchQuery(filters, query))
}

findAll(queryKey?: QueryKey, filters?: QueryFilters): Query[]
findAll(filters?: QueryFilters): Query[]
findAll(arg1?: QueryKey | QueryFilters, arg2?: QueryFilters): Query[]
findAll(arg1?: QueryKey | QueryFilters, arg2?: QueryFilters): Query[] {
/**
* @deprecated This method should be used with only one object argument.
*/
findAll(
queryKey?: QueryKey,
filters?: OmitKeyof<QueryFilters, 'queryKey'>,
): Query[]
/**
* @deprecated This method should be used with only one object argument.
*/
findAll(
arg1?: QueryKey | QueryFilters,
arg2?: OmitKeyof<QueryFilters, 'queryKey'>,
): Query[]
/**
* @deprecated This method should be used with only one object argument.
*/
findAll(
arg1?: QueryKey | QueryFilters,
arg2?: OmitKeyof<QueryFilters, 'queryKey'>,
): Query[] {
Comment on lines +195 to +215
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tagged jsdoc @deprecated on unnecessary type overriding of QueryCache's methods

const [filters] = parseFilterArgs(arg1, arg2)
return Object.keys(filters).length > 0
? this.queries.filter((query) => matchQuery(filters, query))
Expand Down
Loading
Loading