You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
refactor(queryClient): add dev warning with queryDefaults (#3249)
* refactor(QueryClient): add dev warning
Warn when several query defaults match a given key.
Could be error prone if the returned defaults are not the expected ones.
The order of registration does matter.
* test(QueryClient): warning with defaults options
Highlight how query defaults registration order matters.
* doc(QueryClient): add notes about query defaults
In `getQueryDefaults`, the **first** matching default is returned.
In `setQueryDefaults`, highlight how the registration order is important.
* doc(QueryClient): fix link to documentation
* test(QueryClient): better test
* refactor(QueryClient): use internal logger
* doc(QueryClient): fix markup
* doc(QueryClient): remove extra entry
* refacto(QueryClient): warn about several query defaults
Warning must be displayed any time a conflict is detected, not just for dev build.
The warning is aimed at helping developers *using* react-query, not those *developping* react-query.
* Update src/core/queryClient.ts
Remove useless optional chaining.
Co-authored-by: Dominik Dorfmeister <[email protected]>
* feat(utils): add assert helper
* refactor(QueryClient): add dev warning for mutation defaults
* Revert "feat(utils): add assert helper"
This reverts commit 05c3fe1.
* refactor(QueryClient): error when several defaults
Review how the check for multiple defaults on a key is raised.
Ensure it remains fast in release build.
* refactor(QueryClient): inline code
Co-authored-by: Guillaume Labat <[email protected]>
Co-authored-by: Dominik Dorfmeister <[email protected]>
> As stated in [`getQueryDefaults`](#queryclientgetquerydefaults), the order of registration of query defaults does matter.
498
+
> Since the **first** matching defaults are returned by `getQueryDefaults`, the registration should be made in the following order: from the **least generic key** to the **most generic one**. This way, in case of specific key, the first matching one would be the expected one.
499
+
494
500
## `queryClient.getMutationDefaults`
495
501
496
502
The `getMutationDefaults` method returns the default options which have been set for specific mutations:
@@ -516,6 +522,8 @@ function Component() {
516
522
-`mutationKey: string | unknown[]`
517
523
-`options: MutationOptions`
518
524
525
+
> Similar to [`setQueryDefaults`](#queryclientsetquerydefaults), the order of registration does matter here.
526
+
519
527
## `queryClient.getQueryCache`
520
528
521
529
The `getQueryCache` method returns the query cache this client is connected to.
// It is ok not having defaults, but it is error prone to have more than 1 default for a given key
555
+
if(matchingDefaults.length>1){
556
+
getLogger().error(
557
+
`[QueryClient] Several query defaults match with key '${JSON.stringify(
558
+
queryKey
559
+
)}'. The first matching query defaults are used. Please check how query defaults are registered. Order does matter here. cf. https://react-query.tanstack.com/reference/QueryClient#queryclientsetquerydefaults.`
// It is ok not having defaults, but it is error prone to have more than 1 default for a given key
600
+
if(matchingDefaults.length>1){
601
+
getLogger().error(
602
+
`[QueryClient] Several mutation defaults match with key '${JSON.stringify(
603
+
mutationKey
604
+
)}'. The first matching mutation defaults are used. Please check how mutation defaults are registered. Order does matter here. cf. https://react-query.tanstack.com/reference/QueryClient#queryclientsetmutationdefaults.`
0 commit comments