Skip to content

feat(openapi): add ordering and pagination to RPC findMany endpoints #2207

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

Merged
merged 36 commits into from
Aug 6, 2025

Conversation

olup
Copy link
Contributor

@olup olup commented Aug 5, 2025

This PR adds support for ordering and pagination parameters to findMany operations in the RPC-style OpenAPI generator.

Changes

  • Added orderBy, cursor, take, and skip parameters to findMany operation definitions in rpc-generator.ts
  • Updated all test baseline YAML files to include the new parameters
  • Added comprehensive runtime tests to verify the functionality

Features Added

  • Ordering: Sort results using orderBy (supports single field or array of fields)
  • Offset-based pagination: Use take and skip parameters
  • Cursor-based pagination: Use cursor parameter for efficient pagination

Why This Change?

The RPC handler already supported these parameters at runtime (it passes them through to Prisma), but they weren't documented in the OpenAPI specification. This made it difficult for API consumers to discover these features. This change brings parity with the REST API generator.

Testing

  • All existing tests pass
  • Added new test case pagination and ordering that verifies:
    • Single and multiple field ordering
    • Take/skip pagination
    • Combined ordering and pagination
    • Cursor-based pagination

🤖 Generated with Claude Code

ymc9 added 30 commits November 22, 2024 11:59
ymc9 and others added 5 commits July 7, 2025 11:56
Add orderBy, cursor, take, and skip parameters to findMany operations in the RPC-style OpenAPI generator. This brings parity with the REST API generator and enables clients to:
- Sort results using orderBy (single or multiple fields)
- Paginate using take/skip (offset-based)
- Use cursor-based pagination

Also added runtime tests to verify the functionality works correctly.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Copy link
Contributor

coderabbitai bot commented Aug 5, 2025

📝 Walkthrough

Walkthrough

The changes refactor the logic for determining the orderByWithRelationInput type in the OpenAPI RPC generator, moving it to be computed once per model. The findMany operation's input schema is expanded to support pagination and ordering. Corresponding tests are added to validate these features, including ordering, limit/offset, and cursor-based pagination.

Changes

Cohort / File(s) Change Summary
OpenAPI RPC Generator Refactor & Pagination
packages/plugins/openapi/src/rpc-generator.ts
Refactored logic to compute orderByWithRelationInput once per model. Extended findMany input schema to support orderBy, cursor, take, and skip parameters. Cleaned up duplicate variable declarations and ensured aggregate/groupBy operations use the precomputed variable.
Pagination and Ordering Tests
packages/server/tests/api/rpc.test.ts
Added a comprehensive test for pagination and ordering on the /post/findMany endpoint. The test covers ordering by different fields, limit/offset, and cursor-based pagination. Also added cleanup steps to existing tests for data isolation.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant RPC API
    participant Database

    Client->>RPC API: POST /post/findMany with { orderBy, take, skip, cursor }
    RPC API->>Database: Query posts with provided pagination and ordering
    Database-->>RPC API: Return paginated, ordered posts
    RPC API-->>Client: Respond with posts data
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15 minutes

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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 explain this code block.
  • 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 explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate unit tests to generate unit tests for 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.

Copy link
Contributor

@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: 1

🧹 Nitpick comments (1)
packages/server/tests/api/rpc.test.ts (1)

134-249: Excellent comprehensive test coverage for pagination and ordering features.

The test is well-structured with proper setup, execution, and cleanup. It comprehensively covers:

  • Single field ordering (title asc, viewCount desc)
  • Multiple field ordering
  • Take/skip pagination
  • Combined ordering and pagination
  • Cursor-based pagination

The test data design with distinct titles and view counts makes verification straightforward.

Consider adding one more assertion to verify the complete ordering in the multiple orderBy test:

 // Test multiple orderBy
 r = await handleRequest({
     method: 'get',
     path: '/post/findMany',
     query: { q: JSON.stringify({ orderBy: [{ published: 'desc' }, { title: 'asc' }] }) },
     prisma,
 });
 expect(r.status).toBe(200);
 expect(r.data[0].title).toBe('A Post');
+expect(r.data.map(post => post.title)).toEqual(['A Post', 'B Post', 'C Post', 'D Post', 'E Post']);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 692b714 and a5abf15.

⛔ Files ignored due to path filters (6)
  • packages/plugins/openapi/tests/baseline/rpc-3.0.0-omit.baseline.yaml is excluded by !**/*.yaml
  • packages/plugins/openapi/tests/baseline/rpc-3.0.0.baseline.yaml is excluded by !**/*.yaml
  • packages/plugins/openapi/tests/baseline/rpc-3.1.0-omit.baseline.yaml is excluded by !**/*.yaml
  • packages/plugins/openapi/tests/baseline/rpc-3.1.0.baseline.yaml is excluded by !**/*.yaml
  • packages/plugins/openapi/tests/baseline/rpc-type-coverage-3.0.0.baseline.yaml is excluded by !**/*.yaml
  • packages/plugins/openapi/tests/baseline/rpc-type-coverage-3.1.0.baseline.yaml is excluded by !**/*.yaml
📒 Files selected for processing (2)
  • packages/plugins/openapi/src/rpc-generator.ts (2 hunks)
  • packages/server/tests/api/rpc.test.ts (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/plugins/openapi/src/rpc-generator.ts (1)
packages/runtime/src/local-helpers/upper-case-first.ts (1)
  • upperCaseFirst (1-3)
🔇 Additional comments (2)
packages/server/tests/api/rpc.test.ts (1)

283-285: Good addition of test cleanup for better isolation.

Adding cleanup at the start of the policy violation test ensures consistent test behavior regardless of execution order.

packages/plugins/openapi/src/rpc-generator.ts (1)

279-285: Perfect implementation of pagination and ordering parameters.

The addition of orderBy, cursor, take, and skip parameters to the findMany operation correctly implements:

  • Single and multiple field ordering via oneOf schema
  • Cursor-based pagination with proper WhereUniqueInput type
  • Offset-based pagination with integer take/skip parameters
  • All parameters appropriately optional

This implementation aligns with Prisma's native API and fulfills the PR objectives.

@ymc9 ymc9 changed the base branch from main to dev August 6, 2025 10:48
@olup
Copy link
Contributor Author

olup commented Aug 6, 2025

My bad, did not catch the dev branch

@ymc9
Copy link
Member

ymc9 commented Aug 6, 2025

My bad, did not catch the dev branch

No worries. Github is missing a feature for setting a default branch for PR ...

@ymc9 ymc9 merged commit a93d73d into zenstackhq:dev Aug 6, 2025
12 checks passed
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.

2 participants