Skip to content
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

Closed
wants to merge 9 commits into from

Conversation

adamraya
Copy link
Collaborator

@adamraya adamraya commented Aug 31, 2022

Description

The PR implements the initial React Query cache state optimizations in commerce-sdk-react:

  • Adjust default queryClient configuration values.
  • Define the Query Key pattern to use in all queries using the Query Key factory pattern. Introducing the usage of the 'entity' and 'scope: list | detail' concepts inside the Query Key.
  • Optimistic updates by seeding future scope: 'detail' queries when executing scope: 'list' queries.
  • TODO: Invalidate queries in the cache after executing mutation queries.

Types of Changes

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Documentation update
  • Breaking change (could cause existing functionality to not work as expected)
  • Other changes (non-breaking changes that does not fit any of the above)

Breaking changes include:

  • Removing a public function or component or prop
  • Adding a required argument to a function
  • Changing the data type of a function parameter or return value
  • Adding a new peer dependency to package.json

Changes

  • (change1)

How to Test-Drive This PR

  • (step1)

Checklists

General

  • Changes are covered by test cases
  • CHANGELOG.md updated with a short description of changes (not required for documentation updates)

Accessibility Compliance

You must check off all items in one of the follow two lists:

  • There are no changes to UI

or...

Localization

  • Changes include a UI text update in the Retail React App (which requires translation)

@@ -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}],
Copy link
Contributor

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}]

Copy link
Contributor

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`

Copy link
Collaborator Author

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 = {
Copy link
Contributor

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'})

Copy link
Collaborator Author

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants