Skip to content

feat(v4): backport some v5 apis to v4 branch #8754

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

Closed
wants to merge 7 commits into from

Conversation

danielpza
Copy link

@danielpza danielpza commented Mar 4, 2025

This is an attempt to backport some of the new api in tanstack query v5 to v4 branch, which should make it easier to developers still on v4 to migrate to v5 by making small incremental changes to the codebase without having to commit to a big change.

Apis to backport:

  • isPending alias to isLoading
  • useSuspenseQuery
  • useSuspenseQueries
  • queryOptions?
  • ...any others?
  • update documentation

Additionally this pull request adds a jsdoc @deprecated label to some of the apis that are removed/renamed in v5 and suggests some alternatives

Compared to v5, no new functionality is added, so when merging back to main discard the changes introduced in this pull request

Copy link

codesandbox-ci bot commented Mar 4, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@danielpza danielpza marked this pull request as ready for review March 4, 2025 21:31
@danielpza
Copy link
Author

Hey @TkDodo / maintainers, hope you are doing good.

Would appreciate some feedback on this, and whether this would be likely accepted or not.

Cheers.

Co-authored-by: Dominik Dorfmeister <[email protected]>
Comment on lines +46 to +54
export interface UseSuspenseQueryOptions<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> extends Omit<
UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
'enabled' | 'suspense' | 'placeholderData' | 'keepPreviousData'
> {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
export interface UseSuspenseQueryOptions<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> extends Omit<
UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
'enabled' | 'suspense' | 'placeholderData' | 'keepPreviousData'
> {}
export interface UseSuspenseQueryOptions<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> extends Omit<
UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
'enabled' | 'suspense' | 'placeholderData' | 'keepPreviousData' | 'useErrorBoundary'
> {}

Comment on lines +17 to +25
return useBaseQuery(
{
...options,
enabled: true,
suspense: true,
placeholderData: undefined,
},
QueryObserver,
) as UseSuspenseQueryResult<TData, TError>
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should avoid allowing useErrorBoundary to be set to false here.

Suggested change
return useBaseQuery(
{
...options,
enabled: true,
suspense: true,
placeholderData: undefined,
},
QueryObserver,
) as UseSuspenseQueryResult<TData, TError>
return useBaseQuery(
{
...options,
enabled: true,
suspense: true,
placeholderData: undefined,
useErrorBoundary: true,
},
QueryObserver,
) as UseSuspenseQueryResult<TData, TError>

Comment on lines +45 to +58
export function queryOptions<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & {
queryKey: TQueryKey
}

export function queryOptions(options: unknown) {
return options
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

In my opinion, we should add type tests for queryOptions using Vitest.
It’s important to guarantee type safety:

  1. queryOptions with useQuery
  2. queryOptions with useQueries
  3. queryOptions with useSuspenseQuery
  4. queryOptions with useSuspenseQueries
  5. queryOptions with queryClient methods

You can refer to the link below for a good example of what and how to type test:
https://github.com/toss/suspensive/tree/main/packages/react-query-4/src

I really appreciate this work — I've created @suspensive/react-query in the past because I use TanStack Query v4 at my company yet.
Your approach will actually help me remove some unnecessary code from my project.

Let’s tackle this step by step and make it perfect.

@danielpza
Copy link
Author

Hey @manudeli, I added you as a collaborator to the fork, I don't plan on working on this in the near future, so please feel free to push changes directly to the branch with your suggestions.

Cheers.

@manudeli
Copy link
Collaborator

Hey @manudeli, I added you as a collaborator to the fork, I don't plan on working on this in the near future, so please feel free to push changes directly to the branch with your suggestions.

Cheers.

Hi @danielpza, thanks for putting together the initial work — the change around isPending is a useful direction.
Rather than continuing directly on your fork, I’m planning to rework this in a new branch so I can better align it. I’ll go ahead and close this PR and open a new one, listing you as a co-author.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants