Skip to content

Commit ade232e

Browse files
committed
docs: update handling relations doc
1 parent 73a1fbe commit ade232e

File tree

1 file changed

+28
-101
lines changed

1 file changed

+28
-101
lines changed

admin/handling-relations.md

Lines changed: 28 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
API Platform Admin handles `to-one` and `to-many` relations automatically.
44

5-
Thanks to [the Schema.org support](schema.org.md), you can easily display the name of a related resource instead of its IRI.
5+
Thanks to [the Schema.org support](./schema-org.md#displaying-related-resources-name-instead-of-its-iri), you can easily display the name of a related resource instead of its IRI.
66

77
## Embedded Relations
88

@@ -11,12 +11,10 @@ If a relation is an array of [embeddeds or an embedded](../core/serialization.md
1111
The embedded data will be displayed as text field and editable as text input: the admin cannot determine the fields present in it.
1212
To display the fields you want, see [this section](handling-relations.md#display-a-field-of-an-embedded-relation).
1313

14-
You can also ask the admin to automatically replace the embedded resources' data by their IRI,
15-
by setting the `useEmbedded` parameter of the Hydra data provider to `false`.
16-
Embedded data is inserted to a local cache: it will not be necessary to make more requests if you reference some fields of the embedded resource later on.
14+
You can also ask the admin to return the embedded resources' IRI instead of the full record, by setting the `useEmbedded` parameter of the Hydra data provider to `false`.
1715

18-
```javascript
19-
// admin/src/App.js
16+
```jsx
17+
// admin/src/App.jsx
2018

2119
import { HydraAdmin, fetchHydra, hydraDataProvider } from '@api-platform/admin';
2220
import { parseHydraDocumentation } from '@api-platform/api-doc-parser';
@@ -31,18 +29,20 @@ const dataProvider = hydraDataProvider({
3129
useEmbedded: false,
3230
});
3331

34-
export default () => (
32+
export const App = () => (
3533
<HydraAdmin dataProvider={dataProvider} entrypoint={entrypoint} />
3634
);
3735
```
3836

37+
**Tip:** Embedded data is inserted to a local cache: it will not be necessary to make more requests if you reference some fields of the embedded resource later on.
38+
3939
## Display a Field of an Embedded Relation
4040

4141
If you have an [embedded relation](../core/serialization.md#embedding-relations) and need to display a nested field, the code you need to write depends of the value of `useEmbedded` of the Hydra data provider.
4242

43-
If `true` (default behavior), you need to use the dot notation to display a field:
43+
If `true` (default behavior), you can use the dot notation to display a field:
4444

45-
```javascript
45+
```jsx
4646
import {
4747
HydraAdmin,
4848
FieldGuesser,
@@ -51,16 +51,16 @@ import {
5151
} from '@api-platform/admin';
5252
import { TextField } from 'react-admin';
5353

54-
const BooksList = (props) => (
55-
<ListGuesser {...props}>
54+
const BooksList = () => (
55+
<ListGuesser>
5656
<FieldGuesser source="title" />
5757
{/* Use react-admin components directly when you want complex fields. */}
5858
<TextField label="Author first name" source="author.firstName" />
5959
</ListGuesser>
6060
);
6161

62-
export default () => (
63-
<HydraAdmin entrypoint={process.env.REACT_APP_API_ENTRYPOINT}>
62+
export const App = () => (
63+
<HydraAdmin entrypoint={...} >
6464
<ResourceGuesser name="books" list={BooksList} />
6565
</HydraAdmin>
6666
);
@@ -96,9 +96,9 @@ For instance, if your API returns:
9696
}
9797
```
9898

99-
If you want to display the author first name in the list, you need to write the following code:
99+
If you want to display the author first name in the list, you need to leverage React Admin's [`<ReferenceField>`](https://marmelab.com/react-admin/ReferenceField.html) to fetch the related record:
100100

101-
```javascript
101+
```jsx
102102
import {
103103
HydraAdmin,
104104
FieldGuesser,
@@ -107,8 +107,8 @@ import {
107107
} from '@api-platform/admin';
108108
import { ReferenceField, TextField } from 'react-admin';
109109

110-
const BooksList = (props) => (
111-
<ListGuesser {...props}>
110+
const BooksList = () => (
111+
<ListGuesser>
112112
<FieldGuesser source="title" />
113113
{/* Use react-admin components directly when you want complex fields. */}
114114
<ReferenceField
@@ -121,8 +121,8 @@ const BooksList = (props) => (
121121
</ListGuesser>
122122
);
123123

124-
export default () => (
125-
<HydraAdmin entrypoint={process.env.REACT_APP_API_ENTRYPOINT}>
124+
export const App = () => (
125+
<HydraAdmin entrypoint={...} >
126126
<ResourceGuesser name="books" list={BooksList} />
127127
<ResourceGuesser name="authors" />
128128
</HydraAdmin>
@@ -131,7 +131,7 @@ export default () => (
131131

132132
## Using an Autocomplete Input for Relations
133133

134-
Let's go one step further thanks to the [customization capabilities](customizing.md) of API Platform Admin by adding autocompletion support to form inputs for relations.
134+
Let's go one step further thanks to the customization capabilities of API Platform Admin by adding autocompletion support to form inputs for relations.
135135

136136
Let's consider an API exposing `Review` and `Book` resources linked by a `many-to-one` relation (through the `book` property).
137137

@@ -192,9 +192,9 @@ class Book
192192

193193
Notice the "partial search" [filter](../core/filters.md) on the `title` property of the `Book` resource class.
194194

195-
Now, let's configure API Platform Admin to enable autocompletion for the relation selector:
195+
Now, let's configure API Platform Admin to enable autocompletion for the book selector. We will leverage the [`<ReferenceInput>`](https://marmelab.com/react-admin/ReferenceInput.html) and [`<AutocompleteInput>`](https://marmelab.com/react-admin/AutocompleteInput.html) components from React Admin:
196196

197-
```javascript
197+
```jsx
198198
import {
199199
HydraAdmin,
200200
ResourceGuesser,
@@ -204,86 +204,13 @@ import {
204204
} from '@api-platform/admin';
205205
import { ReferenceInput, AutocompleteInput } from 'react-admin';
206206

207-
const ReviewsCreate = (props) => (
208-
<CreateGuesser {...props}>
209-
<InputGuesser source="author" />
210-
<ReferenceInput source="book" reference="books">
211-
<AutocompleteInput
212-
filterToQuery={(searchText) => ({ title: searchText })}
213-
optionText="title"
214-
label="Books"
215-
/>
216-
</ReferenceInput>
217-
218-
<InputGuesser source="rating" />
219-
<InputGuesser source="body" />
220-
<InputGuesser source="publicationDate" />
221-
</CreateGuesser>
222-
);
223-
224-
const ReviewsEdit = (props) => (
225-
<EditGuesser {...props}>
226-
<InputGuesser source="author" />
227-
228-
<ReferenceInput source="book" reference="books">
229-
<AutocompleteInput
230-
filterToQuery={(searchText) => ({ title: searchText })}
231-
optionText="title"
232-
label="Books"
233-
/>
234-
</ReferenceInput>
235-
236-
<InputGuesser source="rating" />
237-
<InputGuesser source="body" />
238-
<InputGuesser source="publicationDate" />
239-
</EditGuesser>
240-
);
241-
242-
export default () => (
243-
<HydraAdmin entrypoint={process.env.REACT_APP_API_ENTRYPOINT}>
244-
<ResourceGuesser name="reviews" create={ReviewsCreate} edit={ReviewsEdit} />
245-
</HydraAdmin>
246-
);
247-
```
248-
249-
If the book is embedded into a review and if the `useEmbedded` parameter is `true` (default behavior),
250-
you need to change the `ReferenceInput` for the edit component:
251-
252-
```javascript
253-
import {
254-
HydraAdmin,
255-
ResourceGuesser,
256-
CreateGuesser,
257-
EditGuesser,
258-
InputGuesser,
259-
} from '@api-platform/admin';
260-
import { ReferenceInput, AutocompleteInput } from 'react-admin';
261-
262-
const ReviewsCreate = (props) => (
263-
<CreateGuesser {...props}>
264-
<InputGuesser source="author" />
265-
<ReferenceInput source="book" reference="books">
266-
<AutocompleteInput
267-
filterToQuery={(searchText) => ({ title: searchText })}
268-
optionText="title"
269-
label="Books"
270-
/>
271-
</ReferenceInput>
272-
273-
<InputGuesser source="rating" />
274-
<InputGuesser source="body" />
275-
<InputGuesser source="publicationDate" />
276-
</CreateGuesser>
277-
);
278-
279-
const ReviewsEdit = (props) => (
280-
<EditGuesser {...props}>
207+
const ReviewsEdit = () => (
208+
<EditGuesser>
281209
<InputGuesser source="author" />
282210

283211
<ReferenceInput source="book" reference="books">
284212
<AutocompleteInput
285213
filterToQuery={(searchText) => ({ title: searchText })}
286-
format={(v) => v['@id'] || v}
287214
optionText="title"
288215
label="Books"
289216
/>
@@ -295,11 +222,11 @@ const ReviewsEdit = (props) => (
295222
</EditGuesser>
296223
);
297224

298-
export default () => (
299-
<HydraAdmin entrypoint={process.env.REACT_APP_API_ENTRYPOINT}>
300-
<ResourceGuesser name="reviews" create={ReviewsCreate} edit={ReviewsEdit} />
225+
export const App = () => (
226+
<HydraAdmin entrypoint={...} >
227+
<ResourceGuesser name="reviews" edit={ReviewsEdit} />
301228
</HydraAdmin>
302229
);
303230
```
304231

305-
The autocomplete field should now work properly!
232+
You can now search for books by title in the book selector of the review form.

0 commit comments

Comments
 (0)