@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
package/docs/cache-management.md
DELETED
|
@@ -1,222 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
sidebar_position: 5
|
|
3
|
-
---
|
|
4
|
-
|
|
5
|
-
# Cache Management
|
|
6
|
-
|
|
7
|
-
This guide covers how the OSDK React cache system works and how to manually control it.
|
|
8
|
-
|
|
9
|
-
## Why use @osdk/react?
|
|
10
|
-
|
|
11
|
-
`@osdk/react` gives you behavior that is hard to reproduce with a generic data layer like TanStack Query:
|
|
12
|
-
|
|
13
|
-
- **Normalized object cache.** Every `Todo:1` is stored once. When an action edits it, every list, link, and detail view referencing it updates automatically.
|
|
14
|
-
- **Action-driven invalidation.** Action responses specify which objects were added, modified, or deleted. Lists are re-evaluated against their `where` clauses without manual invalidation.
|
|
15
|
-
- **Optimistic updates with rollback.** `$optimisticUpdate` layers your changes on top of the truth layer; on failure they're rolled back.
|
|
16
|
-
- **Real-time updates.** `streamUpdates: true` keeps lists current via WebSocket without polling.
|
|
17
|
-
- **Function dependency tracking with `useOsdkFunction`.** Pass `dependsOn: [ObjectType]` to refetch when any object of that type changes, or `dependsOnObjects: [instance]` to refetch only when specific instances change. The cache wires the dependencies so your function reruns automatically after relevant actions.
|
|
18
|
-
|
|
19
|
-
## How the Cache Works
|
|
20
|
-
|
|
21
|
-
The `OsdkProvider` creates an `ObservableClient` that maintains a normalized cache of all queries and their results.
|
|
22
|
-
|
|
23
|
-
### What Gets Cached
|
|
24
|
-
|
|
25
|
-
| Data Type | Cache Key Based On |
|
|
26
|
-
| -------------------- | ------------------------------------------------------------------------ |
|
|
27
|
-
| **Objects** | Object type + primary key |
|
|
28
|
-
| **Lists** | Object type + where clause + orderBy |
|
|
29
|
-
| **ObjectSets** | Base object type + transforms (where, withProperties, pivotTo, set ops) |
|
|
30
|
-
| **Links** | Source object + link name + filters |
|
|
31
|
-
| **Aggregations** | Object type + where clause + aggregate definition (+ optional ObjectSet) |
|
|
32
|
-
| **Function queries** | Function apiName + canonicalized parameters |
|
|
33
|
-
|
|
34
|
-
Per-property security metadata is loaded alongside objects when `$loadPropertySecurityMetadata: true` is passed to `useOsdkObject` / `useOsdkObjects`.
|
|
35
|
-
|
|
36
|
-
### Cache Sharing
|
|
37
|
-
|
|
38
|
-
- **Same query = same cache entry**: Two components calling `useOsdkObjects(Todo, { where: { isComplete: false } })` share the same cached result
|
|
39
|
-
- **Different filters = different entries**: `{ isComplete: false }` and `{ isComplete: true }` create separate cache entries
|
|
40
|
-
- **Derived properties create variants**: Adding `withProperties` creates a separate cache entry
|
|
41
|
-
|
|
42
|
-
### Object Normalization
|
|
43
|
-
|
|
44
|
-
Objects are stored once and shared across all queries that reference them:
|
|
45
|
-
|
|
46
|
-
```
|
|
47
|
-
useOsdkObjects(Todo) ─┐
|
|
48
|
-
├─► Todo:1 (single cached instance)
|
|
49
|
-
useOsdkObject(Todo, "1") ─┘
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
When `Todo:1` is updated, all queries referencing it see the change immediately.
|
|
53
|
-
|
|
54
|
-
## Automatic Cache Updates
|
|
55
|
-
|
|
56
|
-
### After Actions
|
|
57
|
-
|
|
58
|
-
When an action completes, the server response specifies which objects were affected:
|
|
59
|
-
|
|
60
|
-
- **Added objects**: Fetched and added to cache, relevant lists re-evaluated
|
|
61
|
-
- **Modified objects**: Re-fetched, all queries containing them updated
|
|
62
|
-
- **Deleted objects**: Removed from cache and all lists
|
|
63
|
-
|
|
64
|
-
### Optimistic Updates
|
|
65
|
-
|
|
66
|
-
With `$optimisticUpdate`:
|
|
67
|
-
|
|
68
|
-
1. Optimistic layer created with your changes
|
|
69
|
-
2. UI shows optimistic state (`isOptimistic: true`)
|
|
70
|
-
3. On success: Server data replaces optimistic layer
|
|
71
|
-
4. On failure: Optimistic layer removed, original state restored
|
|
72
|
-
|
|
73
|
-
### Real-time Updates
|
|
74
|
-
|
|
75
|
-
With `streamUpdates: true`, the cache receives WebSocket updates and applies them automatically. Not available for queries that use `pivotTo` or `withProperties`.
|
|
76
|
-
|
|
77
|
-
## When Manual Invalidation is Needed
|
|
78
|
-
|
|
79
|
-
Automatic updates don't cover:
|
|
80
|
-
|
|
81
|
-
- Data changed by an external system or external cache
|
|
82
|
-
- Data modified outside OSDK (direct API calls, background jobs)
|
|
83
|
-
|
|
84
|
-
## Manual Cache Invalidation
|
|
85
|
-
|
|
86
|
-
The `ObservableClient` provides methods to manually invalidate cached data.
|
|
87
|
-
|
|
88
|
-
### Invalidation Methods
|
|
89
|
-
|
|
90
|
-
#### Object Invalidation
|
|
91
|
-
|
|
92
|
-
| Method | Effect | Use Case |
|
|
93
|
-
| --------------------------------- | --------------------------------------------- | ---------------------------------------- |
|
|
94
|
-
| `invalidateObjects([obj1, obj2])` | Re-fetches specific objects | You know exactly which objects are stale |
|
|
95
|
-
| `invalidateObjectType(Todo)` | Re-fetches all objects and lists of that type | External bulk update |
|
|
96
|
-
| `invalidateAll()` | Re-fetches everything | Last resort |
|
|
97
|
-
|
|
98
|
-
#### Function Invalidation
|
|
99
|
-
|
|
100
|
-
| Method | Effect | Use Case |
|
|
101
|
-
| ------------------------------------------ | --------------------------------------------------- | ------------------------------------- |
|
|
102
|
-
| `invalidateFunction(queryDef, params)` | Re-fetches a specific function query | You know which function call is stale |
|
|
103
|
-
| `invalidateFunction(queryDef)` | Re-fetches ALL queries for that function | External change affecting all calls |
|
|
104
|
-
| `invalidateFunctionsByObject(apiName, pk)` | Re-fetches functions depending on a specific object | Object changed outside action flow |
|
|
105
|
-
|
|
106
|
-
### Usage
|
|
107
|
-
|
|
108
|
-
Get the `ObservableClient` with `useObservableClient` from any component below `OsdkProvider`:
|
|
109
|
-
|
|
110
|
-
```tsx
|
|
111
|
-
import { getEmployeeMetrics, Todo } from "@my/osdk";
|
|
112
|
-
import { useObservableClient } from "@osdk/react";
|
|
113
|
-
|
|
114
|
-
const observableClient = useObservableClient();
|
|
115
|
-
|
|
116
|
-
// Invalidate specific objects
|
|
117
|
-
await observableClient.invalidateObjects([todo1, todo2]);
|
|
118
|
-
|
|
119
|
-
// Invalidate all data for a type
|
|
120
|
-
await observableClient.invalidateObjectType(Todo);
|
|
121
|
-
|
|
122
|
-
// Invalidate a specific function query
|
|
123
|
-
await observableClient.invalidateFunction(getEmployeeMetrics, {
|
|
124
|
-
departmentId: "sales",
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
// Invalidate ALL queries for a function
|
|
128
|
-
await observableClient.invalidateFunction(getEmployeeMetrics);
|
|
129
|
-
|
|
130
|
-
// Invalidate functions that depend on a specific object instance
|
|
131
|
-
// (functions called with dependsOnObjects containing this object)
|
|
132
|
-
await observableClient.invalidateFunctionsByObject("Employee", "emp-123");
|
|
133
|
-
|
|
134
|
-
// Invalidate everything (use sparingly)
|
|
135
|
-
await observableClient.invalidateAll();
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
### Type Isolation
|
|
139
|
-
|
|
140
|
-
`invalidateObjectType(Todo)` only affects Todo-related data:
|
|
141
|
-
|
|
142
|
-
- Re-fetches all Todo objects
|
|
143
|
-
- Re-fetches all Todo lists
|
|
144
|
-
- Re-fetches links where Todo is the source type
|
|
145
|
-
- Does NOT affect other object types, even if linked to Todos
|
|
146
|
-
|
|
147
|
-
### Function Invalidation Details
|
|
148
|
-
|
|
149
|
-
`invalidateFunctionsByObject(apiName, primaryKey)` invalidates function queries that were registered with `dependsOnObjects` containing the specified object:
|
|
150
|
-
|
|
151
|
-
```tsx
|
|
152
|
-
// If useOsdkFunction was called with:
|
|
153
|
-
const { data } = useOsdkFunction(getEmployeeReport, {
|
|
154
|
-
params: { employeeId: employee.$primaryKey },
|
|
155
|
-
dependsOnObjects: [employee],
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
// You can invalidate it by calling:
|
|
159
|
-
await observableClient.invalidateFunctionsByObject(
|
|
160
|
-
"Employee",
|
|
161
|
-
employee.$primaryKey,
|
|
162
|
-
);
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
This is useful when you know a specific object has changed outside of the normal action flow.
|
|
166
|
-
|
|
167
|
-
## Cache with Optimistic Updates
|
|
168
|
-
|
|
169
|
-
```tsx
|
|
170
|
-
import { completeTodo, Todo } from "@my/osdk";
|
|
171
|
-
import { useOsdkAction, useOsdkObject } from "@osdk/react";
|
|
172
|
-
|
|
173
|
-
function TodoView({ todo }: { todo: Todo.OsdkInstance }) {
|
|
174
|
-
const { isOptimistic } = useOsdkObject(todo);
|
|
175
|
-
const { applyAction } = useOsdkAction(completeTodo);
|
|
176
|
-
|
|
177
|
-
const handleComplete = () => {
|
|
178
|
-
applyAction({
|
|
179
|
-
todo,
|
|
180
|
-
isComplete: true,
|
|
181
|
-
$optimisticUpdate: (ou) => {
|
|
182
|
-
ou.updateObject(todo.$clone({ isComplete: true }));
|
|
183
|
-
},
|
|
184
|
-
});
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
return (
|
|
188
|
-
<div>
|
|
189
|
-
{todo.title}
|
|
190
|
-
{isOptimistic && " (Saving...)"}
|
|
191
|
-
<button onClick={handleComplete}>Complete</button>
|
|
192
|
-
</div>
|
|
193
|
-
);
|
|
194
|
-
}
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
## Dev-Mode Debugging
|
|
198
|
-
|
|
199
|
-
The observable client has dev-only debugging knobs on the `OsdkProvider` `devMode` prop. They have no effect in production builds, where the relevant code is stripped at build time.
|
|
200
|
-
|
|
201
|
-
The client's loggers default to the `error` level, which hides the `debug` tracing the observable layer emits. Raise the level to surface it:
|
|
202
|
-
|
|
203
|
-
```tsx
|
|
204
|
-
<OsdkProvider client={client} devMode={{ logLevel: "debug" }}>
|
|
205
|
-
```
|
|
206
|
-
|
|
207
|
-
The cache internals can also log their behavior. `refCounts` logs cache-entry reference-count lifecycle (creation, live counts, time-to-live countdown, cleanup, finalization) and shortens the retention window so cleanup is observable sooner; `cacheKeys` logs cache-key canonicalization lookups:
|
|
208
|
-
|
|
209
|
-
```tsx
|
|
210
|
-
<OsdkProvider client={client} devMode={{ debug: { refCounts: true, cacheKeys: true } }}>
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
If you are configuring the observable client directly, the same options are available as `createObservableClient(client, extraUserAgents, { devMode: { logLevel: "debug", debug: { refCounts: true } } })`.
|
|
214
|
-
|
|
215
|
-
## Best Practices
|
|
216
|
-
|
|
217
|
-
1. **Reuse query parameters.** Two components that pass identical `where` / `orderBy` / `withProperties` share one cache entry. Hoist the parameter object to a module-level constant or memoize it so React doesn't construct a new one each render.
|
|
218
|
-
2. **Prefer object-level invalidation.** `invalidateObjects([todo])` is cheaper than `invalidateObjectType(Todo)`, which is cheaper than `invalidateAll()`. Reach for the broadest only when you cannot identify the affected rows.
|
|
219
|
-
3. **Let actions drive invalidation.** If the change comes from an OSDK action, the cache updates automatically — do not call `invalidate*` afterward. Manual invalidation is for changes that bypass the action flow (external system, scheduled job, direct REST call).
|
|
220
|
-
4. **Use `dependsOn` / `dependsOnObjects` on functions.** Declare what your function reads so the cache can re-run it after relevant actions, instead of invalidating it manually.
|
|
221
|
-
5. **Avoid second sources of truth.** Don't copy OSDK data into Redux, TanStack Query, or local component state — you'll fight optimistic updates and lose normalization.
|
|
222
|
-
6. **Don't `invalidateAll()` on mount.** That defeats caching and triggers a full refetch every time the component tree mounts. If you need a fresh load, scope it to a specific type.
|
package/docs/getting-started.md
DELETED
|
@@ -1,319 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
sidebar_position: 1
|
|
3
|
-
---
|
|
4
|
-
|
|
5
|
-
# Getting Started
|
|
6
|
-
|
|
7
|
-
This guide covers installation, setup, and your first OSDK React application.
|
|
8
|
-
|
|
9
|
-
## Installation
|
|
10
|
-
|
|
11
|
-
### 1. Install `@osdk/react`
|
|
12
|
-
|
|
13
|
-
:::tip If your tooling already installs dependencies, skip this step.
|
|
14
|
-
:::
|
|
15
|
-
|
|
16
|
-
Use whichever package manager your project uses:
|
|
17
|
-
|
|
18
|
-
```bash
|
|
19
|
-
# npm
|
|
20
|
-
npm install @osdk/api @osdk/client @osdk/react
|
|
21
|
-
|
|
22
|
-
# pnpm
|
|
23
|
-
pnpm add @osdk/api @osdk/client @osdk/react
|
|
24
|
-
|
|
25
|
-
# yarn
|
|
26
|
-
yarn add @osdk/api @osdk/client @osdk/react
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
:::warning Version Compatibility
|
|
30
|
-
All `@osdk/*` packages must use **compatible versions**. Mismatched versions (e.g., mixing an old `@osdk/client` with a newer `@osdk/react`) will cause TypeScript errors. See [troubleshooting](#property-store-is-missing-with-osdkprovider) if you encounter issues.
|
|
31
|
-
:::
|
|
32
|
-
|
|
33
|
-
You can find the latest versions on npm:
|
|
34
|
-
|
|
35
|
-
- [@osdk/react versions](https://www.npmjs.com/package/@osdk/react?activeTab=versions)
|
|
36
|
-
- [@osdk/client versions](https://www.npmjs.com/package/@osdk/client?activeTab=versions)
|
|
37
|
-
- [@osdk/api versions](https://www.npmjs.com/package/@osdk/api?activeTab=versions)
|
|
38
|
-
|
|
39
|
-
### 2. Generate Your SDK
|
|
40
|
-
|
|
41
|
-
If you haven't generated an SDK yet:
|
|
42
|
-
|
|
43
|
-
1. Open Developer Console for your Foundry
|
|
44
|
-
2. Click "SDK versions" tab (tag icon in left navbar)
|
|
45
|
-
3. Click "Generate new version" → check "npm" checkbox → select the latest generator
|
|
46
|
-
4. Add the generated SDK to your package.json:
|
|
47
|
-
|
|
48
|
-
```json
|
|
49
|
-
{
|
|
50
|
-
"dependencies": {
|
|
51
|
-
"@my/osdk": "^0.4.0"
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
## Provider Setup
|
|
57
|
-
|
|
58
|
-
Wrap your app with `OsdkProvider` at the root. Every hook in `@osdk/react` reads from it.
|
|
59
|
-
|
|
60
|
-
```tsx
|
|
61
|
-
import { OsdkProvider } from "@osdk/react";
|
|
62
|
-
import client from "./client";
|
|
63
|
-
|
|
64
|
-
ReactDOM.createRoot(document.getElementById("root")!).render(
|
|
65
|
-
<OsdkProvider client={client}>
|
|
66
|
-
<App />
|
|
67
|
-
</OsdkProvider>,
|
|
68
|
-
);
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
Available exports from `@osdk/react`:
|
|
72
|
-
|
|
73
|
-
- `OsdkProvider` - Provider, required at the app root
|
|
74
|
-
- `useOsdkObjects` - Query collections of objects
|
|
75
|
-
- `useOsdkObject` - Query single objects
|
|
76
|
-
- `useOsdkAction` - Execute and validate actions
|
|
77
|
-
- `useLinks` - Navigate object relationships
|
|
78
|
-
- `useObjectSet` - Advanced queries with set operations
|
|
79
|
-
- `useOsdkAggregation` - Server-side aggregations
|
|
80
|
-
- `useOsdkFunction` - Execute and observe OSDK functions
|
|
81
|
-
- `useDebouncedCallback` - Debounce callbacks
|
|
82
|
-
- `useOsdkClient` - Access the OSDK client
|
|
83
|
-
- `useOsdkMetadata` - Fetch type metadata
|
|
84
|
-
|
|
85
|
-
Platform APIs (admin/CBAC) live in a separate entry point at `@osdk/react/platform-apis`. See [Platform APIs](/react/platform-apis).
|
|
86
|
-
|
|
87
|
-
## Quick Start Checklist
|
|
88
|
-
|
|
89
|
-
Before using the data hooks:
|
|
90
|
-
|
|
91
|
-
- [ ] App wrapped in `<OsdkProvider client={client}>`
|
|
92
|
-
- [ ] OsdkProvider at app root (not nested inside components)
|
|
93
|
-
- [ ] Passing configured OSDK client to OsdkProvider
|
|
94
|
-
- [ ] All components using the hooks inside the provider
|
|
95
|
-
|
|
96
|
-
## First Query
|
|
97
|
-
|
|
98
|
-
:::note About `@my/osdk`
|
|
99
|
-
Throughout this documentation, `@my/osdk` refers to **your generated SDK package**. This is created when you generate an SDK in Foundry Developer Console (step 2 above). Replace `@my/osdk` with your actual package name (e.g., `@your-app/sdk`).
|
|
100
|
-
:::
|
|
101
|
-
|
|
102
|
-
```tsx
|
|
103
|
-
import { Todo } from "@my/osdk";
|
|
104
|
-
import { useOsdkObjects } from "@osdk/react";
|
|
105
|
-
|
|
106
|
-
function TodoList() {
|
|
107
|
-
const { data, isLoading, error } = useOsdkObjects(Todo, {
|
|
108
|
-
where: { isComplete: false },
|
|
109
|
-
orderBy: { createdAt: "desc" },
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
if (!data && isLoading) {
|
|
113
|
-
return <div>Loading...</div>;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (error) {
|
|
117
|
-
return <div>Error: {error.message}</div>;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return (
|
|
121
|
-
<div>
|
|
122
|
-
{data?.map(todo => <div key={todo.$primaryKey}>{todo.title}</div>)}
|
|
123
|
-
</div>
|
|
124
|
-
);
|
|
125
|
-
}
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
See [Querying Data](/react/querying-data) for filtering, pagination, real-time updates, and more.
|
|
129
|
-
|
|
130
|
-
## First Action
|
|
131
|
-
|
|
132
|
-
```tsx
|
|
133
|
-
import { completeTodo, Todo } from "@my/osdk";
|
|
134
|
-
import { useOsdkAction } from "@osdk/react";
|
|
135
|
-
|
|
136
|
-
function CompleteTodoButton({ todo }: { todo: Todo.OsdkInstance }) {
|
|
137
|
-
const { applyAction, isPending, error } = useOsdkAction(completeTodo);
|
|
138
|
-
|
|
139
|
-
const handleClick = () => {
|
|
140
|
-
applyAction({ todo, isComplete: true });
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
return (
|
|
144
|
-
<div>
|
|
145
|
-
<button onClick={handleClick} disabled={isPending}>
|
|
146
|
-
{isPending ? "Saving..." : "Complete"}
|
|
147
|
-
</button>
|
|
148
|
-
{error && (
|
|
149
|
-
<div>
|
|
150
|
-
Error: {error.actionValidation?.message
|
|
151
|
-
?? (error.unknown ? String(error.unknown) : "Unknown error")}
|
|
152
|
-
</div>
|
|
153
|
-
)}
|
|
154
|
-
</div>
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
See [Actions](/react/actions) for validation, batch actions, and optimistic updates.
|
|
160
|
-
|
|
161
|
-
## Troubleshooting
|
|
162
|
-
|
|
163
|
-
### "Property 'store' is missing" with OsdkProvider
|
|
164
|
-
|
|
165
|
-
This error occurs when your `@osdk/client` version is incompatible with `@osdk/react`. The versions must match.
|
|
166
|
-
|
|
167
|
-
**Fix:** Ensure all `@osdk/*` packages use compatible versions. Generate a fresh SDK against the same `@osdk/client` line that `@osdk/react` was published against, and update your package.json so `@osdk/api`, `@osdk/client`, and `@osdk/react` all resolve to compatible majors.
|
|
168
|
-
|
|
169
|
-
### "Cannot read property 'observableClient' of undefined"
|
|
170
|
-
|
|
171
|
-
The component is outside `<OsdkProvider>`. Move OsdkProvider higher in your component tree.
|
|
172
|
-
|
|
173
|
-
**Wrong:**
|
|
174
|
-
|
|
175
|
-
```tsx
|
|
176
|
-
function App() {
|
|
177
|
-
return <TodoList />; // No OsdkProvider!
|
|
178
|
-
}
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
**Correct:**
|
|
182
|
-
|
|
183
|
-
```tsx
|
|
184
|
-
ReactDOM.createRoot(document.getElementById("root")!).render(
|
|
185
|
-
<OsdkProvider client={client}>
|
|
186
|
-
<TodoList />
|
|
187
|
-
</OsdkProvider>,
|
|
188
|
-
);
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
### Only some components work with hooks
|
|
192
|
-
|
|
193
|
-
OsdkProvider is not at the root.
|
|
194
|
-
|
|
195
|
-
**Wrong:**
|
|
196
|
-
|
|
197
|
-
```tsx
|
|
198
|
-
function App() {
|
|
199
|
-
return (
|
|
200
|
-
<>
|
|
201
|
-
<Header /> {/* Can't use hooks */}
|
|
202
|
-
<OsdkProvider client={client}>
|
|
203
|
-
<Content /> {/* Only this can use hooks */}
|
|
204
|
-
</OsdkProvider>
|
|
205
|
-
</>
|
|
206
|
-
);
|
|
207
|
-
}
|
|
208
|
-
```
|
|
209
|
-
|
|
210
|
-
**Correct:**
|
|
211
|
-
|
|
212
|
-
```tsx
|
|
213
|
-
ReactDOM.createRoot(document.getElementById("root")!).render(
|
|
214
|
-
<OsdkProvider client={client}>
|
|
215
|
-
<App /> {/* All components can use hooks */}
|
|
216
|
-
</OsdkProvider>,
|
|
217
|
-
);
|
|
218
|
-
```
|
|
219
|
-
|
|
220
|
-
### Hooks return no data
|
|
221
|
-
|
|
222
|
-
Ensure you're passing the same client instance, not creating a new one:
|
|
223
|
-
|
|
224
|
-
**Wrong:**
|
|
225
|
-
|
|
226
|
-
```tsx
|
|
227
|
-
<OsdkProvider client={createNewClient()}> {/* New instance each render */}
|
|
228
|
-
```
|
|
229
|
-
|
|
230
|
-
**Correct:**
|
|
231
|
-
|
|
232
|
-
```tsx
|
|
233
|
-
import client from "./client"; // Created once
|
|
234
|
-
<OsdkProvider client={client}>
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
### "Hooks cannot be conditionally called"
|
|
238
|
-
|
|
239
|
-
Use the `enabled` option instead of conditional hook calls:
|
|
240
|
-
|
|
241
|
-
**Wrong:**
|
|
242
|
-
|
|
243
|
-
```tsx
|
|
244
|
-
if (shouldLoad) {
|
|
245
|
-
const { data } = useOsdkObjects(Todo); // Conditional hook!
|
|
246
|
-
}
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
**Correct:**
|
|
250
|
-
|
|
251
|
-
```tsx
|
|
252
|
-
const { data } = useOsdkObjects(Todo, {
|
|
253
|
-
enabled: shouldLoad,
|
|
254
|
-
});
|
|
255
|
-
```
|
|
256
|
-
|
|
257
|
-
## NPM Peer Dependency Issues
|
|
258
|
-
|
|
259
|
-
If NPM has trouble resolving peer dependencies, add to package.json:
|
|
260
|
-
|
|
261
|
-
```json
|
|
262
|
-
{
|
|
263
|
-
"overrides": {
|
|
264
|
-
"@osdk/client": "$@osdk/client",
|
|
265
|
-
"@osdk/react": "$@osdk/react"
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
```
|
|
269
|
-
|
|
270
|
-
## FAQ
|
|
271
|
-
|
|
272
|
-
### When should I use `@osdk/react` vs React Query?
|
|
273
|
-
|
|
274
|
-
**Use `@osdk/react`** when your application primarily works with OSDK data:
|
|
275
|
-
|
|
276
|
-
- Querying Foundry objects and ontology data
|
|
277
|
-
- Executing actions on Foundry objects
|
|
278
|
-
- Taking advantage of automatic cache management for OSDK objects
|
|
279
|
-
- Real-time updates via WebSocket subscriptions
|
|
280
|
-
|
|
281
|
-
**Use React Query** when you need to make requests to third-party APIs or non-OSDK endpoints:
|
|
282
|
-
|
|
283
|
-
- External REST APIs
|
|
284
|
-
- GraphQL services not backed by Foundry
|
|
285
|
-
- Custom backend services
|
|
286
|
-
|
|
287
|
-
**You can use both together.** `@osdk/react` hooks work independently from React Query. For applications that mix OSDK data with external services, use `@osdk/react` for Foundry operations and React Query for external APIs.
|
|
288
|
-
|
|
289
|
-
```tsx
|
|
290
|
-
import { Todo } from "@my/osdk";
|
|
291
|
-
import { useOsdkObjects } from "@osdk/react";
|
|
292
|
-
import { useQuery } from "@tanstack/react-query";
|
|
293
|
-
|
|
294
|
-
function Dashboard() {
|
|
295
|
-
// Use @osdk/react for Foundry data
|
|
296
|
-
const { data: todos } = useOsdkObjects(Todo);
|
|
297
|
-
|
|
298
|
-
// Use React Query for external APIs
|
|
299
|
-
const { data: weatherData } = useQuery({
|
|
300
|
-
queryKey: ["weather"],
|
|
301
|
-
queryFn: () => fetch("https://api.weather.com/current").then(r => r.json()),
|
|
302
|
-
});
|
|
303
|
-
|
|
304
|
-
return (
|
|
305
|
-
<div>
|
|
306
|
-
<TodoList todos={todos} />
|
|
307
|
-
<WeatherWidget data={weatherData} />
|
|
308
|
-
</div>
|
|
309
|
-
);
|
|
310
|
-
}
|
|
311
|
-
```
|
|
312
|
-
|
|
313
|
-
## Next Steps
|
|
314
|
-
|
|
315
|
-
- [Querying Data](/react/querying-data) - useOsdkObjects, useOsdkObject, useLinks, pagination, real-time updates
|
|
316
|
-
- [Actions](/react/actions) - useOsdkAction, validation, optimistic updates, debouncing
|
|
317
|
-
- [Advanced Queries](/react/advanced-queries) - useObjectSet, derived properties, aggregations
|
|
318
|
-
- [Cache Management](/react/cache-management) - Cache behavior and manual invalidation
|
|
319
|
-
- [Platform APIs](/react/platform-apis) - useCurrentFoundryUser, useFoundryUser, useFoundryUsersList
|