-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
feat(react-query): backport v5 apis about infinite query #9334
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
Open
manudeli
wants to merge
9
commits into
TanStack:v4
Choose a base branch
from
manudeli:react-query/backport-infinite-query
base: v4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+646
−45
Open
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9aaf337
feat(react-query): introduce infinite query options and suspense support
manudeli fb2d620
refactor(react-query): update useSuspenseInfiniteQuery to utilize use…
manudeli 6b61d5e
Merge branch 'v4' into react-query/backport-infinite-query
manudeli 16c08c1
refactor(pr.yml): simplify test job by removing Nx Cloud agent commands
manudeli 22c5ce6
ci(pr.yml): add steps to get base and head commits for `nx affected` …
manudeli dc6bd7c
ci(pr.yml): add main-branch-name input for nx-set-shas action
manudeli 8a7abe4
fix(pr.yml): update HEAD commit reference to use pull request SHA
manudeli c9ddf59
fix(pr.yml): streamline test command by removing unnecessary echo sta…
manudeli c2da2a2
fix(pr.yml): add quotes around variables in test command for consistency
manudeli 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
13 changes: 13 additions & 0 deletions
13
packages/react-query/src/__tests__/infiniteQueryOptions.test.tsx
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,13 @@ | ||
import { infiniteQueryOptions } from '../infiniteQueryOptions' | ||
|
||
describe('infiniteQueryOptions', () => { | ||
it('should return the object received as a parameter without any modification.', () => { | ||
const object = { | ||
queryKey: ['key'], | ||
queryFn: () => Promise.resolve(5), | ||
getNextPageParam: () => null, | ||
} as const | ||
|
||
expect(infiniteQueryOptions(object)).toStrictEqual(object) | ||
}) | ||
}) |
111 changes: 111 additions & 0 deletions
111
packages/react-query/src/__tests__/infiniteQueryOptions.types.test.tsx
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,111 @@ | ||
import { expectTypeOf } from 'expect-type' | ||
import { | ||
type InfiniteData, | ||
type UseInfiniteQueryResult, | ||
useInfiniteQuery, | ||
useQueryClient, | ||
} from '@tanstack/react-query' | ||
|
||
import { useSuspenseInfiniteQuery } from '../useSuspenseInfiniteQuery' | ||
import { infiniteQueryOptions } from '../infiniteQueryOptions' | ||
import { doNotExecute } from './utils' | ||
import type { | ||
DefinedUseInfiniteQueryResult, | ||
UseSuspenseInfiniteQueryResult, | ||
} from '../types' | ||
|
||
const infiniteQuery = { | ||
options: () => | ||
infiniteQueryOptions({ | ||
queryKey: ['key', 1] as const, | ||
queryFn: () => Promise.resolve({ field: 'success' }), | ||
}), | ||
optionsWithInitialData: () => | ||
infiniteQueryOptions({ | ||
queryKey: ['key', 2] as const, | ||
queryFn: () => Promise.resolve({ field: 'success' }), | ||
initialData: () => ({ pageParams: [], pages: [{ field: 'success' }] }), | ||
}), | ||
} | ||
|
||
describe('infiniteQueryOptions', () => { | ||
it('should be used with useInfiniteQuery', () => { | ||
doNotExecute(() => { | ||
expectTypeOf(useInfiniteQuery(infiniteQuery.options())).toEqualTypeOf< | ||
UseInfiniteQueryResult<{ field: string }> | ||
>() | ||
|
||
expectTypeOf( | ||
useInfiniteQuery({ | ||
...infiniteQuery.options(), | ||
select: (data) => ({ | ||
pages: data.pages.map(({ field }) => field), | ||
pageParams: data.pageParams, | ||
}), | ||
}), | ||
).toEqualTypeOf<UseInfiniteQueryResult<string>>() | ||
|
||
expectTypeOf( | ||
useInfiniteQuery(infiniteQuery.optionsWithInitialData()), | ||
).toEqualTypeOf<DefinedUseInfiniteQueryResult<{ field: string }>>() | ||
|
||
expectTypeOf( | ||
useInfiniteQuery({ | ||
...infiniteQuery.optionsWithInitialData(), | ||
select: (data) => ({ | ||
pages: data.pages.map(({ field }) => field), | ||
pageParams: data.pageParams, | ||
}), | ||
}), | ||
).toEqualTypeOf<DefinedUseInfiniteQueryResult<string>>() | ||
|
||
expectTypeOf( | ||
useInfiniteQuery({ | ||
queryKey: ['key', 2] as const, | ||
queryFn: () => Promise.resolve({ field: 'success' }), | ||
initialData: () => ({ | ||
pages: [{ field: 'success' }], | ||
pageParams: [], | ||
}), | ||
select: (data) => ({ | ||
pages: data.pages.map(({ field }) => field), | ||
pageParams: data.pageParams, | ||
}), | ||
}), | ||
).toEqualTypeOf<DefinedUseInfiniteQueryResult<string>>() | ||
}) | ||
}) | ||
it('should be used with useSuspenseInfiniteQuery', () => { | ||
doNotExecute(() => { | ||
expectTypeOf( | ||
useSuspenseInfiniteQuery(infiniteQuery.options()), | ||
).toEqualTypeOf<UseSuspenseInfiniteQueryResult<{ field: string }>>() | ||
|
||
expectTypeOf( | ||
useSuspenseInfiniteQuery({ | ||
...infiniteQuery.options(), | ||
select: (data) => ({ | ||
pages: data.pages.map(({ field }) => field), | ||
pageParams: data.pageParams, | ||
}), | ||
}), | ||
).toEqualTypeOf<UseSuspenseInfiniteQueryResult<string>>() | ||
}) | ||
}) | ||
it('should be used with useQueryClient', () => { | ||
doNotExecute(async () => { | ||
const queryClient = useQueryClient() | ||
|
||
queryClient.invalidateQueries(infiniteQuery.options()) | ||
queryClient.resetQueries(infiniteQuery.options()) | ||
queryClient.removeQueries(infiniteQuery.options()) | ||
queryClient.cancelQueries(infiniteQuery.options()) | ||
queryClient.prefetchQuery(infiniteQuery.options()) | ||
queryClient.refetchQueries(infiniteQuery.options()) | ||
|
||
expectTypeOf( | ||
await queryClient.fetchQuery(infiniteQuery.options()), | ||
).toEqualTypeOf<InfiniteData<{ field: string }>>() | ||
}) | ||
}) | ||
}) |
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
115 changes: 115 additions & 0 deletions
115
packages/react-query/src/__tests__/useSuspenseInfiniteQuery.types.test.tsx
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,115 @@ | ||
import { expectTypeOf } from 'expect-type' | ||
import { infiniteQueryOptions, useSuspenseInfiniteQuery } from '..' | ||
import { doNotExecute, sleep } from './utils' | ||
import type { UseSuspenseInfiniteQueryResult } from '..' | ||
|
||
import type { InfiniteData } from '@tanstack/react-query' | ||
|
||
const queryKey = ['key'] as const | ||
const queryFn = () => sleep(10).then(() => ({ text: 'response' })) | ||
|
||
describe('useSuspenseInfiniteQuery', () => { | ||
it('type check', () => { | ||
doNotExecute(() => { | ||
// @ts-expect-error no arg | ||
useSuspenseInfiniteQuery() | ||
|
||
useSuspenseInfiniteQuery({ | ||
queryKey, | ||
queryFn, | ||
// @ts-expect-error no suspense | ||
suspense: boolean, | ||
}) | ||
useSuspenseInfiniteQuery({ | ||
queryKey, | ||
queryFn, | ||
// @ts-expect-error no useErrorBoundary | ||
useErrorBoundary: boolean, | ||
}) | ||
useSuspenseInfiniteQuery({ | ||
queryKey, | ||
queryFn, | ||
// @ts-expect-error no enabled | ||
enabled: boolean, | ||
}) | ||
useSuspenseInfiniteQuery({ | ||
queryKey, | ||
queryFn, | ||
// @ts-expect-error no placeholderData | ||
placeholderData: 'placeholder', | ||
}) | ||
// eslint-disable-next-line @typescript-eslint/no-unused-expressions | ||
useSuspenseInfiniteQuery({ | ||
queryKey, | ||
queryFn, | ||
// @ts-expect-error no isPlaceholderData | ||
}).isPlaceholderData | ||
useSuspenseInfiniteQuery({ | ||
queryKey, | ||
queryFn, | ||
//@ts-expect-error no networkMode | ||
networkMode: 'always', | ||
}) | ||
|
||
const infiniteQuery = useSuspenseInfiniteQuery({ queryKey, queryFn }) | ||
expectTypeOf(infiniteQuery).toEqualTypeOf< | ||
UseSuspenseInfiniteQueryResult<{ text: string }> | ||
>() | ||
expectTypeOf(infiniteQuery.data).toEqualTypeOf< | ||
InfiniteData<{ text: string }> | ||
>() | ||
expectTypeOf(infiniteQuery.status).toEqualTypeOf<'error' | 'success'>() | ||
|
||
const selectedInfiniteQuery = useSuspenseInfiniteQuery({ | ||
queryKey, | ||
queryFn, | ||
select: (data) => ({ | ||
pages: data.pages.map(({ text }) => text), | ||
pageParams: data.pageParams, | ||
}), | ||
}) | ||
expectTypeOf(selectedInfiniteQuery).toEqualTypeOf< | ||
UseSuspenseInfiniteQueryResult<string> | ||
>() | ||
expectTypeOf(selectedInfiniteQuery.data).toEqualTypeOf< | ||
InfiniteData<string> | ||
>() | ||
expectTypeOf(selectedInfiniteQuery.status).toEqualTypeOf< | ||
'error' | 'success' | ||
>() | ||
|
||
const options = infiniteQueryOptions({ | ||
queryKey, | ||
queryFn, | ||
}) | ||
|
||
const infiniteQueryWithOptions = useSuspenseInfiniteQuery(options) | ||
expectTypeOf(infiniteQueryWithOptions).toEqualTypeOf< | ||
UseSuspenseInfiniteQueryResult<{ text: string }> | ||
>() | ||
expectTypeOf(infiniteQueryWithOptions.data).toEqualTypeOf< | ||
InfiniteData<{ text: string }> | ||
>() | ||
expectTypeOf(infiniteQueryWithOptions.status).toEqualTypeOf< | ||
'error' | 'success' | ||
>() | ||
|
||
const selectedInfiniteQueryWithOptions = useSuspenseInfiniteQuery({ | ||
...options, | ||
select: (data) => ({ | ||
pages: data.pages.map(({ text }) => text), | ||
pageParams: data.pageParams, | ||
}), | ||
}) | ||
expectTypeOf(selectedInfiniteQueryWithOptions).toEqualTypeOf< | ||
UseSuspenseInfiniteQueryResult<string> | ||
>() | ||
expectTypeOf(selectedInfiniteQueryWithOptions.data).toEqualTypeOf< | ||
InfiniteData<string> | ||
>() | ||
expectTypeOf(selectedInfiniteQueryWithOptions.status).toEqualTypeOf< | ||
'error' | 'success' | ||
>() | ||
}) | ||
}) | ||
}) |
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,95 @@ | ||
import type { UseInfiniteQueryOptions } from './types' | ||
import type { | ||
InfiniteData, | ||
NonUndefinedGuard, | ||
OmitKeyof, | ||
QueryKey, | ||
WithRequired, | ||
} from '@tanstack/query-core' | ||
|
||
type UseInfiniteQueryOptionsOmitted< | ||
TQueryFnData = unknown, | ||
TError = unknown, | ||
TData = TQueryFnData, | ||
TQueryKey extends QueryKey = QueryKey, | ||
> = OmitKeyof< | ||
UseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>, | ||
'onSuccess' | 'onError' | 'onSettled' | 'refetchInterval' | ||
> | ||
|
||
type ProhibitedInfiniteQueryOptionsKeyInV5 = keyof Pick< | ||
UseInfiniteQueryOptionsOmitted, | ||
'useErrorBoundary' | 'suspense' | ||
> | ||
|
||
export type UndefinedInitialDataInfiniteOptions< | ||
TQueryFnData, | ||
TError = unknown, | ||
TData = TQueryFnData, | ||
TQueryKey extends QueryKey = QueryKey, | ||
> = UseInfiniteQueryOptionsOmitted<TQueryFnData, TError, TData, TQueryKey> & { | ||
initialData?: undefined | ||
} | ||
|
||
export type DefinedInitialDataInfiniteOptions< | ||
TQueryFnData, | ||
TError = unknown, | ||
TData = TQueryFnData, | ||
TQueryKey extends QueryKey = QueryKey, | ||
> = UseInfiniteQueryOptionsOmitted<TQueryFnData, TError, TData, TQueryKey> & { | ||
initialData: | ||
| NonUndefinedGuard<InfiniteData<TQueryFnData>> | ||
| (() => NonUndefinedGuard<InfiniteData<TQueryFnData>>) | ||
| undefined | ||
} | ||
|
||
export function infiniteQueryOptions< | ||
TQueryFnData, | ||
TError = unknown, | ||
TData = TQueryFnData, | ||
TQueryKey extends QueryKey = QueryKey, | ||
>( | ||
options: WithRequired< | ||
OmitKeyof< | ||
DefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey>, | ||
ProhibitedInfiniteQueryOptionsKeyInV5 | ||
>, | ||
'queryKey' | ||
>, | ||
): WithRequired< | ||
OmitKeyof< | ||
DefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey>, | ||
ProhibitedInfiniteQueryOptionsKeyInV5 | ||
>, | ||
'queryKey' | ||
> | ||
|
||
export function infiniteQueryOptions< | ||
TQueryFnData, | ||
TError = unknown, | ||
TData = TQueryFnData, | ||
TQueryKey extends QueryKey = QueryKey, | ||
>( | ||
options: WithRequired< | ||
OmitKeyof< | ||
UndefinedInitialDataInfiniteOptions< | ||
TQueryFnData, | ||
TError, | ||
TData, | ||
TQueryKey | ||
>, | ||
ProhibitedInfiniteQueryOptionsKeyInV5 | ||
>, | ||
'queryKey' | ||
>, | ||
): WithRequired< | ||
OmitKeyof< | ||
UndefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey>, | ||
ProhibitedInfiniteQueryOptionsKeyInV5 | ||
>, | ||
'queryKey' | ||
> | ||
|
||
export function infiniteQueryOptions(options: unknown) { | ||
return options | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.