reactive/data-client / .agents/skills/data-client-schema
data-client-schema
VerifiedModel the app's data with @data-client schemas, which normalize responses into one shared cache so an update shows everywhere. Use it whenever an endpoint returning data is added or its response changes (resource, RestEndpoint, GraphQL, Endpoint), when the data's shape changes, when data from different endpoints must be joined or related, when deriving views from loaded data, or when one item looks different in two places. Covers Entity, GQLEntity, Collection, Union and Query.
references/side-effects.md
View on GitHub<!-- Generated by `yarn build:skills` from docs/rest/guides/side-effects.md (react). Edit the source doc, not this file. -->
# Mutation Side-Effects
When a mutation changes more than one resource, do not [expire](https://dataclient.io/docs/api/Controller#expireAll) the other
resources. Expiring them causes a slow cascade of refetches. Instead, include _all_ changed resources in the mutation
response. The mutation stays atomic and needs one request.
Network Cascade
```mermaid
sequenceDiagram
autonumber
participant Client
participant Server
Client->>Server: POST Trade
Note over Client,Server: Backend performs trade
Server->>Client: New Trade Object
Note over Client,Server: Client Expires Account
Client->>Server: GET Account
Note over Client,Server: Lookup Account
Server->>Client: Account
```
 Response Bundling
```mermaid
sequenceDiagram
autonumber
participant Client
participant Server
Client->>Server: POST Trade
Note over Client,Server: Backend performs trade
Server->>Client: Trade + Account
```
## Example
A crypto trading platform updates the user's account balance with each trade. So `POST /trade/` returns the new
trade and the updated account.
```json title="POST /trade/"
{
"trade": {
"id": 2893232,
"user": 1,
"amount": "50.2335324",
"coin": "doge",
"created_at": ""
},
"account": {
"id": 899,
"user": 1,
"balance": "1337.00",
"coin_value": "3.50"
}
}
```
Add the account to the endpoint's `schema`.
```typescript title="resources/Trade.ts"
import { resource, Entity } from '@data-client/rest';
import { Account } from './Account';
export class Trade extends Entity {
id = 0;
user = 0;
amount = '0';
coin = '';
created_at = '';
}
export const TradeResource = resource({
path: '/trade/:id',
schema: Trade,
}).extend(Base => ({
createTrade: Base.getList.push.extend({
schema: {
trade: Base.getList.push.schema,
account: Account,
},
}),
}));
```
`createTrade` extends [getList.push](https://dataclient.io/rest/api/resource#push). When the `POST` completes, the store updates both the new
trade and the account.
```typescript title="CreateTrade.tsx"
export default function CreateTrade() {
const ctrl = useController();
const handleSubmit = payload =>
ctrl.fetch(TradeResource.createTrade, {}, payload);
//...
}
```
> **Note**
>
> Define a new [RestEndpoint](https://dataclient.io/rest/api/RestEndpoint) for any custom endpoint. Its `schema` tells Data Client how to
> store the response.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 193k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 193k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 193k | Updated | Claude CodeCodex | Verified | Install |