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

querykeys not being fuzzy matched #5111

Closed
juliusmarminge opened this issue Mar 10, 2023 · 4 comments
Closed

querykeys not being fuzzy matched #5111

juliusmarminge opened this issue Mar 10, 2023 · 4 comments

Comments

@juliusmarminge
Copy link
Contributor

Describe the bug

When filtering queries using getQueryData, it seems like the queryKey isn't being fuzzy matched. In tRPC, we have queryKeys like [["post", "all"], { input: { id: 1 } }], and so we should be able to match this by looking for just [["post", "all"]], but that doesn't seem to be the case.

This is the original issue we got: trpc/trpc#3937

Your minimal, reproducible example

https://stackblitz.com/edit/github-kuehgz-xevj3b?file=package.json

Steps to reproduce

See stackblitz. The only way you seem to get the data is by looking for the key exactly.

Expected behavior

const getQueryData = queryClient.getQueryData(lazyKey); should return "hello", not undefined

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

macOS, Arc

Tanstack Query adapter

react-query

TanStack Query version

4.26.1

TypeScript version

4.8.3

Additional context

No response

@TkDodo
Copy link
Collaborator

TkDodo commented Mar 11, 2023

getQueryData doesn't do fuzzy matching - the filter is set to exact: true:

if (typeof filters.exact === 'undefined') {
filters.exact = true
}

General rule of thumb:

  • functions with a singular in the name (getQueryData) operate on one query, so exact
  • functions with a pluralr in the name (invalidateQueries) operate on multiple queries, so they match fuzzily.

The reason is quite simple:

What would getQueryData(['todos']) return if we have the following keys in the cache:

['todos', 1],
['todos', { done: true }]
['todos']

if we match this fuzzily, it would be non-deterministic which one would be returned. Probably the one that was first inserted, but it's an implementation detail.

I'm not sure what the use-case of yours is, but you can:

  • call queryClient.getQueriesData(key) to get an Array of all matching queries
  • call queryClieng.getQueryCache().find({ queryKey, exact: false }) but again, if you have multiple matching things, don't rely that you always get the same thing back :)

@TkDodo TkDodo closed this as not planned Won't fix, can't repro, duplicate, stale Mar 11, 2023
@juliusmarminge
Copy link
Contributor Author

Alright thanks for the response! Closed our issue too :)

@juliusmarminge
Copy link
Contributor Author

Do you have a section in the docs explaining when you do fuzzy matching? Was going to make a note in our docs about this behavior

@TkDodo
Copy link
Collaborator

TkDodo commented Mar 17, 2023

Do you have a section in the docs explaining when you do fuzzy matching? Was going to make a note in our docs about this behavior

hmm, no 🙈 . Filters are fuzzy unless you specify exact: true, and some functions do that under the hood. This still applies:

General rule of thumb:

functions with a singular in the name (getQueryData) operate on one query, so exact
functions with a pluralr in the name (invalidateQueries) operate on multiple queries, so they match fuzzily.

If you want, you can add it to the QueryFilters page?
https://tanstack.com/query/v4/docs/react/guides/filters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants