Skip to content

Commit d4d456a

Browse files
authored
Merge pull request marmelab#10403 from marmelab/Backport-ReferenceManyToMany--queryOptions-and-mutationOptions-doc
[Doc] Backport `<ReferenceManyToManyField queryOptions>` and `<ReferenceManyToManyInput queryOptions mutationOptions>` doc
2 parents 30bee8b + 0cdde4d commit d4d456a

File tree

2 files changed

+98
-18
lines changed

2 files changed

+98
-18
lines changed

docs/ReferenceManyToManyField.md

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: "The ReferenceManyToManyField Component"
55

66
# `<ReferenceManyToManyField>`
77

8-
This [Enterprise Edition](https://react-admin-ee.marmelab.com)<img class="icon" src="./img/premium.svg" /> component fetches a list of referenced records by lookup in an associative table, and passes the records down to its child component, which must be an iterator component (e.g. `<SingleFieldList>`).
8+
This [Enterprise Edition](https://react-admin-ee.marmelab.com)<img class="icon" src="./img/premium.svg" alt="React Admin Enterprise Edition icon" /> component fetches a list of referenced records by lookup in an associative table, and passes the records down to its child component, which must be an iterator component (e.g. `<SingleFieldList>`).
99

1010
!["ReferenceManyToManyField example showing band's venues"](./img/reference-many-to-many-field.png)
1111

@@ -15,7 +15,7 @@ Note: The `<ReferenceManyToManyField>` cannot currently display multiple records
1515

1616
Let's imagine that you're writing an app managing concerts for artists. The data model features a many-to-many relationship between the `bands` and `venues` tables through a `performances` associative table.
1717

18-
```
18+
```txt
1919
┌─────────┐ ┌──────────────┐ ┌───────────────┐
2020
│ bands │ │ performances │ │ venues │
2121
│---------│ │--------------│ │---------------│
@@ -28,7 +28,7 @@ Let's imagine that you're writing an app managing concerts for artists. The data
2828

2929
In this example, `bands.id` matches `performances.band_id`, and `performances.venue_id` matches `venues.id`.
3030

31-
To allow users see the `venues` for a given `band` in `<SingleFieldList>`, wrap that component in `<ReferenceManyToManyField>` where you define the relationship via the `reference`, `through` and `using` props:
31+
To allow users see the `venues` for a given `band` in `<SingleFieldList>`, wrap that component in `<ReferenceManyToManyField>` where you define the relationship via the `reference`, `through` and `using` props:
3232

3333
```tsx
3434
import React from 'react';
@@ -61,17 +61,18 @@ export const BandShow = () => (
6161
| ----------- | -------- | ------------------------------------------- | -------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
6262
| `children` | Required | `element` | - | An iterator element (e.g. `<SingleFieldList>` or `<Datagrid>`). The iterator element usually has one or more child `<Field>` components. |
6363
| `reference` | Required | `string` | - | Name of the reference resource, e.g. 'venues' |
64-
| `through` | Required | `string` | - | Name of the resource for the associative table, e.g. 'performances'
64+
| `through` | Required | `string` | - | Name of the resource for the associative table, e.g. 'performances' |
6565
| `filter` | Optional | `object` | `{}` | Filter for the associative table (passed to the `getManyReference()` call) |
6666
| `joinLimit` | Optional | `number` | 100 | Limit for the number of results fetched from the associative table. Should be **greater than `perPage`** |
6767
| `perPage` | Optional | `number` | 25 | Limit the number of displayed result after `getManyReference` is called. Useful when using a pagination component. Should be **smaller than `joinLimit`** |
68+
| `queryOptions` | Optional | `UseQueryOptions` | - | Query options for the `getMany` and `getManyReference` calls |
6869
| `sort` | Optional | `{ field: string, order: 'ASC' or 'DESC' }` | `{ field: 'id', order: 'DESC' }` | Sort for the associative table (passed to the `getManyReference()` call) |
6970
| `source` | Optional | `string` | `'id'` | Name of the field containing the identity of the main resource. Used determine the value to look for in the associative table. |
7071
| `using` | Optional | `string` | `'[resource]_id,[reference]_id'` | Tuple (comma separated) of the two field names used as foreign keys, e.g 'band_id,venue_id'. The tuple should start with the field pointing to the resource, and finish with the field pointing to the reference |
7172

7273
## `children`
7374

74-
`<ReferenceManyToManyField>` expects an _iterator_ component as child, i.e. a component working inside a `ListContext`.
75+
`<ReferenceManyToManyField>` expects an _iterator_ component as child, i.e. a component working inside a `ListContext`.
7576

7677
This means you can use a `<Datagrid>` instead of a `<SingleFieldList>`, which is useful if you want to display more details about related records. For instance, to display the venue `name` and `location`:
7778

@@ -105,6 +106,7 @@ export const BandShow = () => (
105106
You can filter the records of the associative table (e.g. `performances`) using the `filter` prop. This `filter` is passed to the `getManyReference()` call.
106107

107108
{% raw %}
109+
108110
```tsx
109111
<ReferenceManyToManyField
110112
reference="venues"
@@ -115,6 +117,7 @@ You can filter the records of the associative table (e.g. `performances`) using
115117
{/* ... */}
116118
</ReferenceManyToManyField>
117119
```
120+
118121
{% endraw %}
119122

120123
## `joinLimit`
@@ -165,6 +168,27 @@ import { Pagination } from 'react-admin';
165168
</ReferenceManyToManyField>
166169
```
167170

171+
## `queryOptions`
172+
173+
Use the `queryOptions` prop to customize the queries for `getMany` and `getManyReference`.
174+
175+
You can for instance use it to pass [a custom meta](./Actions.md#meta-parameter) to the dataProvider.
176+
177+
{% raw %}
178+
179+
```tsx
180+
<ReferenceManyToManyField
181+
reference="venues"
182+
through="performances"
183+
using="band_id,venue_id"
184+
queryOptions={{ meta: { myParameter: 'value' } }}
185+
>
186+
{/* ... */}
187+
</ReferenceManyToManyField>
188+
```
189+
190+
{% endraw %}
191+
168192
## `reference`
169193

170194
The name of the target resource to fetch.
@@ -187,6 +211,7 @@ For instance, if you want to display the `venues` of a given `bands`, through `p
187211
By default, react-admin orders the possible values by `id` desc for the associative table (e.g. `performances`). You can change this order by setting the `sort` prop (an object with `field` and `order` properties) to be applied to the associative resource.
188212

189213
{% raw %}
214+
190215
```tsx
191216
<ReferenceManyToManyField
192217
reference="venues"
@@ -197,6 +222,7 @@ By default, react-admin orders the possible values by `id` desc for the associat
197222
{/* ... */}
198223
</ReferenceManyToManyField>
199224
```
225+
200226
{% endraw %}
201227

202228
## `source`
@@ -242,8 +268,8 @@ You can specify the columns to use in the associative `using` the using prop.
242268

243269
`<ReferenceManyToManyField>` fetches the `dataProvider` twice in a row:
244270

245-
- once to get the records of the associative resource (`performances` in this case), using a `getManyReference()` call
246-
- once to get the records of the reference resource (`venues` in this case), using a `getMany()` call.
271+
- once to get the records of the associative resource (`performances` in this case), using a `getManyReference()` call
272+
- once to get the records of the reference resource (`venues` in this case), using a `getMany()` call.
247273

248274
For instance, if the user displays the band of id `123`, `<ReferenceManyToManyField>` first issues the following query to the `dataProvider`:
249275

@@ -286,4 +312,4 @@ And receives the reference venues:
286312
],
287313
"total": 3
288314
}
289-
```
315+
```

0 commit comments

Comments
 (0)