-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6d6be32
add isPending alias to isLoading
danielpza 2d8f9f9
add useSuspenseQuery
danielpza 5c37944
export useSuspenseQuery
danielpza e86b948
fix isPending for queryobserverrefetecterrorresult
danielpza 0bfc2d4
fix
danielpza 6923c9f
add queryOptions
danielpza e9b4c9d
Apply suggestions from code review
danielpza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { | ||
InitialDataFunction, | ||
QueryFunction, | ||
QueryKey, | ||
} from '@tanstack/query-core' | ||
import type { UseQueryOptions } from './types' | ||
|
||
export type UndefinedInitialDataOptions< | ||
TQueryFnData = unknown, | ||
TError = unknown, | ||
TData = TQueryFnData, | ||
TQueryKey extends QueryKey = QueryKey, | ||
> = UseQueryOptions<TQueryFnData, TError, TData, TQueryKey> & { | ||
initialData?: | ||
| undefined | ||
| InitialDataFunction<NonUndefinedGuard<TQueryFnData>> | ||
| NonUndefinedGuard<TQueryFnData> | ||
} | ||
|
||
type NonUndefinedGuard<T> = T extends undefined ? never : T | ||
|
||
export type DefinedInitialDataOptions< | ||
TQueryFnData = unknown, | ||
TError = unknown, | ||
TData = TQueryFnData, | ||
TQueryKey extends QueryKey = QueryKey, | ||
> = Omit<UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'queryFn'> & { | ||
initialData: | ||
| NonUndefinedGuard<TQueryFnData> | ||
| (() => NonUndefinedGuard<TQueryFnData>) | ||
queryFn?: QueryFunction<TQueryFnData, TQueryKey> | ||
} | ||
|
||
export function queryOptions< | ||
TQueryFnData = unknown, | ||
TError = unknown, | ||
TData = TQueryFnData, | ||
TQueryKey extends QueryKey = QueryKey, | ||
>( | ||
options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>, | ||
): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & { | ||
queryKey: TQueryKey | ||
} | ||
|
||
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 | ||
} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -43,6 +43,16 @@ export interface UseQueryOptions< | |||||||||||||||||||||||||||||||||||||
TQueryKey | ||||||||||||||||||||||||||||||||||||||
> {} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
export interface UseSuspenseQueryOptions< | ||||||||||||||||||||||||||||||||||||||
TQueryFnData = unknown, | ||||||||||||||||||||||||||||||||||||||
TError = unknown, | ||||||||||||||||||||||||||||||||||||||
TData = TQueryFnData, | ||||||||||||||||||||||||||||||||||||||
TQueryKey extends QueryKey = QueryKey, | ||||||||||||||||||||||||||||||||||||||
> extends Omit< | ||||||||||||||||||||||||||||||||||||||
UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, | ||||||||||||||||||||||||||||||||||||||
'enabled' | 'suspense' | 'placeholderData' | 'keepPreviousData' | ||||||||||||||||||||||||||||||||||||||
> {} | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+46
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
export interface UseInfiniteQueryOptions< | ||||||||||||||||||||||||||||||||||||||
TQueryFnData = unknown, | ||||||||||||||||||||||||||||||||||||||
TError = unknown, | ||||||||||||||||||||||||||||||||||||||
|
@@ -68,6 +78,11 @@ export type UseQueryResult< | |||||||||||||||||||||||||||||||||||||
TError = unknown, | ||||||||||||||||||||||||||||||||||||||
> = UseBaseQueryResult<TData, TError> | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
export type UseSuspenseQueryResult<TData = unknown, TError = unknown> = Omit< | ||||||||||||||||||||||||||||||||||||||
DefinedQueryObserverResult<TData, TError>, | ||||||||||||||||||||||||||||||||||||||
'isPlaceholderData' | 'isPreviousData' | ||||||||||||||||||||||||||||||||||||||
> | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
export type DefinedUseBaseQueryResult< | ||||||||||||||||||||||||||||||||||||||
TData = unknown, | ||||||||||||||||||||||||||||||||||||||
TError = unknown, | ||||||||||||||||||||||||||||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,26 @@ | ||||||||||||||||||||||||||||||||||||||||
'use client' | ||||||||||||||||||||||||||||||||||||||||
import { QueryObserver } from '@tanstack/query-core' | ||||||||||||||||||||||||||||||||||||||||
import { useBaseQuery } from './useBaseQuery' | ||||||||||||||||||||||||||||||||||||||||
import type { QueryKey } from '@tanstack/query-core' | ||||||||||||||||||||||||||||||||||||||||
import type { UseSuspenseQueryOptions, UseSuspenseQueryResult } from './types' | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
// HOOK | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
export function useSuspenseQuery< | ||||||||||||||||||||||||||||||||||||||||
TQueryFnData = unknown, | ||||||||||||||||||||||||||||||||||||||||
TError = unknown, | ||||||||||||||||||||||||||||||||||||||||
TData = TQueryFnData, | ||||||||||||||||||||||||||||||||||||||||
TQueryKey extends QueryKey = QueryKey, | ||||||||||||||||||||||||||||||||||||||||
>( | ||||||||||||||||||||||||||||||||||||||||
options: UseSuspenseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, | ||||||||||||||||||||||||||||||||||||||||
): UseSuspenseQueryResult<TData, TError> { | ||||||||||||||||||||||||||||||||||||||||
return useBaseQuery( | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
...options, | ||||||||||||||||||||||||||||||||||||||||
enabled: true, | ||||||||||||||||||||||||||||||||||||||||
suspense: true, | ||||||||||||||||||||||||||||||||||||||||
placeholderData: undefined, | ||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||
QueryObserver, | ||||||||||||||||||||||||||||||||||||||||
) as UseSuspenseQueryResult<TData, TError> | ||||||||||||||||||||||||||||||||||||||||
Comment on lines
+17
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||||||||||||||||||||||||||||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
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.