fix(tanstack-query): client context should include inside query/mutation keys#793
fix(tanstack-query): client context should include inside query/mutation keys#793
Conversation
…ion key Queries have different client context can behave differently, so we should treat them as separate queries
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
WalkthroughThe changes extend the key generation and typing system to support an explicit Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant ProcedureUtils
participant generateOperationKey
Caller->>ProcedureUtils: queryKey({ type, input, context })
ProcedureUtils->>generateOperationKey: generateOperationKey(path, { type, input, context })
generateOperationKey-->>ProcedureUtils: OperationKey tuple (includes context)
ProcedureUtils-->>Caller: OperationKey tuple (includes context)
Possibly related PRs
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (5)
🧰 Additional context used🧬 Code Graph Analysis (1)packages/tanstack-query/src/procedure-utils.ts (3)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
🔇 Additional comments (7)
✨ 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:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. 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 (
|
There was a problem hiding this comment.
Summary of Changes
Hello @unnoq, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request resolves a critical issue in the TanStack Query integration by ensuring that the client context is included in the generated query and mutation keys. This modification is essential for correctly distinguishing operations that might otherwise appear identical but behave differently based on their client-specific configurations, thereby preventing incorrect deduplication and ensuring proper data management within the cache.
Highlights
- Improved Deduplication Logic: This change directly addresses the issue of incorrect deduplication where queries/mutations with varying client contexts (e.g., different retry behaviors) were being treated as identical. By incorporating context into the key, each unique context now results in a unique key, preventing unintended data fetching or caching behaviors.
- Type System Enhancements: Extensive updates have been made to the type definitions, including
OperationKeyOptions,OperationKey,QueryKeyOptions,experimental_StreamedKeyOptions, and the introduction ofMutationKeyOptions. These changes properly propagate and enforce the inclusion ofTClientContextthroughout the key generation and utility functions. - Utility Function Integration & Test Coverage: All relevant utility functions (
queryKey,experimental_streamedKey,experimental_liveKey,infiniteKey,mutationKey) now correctly pass the client context to the underlying key generation logic. Corresponding test cases have been added or updated to validate this new behavior, ensuring the context is properly reflected in the generated keys.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
Codecov ReportAll modified and coverable lines are covered by tests ✅ 📢 Thoughts on this report? Let us know! |
More templates
@orpc/arktype
@orpc/client
@orpc/contract
@orpc/experimental-durable-event-iterator
@orpc/hey-api
@orpc/json-schema
@orpc/nest
@orpc/openapi
@orpc/openapi-client
@orpc/react
@orpc/react-query
@orpc/server
@orpc/shared
@orpc/solid-query
@orpc/standard-server
@orpc/standard-server-aws-lambda
@orpc/standard-server-fetch
@orpc/standard-server-node
@orpc/standard-server-peer
@orpc/svelte-query
@orpc/tanstack-query
@orpc/trpc
@orpc/valibot
@orpc/vue-colada
@orpc/vue-query
@orpc/zod
commit: |
There was a problem hiding this comment.
Code Review
The pull request introduces client context to query/mutation keys, which is essential for differentiating queries. The changes are well-structured and cover necessary parts of the codebase. I have provided suggestions to improve code readability and maintainability.
| options.path, | ||
| { type: 'query', input: optionsIn.input, context: optionsIn.context }, |
There was a problem hiding this comment.
Consider destructuring the optionsIn object to improve readability and make it clear which properties are being used. This can also help prevent accidental usage of unintended properties.
const { input, context } = optionsIn;
const queryKey = optionsIn.queryKey ?? generateOperationKey(
options.path,
{ type: 'query', input, context },
)| options.path, | ||
| { type: 'streamed', input: optionsIn.input, fnOptions: optionsIn.queryFnOptions, context: optionsIn.context }, |
There was a problem hiding this comment.
Consider destructuring the optionsIn object to improve readability and make it clear which properties are being used. This can also help prevent accidental usage of unintended properties.
const { input, queryFnOptions, context } = optionsIn;
const queryKey = optionsIn.queryKey ?? generateOperationKey(
options.path,
{ type: 'streamed', input, fnOptions: queryFnOptions, context },
)| options.path, | ||
| { type: 'live', input: optionsIn.input, context: optionsIn.context }, |
There was a problem hiding this comment.
Consider destructuring the optionsIn object to improve readability and make it clear which properties are being used. This can also help prevent accidental usage of unintended properties.
const { input, context } = optionsIn;
const queryKey = optionsIn.queryKey ?? generateOperationKey(
options.path,
{ type: 'live', input, context },
)| options.path, | ||
| { type: 'mutation', context: optionsIn.context }, |
There was a problem hiding this comment.
Consider destructuring the optionsIn object to improve readability and make it clear which properties are being used. This can also help prevent accidental usage of unintended properties.
const { context } = optionsIn;
const mutationKey = optionsIn.mutationKey ?? generateOperationKey(
options.path,
{ type: 'mutation', context },
)|
However, this doesn't fully solve the root problem. Some client context values aren't suitable for use as query/mutation keys (e.g., functions like callbacks may be ignored by TanStack Query), and users might not want to include certain context values in the key. Additionally, TanStack Query has its own retry behavior. |
Queries have different client context can behave differently, so we should treat them as separate queries
E.g. one infinite retry request, and one normal request can be dedupe into one by tanstack query, while they should behave differently.
Summary by CodeRabbit
New Features
contextobject in key generation utilities, allowing more flexible client context handling across queries, mutations, and related operations.Bug Fixes
contextobject in key generation.Refactor
contextproperty where applicable.