-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
React Query default config and Query Key factory #707
Conversation
@@ -11,6 +11,24 @@ import {UseQueryOptions, UseQueryResult} from '@tanstack/react-query' | |||
|
|||
type Client = ApiClients['shopperProducts'] | |||
|
|||
const productKeys = { | |||
all: [{entity: 'product'}], | |||
useProducts: (arg: Record<string, unknown>) => [{...productKeys.all[0], scope: 'list', ...arg}], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for having the query key to be structured as an array of 1 item (which is an object)?
According to the official doc, it's possible to mix and match the array's content. For example, it can be like:
['product', 'list', {...arg}]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we use a multiple-items array as the query key, we can easily invalidate the queries granularly like this:
queryClient.invalidateQueries(['product']) // will invalidate both `useProduct` and `useProducts`
queryClient.invalidateQueries(['product', 'list']) // will invalidate only `useProducts`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason comes from a TkDodo's blog post: https://tkdodo.eu/blog/leveraging-the-query-function-context#object-query-keys
The author proposes to define all keys as an array with exactly one object.
The object inside the key contains the query data returned and some high-level strings we use to define the 'entity' and 'scope' ideas.
Using an object we don't need o to worry about the specific position of a particular key in the array. Instead, we can obtain the value of a particular key by using the object name destructuring.
And we can still use fuzzy matching query keys when we need to invalidate related queries during mutations.
], | ||
} | ||
|
||
const categoryKeys = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we can make a factory function that allows us to easily generate productKeys
and categoryKeys
. And calling it would be like this:
const categoryKeys = createQueryKeys('category', {useCategories: 'list', useCategory: 'detail'})
const productKeys = createQueryKeys('product', {useProducts: 'list', useProduct: 'detail'})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is the idea of the new NPM package @lukemorales/query-key-factory recently linked from the React Query docs: https://tanstack.com/query/v4/docs/guides/query-keys
I've been testing the package, but so far I see the package is not compatible with the idea mentioned in the previous comment of defining all keys as an array with exactly one object.
# Conflicts: # packages/commerce-sdk-react/src/hooks/ShopperProducts/query.ts # packages/commerce-sdk-react/src/hooks/useAsync.ts # packages/commerce-sdk-react/src/provider.tsx
Description
The PR implements the initial React Query cache state optimizations in
commerce-sdk-react
:queryClient
configuration values.'entity'
and 'scope: list | detail'
concepts inside the Query Key.scope: 'detail'
queries when executingscope: 'list'
queries.Types of Changes
Changes
How to Test-Drive This PR
Checklists
General
Accessibility Compliance
You must check off all items in one of the follow two lists:
or...
Localization