Skip to content

Commit 8889b41

Browse files
authored
Merge pull request #92 from js2me/feature/group-queries
feature: groupQueries
2 parents 74cc70d + 0882738 commit 8889b41

8 files changed

Lines changed: 638 additions & 5 deletions

File tree

.changeset/tidy-planes-attack.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx-tanstack-query": minor
3+
---
4+
5+
added `groupQueries` utility (`QueryGroup`) that aggregates multiple queries into a single reactive view (`data`, `errors`, `status`, `isFetching`, `isSuccess`, etc.)

docs/.vitepress/config.mts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ export default defineDocsVitepressConfig(configs, {
3232
text: "Core API",
3333
link: "/api/Query",
3434
items: [
35-
{ text: "Query", link: "/api/Query" },
36-
{ text: "Mutation", link: "/api/Mutation" },
37-
{ text: "InfiniteQuery", link: "/api/InfiniteQuery" },
38-
{ text: "QueryClient", link: "/api/QueryClient" },
39-
{ text: "Other", link: "/api/other" },
35+
{ text: "Query", link: "/api/Query" },
36+
{ text: "Mutation", link: "/api/Mutation" },
37+
{ text: "InfiniteQuery", link: "/api/InfiniteQuery" },
38+
{ text: "QueryGroup", link: "/api/QueryGroup" },
39+
{ text: "QueryClient", link: "/api/QueryClient" },
40+
{ text: "Other", link: "/api/other" },
4041
],
4142
},
4243
{

docs/api/Query.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ Is `true` whenever the first fetch for a query is in-flight.
181181

182182
- Is the same as `isFetching && isPending`.
183183

184+
### `isPending: boolean`
185+
186+
Will be `true` if there's no cached data and no query attempt was finished yet.
187+
184188
### `isLoadingError: boolean`
185189

186190
Will be `true` if the query failed while fetching for the first time.

docs/api/QueryGroup.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# QueryGroup
2+
3+
Reactive aggregation over multiple [`Query`](/api/Query) and [`InfiniteQuery`](/api/InfiniteQuery) instances with **MobX** reactivity.
4+
5+
[Reference to source code](/src/group-queries.ts)
6+
7+
## Usage
8+
9+
Use the `groupQueries` utility to create a `QueryGroup`:
10+
11+
```ts
12+
import { autorun } from "mobx";
13+
import { groupQueries } from "mobx-tanstack-query";
14+
15+
const petsGroup = groupQueries([catsQuery, dogsQuery]);
16+
17+
autorun(() => {
18+
if (petsGroup.isSuccess) {
19+
console.log(petsGroup.data); // [cats, dogs]
20+
}
21+
});
22+
```
23+
24+
::: tip Similar to `useQueries` + `combine`
25+
Unlike [`useQueries`](https://tanstack.com/query/latest/docs/framework/react/reference/useQueries) from the React adapter, `QueryGroup` does not create or subscribe queries — it aggregates **already created** `Query` / `InfiniteQuery` instances into a single reactive view.
26+
:::
27+
28+
### Reactive queries list
29+
30+
The argument can be a function returning the list of queries.
31+
All observable values inside this function are tracked by **MobX**, so the group is recalculated when the list changes:
32+
33+
```ts{4}
34+
const petsGroup = groupQueries(() => [
35+
catsQuery,
36+
// dogsQuery will be included into aggregation
37+
// only when withDogs becomes true
38+
settings.withDogs && dogsQuery,
39+
]);
40+
```
41+
42+
Falsy entries (`undefined`, `null`, `false`, ...) are ignored by the aggregated flags ([`isFetching`](#isfetching-boolean), [`isSuccess`](#issuccess-boolean), etc.), but keep their positions in [`data`](#data-querygroupdata) and [`errors`](#errors-querygrouperrors):
43+
44+
```ts
45+
const group = groupQueries([catsQuery, null, dogsQuery]);
46+
47+
group.data; // [catsData, undefined, dogsData]
48+
```
49+
50+
### Creating via class
51+
52+
`groupQueries` is just a shortcut for the `QueryGroup` constructor:
53+
54+
```ts
55+
import { QueryGroup } from "mobx-tanstack-query";
56+
57+
const group = new QueryGroup(() => [catsQuery, dogsQuery]);
58+
```
59+
60+
## Properties and methods
61+
62+
### `queries: QueryGroupItem[]`
63+
64+
All non-falsy queries in the group.
65+
66+
### `data: QueryGroupData`
67+
68+
Data of all queries in the group.
69+
Keeps positions of the input list, falsy entries are represented as `undefined`.
70+
71+
```ts
72+
const group = groupQueries([catsQuery, dogsQuery]);
73+
74+
group.data; // [catsData | undefined, dogsData | undefined]
75+
```
76+
77+
### `error: QueryGroupError`
78+
79+
The first non-null [`error`](/api/Query#error-terror-null) of the queries in the group, otherwise `null`.
80+
81+
### `errors: QueryGroupErrors`
82+
83+
Errors of all queries in the group.
84+
Keeps positions of the input list, falsy entries are represented as `null`.
85+
86+
### `isPending: boolean`
87+
88+
`true` if any query in the group is [`pending`](/api/Query#ispending-boolean).
89+
90+
### `isLoading: boolean`
91+
92+
`true` if any query in the group is [`loading`](/api/Query#isloading-boolean) (first fetch is in-flight).
93+
94+
### `isFetching: boolean`
95+
96+
`true` if any query in the group is [`fetching`](/api/Query#isfetching-boolean).
97+
98+
### `isRefetching: boolean`
99+
100+
`true` if any query in the group is [`refetching`](/api/Query#isrefetching-boolean).
101+
102+
### `isPaused: boolean`
103+
104+
`true` if any query in the group is [`paused`](/api/Query#ispaused-boolean).
105+
106+
### `isError: boolean`
107+
108+
`true` if any query in the group has an [`error`](/api/Query#iserror-boolean).
109+
110+
### `isSuccess: boolean`
111+
112+
`true` if **all** queries in the group are [`successfully fetched`](/api/Query#issuccess-boolean).
113+
114+
### `status: QueryStatus`
115+
116+
Aggregated [`status`](/api/Query#status-querystatus) of the group:
117+
118+
- `success` if all queries are succeeded
119+
- `error` if any query has an error
120+
- `pending` otherwise
121+
122+
### `fetchStatus: FetchStatus`
123+
124+
Aggregated [`fetchStatus`](/api/Query#fetchstatus-fetchstatus) of the group:
125+
126+
- `fetching` if any query is fetching
127+
- `paused` if any query is paused
128+
- `idle` otherwise
129+
130+
### `refetch(options?)`
131+
132+
[Refetches](/api/Query#refetch-options) all queries in the group.
133+
134+
```ts
135+
await group.refetch();
136+
```

0 commit comments

Comments
 (0)