@stonecrop/graphql-middleware 0.13.14 → 0.14.0
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/README.md +19 -18
- package/dist/graphql-middleware.d.ts +44 -74
- package/dist/index.js +314 -354
- package/dist/index.js.map +1 -1
- package/dist/src/dispatch/transition.d.ts +20 -8
- package/dist/src/dispatch/transition.d.ts.map +1 -1
- package/dist/src/dispatch/transition.js +33 -9
- package/dist/src/index.d.ts +2 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -2
- package/dist/src/plugin/postgraphile.d.ts +3 -3
- package/dist/src/plugin/postgraphile.d.ts.map +1 -1
- package/dist/src/plugin/postgraphile.js +48 -23
- package/dist/src/registry/doctypes.d.ts.map +1 -1
- package/dist/src/registry/doctypes.js +17 -5
- package/dist/src/typeDefs.d.ts.map +1 -1
- package/dist/src/typeDefs.js +15 -10
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ GraphQL backend for the Stonecrop framework. Reads doctype schemas and exposes t
|
|
|
6
6
|
|
|
7
7
|
- **Record fetching** — Resolves `stonecropRecord` and `stonecropRecords` via direct parameterised SQL, batched via `loadOneWithPgClient` from `@dataplan/pg`
|
|
8
8
|
- **Fetch-strategy dispatch** — For `stonecropRecord`, iterates `meta.links` and applies `sync` / `lazy` / `custom` strategies; sync links issue additional SQL and merge results into the record; lazy links are absent from the response
|
|
9
|
-
- **Action dispatch** —
|
|
9
|
+
- **Action dispatch** — Runs a doctype's declared `workflow.actions` as guarded state transitions (`applyGuardedTransition`) via `sideEffectWithPgClient`
|
|
10
10
|
- **Preset** — `createStonecropPreset()` wraps `PostGraphileAmberPreset` so user apps never import from PostGraphile directly
|
|
11
11
|
|
|
12
12
|
## Setup
|
|
@@ -17,11 +17,9 @@ import {
|
|
|
17
17
|
createStonecropPlugin,
|
|
18
18
|
makePgService,
|
|
19
19
|
loadDoctypes,
|
|
20
|
-
registerBuiltinHandlers,
|
|
21
20
|
} from '@stonecrop/graphql-middleware'
|
|
22
21
|
|
|
23
22
|
loadDoctypes('./doctypes')
|
|
24
|
-
registerBuiltinHandlers()
|
|
25
23
|
|
|
26
24
|
const preset: GraphileConfig.Preset = {
|
|
27
25
|
extends: [createStonecropPreset()],
|
|
@@ -34,7 +32,8 @@ const preset: GraphileConfig.Preset = {
|
|
|
34
32
|
|
|
35
33
|
| Option | Default | Description |
|
|
36
34
|
|--------|---------|-------------|
|
|
37
|
-
| `
|
|
35
|
+
| `debug` | `false` | Log SQL run inside custom `loadOneWithPgClient` callbacks (prefixed `[@stonecrop/graphql-middleware]`) |
|
|
36
|
+
| `tables` | `{}` | Override the PostgreSQL `FROM` target per doctype (name → table; bare `'plan'` or schema-qualified `'orpin.plan'`). Defaults to `camelToSnake(name)`. |
|
|
38
37
|
|
|
39
38
|
## Debugging
|
|
40
39
|
|
|
@@ -94,8 +93,8 @@ Each doctype JSON file defines structure, relationships, and workflow:
|
|
|
94
93
|
{
|
|
95
94
|
"name": "SalesOrder",
|
|
96
95
|
"fields": [
|
|
97
|
-
{ "fieldname": "id", "
|
|
98
|
-
{ "fieldname": "status", "
|
|
96
|
+
{ "fieldname": "id", "component": "ATextInput" },
|
|
97
|
+
{ "fieldname": "status", "component": "ADropdown" }
|
|
99
98
|
],
|
|
100
99
|
"links": {
|
|
101
100
|
"items": {
|
|
@@ -108,7 +107,7 @@ Each doctype JSON file defines structure, relationships, and workflow:
|
|
|
108
107
|
"workflow": {
|
|
109
108
|
"states": ["Draft", "Submitted"],
|
|
110
109
|
"actions": {
|
|
111
|
-
"submit": { "label": "Submit", "
|
|
110
|
+
"submit": { "label": "Submit", "allowedStates": ["Draft"], "nextState": "Submitted" }
|
|
112
111
|
}
|
|
113
112
|
}
|
|
114
113
|
}
|
|
@@ -128,26 +127,28 @@ When `fetch` is omitted, `noneOrMany`/`atLeastOne` links default to `sync` (limi
|
|
|
128
127
|
|
|
129
128
|
### Display-only fields
|
|
130
129
|
|
|
131
|
-
Fields with `
|
|
130
|
+
Fields with `computed: true` have no backing database column and are excluded from all SQL queries:
|
|
132
131
|
|
|
133
132
|
```json
|
|
134
|
-
{ "fieldname": "planner", "
|
|
133
|
+
{ "fieldname": "planner", "component": "Planner", "computed": true, "label": "Resource Planner" }
|
|
135
134
|
```
|
|
136
135
|
|
|
137
136
|
## Actions
|
|
138
137
|
|
|
139
|
-
|
|
138
|
+
Actions are declared per doctype in `workflow.actions` — there is no separate handler registry. Each action names the states it may run from (`allowedStates`) and the state the record moves to (`nextState`):
|
|
140
139
|
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
140
|
+
```json
|
|
141
|
+
"workflow": {
|
|
142
|
+
"states": ["Draft", "Submitted", "Cancelled"],
|
|
143
|
+
"actions": {
|
|
144
|
+
"submit": { "label": "Submit", "allowedStates": ["Draft"], "nextState": "Submitted" },
|
|
145
|
+
"cancel": { "label": "Cancel", "allowedStates": ["Draft", "Submitted"], "nextState": "Cancelled" }
|
|
146
|
+
}
|
|
147
|
+
}
|
|
149
148
|
```
|
|
150
149
|
|
|
150
|
+
The `stonecropAction(doctype, action, args)` mutation dispatches through `applyGuardedTransition`: it reads the record's `status`, rejects the action if the current state is not in `allowedStates` (`isActionAllowedInState`), then writes `nextState`. The record is identified by `args[0].id`. Actions with no `nextState` (stateless commands) and self-transitions have no state target and are rejected by this backend rather than silently succeeding.
|
|
151
|
+
|
|
151
152
|
## Custom fetch handlers
|
|
152
153
|
|
|
153
154
|
Register custom fetch handlers for links that use `{ "method": "custom" }`:
|
|
@@ -7,55 +7,39 @@ import type { PgClient } from '@dataplan/pg';
|
|
|
7
7
|
import { ValidationError } from '@stonecrop/schema';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
10
|
+
* Apply a workflow action's state transition on the server, enforcing `allowedStates`.
|
|
11
11
|
*
|
|
12
|
-
* The
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
12
|
+
* The server owns the transition: it reads the record's authoritative current state,
|
|
13
|
+
* rejects the action when `isActionAllowedInState` denies it, then writes `nextState`
|
|
14
|
+
* verbatim. Storage access is injected via `io` so this one guard serves every backend
|
|
15
|
+
* and can never disagree with the frontend's `getAvailableTransitions`, which shares the
|
|
16
|
+
* same predicate.
|
|
16
17
|
*
|
|
17
|
-
*
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
* Rows returned by raw `pgClient.query()` carry **snake_case column names**,
|
|
26
|
-
* not the camelCase fieldnames the client expects (see ADR 0004 — the
|
|
27
|
-
* middleware's own read paths alias columns at the SQL layer, e.g.
|
|
28
|
-
* `"display_name" AS "displayName"`). Handlers querying with `pgClient`
|
|
29
|
-
* own the same conversion for their return values — see {@link ActionHandler}.
|
|
30
|
-
*/
|
|
31
|
-
pgClient?: PgClient;
|
|
32
|
-
/** Additional context properties */
|
|
33
|
-
[key: string]: unknown;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Action handler function signature.
|
|
38
|
-
*
|
|
39
|
-
* The resolved value is passed through verbatim as `ActionResult.data` —
|
|
40
|
-
* the middleware applies no transformation. Return **API-layer data**: plain
|
|
41
|
-
* objects keyed by camelCase fieldnames, matching what `stonecropRecord`
|
|
42
|
-
* returns (see ADR 0004).
|
|
18
|
+
* There are three action shapes this guard distinguishes:
|
|
19
|
+
* - A cross-state **transition** (has `nextState`): writes the new `status`, guarded by `allowedStates`.
|
|
20
|
+
* - A **self-transition** (`selfTransition: true`, no `nextState`, e.g. `Save`): stays in the current
|
|
21
|
+
* state and persists record field `data` in place via `io.writeData`, guarded by `allowedStates`.
|
|
22
|
+
* A backend without a data-write path rejects it loudly instead of dropping it silently.
|
|
23
|
+
* - Anything else with no `nextState` and no `selfTransition` flag is a genuine authoring mistake (or a
|
|
24
|
+
* stateless side-effect command whose server handler is not wired — the intended `callHandler` primitive
|
|
25
|
+
* is still unimplemented): it fails loudly before touching the backend rather than reporting a false success.
|
|
43
26
|
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* snake_case column names to the client.
|
|
49
|
-
*
|
|
50
|
-
* @public
|
|
51
|
-
*/
|
|
52
|
-
export declare type ActionHandler = (args: unknown[], context: ActionContext) => Promise<unknown>;
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Built-in handlers available for registration
|
|
27
|
+
* @param actionDef - The action's `label`, `allowedStates`, and either `nextState` (transition) or `selfTransition`
|
|
28
|
+
* @param io - Backend read/write closures
|
|
29
|
+
* @param data - Record field data for a self-transition's mutate-in-place write (ignored by transitions)
|
|
30
|
+
* @returns Action result envelope — the resulting `data.state` for a transition, the full record for a self-transition
|
|
56
31
|
* @public
|
|
57
32
|
*/
|
|
58
|
-
export declare
|
|
33
|
+
export declare function applyGuardedTransition(actionDef: {
|
|
34
|
+
label?: string;
|
|
35
|
+
allowedStates?: string[];
|
|
36
|
+
nextState?: string;
|
|
37
|
+
selfTransition?: boolean;
|
|
38
|
+
}, io: GuardedTransitionIO, data?: Record<string, unknown>): Promise<{
|
|
39
|
+
success: boolean;
|
|
40
|
+
data: unknown;
|
|
41
|
+
error: string | null;
|
|
42
|
+
}>;
|
|
59
43
|
|
|
60
44
|
/**
|
|
61
45
|
* Remove all registered fetch handlers. Primarily for test isolation.
|
|
@@ -63,12 +47,6 @@ export declare const builtinHandlers: Record<string, ActionHandler>;
|
|
|
63
47
|
*/
|
|
64
48
|
export declare function clearFetchHandlers(): void;
|
|
65
49
|
|
|
66
|
-
/**
|
|
67
|
-
* Clear all registered handlers
|
|
68
|
-
* @public
|
|
69
|
-
*/
|
|
70
|
-
export declare function clearHandlers(): void;
|
|
71
|
-
|
|
72
50
|
/**
|
|
73
51
|
* Clear all registered doctypes
|
|
74
52
|
* @public
|
|
@@ -198,13 +176,6 @@ export declare function getAllMeta(): DoctypeMeta[];
|
|
|
198
176
|
*/
|
|
199
177
|
export declare function getFetchHandler(name: string): FetchHandler | undefined;
|
|
200
178
|
|
|
201
|
-
/**
|
|
202
|
-
* Get a registered handler by name
|
|
203
|
-
* @param name - Name of the action handler to retrieve
|
|
204
|
-
* @public
|
|
205
|
-
*/
|
|
206
|
-
export declare function getHandler(name: string): ActionHandler | undefined;
|
|
207
|
-
|
|
208
179
|
/**
|
|
209
180
|
* Get a doctype by name
|
|
210
181
|
* @param name - Name of the doctype to retrieve
|
|
@@ -213,11 +184,24 @@ export declare function getHandler(name: string): ActionHandler | undefined;
|
|
|
213
184
|
export declare function getMeta(name: string): DoctypeMeta | undefined;
|
|
214
185
|
|
|
215
186
|
/**
|
|
216
|
-
*
|
|
217
|
-
*
|
|
187
|
+
* Backend IO the dispatch layer injects so the transition logic stays storage-agnostic.
|
|
188
|
+
* The same guard runs whether the record lives in Postgres, a mock executor, or an
|
|
189
|
+
* in-memory Map — only these two closures change per backend.
|
|
218
190
|
* @public
|
|
219
191
|
*/
|
|
220
|
-
export declare
|
|
192
|
+
export declare interface GuardedTransitionIO {
|
|
193
|
+
/** Read the record's current workflow state (the value of its `status` field), or undefined if unknown. */
|
|
194
|
+
readState: () => Promise<string | undefined>;
|
|
195
|
+
/** Persist the record's new workflow state, written verbatim. */
|
|
196
|
+
writeState: (nextState: string) => Promise<void>;
|
|
197
|
+
/**
|
|
198
|
+
* Persist record field data for a mutate-in-place self-transition, returning the full updated
|
|
199
|
+
* record (so the client writeback reflects the new data). Optional: a backend with no data-write
|
|
200
|
+
* path (e.g. PostGraphile today, which writes only `status`) omits it, and a self-transition is
|
|
201
|
+
* then rejected loudly rather than silently dropped.
|
|
202
|
+
*/
|
|
203
|
+
writeData?: (patch: Record<string, unknown>) => Promise<Record<string, unknown>>;
|
|
204
|
+
}
|
|
221
205
|
|
|
222
206
|
/**
|
|
223
207
|
* Check if a doctype is registered
|
|
@@ -255,12 +239,6 @@ export declare interface LoadDoctypesOptions {
|
|
|
255
239
|
|
|
256
240
|
export { makePgService }
|
|
257
241
|
|
|
258
|
-
/**
|
|
259
|
-
* Register all built-in handlers
|
|
260
|
-
* @public
|
|
261
|
-
*/
|
|
262
|
-
export declare function registerBuiltinHandlers(): void;
|
|
263
|
-
|
|
264
242
|
/**
|
|
265
243
|
* Register a custom fetch handler by name.
|
|
266
244
|
* The name must match the `handler` field on a `CustomFetch` strategy declaration.
|
|
@@ -268,14 +246,6 @@ export declare function registerBuiltinHandlers(): void;
|
|
|
268
246
|
*/
|
|
269
247
|
export declare function registerFetchHandler(name: string, handler: FetchHandler): void;
|
|
270
248
|
|
|
271
|
-
/**
|
|
272
|
-
* Register an action handler
|
|
273
|
-
* @param name - Unique name for the action handler
|
|
274
|
-
* @param handler - Action handler function to register
|
|
275
|
-
* @public
|
|
276
|
-
*/
|
|
277
|
-
export declare function registerHandler(name: string, handler: ActionHandler): void;
|
|
278
|
-
|
|
279
249
|
/**
|
|
280
250
|
* Options for creating a Stonecrop PostGraphile plugin.
|
|
281
251
|
* @public
|