|
| 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