Replies: 2 comments
-
Thank you for the question, that's an interesting problem for sure. It seems that exact matches should take precedence over loose ones. However, even if we do that, in case of multiple loose matches we would still have to depend on the order I think ... |
Beta Was this translation helpful? Give feedback.
-
Exact match might not be the solution, at least for some use-cases. As a temporary addition, could we imagine something like that? (Pure JS, I'm not TS-fluent yet 😅) findQueryDefaults(queryKey) {
// First retrieve all matching defaults for the given key
const matchingDefaults = this.queryDefaults.filter((x) =>
partialMatchKey(queryKey, x.queryKey)
)
// It is ok not having defaults, but it is error prone to have more than 1 default for a given key
assert(
matchingDefaults.length <= 1,
`[QueryClient] Several defaults match with key '${JSON.stringify(
queryKey
)}'. Please check how query defaults are registered. Order does matter here. cf. http://react-query.com/some/link/to/document#queryDefaults`
)
// Explicitly returns the first one
const firstMatchingDefaults = matchingDefaults?.[0]
return firstMatchingDefaults?.defaultOptions || undefined
}
getQueryDefaults(queryKey) {
const queryDefaults = this.findQueryDefaults(queryKey)
return queryDefaults?.defaultOptions
} A little bit of overhead since we browse the whole |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
We use
setQueryDefaults
/getQueryDefaults
in our app to factorise and centralize query behaviours while keeping usage simple.Once defaults are registered, the only thing we use is a key factory like:
To be able to do that, we register defaults for our queries.
On some of our entities, we have several defaults, associated to different keys.
While implementing all this, we identify a behaviour which could be considered as WAD, but which triggers the discussion here, since it may also be considered as a bug.
The following CodeSandbox tries to illustrate the issue: https://codesandbox.io/s/nifty-ride-q3ln3?file=/src/index.js.
The "problem" is the order of registration of query defaults.
After some code reading sessions, the behaviour is WAD in the sense that
queryDefaults
ofQueryClient
is an Array.When registering some defaults, RQ looks for existing default with strict key matching and either replaces existing defaults (no order change) or push into an array.
To get query defaults from a key, RQ checks for defaults using a loose key matching AND
find
Array method.==> Since
find
Array method is used, the first matching defaults are returned, which may not be the expected ones. <==There seems to be no way to prevent from these default key collisions to happen, some actions could be taken:
path
in standard client-side router, thequeryDefaults
associated with the most generic key must be registered last.Note:
partialMatchKey
(used ingetQueryDefaults
) uses internallypartialDeepEqual
(code here).We could narrow the behaviour above with the following code:
Feedbacks would be welcome.
And contributing to either the doc or the code is an option for us.
Thanks for reading!
Beta Was this translation helpful? Give feedback.
All reactions