Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

arvi18
Copy link

@arvi18 arvi18 commented Apr 23, 2025

let's bring back imperative infinite queries, but only allow them in an all-or-nothing mode, dependent on if getNextPageParam has been passed

Summary by CodeRabbit

  • New Features

    • Added support for explicitly specifying a page parameter when fetching the next or previous page in infinite queries.
  • Bug Fixes

    • Improved handling and safety for optional page parameter functions, reducing potential errors when not provided.
  • Tests

    • Added new tests to verify correct application of page parameters and TypeScript typings for infinite query fetching methods.
  • Refactor

    • Unified and clarified logic for determining fetch direction and page parameters in infinite queries.
  • Documentation

    • Updated TypeScript types to reflect new optional page parameter behavior.

let's bring back imperative infinite queries, but only allow them in an all-or-nothing mode, dependent on if `getNextPageParam` has been passed
@arvi18
Copy link
Author

arvi18 commented Apr 23, 2025

View your CI Pipeline Execution ↗ for commit 644677e.

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ❌ Failed 2m 9s View ↗
nx run-many --target=build --exclude=examples/*... ❌ Failed 6s View ↗

☁️ Nx Cloud last updated this comment at 2025-02-14 14:45:30 UTC

@arvi18
Copy link
Author

arvi18 commented Apr 23, 2025

Thanks for this! Can we get it merged? It's critical to our use case of using it with AgGrid's serverside row model.

@arvi18
Copy link
Author

arvi18 commented Apr 23, 2025

Hey @TkDodo as @riwu said for us it is also important any update when it will be merged?

@arvi18
Copy link
Author

arvi18 commented Apr 23, 2025

any update for this?

Copy link

coderabbitai bot commented Apr 23, 2025

Walkthrough

This update introduces enhancements to the infinite query logic and its associated type definitions and tests. The primary changes include allowing explicit pageParam values to be passed when fetching the next or previous page in infinite queries, updating the metadata structure to support this, and making the getNextPageParam function optional in the options interface. The observer methods are refactored to accept and forward the pageParam. Tests are added to verify both the runtime behavior and the TypeScript typings for these new capabilities. No breaking changes to public APIs are introduced.

Changes

File(s) Change Summary
packages/query-core/src/infiniteQueryObserver.ts Refactored fetchNextPage and fetchPreviousPage to accept and forward an explicit pageParam in their options, updating their signatures accordingly.
packages/query-core/src/infiniteQueryBehavior.ts Refactored logic to handle explicit pageParam in fetch metadata, unified direction and parameter extraction, and made getNextPageParam optional.
packages/query-core/src/query.ts Extended FetchMeta interface to include an optional pageParam property in fetchMore.
packages/query-core/src/types.ts Made getNextPageParam optional in InfiniteQueryPageParamsOptions interface.
packages/query-core/src/__tests__/infiniteQueryBehavior.test.tsx Added a test verifying that explicit pageParam values are correctly applied and preserved in infinite query behavior.
packages/query-core/src/__tests__/infiniteQueryObserver.test-d.tsx Added type-level tests ensuring correct typing for fetchNextPage and fetchPreviousPage based on the presence of getNextPageParam.

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
Loading

Poem

🐇
Pages turn with nimble paws,
Now with params, without a pause.
Forward, backward, hop and fetch—
Infinite queries, no longer a stretch!
Types are checked, the tests are bright,
Rabbit devs rejoice tonight!
🥕✨

✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@visz11
Copy link
Collaborator

visz11 commented Apr 23, 2025

@coderabbitai full review

Copy link

coderabbitai bot commented Apr 23, 2025

✅ Actions performed

Full review triggered.

Copy link

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between 4a5990e and 644677e.

📒 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 explicit pageParam during fetch operations, which is essential for implementing imperative infinite queries. This change works well with the corresponding updates in the infiniteQueryObserver where the fetchNextPage and fetchPreviousPage 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 if getNextPageParam is omitted, users must provide explicit page parameters. This also matches the existing optional nature of getPreviousPageParam.

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, the fetchNextPage and fetchPreviousPage methods don't expect or allow a pageParam 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, the fetchNextPage and fetchPreviousPage methods require a pageParam 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:

  1. Initial fetch with the initialPageParam
  2. Fetching next page with explicit pageParam
  3. Fetching previous page with explicit pageParam
  4. 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 access

Good 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 pages

The 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 the fetchMore.direction property directly.


91-95: Support for explicit page parameters

This is the core change that implements the imperative infinite query functionality. The code now checks if an explicit pageParam was provided in fetchMore, 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 function

This 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 getNextPageParam

Good addition of optional chaining to handle cases where getNextPageParam is undefined. This prevents runtime errors when operating in imperative mode without the parameter functions.

Comment on lines +138 to 150
fetchPreviousPage({
pageParam,
...options
}: FetchPreviousPageOptions = {}): Promise<
InfiniteQueryObserverResult<TData, TError>
> {
return this.fetch({
...options,
meta: {
fetchMore: { direction: 'backward' },
fetchMore: { direction: 'backward', pageParam },
},
})
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

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.

Comment on lines +127 to 136
fetchNextPage({ pageParam, ...options }: FetchNextPageOptions = {}): Promise<
InfiniteQueryObserverResult<TData, TError>
> {
return this.fetch({
...options,
meta: {
fetchMore: { direction: 'forward' },
fetchMore: { direction: 'forward', pageParam },
},
})
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

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.

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