Skip to content
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

refactor(tanstack): merge zenstack query/mutation options into tanstack options #1136

Merged
merged 2 commits into from
Mar 14, 2024

Conversation

ymc9
Copy link
Member

@ymc9 ymc9 commented Mar 14, 2024

Fixes #835

Summary by CodeRabbit

  • New Features
    • Enhanced query and mutation hooks in the Tanstack Query plugin to support additional options for more flexible and powerful data fetching and mutation capabilities.
    • Introduced ExtraQueryOptions and ExtraMutationOptions for extended control over queries and mutations, including handling of optimistic updates.
  • Refactor
    • Updated Tanstack Query plugin to support version 5, removing outdated version-specific conditional logic.
    • Improved export organization for better usability across different frontend frameworks (React, Vue, Svelte).
    • Utilized pattern matching in options handling to simplify and enhance code readability.
  • Bug Fixes
    • Fixed inconsistencies in query key generation, ensuring more reliable cache behavior and data fetching.
  • Tests
    • Updated tests to reflect new versioning and added features, ensuring compatibility and reliability of the plugin across versions and frameworks.

Copy link
Contributor

coderabbitai bot commented Mar 14, 2024

Walkthrough

Walkthrough

The changes primarily revolve around enhancing the Tanstack Query plugin to support version 5, with significant updates in handling query and mutation hooks. This includes the removal of useHooksContext import in SWR, updates in version checks, removal of conditional logic for optimistic updates, and the introduction of ts-pattern for better option handling. New types ExtraMutationOptions and ExtraQueryOptions are introduced, alongside refactoring for more flexible query key generation and options handling across React, Vue, and Svelte integrations.

Changes

Files Change Summary
.../swr/src/generator.ts Removed import of useHooksContext from @zenstackhq/swr/runtime.
.../tanstack-query/src/generator.ts Updated version check to 'v5', removed optimistic updates logic, added ts-pattern import, refactored query and mutation hooks generation, added imports for ExtraQueryOptions and ExtraMutationOptions.
.../tanstack-query/src/runtime-v5/... Reorganized exports, introduced new exports for query handling, added ExtraMutationOptions and ExtraQueryOptions, refactored query and mutation functions, and removed optimisticUpdate parameter.
.../tanstack-query/src/runtime/common.ts Introduced ExtraMutationOptions and ExtraQueryOptions, refactored getQueryKey function.
.../tanstack-query/src/runtime/index.ts Reorganized exports, added new exports related to query handling.
.../tanstack-query/src/runtime/... (react, svelte, vue) Added ExtraMutationOptions and ExtraQueryOptions, refactored query and mutation hooks, updated useModelQuery, useInfiniteModelQuery, and useModelMutation functions.
.../tanstack-query/tests/plugin.test.ts Updated the version property from 'v5' to 'v4' for react, vue, and svelte targets.
.../tanstack-query/tests/react-hooks-v5.test.tsx Updated useModelQuery and useModelMutation to include additional options, modified getQueryKey calls.
.../tanstack-query/tests/react-hooks.test.tsx Updated test suite description to 'Tanstack Query React Hooks V4 Test', modified useModelQuery calls to include { optimisticUpdate: true }.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

Note: Auto-reply has been disabled for this repository by the repository owner. 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 generate unit tests 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 6db11e2 and 107ee04.
Files ignored due to path filters (2)
  • packages/plugins/tanstack-query/package.json is excluded by: !**/*.json
  • pnpm-lock.yaml is excluded by: !**/*.yaml
Files selected for processing (13)
  • packages/plugins/swr/src/generator.ts (1 hunks)
  • packages/plugins/tanstack-query/src/generator.ts (8 hunks)
  • packages/plugins/tanstack-query/src/runtime-v5/index.ts (1 hunks)
  • packages/plugins/tanstack-query/src/runtime-v5/react.ts (8 hunks)
  • packages/plugins/tanstack-query/src/runtime-v5/svelte.ts (6 hunks)
  • packages/plugins/tanstack-query/src/runtime/common.ts (2 hunks)
  • packages/plugins/tanstack-query/src/runtime/index.ts (1 hunks)
  • packages/plugins/tanstack-query/src/runtime/react.ts (6 hunks)
  • packages/plugins/tanstack-query/src/runtime/svelte.ts (6 hunks)
  • packages/plugins/tanstack-query/src/runtime/vue.ts (6 hunks)
  • packages/plugins/tanstack-query/tests/plugin.test.ts (6 hunks)
  • packages/plugins/tanstack-query/tests/react-hooks-v5.test.tsx (12 hunks)
  • packages/plugins/tanstack-query/tests/react-hooks.test.tsx (13 hunks)
Additional comments: 38
packages/plugins/tanstack-query/src/runtime/index.ts (1)
  • 1-7: The export reorganization and additions in index.ts are correctly implemented, ensuring that the necessary types and functions for query handling are available. This change aligns with the PR's objectives to enhance query and mutation handling capabilities.
packages/plugins/tanstack-query/src/runtime-v5/index.ts (1)
  • 1-7: The changes in runtime-v5/index.ts correctly introduce new exports for enhanced query and mutation handling in version 5 of the library. This ensures consistency and availability of necessary types across different versions.
packages/plugins/tanstack-query/tests/plugin.test.ts (3)
  • 56-56: The update of the version property from 'v5' to 'v4' for the react target in the plugin configuration test seems to contradict the PR's aim of adapting to Tanstack v5. Please verify if this change is intentional and aligns with the overall objectives.
  • 99-99: Similar to the react target, updating the version property for the vue target also requires verification to ensure it aligns with the PR's objectives of adapting to Tanstack v5.
  • 142-142: The update of the version property for the svelte target, like the previous two, needs verification to confirm its alignment with the PR's objectives regarding Tanstack v5 adaptation.
packages/plugins/tanstack-query/src/runtime/react.ts (4)
  • 22-23: The addition of ExtraMutationOptions and ExtraQueryOptions types is correctly implemented, enhancing the flexibility and customization capabilities of query and mutation hooks.
  • 63-71: The update to useModelQuery to include ExtraQueryOptions and the modification of the getQueryKey call align with the PR's objectives to enhance query handling. Ensure that the default behavior for optimisticUpdate is clearly documented for users.
  • 95-95: The handling of infinite and optimisticUpdate properties in useInfiniteModelQuery is correctly implemented, ensuring consistent and flexible query key generation.
  • 122-130: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [125-148]

The updates to useModelMutation to include ExtraMutationOptions and handle invalidateQueries and optimisticUpdate based on options are well-implemented. It's important to ensure that these options are clearly documented for users to understand their impact.

packages/plugins/tanstack-query/src/runtime/svelte.ts (4)
  • 22-23: The addition of ExtraMutationOptions and ExtraQueryOptions types for the Svelte runtime is correctly implemented, enhancing the customization capabilities of query and mutation hooks.
  • 63-71: The update to useModelQuery to include ExtraQueryOptions and the modification of the getQueryKey call align with the PR's objectives to enhance query handling in the Svelte runtime. Ensure that the default behavior for optimisticUpdate is clearly documented for users.
  • 95-95: The handling of infinite and optimisticUpdate properties in useInfiniteModelQuery is correctly implemented for the Svelte runtime, ensuring consistent and flexible query key generation.
  • 121-129: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [124-147]

The updates to useModelMutation to include ExtraMutationOptions and handle invalidateQueries and optimisticUpdate based on options are well-implemented for the Svelte runtime. It's important to ensure that these options are clearly documented for users to understand their impact.

packages/plugins/tanstack-query/src/runtime/vue.ts (4)
  • 23-24: The addition of ExtraMutationOptions and ExtraQueryOptions types for the Vue runtime is correctly implemented, enhancing the customization capabilities of query and mutation hooks.
  • 65-73: The update to useModelQuery to include ExtraQueryOptions and the modification of the getQueryKey call align with the PR's objectives to enhance query handling in the Vue runtime. Ensure that the default behavior for optimisticUpdate is clearly documented for users.
  • 97-97: The handling of infinite and optimisticUpdate properties in useInfiniteModelQuery is correctly implemented for the Vue runtime, ensuring consistent and flexible query key generation.
  • 125-133: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [128-152]

The updates to useModelMutation to include ExtraMutationOptions and handle invalidateQueries and optimisticUpdate based on options are well-implemented for the Vue runtime. It's important to ensure that these options are clearly documented for users to understand their impact.

packages/plugins/tanstack-query/src/runtime-v5/svelte.ts (4)
  • 25-26: The addition of ExtraMutationOptions and ExtraQueryOptions types for the Svelte runtime in version 5 is correctly implemented, enhancing the customization capabilities of query and mutation hooks.
  • 66-73: The update to useModelQuery to include ExtraQueryOptions and the modification of the getQueryKey call align with the PR's objectives to enhance query handling in the Svelte runtime for version 5. Ensure that the default behavior for optimisticUpdate is clearly documented for users.
  • 113-113: The handling of infinite and optimisticUpdate properties in useInfiniteModelQuery is correctly implemented for the Svelte runtime in version 5, ensuring consistent and flexible query key generation.
  • 160-168: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [163-185]

The updates to useModelMutation to include ExtraMutationOptions and handle invalidateQueries and optimisticUpdate based on options are well-implemented for the Svelte runtime in version 5. It's important to ensure that these options are clearly documented for users to understand their impact.

packages/plugins/tanstack-query/src/runtime-v5/react.ts (4)
  • 27-28: The addition of ExtraMutationOptions and ExtraQueryOptions types for the React runtime in version 5 is correctly implemented, enhancing the customization capabilities of query and mutation hooks.
  • 67-75: The update to useModelQuery to include ExtraQueryOptions and the modification of the getQueryKey call align with the PR's objectives to enhance query handling in the React runtime for version 5. Ensure that the default behavior for optimisticUpdate is clearly documented for users.
  • 127-127: The handling of infinite and optimisticUpdate properties in useInfiniteModelQuery is correctly implemented for the React runtime in version 5, ensuring consistent and flexible query key generation.
  • 180-188: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [183-205]

The updates to useModelMutation to include ExtraMutationOptions and handle invalidateQueries and optimisticUpdate based on options are well-implemented for the React runtime in version 5. It's important to ensure that these options are clearly documented for users to understand their impact.

packages/plugins/tanstack-query/src/runtime/common.ts (3)
  • 46-56: The introduction of ExtraMutationOptions with invalidateQueries and optimisticUpdate properties provides clear, flexible configuration for mutations. This is a positive change that enhances the library's functionality.
  • 61-66: The ExtraQueryOptions type with an optimisticUpdate property is a good addition, allowing for more granular control over query behavior. This aligns well with the goal of enhancing customization capabilities for users.
  • 138-155: The refactoring of getQueryKey to accept an options object instead of separate boolean parameters is a significant improvement. It simplifies the function signature and makes the function more extensible for future options. However, ensure that all calls to this function throughout the codebase have been updated to pass an options object.
packages/plugins/swr/src/generator.ts (1)
  • 69-69: The removal of useHooksContext import aligns with the PR's objective to shift away from Zenstack-specific hooks. Ensure that this change does not negatively impact any functionalities that previously relied on useHooksContext.
Verification successful

The removal of useHooksContext import from generator.ts in the swr plugin does not negatively impact any functionalities within this file, as the script results indicate no usage of useHooksContext. This aligns with the PR's objective to shift away from Zenstack-specific hooks.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for usage of useHooksContext in the current file. Expecting no results.
rg --type typescript 'useHooksContext' packages/plugins/swr/src/generator.ts

Length of output: 111



Script:

#!/bin/bash
# Correcting the previous script to search for usage of useHooksContext in the current file without specifying file type.
rg 'useHooksContext' packages/plugins/swr/src/generator.ts

Length of output: 58

packages/plugins/tanstack-query/tests/react-hooks.test.tsx (2)
  • 17-17: Updating the test suite description to 'Tanstack Query React Hooks V4 Test' accurately reflects the versioning changes and the scope of the tests. This is a good practice for maintaining clarity in test documentation.
  • 165-165: Adding { optimisticUpdate: true } to useModelQuery calls in several tests is a necessary adjustment to align with the new ExtraQueryOptions type and to test the optimistic update feature. This change ensures that the tests cover the new functionality introduced in the PR.

Also applies to: 188-189, 221-221, 244-245, 319-319, 342-343, 415-415, 438-439

packages/plugins/tanstack-query/tests/react-hooks-v5.test.tsx (2)
  • 223-232: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [208-229]

The introduction of optimisticUpdate and invalidateQueries options in useModelQuery and useModelMutation calls enhances the flexibility and control over query and mutation behavior. This is a positive change, aligning with the PR objectives to integrate Zenstack options into Tanstack options for improved customization capabilities.

  • 238-240: The use of getQueryKey with { infinite: false, optimisticUpdate: true } options in cache data retrieval is a significant improvement. It streamlines the process of generating and using query keys, making it more consistent and flexible across different runtime environments. This change supports the PR's goal of enhancing query and mutation handling capabilities.
packages/plugins/tanstack-query/src/generator.ts (5)
  • 20-20: The introduction of ts-pattern for pattern matching is a notable improvement. It enhances the flexibility and readability of the options handling logic in the generation of query and mutation hooks. This aligns with the PR's objective to provide more detailed configuration capabilities for query and mutation behaviors.
  • 41-41: Updating the version check to default to 'v5' and ensuring only 'v4' and 'v5' are supported versions is a necessary change. It reflects the focus of this PR on integrating Zenstack options into Tanstack v5, ensuring that the generator logic is aligned with the supported versions of Tanstack.
  • 546-546: The addition of ExtraQueryOptions and ExtraMutationOptions to the base imports is a critical update. It ensures that these new types are available for use in the generated query and mutation hooks, supporting the PR's goal of enhancing customization capabilities for users. This change is essential for allowing more detailed configuration of query and mutation behaviors.
  • 597-628: The use of ts-pattern for generating query options based on the target framework, return type, data type, and version is an excellent application of pattern matching. It simplifies the logic for determining the appropriate options type for different scenarios, enhancing the maintainability and readability of the code. This approach aligns with the PR's objectives by providing a more flexible and standardized way to configure query hooks.
  • 633-643: Similarly, the application of ts-pattern for generating mutation options based on the target framework, return type, and arguments type is a valuable improvement. It streamlines the process of determining the correct options type for mutation hooks, contributing to the PR's goal of enhancing the flexibility and customization of mutation handling across different frameworks.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 107ee04 and 2fdf090.
Files selected for processing (1)
  • tests/integration/tests/cli/plugins.test.ts (1 hunks)
Additional comments: 2
tests/integration/tests/cli/plugins.test.ts (2)
  • 74-74: The version of @tanstack/react-query has been updated from ^4.0.0 to ^5.0.0. This change aligns with the PR's objective to support Tanstack v5. Ensure that all tests and functionalities relying on @tanstack/react-query are compatible with version 5.0.0, as major version updates can introduce breaking changes.
Verification successful

The update of @tanstack/react-query to version ^5.0.0 in the test file is consistent with the codebase's explicit support for this version, as evidenced by specific tests and runtime logic designed for version 5. This indicates that the change aligns with the project's requirements and compatibility considerations. Therefore, the version update in the PR is verified to be appropriate for the project's current state and objectives.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify if there are specific tests targeting @tanstack/react-query features that might be affected by the version update.
rg --type ts "react-query"

Length of output: 4478

* 71-77: > 📝 **NOTE** > This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [1-1]

The file structure and setup for testing plugins, including the updated @tanstack/react-query, follow best practices for isolation and environment management. Consider revisiting the commented-out package managers (pnpm, pnpm-workspace) and plugin configurations in future work to expand test coverage.

@ymc9 ymc9 merged commit 0d6674d into v2 Mar 14, 2024
12 checks passed
@ymc9 ymc9 deleted the refactor/hooks-options branch March 14, 2024 02:25
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.

1 participant