feat: overwrite query key on trpc.useQuery options
Problem
Describe the feature you'd like to request I have query procedure `listPosts` which accepts some generic `parameters` and and optional `lastUpdatedAt` timestamp. The query key looks something like this: [code block] This query is expensive, so I cache it also on the server with the given `parameters` as caching key in case multiple clients request the same data. When I receive the same `parameters` on the server, I return the data from the cache. Now I need some way for clients to force a refresh on the server returning new data instead of cached data for the same `parameters`. So in this case, I query the `listPosts` with an optional `lastUpdatedAt` timestamp. The query key is now: [code block] The servers receives the same `parameters` as before and finds the data in the cache, but the `lastUpdatedAt` timestamp is greater than the cached timestamp. So it runs the expensive operation on the server and returns the new data (and caches it). Now I have two query keys on the client which are logically the same. I must update the query data of key 1 and key 2 with new data, because query key 2 is only temporary to trigger a server cache-invalidation. The `lastUpdatedAt` that I sent in query key 2 is a local state that will get lost after page navigation. When I later get back to the `ListPosts` page, it will use query key 1 again with data which was maybe set by query key 2. I'm currently using `onSuccess` of `useQuery` to set the query data of all query keys matching t
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Solution: feat: overwrite query key on trpc.useQuery options
Hard disagree. The abstraction over cache keys is leaking. Utilizing other hooks to deal with the missing feature is not cleaner calling code by any stretch.
Trust Score
3 verifications
- 1
Hard disagree. The abstraction over cache keys is leaking. Utilizing other hooks
Hard disagree. The abstraction over cache keys is leaking. Utilizing other hooks to deal with the missing feature is not cleaner calling code by any stretch.
Validation
Resolved in trpc/trpc GitHub issue #4989. Community reactions: 4 upvotes.
Verification Summary
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep