Query key choice for join-like queries #6678
Replies: 3 comments 2 replies
-
Maybe you should create one more entity with an appropriate name? And that new entity should have its own invalidate-function. |
Beta Was this translation helpful? Give feedback.
-
Yes, that would work for that example but I think I over-simplified my use case, the two tables are inextricably linked in the request e,g. useQuery({
queryKey: ['/table/a/rows', '/table/b/rows'],
queryFn: async () => {
return getAndJoinTwoTables('/table/a/rows', { foreignKey: 'id', foreignTable: '/table/b/rows' )
}
}) |
Beta Was this translation helpful? Give feedback.
-
I see two ways to achieve this: Object Query KeysArray Query Keys have a hierarchy, so in your case,
Now,
as well as
and both will invalidate the one entry you have. I've written about this pattern here: https://tkdodo.eu/blog/leveraging-the-query-function-context#object-query-keys useQueries with combineAnother question that came to my mind is: why do we have one query that fetches both
This also has the advantage of I'd choose the useQueries option with combine if you're already on v5. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
As asked by @TkDodo, I've re-posted this on here for posterity.
I'm not sure how to specify the query keys in a way that makes invalidation sensible to achieve in a join-like situation.
I essentially have a request that does a join, e.g.
If I update table a, that's easy:
client.invalidateQueries(['/tables/a/rows'])
for a non-exact match.But if I update table b, I'm not sure what the best approach would be?
Beta Was this translation helpful? Give feedback.
All reactions