Skip to content

Commit a8af9dd

Browse files
Remove detailed examples for working with relationships in jsonapi-react-tools documentation
1 parent 0d85650 commit a8af9dd

1 file changed

Lines changed: 1 addition & 171 deletions

File tree

docs/docs/integrations/react-tools.md

Lines changed: 1 addition & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -137,177 +137,7 @@ JsonApiToolkit supports rich querying via URL parameters. `jsonapi-react-tools`
137137

138138
### Working with Relationships (Included Resources)
139139

140-
JsonApiToolkit responses separate primary resource data from related resources, which are returned in an `included` array. The primary resources relationships contain only resource identifiers. To work with full resource objects and ensure type safety, you must first define your resource attributes and create a central type registry that maps resource type strings to these attribute interfaces.
141-
142-
#### Define Resource Attributes & Create a Type Registry
143-
144-
First, define interfaces for each resource's attributes:
145-
146-
```ts
147-
export interface CompanyAttributes {
148-
companyName: string;
149-
companyCode: string;
150-
establishedAt: string;
151-
}
152-
153-
export interface LocationAttributes {
154-
address: string;
155-
city: string;
156-
country: string;
157-
}
158-
159-
export interface EmployeeAttributes {
160-
firstName: string;
161-
lastName: string;
162-
email: string;
163-
}
164-
```
165-
166-
Then, create a registry that maps the resource type strings (as they are returned by your API) to your interfaces:
167-
168-
```ts
169-
import { JsonApiTypeRegistry } from "@intility/jsonapi-react-tools";
170-
import { CompanyAttributes } from "./Company";
171-
import { LocationAttributes } from "./Location";
172-
import { EmployeeAttributes } from "./Employee";
173-
174-
export interface AppTypeRegistry extends JsonApiTypeRegistry {
175-
companies: CompanyAttributes;
176-
locations: LocationAttributes;
177-
employees: EmployeeAttributes;
178-
}
179-
```
180-
181-
This registry is used throughout your application to ensure that all JSON:API responses are strongly typed.
182-
183-
#### 2. Extracting Related Resources
184-
185-
There are two cases for extracting related resources from a JSON:API response:
186-
187-
**A. Single Resource Responses:**
188-
When fetching a single resource (using a `JsonApiDocument`), all related resources are still available in the `included` array. In this case, you can use the built-in `getIncludedOfType` helper to filter the `included` array by resource type. For example:
189-
190-
```tsx
191-
import { getIncludedOfType } from "@intility/jsonapi-react-tools";
192-
193-
export function CompanyDetail({ companyId }: { companyId: string }) {
194-
const { data: companyDocument, isLoading } = useCompany(companyId);
195-
196-
if (isLoading || !companyDocument || !companyDocument.data) {
197-
return <div>Loading company details...</div>;
198-
}
199-
200-
// Use getIncludedOfType to extract related locations and employees
201-
const locations = getIncludedOfType(companyDocument, "locations");
202-
const employees = getIncludedOfType(companyDocument, "employees");
203-
204-
return (
205-
<div>
206-
<h2>{companyDocument.data.attributes.companyName}</h2>
207-
<p>Code: {companyDocument.data.attributes.companyCode}</p>
208-
<p>
209-
Established:{" "}
210-
<FormatDate date={companyDocument.data.attributes.establishedAt} />
211-
</p>
212-
<section>
213-
<h3>Locations</h3>
214-
<ul>
215-
{locations.map((loc) => (
216-
<li key={loc.id}>
217-
{loc.attributes.address}, {loc.attributes.city},{" "}
218-
{loc.attributes.country}
219-
</li>
220-
))}
221-
</ul>
222-
</section>
223-
<section>
224-
<h3>Employees</h3>
225-
<ul>
226-
{employees.map((emp) => (
227-
<li key={emp.id}>
228-
{emp.attributes.firstName} {emp.attributes.lastName} -{" "}
229-
{emp.attributes.email}
230-
</li>
231-
))}
232-
</ul>
233-
</section>
234-
</div>
235-
);
236-
}
237-
```
238-
239-
In this example, the companys related locations and employees are retrieved directly from the `included` array using `getIncludedOfType`.
240-
241-
**B. Collection Responses:**
242-
When dealing with collection responses (using a `JsonApiCollectionDocument`), each primary resource defines its relationships with only resource identifiers. To resolve these identifiers into full resource objects, use the `resolveRelationship` helper. For example, in a component listing companies:
243-
244-
```tsx
245-
import { useCompanies } from "~/hooks/useCompanies";
246-
import { Table } from "@intility/bifrost-react";
247-
import { resolveRelationship } from "@intility/jsonapi-react-tools";
248-
249-
export function CompanyList() {
250-
const { data: companiesDocument, isLoading } = useCompanies();
251-
252-
if (isLoading || !companiesDocument) {
253-
return <div>Loading companies...</div>;
254-
}
255-
256-
return (
257-
<Table>
258-
<Table.Header>
259-
<Table.Row>
260-
<Table.HeaderCell>Company Name</Table.HeaderCell>
261-
<Table.HeaderCell>Locations</Table.HeaderCell>
262-
<Table.HeaderCell>Employees</Table.HeaderCell>
263-
</Table.Row>
264-
</Table.Header>
265-
<Table.Body>
266-
{companiesDocument.data.map((company) => {
267-
// Resolve the 'locations' and 'employees' relationships for each company.
268-
// Note: The relationship names here (e.g., "locations") must match those
269-
// returned by your API.
270-
const companyLocations = company.relationships?.locations?.data
271-
? resolveRelationship(
272-
company.relationships.locations.data,
273-
companiesDocument,
274-
"locations"
275-
)
276-
: [];
277-
const companyEmployees = company.relationships?.employees?.data
278-
? resolveRelationship(
279-
company.relationships.employees.data,
280-
companiesDocument,
281-
"employees"
282-
)
283-
: [];
284-
285-
return (
286-
<Table.Row key={company.id}>
287-
<Table.Cell>{company.attributes.companyName}</Table.Cell>
288-
<Table.Cell>
289-
{companyLocations.map((loc) => loc.attributes.address).join(
290-
", "
291-
)}
292-
</Table.Cell>
293-
<Table.Cell>
294-
{companyEmployees
295-
.map(
296-
(emp) =>
297-
`${emp.attributes.firstName} ${emp.attributes.lastName}`
298-
)
299-
.join(", ")}
300-
</Table.Cell>
301-
</Table.Row>
302-
);
303-
})}
304-
</Table.Body>
305-
</Table>
306-
);
307-
}
308-
```
309-
310-
Here, the `resolveRelationship` helper maps the relationship identifiers (from the primary company resource) to the full location and employee objects from the collections `included` array.
140+
WIP
311141

312142
### Further Information
313143

0 commit comments

Comments
 (0)