You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
queryKeyHashFn() overwrites all other useQuery() calls #6237
now the query we pass in here is the one that exists (['posts']), which is the one with the custom queryKeyHashFn.
BUT the queryKey we want to compare it to is something like ['post', 29]. So now we take ['post', 29] and we hash it with the options from the ['posts'] query.
Note that this doesn't really affect useQuery calls as your issue states, this is only for matching a query against another query, which is what getQueryData does.
Tbh, the only use-cases I have seen for a custom hashFn was to set it globally to allow for non-serializable values. What's your use-case where you have to set this on a per-query basis?
We could say the behaviour to perform the hash of the queries to test with the hashing function of the Query we're currently comparing against is wrong and just use the default hashKey method, but this would lead to no query being found if that query actually uses a custom hashFn. So, I don't see a way to "fix" this right now.
@TkDodo thanks for the explanation, that makes sense.
I'm using tRPC and its React Query integration, so the query key components are determined by tRPC.
I have query procedure listPosts which accepts some generic parameters and and optional lastUpdatedAt timestamp.
The query key looks something like this:
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:
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:
That means it sets the data for query key 1 (without lastUpdatedAt) and 2 (with lastUpdatedAt).
I know that onSuccess was deprecated in v5, so I'm looking for an alternative option. The queryKeyHashFn sounded like it would allow me to manipulate the query key components (provided by tRPC) and simply remove the lastUpdatedAt from the key (but keep it in the input). This way, I would only have query key 1 and the result from the server would always update this one query key. Furthermore, because I only have query key 1 which is always the same from React Query's perspective, I assume I would have to use refetch() to trigger a request to the server with a new lastUpdatedAt in the request payload.
The other alternative would be to have an option on tRPC to change the way it constructs its query keys. But I think they would refer me directly to the `queryKeyHashFn option.
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
If you really must do that, I would put lastUpdatedAt into the meta field. Not sure if trpc supports this though. Values in meta are passed to the queryFn without being part of the key.
Otherwise, invalidating the cache on the server seems like a mutation to me that you can fire?
Thanks, I will investigate this further. I assume, as a last resort, I could also use data from useQuery as deps for useEffect to synchronize all query keys when new data was fetched?
Yes, invalidation sounds like a good fit for a mutations. I didn't go that way, because I'm using DynamoDB to cache the data and the reads are eventually consistent. That means I can't guarantee that the request after the cache invalidation will receive the newest data (= no data).
Activity
TkDodo commentedon Oct 30, 2023
That's a tricky situation. Here's what's happening:
queryClient.getQueryDataagainst all keys:query/examples/react/basic/src/index.jsx
Lines 72 to 81 in e4e8930
getQueryDataperforms anexactsearch against all queries in the cache.query/packages/query-core/src/utils.ts
Lines 102 to 105 in e4e8930
querywe pass in here is the one that exists (['posts']), which is the one with the customqueryKeyHashFn.queryKeywe want to compare it to is something like['post', 29]. So now we take['post', 29]and we hash it with the options from the['posts']query.Note that this doesn't really affect
useQuerycalls as your issue states, this is only for matching a query against another query, which is whatgetQueryDatadoes.Tbh, the only use-cases I have seen for a custom hashFn was to set it globally to allow for non-serializable values. What's your use-case where you have to set this on a per-query basis?
We could say the behaviour to perform the hash of the queries to test with the hashing function of the Query we're currently comparing against is wrong and just use the default
hashKeymethod, but this would lead to no query being found if that query actually uses a custom hashFn. So, I don't see a way to "fix" this right now.zirkelc commentedon Oct 30, 2023
@TkDodo thanks for the explanation, that makes sense.
I'm using tRPC and its React Query integration, so the query key components are determined by tRPC.
I have query procedure
listPostswhich accepts some genericparametersand and optionallastUpdatedAttimestamp.The query key looks something like this:
This query is expensive, so I cache it also on the server with the given
parametersas caching key in case multiple clients request the same data. When I receive the sameparameterson 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 thelistPostswith an optionallastUpdatedAttimestamp. The query key is now:The servers receives the same
parametersas before and finds the data in the cache, but thelastUpdatedAttimestamp 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
lastUpdatedAtthat I sent in query key 2 is a local state that will get lost after page navigation. When I later get back to theListPostspage, it will use query key 1 again with data which was maybe set by query key 2.I'm currently using
onSuccessofuseQueryto set the query data of all query keys matching:That means it sets the data for query key 1 (without
lastUpdatedAt) and 2 (withlastUpdatedAt).I know that
onSuccesswas deprecated in v5, so I'm looking for an alternative option. ThequeryKeyHashFnsounded like it would allow me to manipulate the query key components (provided by tRPC) and simply remove thelastUpdatedAtfrom the key (but keep it in theinput). This way, I would only have query key 1 and the result from the server would always update this one query key. Furthermore, because I only have query key 1 which is always the same from React Query's perspective, I assume I would have to userefetch()to trigger a request to the server with a newlastUpdatedAtin the request payload.The other alternative would be to have an option on tRPC to change the way it constructs its query keys. But I think they would refer me directly to the `queryKeyHashFn option.
TkDodo commentedon Oct 30, 2023
If you really must do that, I would put
lastUpdatedAtinto themetafield. Not sure if trpc supports this though. Values inmetaare passed to the queryFn without being part of the key.Otherwise, invalidating the cache on the server seems like a mutation to me that you can fire?
zirkelc commentedon Oct 30, 2023
Thanks, I will investigate this further. I assume, as a last resort, I could also use
datafromuseQueryas deps foruseEffectto synchronize all query keys when new data was fetched?Yes, invalidation sounds like a good fit for a mutations. I didn't go that way, because I'm using DynamoDB to cache the data and the reads are eventually consistent. That means I can't guarantee that the request after the cache invalidation will receive the newest data (= no data).