question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Customize default query keys

See original GitHub issue

Is 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:open
  • Created 2 years ago
  • Reactions:5
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
anymaniaxcommented, Jul 10, 2022

@missing1984 it’s on my todo list for the next version. Sorry I have a lot of stuff to do lately

3reactions
mpetito-envativecommented, Nov 1, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found