-
Notifications
You must be signed in to change notification settings - Fork 0
feat: imperative infinite queries #6
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?
Conversation
let's bring back imperative infinite queries, but only allow them in an all-or-nothing mode, dependent on if `getNextPageParam` has been passed
View your CI Pipeline Execution ↗ for commit 644677e.
☁️ Nx Cloud last updated this comment at |
Thanks for this! Can we get it merged? It's critical to our use case of using it with AgGrid's serverside row model. |
any update for this? |
WalkthroughThis update introduces enhancements to the infinite query logic and its associated type definitions and tests. The primary changes include allowing explicit Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant InfiniteQueryObserver
participant InfiniteQueryBehavior
participant Query
User->>InfiniteQueryObserver: fetchNextPage({ pageParam })
InfiniteQueryObserver->>Query: fetch({ meta: { fetchMore: { direction: 'forward', pageParam } } })
Query->>InfiniteQueryBehavior: handle fetchMore with direction and pageParam
InfiniteQueryBehavior->>Query: Return result with correct page data
Query->>InfiniteQueryObserver: Resolve fetch promise
InfiniteQueryObserver->>User: Return updated query result
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
@coderabbitai full review |
✅ Actions performedFull review triggered. |
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.
Actionable comments posted: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
packages/query-core/src/__tests__/infiniteQueryBehavior.test.tsx
(1 hunks)packages/query-core/src/__tests__/infiniteQueryObserver.test-d.tsx
(1 hunks)packages/query-core/src/infiniteQueryBehavior.ts
(4 hunks)packages/query-core/src/infiniteQueryObserver.ts
(1 hunks)packages/query-core/src/query.ts
(1 hunks)packages/query-core/src/types.ts
(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/query-core/src/__tests__/infiniteQueryBehavior.test.tsx (2)
packages/query-core/src/__tests__/utils.ts (1)
queryKey
(24-27)packages/query-core/src/infiniteQueryObserver.ts (1)
InfiniteQueryObserver
(27-198)
🪛 GitHub Actions: pr
packages/query-core/src/infiniteQueryObserver.ts
[error] 122-122: TypeScript error TS2339: Property 'pageParam' does not exist on type 'FetchNextPageOptions'.
🔇 Additional comments (10)
packages/query-core/src/query.ts (1)
92-92
: Looks good! Adding support for explicit pageParam in fetchMore metadata.This addition to the
FetchMeta
interface enables passing an explicitpageParam
during fetch operations, which is essential for implementing imperative infinite queries. This change works well with the corresponding updates in theinfiniteQueryObserver
where thefetchNextPage
andfetchPreviousPage
methods pass this parameter through.packages/query-core/src/types.ts (1)
274-274
: Good change - making getNextPageParam optional aligns with the all-or-nothing imperative mode.Making
getNextPageParam
optional is a key change that enables imperative infinite queries. This aligns with the PR objective to support imperative infinite queries in an all-or-nothing mode, where ifgetNextPageParam
is omitted, users must provide explicit page parameters. This also matches the existing optional nature ofgetPreviousPageParam
.packages/query-core/src/__tests__/infiniteQueryObserver.test-d.tsx (2)
76-95
: Great type test for the case when getNextPageParam is defined.This test correctly verifies that when
getNextPageParam
is provided, thefetchNextPage
andfetchPreviousPage
methods don't expect or allow apageParam
argument, as it would be calculated by the function. This ensures type safety and prevents confusion in the API.
97-117
: Excellent type test for the case when getNextPageParam is missing.This complementary test ensures that when
getNextPageParam
is omitted, thefetchNextPage
andfetchPreviousPage
methods require apageParam
argument. This enforces the all-or-nothing behavior described in the PR objectives and ensures developers can't accidentally omit required parameters.packages/query-core/src/__tests__/infiniteQueryBehavior.test.tsx (1)
204-306
: Well-designed test for imperative infinite queries functionality.This comprehensive test verifies that explicit
pageParam
values are correctly applied when fetching pages. It covers the key scenarios:
- Initial fetch with the initialPageParam
- Fetching next page with explicit pageParam
- Fetching previous page with explicit pageParam
- Refetching - verifying that manually specified pageParams are preserved
The test provides good coverage of the imperative mode and ensures it works correctly with both new page fetches and refetches. The explicit assertions for each step make the test clear and maintainable.
packages/query-core/src/infiniteQueryBehavior.ts (5)
17-17
: Simplified metadata accessGood change to directly access the
fetchMore
object from the metadata, making the code cleaner and easier to understand.
84-86
: Improved condition for fetching next/previous pagesThe condition now checks for both
fetchMore
existence and the presence of old pages, which is more explicit and handles edge cases better. The direction determination is also cleaner, using thefetchMore.direction
property directly.
91-95
: Support for explicit page parametersThis is the core change that implements the imperative infinite query functionality. The code now checks if an explicit
pageParam
was provided infetchMore
, and only falls back to using the page parameter functions if it wasn't. This aligns perfectly with the PR objective of supporting explicit page parameters when needed.
103-105
: Handling optional getNextPageParam functionThis change adds support for cases where
getNextPageParam
is not provided, falling back to using either the old page parameter or the initial page parameter. This is necessary to make the function optional and support the imperative infinite query mode.
142-148
: Added optional chaining for getNextPageParamGood addition of optional chaining to handle cases where
getNextPageParam
is undefined. This prevents runtime errors when operating in imperative mode without the parameter functions.
fetchPreviousPage({ | ||
pageParam, | ||
...options | ||
}: FetchPreviousPageOptions = {}): Promise< | ||
InfiniteQueryObserverResult<TData, TError> | ||
> { | ||
return this.fetch({ | ||
...options, | ||
meta: { | ||
fetchMore: { direction: 'backward' }, | ||
fetchMore: { direction: 'backward', pageParam }, | ||
}, | ||
}) | ||
} |
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.
Support for explicit pageParam added to fetchPreviousPage
This change mirrors the update to fetchNextPage
, adding support for explicit page parameters to the fetchPreviousPage
method. This implementation maintains consistency between both pagination directions.
The same type issue applies here - ensure that FetchPreviousPageOptions
type is updated to include the pageParam
property.
fetchNextPage({ pageParam, ...options }: FetchNextPageOptions = {}): Promise< | ||
InfiniteQueryObserverResult<TData, TError> | ||
> { | ||
return this.fetch({ | ||
...options, | ||
meta: { | ||
fetchMore: { direction: 'forward' }, | ||
fetchMore: { direction: 'forward', pageParam }, | ||
}, | ||
}) | ||
} |
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.
Good update to support imperative infinite queries
The fetchNextPage
method now accepts an optional pageParam
property which enables users to explicitly control the pagination, aligning with the PR's objective to reintroduce imperative infinite queries functionality.
A small issue to note: the pipeline shows a TypeScript error (TS2339) indicating that 'pageParam' doesn't exist on the FetchNextPageOptions
type. The corresponding type definition should be updated.
let's bring back imperative infinite queries, but only allow them in an all-or-nothing mode, dependent on if
getNextPageParam
has been passedSummary by CodeRabbit
New Features
Bug Fixes
Tests
Refactor
Documentation