-
Notifications
You must be signed in to change notification settings - Fork 0
Clone feature/imperative infinite queries #18
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ export function infiniteQueryBehavior<TQueryFnData, TError, TData, TPageParam>( | |
| return { | ||
| onFetch: (context, query) => { | ||
| const options = context.options as InfiniteQueryPageParamsOptions<TData> | ||
| const direction = context.fetchOptions?.meta?.fetchMore?.direction | ||
| const fetchMore = context.fetchOptions?.meta?.fetchMore | ||
| const oldPages = context.state.data?.pages || [] | ||
| const oldPageParams = context.state.data?.pageParams || [] | ||
| let result: InfiniteData<unknown> = { pages: [], pageParams: [] } | ||
|
|
@@ -81,14 +81,17 @@ export function infiniteQueryBehavior<TQueryFnData, TError, TData, TPageParam>( | |
| } | ||
|
|
||
| // fetch next / previous page? | ||
| if (direction && oldPages.length) { | ||
| const previous = direction === 'backward' | ||
| if (fetchMore && oldPages.length) { | ||
| const previous = fetchMore.direction === 'backward' | ||
| const pageParamFn = previous ? getPreviousPageParam : getNextPageParam | ||
| const oldData = { | ||
| pages: oldPages, | ||
| pageParams: oldPageParams, | ||
| } | ||
| const param = pageParamFn(options, oldData) | ||
| const param = | ||
| fetchMore.pageParam === undefined | ||
| ? pageParamFn(options, oldData) | ||
| : fetchMore.pageParam | ||
|
Comment on lines
+91
to
+94
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. Type Definition and Runtime Behavior MismatchThe runtime logic allows Standards
|
||
|
|
||
| result = await fetchPage(oldData, param, previous) | ||
| } else { | ||
|
Comment on lines
+85
to
97
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. Explicit Please let the - if (fetchMore && oldPages.length) {
- const previous = fetchMore.direction === 'backward'
- const pageParamFn = previous ? getPreviousPageParam : getNextPageParam
- const oldData = {
- pages: oldPages,
- pageParams: oldPageParams,
- }
- const param =
- fetchMore.pageParam === undefined
- ? pageParamFn(options, oldData)
- : fetchMore.pageParam
-
- result = await fetchPage(oldData, param, previous)
+ if (fetchMore) {
+ const previous = fetchMore.direction === 'backward'
+ const pageParamFn = previous ? getPreviousPageParam : getNextPageParam
+ const baseData = oldPages.length
+ ? { pages: oldPages, pageParams: oldPageParams }
+ : { pages: [], pageParams: [] }
+ const param =
+ fetchMore.pageParam !== undefined
+ ? fetchMore.pageParam
+ : oldPages.length
+ ? pageParamFn(options, baseData)
+ : options.initialPageParam
+
+ result = await fetchPage(baseData, param, previous)
🤖 Prompt for AI Agents |
||
|
|
@@ -97,8 +100,8 @@ export function infiniteQueryBehavior<TQueryFnData, TError, TData, TPageParam>( | |
| // Fetch all pages | ||
| do { | ||
| const param = | ||
| currentPage === 0 | ||
| ? (oldPageParams[0] ?? options.initialPageParam) | ||
| currentPage === 0 || !options.getNextPageParam | ||
| ? (oldPageParams[currentPage] ?? options.initialPageParam) | ||
| : getNextPageParam(options, result) | ||
|
Comment on lines
102
to
105
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. The logic for determining the The fallback to const param =
currentPage === 0
? oldPageParams[0] ?? options.initialPageParam
: !options.getNextPageParam
? oldPageParams[currentPage]
: getNextPageParam(options, result) |
||
| if (currentPage > 0 && param == null) { | ||
| break | ||
|
|
@@ -136,7 +139,7 @@ function getNextPageParam( | |
| ): unknown | undefined { | ||
| const lastIndex = pages.length - 1 | ||
| return pages.length > 0 | ||
| ? options.getNextPageParam( | ||
| ? options.getNextPageParam?.( | ||
| pages[lastIndex], | ||
| pages, | ||
| pageParams[lastIndex], | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -124,24 +124,27 @@ export class InfiniteQueryObserver< | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fetchNextPage( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| options?: FetchNextPageOptions, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): Promise<InfiniteQueryObserverResult<TData, TError>> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fetchNextPage({ pageParam, ...options }: FetchNextPageOptions = {}): Promise< | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| InfiniteQueryObserverResult<TData, TError> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| > { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return this.fetch({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...options, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| meta: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fetchMore: { direction: 'forward' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fetchMore: { direction: 'forward', pageParam }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+127
to
136
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. Critical: Add The pipeline is failing because To fix this, update the export interface FetchNextPageOptions extends ResultOptions {
+ /**
+ * Optional page parameter to use when fetching the next page.
+ * If provided, this will override the value computed from getNextPageParam.
+ */
+ pageParam?: unknown
/**
* If set to `true`, calling `fetchNextPage` repeatedly will invoke `queryFn` every time,
* whether the previous invocation has resolved or not. Also, the result from previous invocations will be ignored.
*
* If set to `false`, calling `fetchNextPage` repeatedly won't have any effect until the first invocation has resolved.
*
* Defaults to `true`.
*/
cancelRefetch?: boolean
}
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fetchPreviousPage( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| options?: FetchPreviousPageOptions, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): Promise<InfiniteQueryObserverResult<TData, TError>> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fetchPreviousPage({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pageParam, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...options | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }: FetchPreviousPageOptions = {}): Promise< | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| InfiniteQueryObserverResult<TData, TError> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| > { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return this.fetch({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...options, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| meta: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fetchMore: { direction: 'backward' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fetchMore: { direction: 'backward', pageParam }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+127
to
150
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. New Feature Lacks Required Feature FlagThis change introduces a significant new feature (imperative page parameters) without being gated by a feature flag. This violates the organization guideline for new functionality, which is crucial for controlled rollouts and ensuring a clear rollback plan. The new API surface should be conditionally enabled via a feature flag. Commitable Suggestion
Suggested change
Standards
Comment on lines
+138
to
150
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. Critical: Add The same issue applies here— Update the export interface FetchPreviousPageOptions extends ResultOptions {
+ /**
+ * Optional page parameter to use when fetching the previous page.
+ * If provided, this will override the value computed from getPreviousPageParam.
+ */
+ pageParam?: unknown
/**
* If set to `true`, calling `fetchPreviousPage` repeatedly will invoke `queryFn` every time,
* whether the previous invocation has resolved or not. Also, the result from previous invocations will be ignored.
*
* If set to `false`, calling `fetchPreviousPage` repeatedly won't have any effect until the first invocation has resolved.
*
* Defaults to `true`.
*/
cancelRefetch?: boolean
}
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -271,7 +271,7 @@ export interface InfiniteQueryPageParamsOptions< | |||||
| * This function can be set to automatically get the next cursor for infinite queries. | ||||||
| * The result will also be used to determine the value of `hasNextPage`. | ||||||
| */ | ||||||
| getNextPageParam: GetNextPageParamFunction<TPageParam, TQueryFnData> | ||||||
| getNextPageParam?: GetNextPageParamFunction<TPageParam, TQueryFnData> | ||||||
|
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. API Contract Changed Without Feature FlagA significant new feature allowing imperative page fetching has been introduced by making Commitable Suggestion
Suggested change
Standards
|
||||||
| } | ||||||
|
|
||||||
| export type ThrowOnError< | ||||||
|
|
||||||
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.
Incorrect Type Test Allows Undefined Arguments
The type test asserts that the options argument for
fetchNextPagecan beundefined, which contradicts the test's stated goal to 'require pageParam'. This lenient type allows callingfetchNextPage()without arguments whengetNextPageParamis missing, which can lead to a silent runtime failure where thequeryFnreceivespageParam: undefined. The type should enforce that the options object containingpageParamis mandatory.Standards