The order's machine
Draft an order, then advance it one step at a time. The record names the steps that are legal right now, and a refusal names what it was waiting for.
An order is an event-sourced record. The API exposes its machine as two mutations: draftOrder makes one, and advanceOrder moves it one step. There is no mutation per stage, because what is legal depends on where the order is, and the record says so.
Draft
mutation DraftOrder($input: DraftOrderInput!) {
draftOrder(input: $input) {
...OrderRecord
}
}{
"input": {
"idempotencyKey": "po-4471",
"clientId": "Q2xpZW50OkNMVF8…",
"accountNumber": "IRA-20931",
"depository": "ids_texas"
}
}A client, an account and a depository make a draft. The depository decides the minimum order and which wholesalers can fill it; it can still change until a direction letter names it.
Advance
mutation AdvanceOrder($input: AdvanceOrderInput!) {
advanceOrder(input: $input) {
...OrderRecord
}
}step is a @oneOf input: exactly one of the keys, and the key is the step.
| Step | Input | What it does |
|---|---|---|
submitProductSheet | { budget, lines: [{ productId, quantity, markup }], then? } | The dealer's sheet: what to buy, at what markup, within what budget. then: issue or then: lock carries straight on. |
changeDepository | { depository } | Moves the order before a letter names a depository. |
issueDirection | { authorizedMaximum? } | Issues the direction of investment from the sheet. authorizedMaximum states what you expect it to say; a mismatch is refused. |
sendDirection | { recipientEmail? } | Sends the letter to the holder for signature. recipientEmail states who you expect; a mismatch is refused. |
lockPrices | { expectedAllIn? } | Locks prices on the signed direction. expectedAllIn states the total you showed; if the desk's indicative total has moved, the lock is refused with quote_moved and you show the new one. |
{
"input": {
"idempotencyKey": "po-4471-lock",
"orderId": "T3JkZXI6T1JEX…",
"step": { "lockPrices": { "expectedAllIn": "51234.00" } }
}
}What is legal right now
nextSteps on the record is the list of steps advanceOrder would take. Read it rather than hard-coding the flow:
draftwith no sheet:submitProductSheet,changeDepository.draftwith a sheet:submitProductSheet,changeDepository,issueDirection.direction_pending:sendDirection,issueDirection(reissue),submitProductSheet.direction_signed:lockPrices.lockedand after: nothing; the order runs on its own clocks.
A step out of turn is refused with OrderNotInStage and extensions.nextSteps, the list above at that moment.
Lock before funding
Prices lock on the signed direction before funds land. After the lock the funding clock runs: the custodian wires against the lock, the hold period passes, metal releases, ships, is delivered. Those stages are the order's own; the API reads them and webhooks announce them.
Idempotency
Every mutation takes an idempotencyKey, yours, unique per intent. A repeat with the same key and the same request answers with the first run's order and does nothing. The same key with a different request is refused with IdempotencyConflict. Keys are scoped to your client.
Use your own reference as the key where you have one: a purchase order number for the draft, that number plus the step for each advance.
After the lock
| Fact | Where it shows | Webhook |
|---|---|---|
| Direction issued, sent, signed | direction.stage, direction.*At | order.direction_issued, order.direction_signed |
| Locked | status: locked, lock { allIn, fundingDeadlineAt, holdEndsAt } | order.locked |
| Funded | funding.receivedAt, settlementStep: funded | order.funded |
| Released | release.releasedAt, settlementStep: released | order.released |
| Shipped, delivered | delivery.*At, settlementStep | order.shipped, order.delivered, order.delivery_exception |
| Cancelled | status: cancelled, cancellation { reason, detail, at } | order.cancelled |
nextDeadlineAt is the next clock that matters: the funding deadline after the lock, the delivery check after release.