query: queryClient.setQueryData does not support queryKeyHashFn
Describe the bug
Currently if we use queryKeyHashFn to customize the has key, in useQuery hook, it will result a string key. However when we want to update the client (cached) data for that key using queryClient.setQueryData, it does not support queryKeyHashFn and always updates the key as an array which is causing a key mismatched.
To Reproduce Steps to reproduce the behavior:
- Create a query with
useQuery(['a', 'b', 'c'], getFunction, { useQuery: () => ['e'] }); - After mutation, try to update the cached data for that query
queryClient.setQueryData(['e'], updatedObject)orqueryClient.setQueryData('e' updatedObject)
Expected behavior The data, associated to that query should be updated and replace the old data, but as the key is mismatched, you will see 2 query on the devtool.
React Query Version: 3.16.0
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 1
- Comments: 18 (2 by maintainers)
@TkDodo Sorry if it’s not clear. I’m not overriding the hash Here is the case,
useQuery, the queryFn I have is expecting some more data to be passed to it. therefore I’m doing something likeuseQuery([QUERY_KEY, GRAPHQL_CLIENT, USER_ID], myQueryFn, {myQueryKeyHashFn, ...options});and then themyQueryFnis reading/extractingGRAPHQL_CLIENTand theUSER_IDand using them under the hood. Please note that I’m also usingmyQueryKeyHashFnto make that complex array[QUERY_KEY, GRAPHQL_CLIENT, USER_ID]to a simple string likeCUSTOM_QUERY_KEY.setQueryDataand I like to be able to useCUSTOM_QUERY_KEYas the passed key.The issue is by doing so, the query is not matched with the initial one and therefore it’s not updating the main query client data.