-
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
Changes from 3 commits
5a9683f
210cd5c
73314a2
261be53
556b66b
d9fcc9d
21f7381
20c4384
71e0b89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2022, Salesforce, Inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
import {DefaultOptions, QueryClientConfig} from '@tanstack/react-query' | ||
|
||
const defaultOptions: DefaultOptions = { | ||
queries: { | ||
retry: 2, | ||
refetchOnMount: 'always', | ||
refetchOnWindowFocus: 'always', | ||
refetchOnReconnect: 'always', | ||
cacheTime: Infinity, //30 seconds | ||
refetchInterval: 1000 * 30, //30 seconds | ||
refetchIntervalInBackground: false, | ||
suspense: false, | ||
staleTime: 1000 * 30 | ||
}, | ||
mutations: { | ||
retry: 2 | ||
} | ||
} | ||
|
||
const defaultQueryClientConfig: QueryClientConfig = {defaultOptions} | ||
|
||
export {defaultQueryClientConfig} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}], | ||
useProduct: (arg: Record<string, unknown>) => [ | ||
{...productKeys.all[0], scope: 'detail', ...arg}, | ||
], | ||
} | ||
|
||
const categoryKeys = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 commentThe 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. |
||
all: [{entity: ['category']}], | ||
useCategories: (arg: Record<string, unknown>) => [ | ||
{...categoryKeys.all[0], scope: 'list', ...arg}, | ||
], | ||
useCategory: (arg: Record<string, unknown>) => [ | ||
{...categoryKeys.all[0], scope: 'detail', ...arg}, | ||
], | ||
} | ||
|
||
type UseProductsParameters = NonNullable<Argument<Client['getProducts']>>['parameters'] | ||
type UseProductsHeaders = NonNullable<Argument<Client['getProducts']>>['headers'] | ||
type UseProductsArg = {headers?: UseProductsHeaders; rawResponse?: boolean} & UseProductsParameters | ||
|
@@ -39,7 +57,10 @@ function useProducts( | |
const {shopperProducts: client} = useCommerceApi() | ||
const {headers, rawResponse, ...parameters} = arg | ||
return useAsync( | ||
['products', arg], | ||
// Query Key needs to match "options" | ||
// ['products', {headers, rawResponse, ...parameters}], | ||
productKeys.useProducts(arg), | ||
// Don't send "options" different from the queryKey | ||
() => client.getProducts({parameters, headers}, rawResponse), | ||
options | ||
) | ||
|
@@ -73,7 +94,7 @@ function useProduct( | |
const {headers, rawResponse, ...parameters} = arg | ||
const {shopperProducts: client} = useCommerceApi() | ||
return useAsync( | ||
['product', arg], | ||
productKeys.useProduct(arg), | ||
() => client.getProduct({parameters, headers}, rawResponse), | ||
options | ||
) | ||
|
@@ -111,7 +132,7 @@ function useCategories( | |
|
||
const {shopperProducts: client} = useCommerceApi() | ||
return useAsync( | ||
['categories', arg], | ||
categoryKeys.useCategories(arg), | ||
() => client.getCategories({parameters, headers}, rawResponse), | ||
options | ||
) | ||
|
@@ -148,7 +169,7 @@ function useCategory( | |
|
||
const {shopperProducts: client} = useCommerceApi() | ||
return useAsync( | ||
['category', arg], | ||
categoryKeys.useCategory(arg), | ||
() => client.getCategory({parameters, headers}, rawResponse), | ||
options | ||
) | ||
|
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:
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.