Skip to content

Commit 3124193

Browse files
authored
docs: describe query default changes in v5 (#8488)
Fixes #8485
1 parent 8f72f59 commit 3124193

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

docs/framework/react/guides/migrating-to-v5.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,33 @@ This last change is technically a breaking one, and was made so we don't prematu
420420
+ </HydrationBoundary> // [!code ++]
421421
```
422422

423+
### Query defaults changes
424+
425+
`queryClient.getQueryDefaults` will now merge together all matching registrations instead of returning only the first matching registration.
426+
427+
As a result, calls to `queryClient.setQueryDefaults` should now be ordered with _increasing_ specificity.
428+
That is, registrations should be made from the **most generic key** to the **least generic one**.
429+
430+
For example:
431+
432+
```ts
433+
+ queryClient.setQueryDefaults(['todo'], { // [!code ++]
434+
+ retry: false, // [!code ++]
435+
+ staleTime: 60_000, // [!code ++]
436+
+ }) // [!code ++]
437+
queryClient.setQueryDefaults(['todo', 'detail'], {
438+
+ retry: true, // [!code --]
439+
retryDelay: 1_000,
440+
staleTime: 10_000,
441+
})
442+
- queryClient.setQueryDefaults(['todo'], { // [!code --]
443+
- retry: false, // [!code --]
444+
- staleTime: 60_000, // [!code --]
445+
- }) // [!code --]
446+
```
447+
448+
Note that in this specific example, `retry: true` was added to the `['todo', 'detail']` registration to counteract it now inheriting `retry: false` from the more general registration. The specific changes needed to maintain exact behavior will vary depending on your defaults.
449+
423450
[//]: # 'FrameworkSpecificBreakingChanges'
424451

425452
## New Features 🚀

docs/reference/QueryClient.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,8 @@ The `getQueryDefaults` method returns the default options which have been set fo
522522
const defaultOptions = queryClient.getQueryDefaults(['posts'])
523523
```
524524

525-
> Note that if several query defaults match the given query key, the **first** matching one is returned.
526-
> This could lead to unexpected behaviours. See [`setQueryDefaults`](#queryclientsetquerydefaults).
525+
> Note that if several query defaults match the given query key, they will be merged together based on the order of registration.
526+
> See [`setQueryDefaults`](#queryclientsetquerydefaults).
527527
528528
## `queryClient.setQueryDefaults`
529529

@@ -543,7 +543,8 @@ function Component() {
543543
- `options: QueryOptions`
544544

545545
> As stated in [`getQueryDefaults`](#queryclientgetquerydefaults), the order of registration of query defaults does matter.
546-
> 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.
546+
> Since the matching defaults are merged by `getQueryDefaults`, the registration should be made in the following order: from the **most generic key** to the **least generic one** .
547+
> This way, more specific defaults will override more generic defaults.
547548
548549
## `queryClient.getMutationDefaults`
549550

0 commit comments

Comments
 (0)