query icon indicating copy to clipboard operation
query copied to clipboard

remove `queryHash` from useQuery options

Open TkDodo opened this issue 3 years ago • 2 comments

context

The queryHash is the result of the query key hashing, produced by the queryKeyHashFn. This function defaults to a stable JSON.stringify, but can also be passed in by users to do custom hashing, e.g. when non-json serializable things are used in the queryKey.

According to our TypeScript types, we can also pass in a queryHash directly into useQuery. This is not only undocumented, it's also redundant because you can achieve the same thing with a queryKeyHashFn:

- useQuery({ queryKey, queryFn, queryHash: 'myHash' })
+ useQuery({ queryKey, queryFn, queryKeyHashFn: () => 'myHash' })

proposal

  • remove queryHash from useQuery options
  • we'll still need the queryHash on the internal type DefaultedQueryObserverOptions, because that's where it gets set:

https://github.com/TanStack/query/blob/1e0745a6cc80cbdbafc3153812b7982a8f549447/packages/query-core/src/queryClient.ts#L740-L745

TkDodo avatar Dec 23 '22 07:12 TkDodo

Hi @TkDodo , if I understand the issue correctly... this would mean omitting out queryHash option from the UseQueryOptions type used in react query and other similar packages?

abhushanaj avatar Dec 25 '22 08:12 abhushanaj

I haven't thought it through completely, but removing it from UseQueryOptions and instead adding it to DefaultedQueryObserverOptions was my plan.

TkDodo avatar Dec 25 '22 12:12 TkDodo

looked at this today and I missed a couple of things, mainly hydration. There, we create a query with a specific hash, and we don't have the options to re-execute the hashing functions. I guess we have to close this.

TkDodo avatar Jan 20 '23 21:01 TkDodo