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:

  1. Create a query with useQuery(['a', 'b', 'c'], getFunction, { useQuery: () => ['e'] });
  2. After mutation, try to update the cached data for that query queryClient.setQueryData(['e'], updatedObject) or queryClient.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)

Commits related to this issue

Most upvoted comments

@TkDodo Sorry if it’s not clear. I’m not overriding the hash Here is the case,

  1. When I’m using useQuery, the queryFn I have is expecting some more data to be passed to it. therefore I’m doing something like useQuery([QUERY_KEY, GRAPHQL_CLIENT, USER_ID], myQueryFn, {myQueryKeyHashFn, ...options}); and then the myQueryFn is reading/extracting GRAPHQL_CLIENT and the USER_ID and using them under the hood. Please note that I’m also using myQueryKeyHashFn to make that complex array [QUERY_KEY, GRAPHQL_CLIENT, USER_ID] to a simple string like CUSTOM_QUERY_KEY.
  2. After successful mutation, I like to call setQueryData and I like to be able to use CUSTOM_QUERY_KEY as 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.