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

fix(tanstack-query): non-zenstack queries are accidentally visited during optimistic update #1775

Merged
merged 3 commits into from
Oct 14, 2024

Conversation

ymc9
Copy link
Member

@ymc9 ymc9 commented Oct 14, 2024

Fixes #1774

Copy link
Contributor

coderabbitai bot commented Oct 14, 2024

📝 Walkthrough

Walkthrough

The changes in this pull request enhance the functionality of optimistic updates and query invalidation in the Tanstack Query library. A new function, isZenStackQueryKey, is introduced to validate query keys, ensuring that only valid keys are processed during optimistic updates. The optimisticUpdate function is refined for better clarity, maintainability, and error handling. Additionally, the test suite is updated with a new test case to validate the interaction between optimistic updates and non-Zenstack queries.

Changes

File Path Change Summary
packages/plugins/tanstack-query/src/runtime/common.ts - Added isZenStackQueryKey function for query key validation.
- Improved optimisticUpdate function with better destructuring and error handling.
packages/plugins/tanstack-query/tests/react-hooks-v5.test.tsx - Added a new test case for useQuery to validate optimistic updates with non-Zenstack queries.
packages/plugins/tanstack-query/tests/plugin.test.ts - Updated @tanstack/react-query dependency from ^5.0.0 to 5.56.x.
- Updated @tanstack/vue-query and @tanstack/svelte-query dependencies to latest versions.
- Added new source file suspense.ts with suspenseInfiniteQuery function.
tests/integration/tests/cli/plugins.test.ts - Updated @tanstack/react-query dependency from ^5.0.0 to 5.56.x in initProject function.

Assessment against linked issues

Objective Addressed Explanation
Ensure optimistic updates do not fail silently (#1774)

Possibly related PRs


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:

‼️ 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 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 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: 0

🧹 Outside diff range and nitpick comments (1)
packages/plugins/tanstack-query/tests/react-hooks-v5.test.tsx (1)

867-930: LGTM: New test case for mixed zenstack and non-zenstack queries

The new test case "optimistic update mixed with non-zenstack queries" is a valuable addition. It correctly tests the behavior of optimistic updates in the presence of both zenstack and non-zenstack queries.

However, consider the following suggestions to enhance the test:

  1. After the optimistic update, verify that the non-zenstack query data remains unchanged. This would ensure that the optimistic update doesn't affect unrelated queries.

  2. Consider adding a comment explaining the purpose of this test case, highlighting the importance of isolating zenstack and non-zenstack query behaviors.

Here's a suggested addition to the test case:

// After line 929, add:
// Verify that the non-zenstack query remains unaffected
expect(myQueryResult.current.data).toEqual({ data: 'myData' });

// Consider adding this comment at the beginning of the test case (line 867):
// This test ensures that optimistic updates for zenstack queries
// do not interfere with non-zenstack queries
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between aae9b60 and 08ac462.

📒 Files selected for processing (2)
  • packages/plugins/tanstack-query/src/runtime/common.ts (2 hunks)
  • packages/plugins/tanstack-query/tests/react-hooks-v5.test.tsx (2 hunks)
🧰 Additional context used
🔇 Additional comments (4)
packages/plugins/tanstack-query/tests/react-hooks-v5.test.tsx (1)

6-6: LGTM: New import for useQuery

The addition of useQuery import from '@tanstack/react-query-v5' is correct and necessary for the new test case.

packages/plugins/tanstack-query/src/runtime/common.ts (3)

388-391: Correctly Skipping Non-ZenStack Queries

The addition of the isZenStackQueryKey check ensures that non-ZenStack queries are appropriately skipped during optimistic updates. This prevents unintended side effects on unrelated queries.


Line range hint 400-406: Respecting Query's Optimistic Update Configuration

The check for the optimisticUpdate flag in queryOptions correctly skips queries that have opted out of optimistic updates. This honors the query's configuration and enhances control over the optimistic update behavior.


459-469: Implementation of isZenStackQueryKey Function

The isZenStackQueryKey function accurately determines whether a query key corresponds to a ZenStack query by checking its length and prefix. This effectively filters out non-ZenStack queries during the optimistic update process.

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

🧹 Outside diff range and nitpick comments (1)
tests/integration/tests/cli/plugins.test.ts (1)

Line range hint 1-391: Summary and Next Steps

The update to the Tanstack Query version is a good step towards addressing the optimistic update issue mentioned in the PR objectives. The extensive test suite in this file provides robust coverage for various plugin scenarios, which is crucial for maintaining system integrity.

To fully address the PR objectives:

  1. Ensure that the optimistic update functionality is specifically tested, either in this file or in a separate test file dedicated to Tanstack Query behavior.
  2. Consider adding a test case that reproduces the issue mentioned in Tanstack-query optimistic update occasionally fails silently #1774 (Tanstack-query optimistic update occasionally fails silently) to prevent regression.
  3. If the optimistic update fix requires changes to the plugin implementation, make sure to update the relevant plugin tests in this file.

These steps will help ensure that the PR fully addresses the reported issue while maintaining the overall integrity of the plugin system.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 08ac462 and a8ed078.

⛔ Files ignored due to path filters (2)
  • packages/plugins/tanstack-query/package.json is excluded by !**/*.json
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml, !**/*.yaml
📒 Files selected for processing (2)
  • packages/plugins/tanstack-query/tests/plugin.test.ts (1 hunks)
  • tests/integration/tests/cli/plugins.test.ts (1 hunks)
🧰 Additional context used
🔇 Additional comments (3)
tests/integration/tests/cli/plugins.test.ts (2)

Line range hint 1-391: Verify test suite integrity after version update.

While the change to the Tanstack Query version is localized, it's crucial to ensure that it doesn't inadvertently affect other parts of the test suite.

Please run the entire test suite to confirm that all tests still pass with the updated Tanstack Query version. You can use the following command:

This will help catch any potential regressions caused by the version update.


76-76: Approve version update with a compatibility check suggestion.

The update to @tanstack/react-query@5.56.x is appropriate for pinning the major version while allowing minor updates. This change aligns with the PR objective of addressing issues with Tanstack-query.

To ensure this change doesn't introduce any compatibility issues, please run the following command to check for any breaking changes or deprecations in the Tanstack Query changelog between the previous version and 5.56.x:

This will help identify any potential issues that might affect the optimistic update functionality or other areas of the codebase.

packages/plugins/tanstack-query/tests/plugin.test.ts (1)

Line range hint 1-359: Verify test suite integrity after @tanstack/react-query version update.

The change to the @tanstack/react-query version in the 'react-query run plugin v5' test case is the only modification in this file. While this change is isolated, it's crucial to ensure that it doesn't inadvertently affect other tests or the overall behavior of the plugin.

To maintain the integrity of the test suite:

  1. Run the entire test suite to confirm that all tests still pass with the updated dependency version.
  2. Pay special attention to any tests related to optimistic updates, as mentioned in the PR objectives.
  3. Consider adding specific tests for the optimistic update scenario that this PR aims to fix.

Please run the following script to execute the test suite and check for any failures or unexpected behaviors:

#!/bin/bash
# Description: Run the test suite and highlight any failures or warnings

# Run the test suite
npm test -- packages/plugins/tanstack-query/tests/plugin.test.ts > test_output.txt 2>&1

# Check for any test failures
if grep -q "FAIL" test_output.txt; then
  echo "Test failures detected:"
  grep -A 5 "FAIL" test_output.txt
else
  echo "All tests passed successfully."
fi

# Check for any warnings
if grep -q "Warning" test_output.txt; then
  echo "Warnings detected:"
  grep -A 2 "Warning" test_output.txt
fi

# Clean up
rm test_output.txt

This script will help ensure that the version update hasn't introduced any regressions in the test suite.

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: 0

🧹 Outside diff range and nitpick comments (2)
tests/integration/tests/enhancements/with-delegate/plugin-interaction.test.ts (2)

22-22: Approve the version update with a minor suggestion.

The update to '@tanstack/react-query@5.56.x' is a good change as it helps ensure consistency in testing environments. This aligns well with the PR objective of addressing issues with Tanstack-query optimistic updates.

Consider adding a comment explaining why this specific version was chosen. For example:

extraDependencies: [
  '@tanstack/react-query@5.56.x', // Specific version to reproduce and fix optimistic update issue
],

This would provide context for future maintainers about the reasoning behind this version constraint.


Line range hint 1-156: Consider reviewing other test cases for version consistency.

While the update to the Tanstack Query test case is appropriate, it might be beneficial to review the other test cases (swr, trpc, zod) to ensure they are also using specific versions of their respective dependencies. This could help maintain consistency across all integration tests and potentially prevent future issues related to dependency versioning.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between a8ed078 and 3a284cf.

📒 Files selected for processing (1)
  • tests/integration/tests/enhancements/with-delegate/plugin-interaction.test.ts (1 hunks)
🧰 Additional context used

@ymc9 ymc9 merged commit 178f697 into dev Oct 14, 2024
13 checks passed
@ymc9 ymc9 deleted the fix/issue-1774 branch October 14, 2024 18:54
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