@osdk/react 2.94.0 → 2.94.1-main-718be7c10524d5aa3086fbfd38785c0abd0a2c87
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/build/browser/new/platform-apis/admin/useCurrentFoundryUser.js.map +1 -1
- package/build/browser/new/platform-apis/admin/useFoundryUser.js.map +1 -1
- package/build/browser/new/platform-apis/admin/useFoundryUsersList.js.map +1 -1
- package/build/browser/new/useLinks.js.map +1 -1
- package/build/browser/new/useObjectSet.js.map +1 -1
- package/build/browser/new/useOsdkAggregation.js +2 -5
- package/build/browser/new/useOsdkAggregation.js.map +1 -1
- package/build/browser/new/useOsdkObjects.js.map +1 -1
- package/build/browser/util/UserAgent.js +1 -1
- package/build/browser/util/UserAgent.js.map +1 -1
- package/build/cjs/{chunk-EJT5W6SI.cjs → chunk-4PFYL4UI.cjs} +3 -3
- package/build/cjs/chunk-4PFYL4UI.cjs.map +1 -0
- package/build/cjs/index.cjs +15 -15
- package/build/cjs/public/experimental.cjs +16 -16
- package/build/cjs/public/experimental.d.cts +9 -12
- package/build/cjs/public/platform-apis.d.cts +1 -4
- package/build/esm/new/platform-apis/admin/useCurrentFoundryUser.js.map +1 -1
- package/build/esm/new/platform-apis/admin/useFoundryUser.js.map +1 -1
- package/build/esm/new/platform-apis/admin/useFoundryUsersList.js.map +1 -1
- package/build/esm/new/useLinks.js.map +1 -1
- package/build/esm/new/useObjectSet.js.map +1 -1
- package/build/esm/new/useOsdkAggregation.js +2 -5
- package/build/esm/new/useOsdkAggregation.js.map +1 -1
- package/build/esm/new/useOsdkObjects.js.map +1 -1
- package/build/esm/util/UserAgent.js +1 -1
- package/build/esm/util/UserAgent.js.map +1 -1
- package/build/types/new/platform-apis/admin/useCurrentFoundryUser.d.ts +0 -1
- package/build/types/new/platform-apis/admin/useCurrentFoundryUser.d.ts.map +1 -1
- package/build/types/new/platform-apis/admin/useFoundryUser.d.ts +1 -2
- package/build/types/new/platform-apis/admin/useFoundryUser.d.ts.map +1 -1
- package/build/types/new/platform-apis/admin/useFoundryUsersList.d.ts +0 -1
- package/build/types/new/platform-apis/admin/useFoundryUsersList.d.ts.map +1 -1
- package/build/types/new/useLinks.d.ts +2 -2
- package/build/types/new/useObjectSet.d.ts +2 -2
- package/build/types/new/useOsdkAggregation.d.ts +3 -6
- package/build/types/new/useOsdkAggregation.d.ts.map +1 -1
- package/build/types/new/useOsdkObjects.d.ts +2 -2
- package/docs/actions.mdx +314 -0
- package/docs/aggregations.mdx +256 -0
- package/docs/cache-invalidation.mdx +229 -0
- package/docs/devtools/cache.mdx +29 -0
- package/docs/devtools/components.mdx +40 -0
- package/docs/devtools/console.mdx +29 -0
- package/docs/devtools/index.mdx +151 -0
- package/docs/devtools/overview.mdx +45 -0
- package/docs/functions.mdx +246 -0
- package/docs/index.mdx +62 -0
- package/docs/links.mdx +270 -0
- package/docs/meta.json +30 -0
- package/docs/object-sets.mdx +296 -0
- package/docs/objects.mdx +489 -0
- package/docs/optimistic-updates.mdx +164 -0
- package/docs/platform-apis.mdx +296 -0
- package/docs/testing.mdx +348 -0
- package/package.json +6 -6
- package/build/cjs/chunk-EJT5W6SI.cjs.map +0 -1
- package/docs/actions.md +0 -432
- package/docs/advanced-queries.md +0 -678
- package/docs/cache-management.md +0 -222
- package/docs/getting-started.md +0 -319
- package/docs/platform-apis.md +0 -234
- package/docs/prerequisites.md +0 -48
- package/docs/querying-data.md +0 -810
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Call functions
|
|
3
|
+
description: Run Foundry functions from React with useOsdkFunction and useOsdkFunctions, with caching, dependency-based refetching, and error handling.
|
|
4
|
+
foundryDocs:
|
|
5
|
+
- title: Functions
|
|
6
|
+
path: functions/overview
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
{/* cspell:ignore refetched refetches refetching */}
|
|
10
|
+
|
|
11
|
+
Use `useOsdkFunction` to call a Foundry function (an ontology query) and render its result. The result is cached and shared across components, and you can tell the cache which objects the function reads so it reruns after actions change them.
|
|
12
|
+
|
|
13
|
+
## Call a function
|
|
14
|
+
|
|
15
|
+
Pass the function from your generated SDK and its parameters:
|
|
16
|
+
|
|
17
|
+
```tsx title="src/EmployeeMetrics.tsx"
|
|
18
|
+
import { getEmployeeMetrics } from "@my-app/sdk";
|
|
19
|
+
import { useOsdkFunction } from "@osdk/react";
|
|
20
|
+
|
|
21
|
+
export function EmployeeMetrics({ department }: { department: string }) {
|
|
22
|
+
const { data, isLoading, error } = useOsdkFunction(getEmployeeMetrics, {
|
|
23
|
+
params: { department },
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return (
|
|
27
|
+
<section>
|
|
28
|
+
{isLoading && <span>Updating…</span>}
|
|
29
|
+
{error && <p role="alert">Could not load metrics: {error.message}</p>}
|
|
30
|
+
{data && <p>Headcount: {data.headcount}</p>}
|
|
31
|
+
</section>
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`params` is typed from the function's signature. Required parameters must be present, and optional ones can be left out. For a function with no parameters, leave out `params`, or pass no options at all:
|
|
37
|
+
|
|
38
|
+
```tsx title="src/EmployeeCount.tsx"
|
|
39
|
+
import { countEmployees } from "@my-app/sdk";
|
|
40
|
+
import { useOsdkFunction } from "@osdk/react";
|
|
41
|
+
|
|
42
|
+
export function EmployeeCount() {
|
|
43
|
+
const { data } = useOsdkFunction(countEmployees);
|
|
44
|
+
|
|
45
|
+
return <span>{data ?? "…"} employees</span>;
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Options
|
|
50
|
+
|
|
51
|
+
| Option | Type | Default | Description |
|
|
52
|
+
| --- | --- | --- | --- |
|
|
53
|
+
| `params` | Function parameters | `undefined` | Arguments for the function. Omit for functions without parameters. |
|
|
54
|
+
| `dependsOn` | `Array<ObjectTypeDefinition \| string>` | `undefined` | Object types the function reads. A change to any object of these types refetches it. Pass the type from your SDK or its API name. |
|
|
55
|
+
| `dependsOnObjects` | `Array<Osdk.Instance \| ObjectSet>` | `undefined` | Specific objects, or object sets, the function reads. See [Refetch when specific objects change](#refetch-when-specific-objects-change). |
|
|
56
|
+
| `dedupeIntervalMs` | `number` | `2000` | Within this many milliseconds of the last fetch, a new subscriber reuses the cached result instead of calling the function again. |
|
|
57
|
+
| `enabled` | `boolean` | `true` | When `false`, the function is not called and `data` stays `undefined`. |
|
|
58
|
+
|
|
59
|
+
### Return value
|
|
60
|
+
|
|
61
|
+
| Field | Type | Description |
|
|
62
|
+
| --- | --- | --- |
|
|
63
|
+
| `data` | Function return type `\| undefined` | The result. `undefined` before the first result arrives and after an error. |
|
|
64
|
+
| `isLoading` | `boolean` | `true` while a call is in flight, including refetches. The previous `data` stays available while it refetches. |
|
|
65
|
+
| `error` | `Error \| undefined` | Set when the last call failed. |
|
|
66
|
+
| `lastUpdated` | `number` | Time of the last successful call, in milliseconds since the epoch. `0` before the first result and after a failed call. |
|
|
67
|
+
| `refetch` | `() => Promise<void>` | Calls the function again with the same parameters, ignoring `dedupeIntervalMs`. |
|
|
68
|
+
|
|
69
|
+
## How results are cached
|
|
70
|
+
|
|
71
|
+
Each result is cached under the function's API name, its version, and its parameters. Parameters are compared by value, so two components that call `getEmployeeMetrics` with `{ department: "Sales" }` share one cache entry and one network request. A different `department` is a separate entry.
|
|
72
|
+
|
|
73
|
+
A cached result does not expire on a timer. It is refetched when:
|
|
74
|
+
|
|
75
|
+
- you call `refetch`, or invalidate it through the observable client (see [Invalidate the cache](./cache-invalidation.mdx)),
|
|
76
|
+
- an action edits an object the function depends on (`dependsOn` or `dependsOnObjects`), or
|
|
77
|
+
- a component subscribes after `dedupeIntervalMs` has passed since the last call.
|
|
78
|
+
|
|
79
|
+
Without `dependsOn` or `dependsOnObjects`, actions never refetch the function, because the cache cannot know what it reads. For the full cache model, see [Caching](../(docs)/concepts/caching.mdx).
|
|
80
|
+
|
|
81
|
+
## Refetch when objects change
|
|
82
|
+
|
|
83
|
+
Declare the object types your function reads with `dependsOn`. After an action adds, modifies, or deletes an object of one of those types, the function runs again:
|
|
84
|
+
|
|
85
|
+
```tsx title="src/DepartmentHeadcount.tsx"
|
|
86
|
+
import { Employee, getEmployeeMetrics } from "@my-app/sdk";
|
|
87
|
+
import { useOsdkFunction } from "@osdk/react";
|
|
88
|
+
|
|
89
|
+
export function DepartmentHeadcount({ department }: { department: string }) {
|
|
90
|
+
const { data, isLoading, refetch } = useOsdkFunction(getEmployeeMetrics, {
|
|
91
|
+
params: { department },
|
|
92
|
+
dependsOn: [Employee],
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
return (
|
|
96
|
+
<div>
|
|
97
|
+
{data && <span>Headcount: {data.headcount}</span>}
|
|
98
|
+
{isLoading && <span> (updating)</span>}
|
|
99
|
+
<button onClick={() => void refetch()}>Refresh</button>
|
|
100
|
+
</div>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
`dependsOn` also accepts API name strings, such as `dependsOn: ["Employee"]`, which is useful when you do not import the type.
|
|
106
|
+
|
|
107
|
+
### Refetch when specific objects change
|
|
108
|
+
|
|
109
|
+
`dependsOnObjects` narrows the trigger. Pass object instances, and the function reruns only when one of those exact objects (matched by type and primary key) is added or modified. Deleting one does not rerun it; use `dependsOn` for that:
|
|
110
|
+
|
|
111
|
+
```tsx title="src/EmployeeReport.tsx"
|
|
112
|
+
import { Employee, getEmployeeReport } from "@my-app/sdk";
|
|
113
|
+
import { useOsdkFunction } from "@osdk/react";
|
|
114
|
+
|
|
115
|
+
export function EmployeeReport({ employee }: { employee: Employee.OsdkInstance }) {
|
|
116
|
+
const { data } = useOsdkFunction(getEmployeeReport, {
|
|
117
|
+
params: { employeeId: employee.$primaryKey },
|
|
118
|
+
dependsOnObjects: [employee],
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
return <pre>{data ? JSON.stringify(data, null, 2) : "Loading report…"}</pre>;
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
`dependsOnObjects` also accepts object sets. The cache looks up the set's object type in the background and then treats it like `dependsOn` for that type: a change to any object of that type refetches the function, not only changes to objects in the set. Because the result is the same, pass the type to `dependsOn` instead. It takes effect immediately and avoids a type error, since a typed object set such as `Employee.ObjectSet` is not assignable to the option's `ObjectSet<ObjectTypeDefinition>` type.
|
|
126
|
+
|
|
127
|
+
<Callout type="warn">
|
|
128
|
+
The first component to subscribe to a function and parameter combination sets its dependencies. A later component that passes different `dependsOn` or `dependsOnObjects` for the same parameters shares the existing entry and its dependencies. Pass the same dependency options everywhere you call a function with the same parameters.
|
|
129
|
+
</Callout>
|
|
130
|
+
|
|
131
|
+
## Wait for inputs
|
|
132
|
+
|
|
133
|
+
Hooks cannot be called conditionally. To hold a call until its inputs exist, set `enabled`:
|
|
134
|
+
|
|
135
|
+
```tsx title="src/ReportForEmployee.tsx"
|
|
136
|
+
import { Employee, getEmployeeReport } from "@my-app/sdk";
|
|
137
|
+
import { useOsdkFunction, useOsdkObject } from "@osdk/react";
|
|
138
|
+
|
|
139
|
+
export function ReportForEmployee({ employeeId }: { employeeId: number }) {
|
|
140
|
+
const { object: employee } = useOsdkObject(Employee, employeeId);
|
|
141
|
+
|
|
142
|
+
const { data, isLoading } = useOsdkFunction(getEmployeeReport, {
|
|
143
|
+
params: { employeeId },
|
|
144
|
+
dependsOnObjects: employee ? [employee] : [],
|
|
145
|
+
enabled: employee != null,
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
return (
|
|
149
|
+
<div>
|
|
150
|
+
<h2>{employee?.fullName ?? "Loading employee…"}</h2>
|
|
151
|
+
{isLoading && <span>Loading report…</span>}
|
|
152
|
+
{data && <pre>{JSON.stringify(data, null, 2)}</pre>}
|
|
153
|
+
</div>
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
While `enabled` is `false`, `isLoading` is `false` and `data` is `undefined`.
|
|
159
|
+
|
|
160
|
+
## Handle errors
|
|
161
|
+
|
|
162
|
+
When a call fails, `error` holds the `Error` and `data` becomes `undefined`. The error stays until a later call succeeds, so render it next to the rest of the component rather than in place of it. `refetch` doubles as a retry:
|
|
163
|
+
|
|
164
|
+
```tsx title="src/MetricsWithRetry.tsx"
|
|
165
|
+
import { getEmployeeMetrics } from "@my-app/sdk";
|
|
166
|
+
import { useOsdkFunction } from "@osdk/react";
|
|
167
|
+
|
|
168
|
+
export function MetricsWithRetry({ department }: { department: string }) {
|
|
169
|
+
const { data, error, isLoading, refetch } = useOsdkFunction(
|
|
170
|
+
getEmployeeMetrics,
|
|
171
|
+
{ params: { department } },
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
return (
|
|
175
|
+
<section>
|
|
176
|
+
{data && <p>Headcount: {data.headcount}</p>}
|
|
177
|
+
{error && (
|
|
178
|
+
<p role="alert">
|
|
179
|
+
{error.message}{" "}
|
|
180
|
+
<button disabled={isLoading} onClick={() => void refetch()}>
|
|
181
|
+
Retry
|
|
182
|
+
</button>
|
|
183
|
+
</p>
|
|
184
|
+
)}
|
|
185
|
+
</section>
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Errors from Foundry are `PalantirApiError` instances, exported from `@osdk/client`, which carry `errorName`, `errorCode`, and `errorInstanceId`. See [Loading and errors](../(docs)/concepts/loading-and-errors.mdx) for patterns that apply to every hook.
|
|
191
|
+
|
|
192
|
+
## Call several functions
|
|
193
|
+
|
|
194
|
+
`useOsdkFunctions` runs a list of function calls and returns one result per call, in the same order. Each result has the same shape as the `useOsdkFunction` return value.
|
|
195
|
+
|
|
196
|
+
```ts title="src/functionCall.ts"
|
|
197
|
+
import type { QueryDefinition } from "@osdk/client";
|
|
198
|
+
import type { UseOsdkFunctionOptions, UseOsdkFunctionsProps } from "@osdk/react";
|
|
199
|
+
|
|
200
|
+
type FunctionCall = UseOsdkFunctionsProps["queries"][number];
|
|
201
|
+
|
|
202
|
+
export function functionCall<Q extends QueryDefinition<unknown>>(
|
|
203
|
+
queryDefinition: Q,
|
|
204
|
+
options?: UseOsdkFunctionOptions<Q>,
|
|
205
|
+
): FunctionCall {
|
|
206
|
+
return { queryDefinition, options } as unknown as FunctionCall;
|
|
207
|
+
}
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
```tsx title="src/DepartmentCards.tsx"
|
|
211
|
+
import { getEmployeeMetrics } from "@my-app/sdk";
|
|
212
|
+
import { useOsdkFunctions } from "@osdk/react";
|
|
213
|
+
import { functionCall } from "./functionCall";
|
|
214
|
+
|
|
215
|
+
export function DepartmentCards({ departments }: { departments: string[] }) {
|
|
216
|
+
const results = useOsdkFunctions({
|
|
217
|
+
queries: departments.map((department) =>
|
|
218
|
+
functionCall(getEmployeeMetrics, { params: { department } }),
|
|
219
|
+
),
|
|
220
|
+
maxConcurrent: 4,
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
return (
|
|
224
|
+
<ul>
|
|
225
|
+
{results.map((result, index) => (
|
|
226
|
+
<li key={departments[index]}>
|
|
227
|
+
{departments[index]}:{" "}
|
|
228
|
+
{result.error
|
|
229
|
+
? result.error.message
|
|
230
|
+
: result.isLoading
|
|
231
|
+
? "…"
|
|
232
|
+
: JSON.stringify(result.data)}
|
|
233
|
+
</li>
|
|
234
|
+
))}
|
|
235
|
+
</ul>
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
Each call is cached exactly as if it came from `useOsdkFunction`, so a call that another component already made is served from the cache. Because the list can mix functions, `queries` is typed for any function and does not accept a specific function's `params` as written. The `functionCall` helper above checks `params` against each function, then widens the entry. Narrow or cast each result's `data` before use.
|
|
241
|
+
|
|
242
|
+
| Option | Type | Default | Description |
|
|
243
|
+
| --- | --- | --- | --- |
|
|
244
|
+
| `queries` | `Array<{ queryDefinition, options? }>` | Required | The calls to make. `options` takes the same fields as the `useOsdkFunction` options: `params`, `dependsOn`, `dependsOnObjects`, `dedupeIntervalMs`, and `enabled`. |
|
|
245
|
+
| `enabled` | `boolean` | `true` | When `false`, no call runs. A call whose own `options.enabled` is `false` is skipped either way. |
|
|
246
|
+
| `maxConcurrent` | `number` | `undefined` | Limit on calls in flight at once. The next call starts when a running one finishes, successfully or not. Use it for functions that are slow or return large payloads. Without it, all calls start together. |
|
package/docs/index.mdx
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: React SDK
|
|
3
|
+
description: Build reactive Foundry apps with @osdk/react hooks backed by one shared, normalized cache.
|
|
4
|
+
foundryDocs:
|
|
5
|
+
- title: OSDK React applications
|
|
6
|
+
path: ontology-sdk-react-applications/overview
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
`@osdk/react` wraps the OSDK client in hooks. Every hook reads from one normalized cache, so a list, a detail view, and a link panel that show the same object stay in sync, and an action updates them all without manual refetching.
|
|
10
|
+
|
|
11
|
+
```tsx title="src/EmployeeList.tsx"
|
|
12
|
+
import { useOsdkObjects } from "@osdk/react";
|
|
13
|
+
import { Employee } from "@my-app/sdk";
|
|
14
|
+
|
|
15
|
+
export function EmployeeList() {
|
|
16
|
+
const { data, isLoading, error } = useOsdkObjects(Employee, {
|
|
17
|
+
where: { department: "Engineering" },
|
|
18
|
+
orderBy: { fullName: "asc" },
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
return (
|
|
22
|
+
<section aria-busy={isLoading}>
|
|
23
|
+
{error != null && <p role="alert">Could not load employees.</p>}
|
|
24
|
+
<ul>
|
|
25
|
+
{data?.map((employee) => (
|
|
26
|
+
<li key={employee.$primaryKey}>{employee.fullName}</li>
|
|
27
|
+
))}
|
|
28
|
+
</ul>
|
|
29
|
+
</section>
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
New to OSDK? Start with the [Quickstart](../(docs)/quickstart.mdx), then [create a client and add OsdkProvider](../(docs)/client.mdx).
|
|
35
|
+
|
|
36
|
+
## Hooks
|
|
37
|
+
|
|
38
|
+
| Hook | Use it to | Guide |
|
|
39
|
+
| --- | --- | --- |
|
|
40
|
+
| `useOsdkObjects` | Load a list of objects with filters, sorting, and paging | [Query objects](./objects.mdx) |
|
|
41
|
+
| `useOsdkObject` | Load one object by primary key | [Query objects](./objects.mdx#load-one-object) |
|
|
42
|
+
| `useLinks` | Load the objects linked to an object | [Follow links](./links.mdx) |
|
|
43
|
+
| `useObjectSet` | Combine, intersect, subtract, or pivot object sets | [Work with object sets](./object-sets.mdx) |
|
|
44
|
+
| `useStableObjectSet` | Keep an object set referentially stable across renders | [Work with object sets](./object-sets.mdx) |
|
|
45
|
+
| `useOsdkAggregation` | Count, sum, average, and group on the server | [Aggregate data](./aggregations.mdx) |
|
|
46
|
+
| `useOsdkAction` | Apply and validate actions | [Apply actions](./actions.mdx) |
|
|
47
|
+
| `useDebouncedCallback` | Debounce validation while a user types | [Apply actions](./actions.mdx) |
|
|
48
|
+
| `useOsdkFunction`, `useOsdkFunctions` | Call Ontology functions with caching | [Call functions](./functions.mdx) |
|
|
49
|
+
| `useObservableClient` | Invalidate cached data by hand | [Invalidate the cache](./cache-invalidation.mdx) |
|
|
50
|
+
| `useOsdkClient` | Get the client for one-off, imperative calls | [Create a client](../(docs)/client.mdx) |
|
|
51
|
+
| `useOsdkMetadata` | Read object and action type metadata | [Reference](/reference) |
|
|
52
|
+
|
|
53
|
+
User, group, and marking hooks live in `@osdk/react/platform-apis`; see [Users and markings](./platform-apis.mdx). Test utilities live in `@osdk/react/testing`; see [Test your app](./testing.mdx).
|
|
54
|
+
|
|
55
|
+
## How data flows
|
|
56
|
+
|
|
57
|
+
<Cards>
|
|
58
|
+
<Card title="Caching and reactivity" href="/concepts/caching">How hooks share one cache and stay current.</Card>
|
|
59
|
+
<Card title="Loading and errors" href="/concepts/loading-and-errors">Render without flashing while data refreshes.</Card>
|
|
60
|
+
<Card title="Optimistic updates" href="/react/optimistic-updates">Show the result of an action before the server confirms it.</Card>
|
|
61
|
+
<Card title="Devtools" href="/devtools">Inspect the cache, queries, and actions in your running app.</Card>
|
|
62
|
+
</Cards>
|
package/docs/links.mdx
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Follow links
|
|
3
|
+
description: Load the objects linked to one or more source objects with useLinks, or pivot a whole query to a linked type with pivotTo.
|
|
4
|
+
foundryDocs:
|
|
5
|
+
- title: Link types
|
|
6
|
+
path: object-link-types/link-types-overview
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
Links connect objects of one type to objects of another, such as an employee to their office or a manager to their reports. Use `useLinks` to load the linked objects of objects you already have, and `pivotTo` to turn a query for one type into a query for the linked type.
|
|
10
|
+
|
|
11
|
+
The samples on this page use these links from the example SDK:
|
|
12
|
+
|
|
13
|
+
| Link | From | To | Cardinality |
|
|
14
|
+
| --- | --- | --- | --- |
|
|
15
|
+
| `lead` | `Employee` | `Employee` | One |
|
|
16
|
+
| `reports` | `Employee` | `Employee` | Many |
|
|
17
|
+
| `office` | `Employee` | `Office` | One |
|
|
18
|
+
| `occupants` | `Office` | `Employee` | Many |
|
|
19
|
+
|
|
20
|
+
## Load linked objects
|
|
21
|
+
|
|
22
|
+
Pass a source object and a link name. `links` holds the linked objects:
|
|
23
|
+
|
|
24
|
+
```tsx title="src/DirectReports.tsx"
|
|
25
|
+
import type { Employee } from "@my-app/sdk";
|
|
26
|
+
import { useLinks } from "@osdk/react";
|
|
27
|
+
|
|
28
|
+
export function DirectReports({ manager }: { manager: Employee.OsdkInstance }) {
|
|
29
|
+
const { links, isLoading, error } = useLinks(manager, "reports");
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<div>
|
|
33
|
+
<h3>Reports{links && ` (${links.length})`}</h3>
|
|
34
|
+
{isLoading && !links && <p>Loading…</p>}
|
|
35
|
+
{error && <p role="alert">{error.message}</p>}
|
|
36
|
+
<ul>
|
|
37
|
+
{links?.map((report) => (
|
|
38
|
+
<li key={report.$primaryKey}>{report.fullName}</li>
|
|
39
|
+
))}
|
|
40
|
+
</ul>
|
|
41
|
+
</div>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
TypeScript checks the link name against the source type and types `links` as the linked object type.
|
|
47
|
+
|
|
48
|
+
### Links to one object
|
|
49
|
+
|
|
50
|
+
A link to a single object uses the same hook. `links` is an array with at most one element:
|
|
51
|
+
|
|
52
|
+
```tsx title="src/OfficeLabel.tsx"
|
|
53
|
+
import type { Employee } from "@my-app/sdk";
|
|
54
|
+
import { useLinks } from "@osdk/react";
|
|
55
|
+
|
|
56
|
+
export function OfficeLabel({ employee }: { employee: Employee.OsdkInstance }) {
|
|
57
|
+
const { links } = useLinks(employee, "office");
|
|
58
|
+
const office = links?.[0];
|
|
59
|
+
|
|
60
|
+
return <span>{office ? `${office.name}, ${office.city}` : "No office"}</span>;
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Filter, sort, and paginate links
|
|
65
|
+
|
|
66
|
+
`useLinks` accepts `where`, `orderBy`, `$select`, and `pageSize` for the linked type. They work as they do in [`useOsdkObjects`](./objects.mdx#filter-with-where). `fetchMore` loads the next page and is `undefined` when there are no more.
|
|
67
|
+
|
|
68
|
+
```tsx title="src/SeniorReports.tsx"
|
|
69
|
+
import type { Employee } from "@my-app/sdk";
|
|
70
|
+
import { useLinks } from "@osdk/react";
|
|
71
|
+
|
|
72
|
+
export function SeniorReports({ manager }: { manager: Employee.OsdkInstance }) {
|
|
73
|
+
const { links, isLoading, fetchMore } = useLinks(manager, "reports", {
|
|
74
|
+
where: { jobTitle: { $startsWith: "Senior" } },
|
|
75
|
+
orderBy: { fullName: "asc" },
|
|
76
|
+
$select: ["fullName", "jobTitle"],
|
|
77
|
+
pageSize: 25,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<div>
|
|
82
|
+
<ul>
|
|
83
|
+
{links?.map((report) => (
|
|
84
|
+
<li key={report.$primaryKey}>
|
|
85
|
+
{report.fullName}, {report.jobTitle}
|
|
86
|
+
</li>
|
|
87
|
+
))}
|
|
88
|
+
</ul>
|
|
89
|
+
{fetchMore && (
|
|
90
|
+
<button onClick={() => void fetchMore()} disabled={isLoading}>
|
|
91
|
+
Load more
|
|
92
|
+
</button>
|
|
93
|
+
)}
|
|
94
|
+
</div>
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
To load more pages without a button, set `autoFetchMore`: `true` loads every page, and a number loads at least that many linked objects per source.
|
|
100
|
+
|
|
101
|
+
```tsx
|
|
102
|
+
const { links } = useLinks(manager, "reports", { pageSize: 20, autoFetchMore: 100 });
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Wait for the source object
|
|
106
|
+
|
|
107
|
+
The source can be `undefined`, which lets you chain `useLinks` after a hook that is still loading. With no source, the hook makes no request and returns an empty `links` array. Set `enabled` if you want `links` to stay `undefined` until the source arrives:
|
|
108
|
+
|
|
109
|
+
```tsx title="src/ManagerReports.tsx"
|
|
110
|
+
import { Employee } from "@my-app/sdk";
|
|
111
|
+
import { useLinks, useOsdkObject } from "@osdk/react";
|
|
112
|
+
|
|
113
|
+
export function ManagerReports({ employeeId }: { employeeId: number }) {
|
|
114
|
+
const { object: manager } = useOsdkObject(Employee, employeeId);
|
|
115
|
+
const { links: reports, isLoading } = useLinks(manager, "reports", {
|
|
116
|
+
enabled: manager != null,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
return (
|
|
120
|
+
<section>
|
|
121
|
+
<h2>{manager?.fullName ?? "Loading…"}</h2>
|
|
122
|
+
{isLoading && !reports && <p>Loading reports…</p>}
|
|
123
|
+
<ul>
|
|
124
|
+
{reports?.map((report) => (
|
|
125
|
+
<li key={report.$primaryKey}>{report.fullName}</li>
|
|
126
|
+
))}
|
|
127
|
+
</ul>
|
|
128
|
+
</section>
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Load links from many objects
|
|
134
|
+
|
|
135
|
+
Pass an array of source objects to load their links in one hook. `links` is the combined list. `linkedObjectsBySourcePrimaryKey` maps each source's primary key to its own linked objects, so you can group them:
|
|
136
|
+
|
|
137
|
+
```tsx title="src/TeamOffices.tsx"
|
|
138
|
+
import type { Employee } from "@my-app/sdk";
|
|
139
|
+
import { useLinks } from "@osdk/react";
|
|
140
|
+
|
|
141
|
+
export function TeamOffices({ team }: { team: Employee.OsdkInstance[] }) {
|
|
142
|
+
const { linkedObjectsBySourcePrimaryKey } = useLinks(team, "office");
|
|
143
|
+
|
|
144
|
+
return (
|
|
145
|
+
<ul>
|
|
146
|
+
{team.map((employee) => {
|
|
147
|
+
const office = linkedObjectsBySourcePrimaryKey.get(employee.$primaryKey)?.[0];
|
|
148
|
+
return (
|
|
149
|
+
<li key={employee.$primaryKey}>
|
|
150
|
+
{employee.fullName}: {office?.name ?? "No office"}
|
|
151
|
+
</li>
|
|
152
|
+
);
|
|
153
|
+
})}
|
|
154
|
+
</ul>
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Control refetching
|
|
160
|
+
|
|
161
|
+
Two `useLinks` calls with the same source and options share one request if the second starts within `dedupeIntervalMs` (2 seconds by default). Raise it to refetch less often on remount, or set it to `Infinity` to refetch only when the cache is invalidated, for example by an action:
|
|
162
|
+
|
|
163
|
+
```tsx
|
|
164
|
+
const { links } = useLinks(employee, "reports", { dedupeIntervalMs: Infinity });
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
`mode` overrides the cache policy for one call:
|
|
168
|
+
|
|
169
|
+
| `mode` | Behavior |
|
|
170
|
+
| --- | --- |
|
|
171
|
+
| `undefined` (default) | Fetch unless a recent result is cached. |
|
|
172
|
+
| `"force"` | Always fetch. |
|
|
173
|
+
| `"offline"` | Read the cache only; never make a request. |
|
|
174
|
+
|
|
175
|
+
## Pivot with `pivotTo`
|
|
176
|
+
|
|
177
|
+
`useLinks` starts from objects you already hold. When you want the linked objects of every object that matches a filter, pivot the query instead. `useOsdkObjects` takes a `pivotTo` option: `where` filters the source objects, and `data` contains the linked objects.
|
|
178
|
+
|
|
179
|
+
```tsx title="src/SalesOffices.tsx"
|
|
180
|
+
import { Employee } from "@my-app/sdk";
|
|
181
|
+
import { useOsdkObjects } from "@osdk/react";
|
|
182
|
+
|
|
183
|
+
export function SalesOffices() {
|
|
184
|
+
const { data: offices } = useOsdkObjects(Employee, {
|
|
185
|
+
where: { department: "Sales" },
|
|
186
|
+
pivotTo: "office",
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
return (
|
|
190
|
+
<ul>
|
|
191
|
+
{offices?.map((office) => (
|
|
192
|
+
<li key={office.$primaryKey}>
|
|
193
|
+
{office.name}, {office.city}
|
|
194
|
+
</li>
|
|
195
|
+
))}
|
|
196
|
+
</ul>
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
The pivot runs on the server, so the result is one deduplicated list of offices, however many employees share one.
|
|
202
|
+
|
|
203
|
+
With `useObjectSet`, call `pivotTo` on the object set you pass in. The result is then typed as the linked type, which the hook's own `pivotTo` option does not do:
|
|
204
|
+
|
|
205
|
+
```tsx title="src/EngineeringReports.tsx"
|
|
206
|
+
import { Employee } from "@my-app/sdk";
|
|
207
|
+
import { useObjectSet, useOsdkClient } from "@osdk/react";
|
|
208
|
+
import { useMemo } from "react";
|
|
209
|
+
|
|
210
|
+
export function EngineeringReports() {
|
|
211
|
+
const client = useOsdkClient();
|
|
212
|
+
const reports = useMemo(
|
|
213
|
+
() => client(Employee).where({ department: "Engineering" }).pivotTo("reports"),
|
|
214
|
+
[client],
|
|
215
|
+
);
|
|
216
|
+
const { data } = useObjectSet(reports, { orderBy: { fullName: "asc" } });
|
|
217
|
+
|
|
218
|
+
return (
|
|
219
|
+
<ul>
|
|
220
|
+
{data?.map((report) => (
|
|
221
|
+
<li key={report.$primaryKey}>{report.fullName}</li>
|
|
222
|
+
))}
|
|
223
|
+
</ul>
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
Pivoted queries do not receive real-time updates. See [Stream real-time updates](./objects.mdx#stream-real-time-updates).
|
|
229
|
+
|
|
230
|
+
## Choose an approach
|
|
231
|
+
|
|
232
|
+
| You have | You want | Use |
|
|
233
|
+
| --- | --- | --- |
|
|
234
|
+
| One object, or a few | Its linked objects | `useLinks(objects, link)` |
|
|
235
|
+
| Several objects | Linked objects grouped by source | `useLinks(objects, link)` and `linkedObjectsBySourcePrimaryKey` |
|
|
236
|
+
| A filter on the source type | All linked objects, deduplicated | `useOsdkObjects(type, { where, pivotTo })` |
|
|
237
|
+
| An object set from set operations | All linked objects | `useObjectSet(objectSet.pivotTo(link))` |
|
|
238
|
+
| A list of objects | One value from a linked object on each row, such as the lead's name | A [derived property](./object-sets.mdx#add-derived-properties) with `selectProperty` |
|
|
239
|
+
| A list of objects | A count or sum over each row's links | A [derived property](./object-sets.mdx#add-derived-properties) with `aggregate` |
|
|
240
|
+
|
|
241
|
+
Derived properties avoid one `useLinks` call per row: the server computes the value and returns it on each object.
|
|
242
|
+
|
|
243
|
+
## Reference
|
|
244
|
+
|
|
245
|
+
### `useLinks(objects, linkName, options?)`
|
|
246
|
+
|
|
247
|
+
`objects` is one `Osdk.Instance`, an array of them, or `undefined`.
|
|
248
|
+
|
|
249
|
+
| Option | Type | Default | Description |
|
|
250
|
+
| --- | --- | --- | --- |
|
|
251
|
+
| `where` | `WhereClause<L>` | | Filter the linked objects. |
|
|
252
|
+
| `orderBy` | `{ [property]?: "asc" \| "desc" }` | | Sort the linked objects. |
|
|
253
|
+
| `$select` | `PropertyKeys<L>[]` | Server default set | Properties to load for each linked object. |
|
|
254
|
+
| `pageSize` | `number` | `100` | Linked objects per page. |
|
|
255
|
+
| `autoFetchMore` | `boolean \| number` | | Load pages automatically: `true` for all, a number for at least that many per source. |
|
|
256
|
+
| `enabled` | `boolean` | `true` | When `false`, no request is made and `links` is `undefined`. |
|
|
257
|
+
| `mode` | `"force" \| "offline"` | | Cache policy override. See [Control refetching](#control-refetching). |
|
|
258
|
+
| `dedupeIntervalMs` | `number` | `2000` | Identical calls within this window share one request. |
|
|
259
|
+
| `$includeAllBaseObjectProperties` | `boolean` | `false` | When the link targets an interface, also load the implementing type's properties. |
|
|
260
|
+
| `resolveToObjectType` | `boolean` | `false` | When the link targets an interface, return implementing object type instances. |
|
|
261
|
+
|
|
262
|
+
| Return | Type | Description |
|
|
263
|
+
| --- | --- | --- |
|
|
264
|
+
| `links` | `Osdk.Instance<L>[] \| undefined` | Linked objects from all sources. |
|
|
265
|
+
| `linkedObjectsBySourcePrimaryKey` | `ReadonlyMap<string \| number, readonly Osdk.Instance<L>[]>` | Linked objects keyed by source primary key. Empty until loaded. |
|
|
266
|
+
| `isLoading` | `boolean` | `true` while a request is in flight. |
|
|
267
|
+
| `error` | `Error \| undefined` | The last error, if the load failed. |
|
|
268
|
+
| `isOptimistic` | `boolean` | Whether the links include unconfirmed optimistic changes. |
|
|
269
|
+
| `fetchMore` | `(() => Promise<unknown>) \| undefined` | Load the next page. `undefined` when there are no more pages. |
|
|
270
|
+
| `hasMore` | `boolean` | Whether more pages exist. |
|
package/docs/meta.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"title": "React",
|
|
3
|
+
"description": "Hooks for building reactive OSDK apps with @osdk/react, plus the in-app devtools.",
|
|
4
|
+
"icon": "Atom",
|
|
5
|
+
"root": true,
|
|
6
|
+
"pages": [
|
|
7
|
+
"index",
|
|
8
|
+
"---Read data---",
|
|
9
|
+
"objects",
|
|
10
|
+
"links",
|
|
11
|
+
"object-sets",
|
|
12
|
+
"aggregations",
|
|
13
|
+
"---Write data---",
|
|
14
|
+
"actions",
|
|
15
|
+
"optimistic-updates",
|
|
16
|
+
"functions",
|
|
17
|
+
"---Cache---",
|
|
18
|
+
"cache-invalidation",
|
|
19
|
+
"---Platform---",
|
|
20
|
+
"platform-apis",
|
|
21
|
+
"---Quality---",
|
|
22
|
+
"testing",
|
|
23
|
+
"---Devtools---",
|
|
24
|
+
"devtools/index",
|
|
25
|
+
"devtools/overview",
|
|
26
|
+
"devtools/components",
|
|
27
|
+
"devtools/console",
|
|
28
|
+
"devtools/cache"
|
|
29
|
+
]
|
|
30
|
+
}
|