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

feat: Add type to react-query query key - Attempt round 2! #3170

Merged
merged 13 commits into from
Nov 14, 2022

Conversation

JonParton
Copy link
Contributor

Closes #3128

transferred from #3165

🎯 Changes

This pull request alters the react-query queryKey to also include the type of the query (query or infinite) as well as placing the input within an object alongside it.

New key Structure:

[ 
   string[], //arrayPath!
   { 
      input?: any; 
      type: 'query' | 'infinite' 
   }
]

Example:
image

This change will mean that an infinite query and a query of the same endpoint will have different entries within the cache, which is required as the data structures are different!!

This key structure also maintains the ability to invalidate specifically query's or infinite's across entire routers as well as filtering queries using their input when interacting with the react-query queryClient directly (Escape Hatch! 👨‍🚒).

🔨 Possible improvements

Currently the exposed invalidate() function on a router or on a leaf node will invalidate both queries and infiniteQueries with no way to filter. The ability to specify only queries or infiniteQueries could be added at a later date but I think for now this should be fine and in line with the philosophy of being indiscriminate added in #3124

✅ Checklist

  • I have followed the steps listed in the Contributing guide.
  • If necessary, I have added documentation related to the changes made.
  • I have added or updated the tests related to the changes made.

@vercel
Copy link

vercel bot commented Nov 14, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
next-prisma-starter ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Nov 14, 2022 at 1:26PM (UTC)
www ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Nov 14, 2022 at 1:26PM (UTC)

@JonParton
Copy link
Contributor Author

review comments from @KATT can be found on the closed fork #3165 (My bad! 🤦)

@JonParton
Copy link
Contributor Author

  • remaining action is to add a test for utils.invalidate() to show it works on both infinite and query => I can do this later but may get held up with my work day; feel free to add if it is holding things up!

Copy link
Member

@KATT KATT left a 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 great, but I would love some more testing. Happy to merge as-is for now though!

  • Make sure that invalidate() or reset etc will invalidate both queries and infinite queries. Only need to test 1 I think, they are exactly the same

@@ -499,7 +502,7 @@ export const appRouter = router({
The query key used for `trpc.user.byId.useQuery({ id: 10 })` would change:

- Key in V9: `["user.byId", { id: 10 }]`
- Key in v10:`[["user", "byId"],{ id:10 }]`
- Key in v10:`[["user", "byId"],{ input: { id:10 }, type: 'query' }]`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice catch

@@ -301,7 +301,7 @@ export function createHooksInternal<
(...args: any[]) => {
const [queryKey, ...rest] = args;
return queryClient.invalidateQueries(
getArrayQueryKey(queryKey),
getArrayQueryKey(queryKey, 'any'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is prefect and that we don't need to allow granular invalidation for now, I don't see why anyone would not want to invalidate the same query across both methods of fetching it.

@@ -312,23 +312,25 @@ export function createHooksInternal<
const [queryKey, ...rest] = args;

return queryClient.refetchQueries(
getArrayQueryKey(queryKey),
getArrayQueryKey(queryKey, 'any'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is prefect and that we don't need to allow granular invalidation for now, I don't see why anyone would not want to refetch the same query across both methods of fetching it.

...rest,
);
},
[queryClient],
),
cancelQuery: useCallback(
(pathAndInput) => {
return queryClient.cancelQueries(getArrayQueryKey(pathAndInput));
return queryClient.cancelQueries(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is prefect and that we don't need to allow granular invalidation for now, I don't see why anyone would not want to cancel the same query across both methods of fetching it.

@KATT KATT enabled auto-merge (squash) November 14, 2022 13:19
@KATT KATT mentioned this pull request Nov 14, 2022
1 task
@KATT KATT merged commit 50b2acd into next Nov 14, 2022
@KATT KATT deleted the round-2-add-type-to-reactquery-query-key branch November 14, 2022 13:26
KATT added a commit that referenced this pull request Nov 18, 2022
Co-authored-by: KATT <alexander@n1s.se>
KATT added a commit that referenced this pull request Nov 26, 2022
Co-authored-by: KATT <alexander@n1s.se>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
♻️ autoupdate @trpc/react-query 🕸 www Stuff to do with the website (`/www` folder)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: prefix infinite queries differently to "normal" queries?
2 participants