Customize default query keys
See original GitHub issueIs it possible to configure the convention used for query key generation?
I see that you can override the query key when using the generated hook, but this requires remembering to do so each time. Instead, I’d like to configure how the query key is constructed project-wide because our APIs already have consistent hierarchical routes.
For ex., instead of this generated query key:
export const getGetResourceByIdQueryKey = (resourceId: number,) => [`/resource/${resourceId}`];
It would make a lot more sense in my project for the query key to be split by path segments, e.g.:
['resource', resourceId]
I don’t see any obvious way to configure or override the convention used for query keys.
My motivation is to simplify query invalidation by having predictable query keys and support prefix invalidation.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:5
- Comments:11 (4 by maintainers)
Top Results From Across the Web
Default Query Function - TanStack
Define a default query function that will receive the query key ... provide the default query function to your app with defaultOptions.
Read more >Customize design settings for objects in your database
You can set database options that can be used to modify the design of Access desktop database objects by clicking File, Options, and...
Read more >Effective React Query Keys | TkDodo's blog
Learn how to structure React Query Keys effectively as your App grows. ... I usually only export custom hooks, so the actual Query...
Read more >Configuring the Apollo Client cache - Apollo GraphQL Docs
By default, the cache uses the __typename field as part of the cache ID for every ... Each key in the object is...
Read more >Manipulating the cache key | Fastly Help Guides
By default, Fastly uses the URL and the Host of a request (plus a ... the cache key, but you don't want to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@missing1984 it’s on my todo list for the next version. Sorry I have a lot of stuff to do lately
It seems like this would be a runtime behavior because the queryKey depends on properties passed into the query hook.
It is already possible to do something similar with the mutator override for the purpose of replacing the http request. Perhaps a queryKey override would allow for replacement of the queryKeyFn at runtime?
Alternatively (or in addition), it might make sense to change the default convention used for queryKey generation. I know this would be a breaking change, but I can’t think of any disadvantage to splitting the route on
/
and it would offer an easy path for prefix invalidation if routes already represent a hierarchy.