@stonecrop/graphql-middleware 0.13.12 → 0.13.14

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.
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Backend IO the dispatch layer injects so the transition logic stays storage-agnostic.
3
+ * The same guard runs whether the record lives in Postgres, a mock executor, or an
4
+ * in-memory Map — only these two closures change per backend.
5
+ * @public
6
+ */
7
+ export interface GuardedTransitionIO {
8
+ /** Read the record's current workflow state (the value of its `status` field), or undefined if unknown. */
9
+ readState: () => Promise<string | undefined>;
10
+ /** Persist the record's new workflow state, written verbatim. */
11
+ writeState: (nextState: string) => Promise<void>;
12
+ }
13
+ /**
14
+ * Apply a workflow action's state transition on the server, enforcing `allowedStates`.
15
+ *
16
+ * The server owns the transition: it reads the record's authoritative current state,
17
+ * rejects the action when `isActionAllowedInState` denies it, then writes `nextState`
18
+ * verbatim. Storage access is injected via `io` so this one guard serves every backend
19
+ * and can never disagree with the frontend's `getAvailableTransitions`, which shares the
20
+ * same predicate.
21
+ *
22
+ * An action with no `nextState` is a side-effect-only action (e.g. `Save`): the transition
23
+ * dispatch has nothing to apply for it, and the side effect must run through a wired handler
24
+ * that this path does not yet provide. Rather than report a false success while silently
25
+ * dropping the request, it fails loudly. (A `callHandler` primitive to invoke registered
26
+ * handlers by key is the intended home for those side effects; it is not implemented yet.)
27
+ *
28
+ * @param actionDef - The action's `label`, `allowedStates` (where it may run) and `nextState` (where it lands)
29
+ * @param io - Backend read/write closures
30
+ * @returns Action result envelope with the resulting state in `data.state`
31
+ * @public
32
+ */
33
+ export declare function applyGuardedTransition(actionDef: {
34
+ label?: string;
35
+ allowedStates?: string[];
36
+ nextState?: string;
37
+ }, io: GuardedTransitionIO): Promise<{
38
+ success: boolean;
39
+ data: unknown;
40
+ error: string | null;
41
+ }>;
42
+ //# sourceMappingURL=transition.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transition.d.ts","sourceRoot":"","sources":["../../../src/dispatch/transition.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,WAAW,mBAAmB;IACnC,2GAA2G;IAC3G,SAAS,EAAE,MAAM,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAA;IAC5C,iEAAiE;IACjE,UAAU,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CAChD;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,sBAAsB,CAC3C,SAAS,EAAE;IAAE,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,EAC3E,EAAE,EAAE,mBAAmB,GACrB,OAAO,CAAC;IAAE,OAAO,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC,CAgCpE"}
@@ -0,0 +1,50 @@
1
+ import { isActionAllowedInState } from '@stonecrop/schema';
2
+ /**
3
+ * Apply a workflow action's state transition on the server, enforcing `allowedStates`.
4
+ *
5
+ * The server owns the transition: it reads the record's authoritative current state,
6
+ * rejects the action when `isActionAllowedInState` denies it, then writes `nextState`
7
+ * verbatim. Storage access is injected via `io` so this one guard serves every backend
8
+ * and can never disagree with the frontend's `getAvailableTransitions`, which shares the
9
+ * same predicate.
10
+ *
11
+ * An action with no `nextState` is a side-effect-only action (e.g. `Save`): the transition
12
+ * dispatch has nothing to apply for it, and the side effect must run through a wired handler
13
+ * that this path does not yet provide. Rather than report a false success while silently
14
+ * dropping the request, it fails loudly. (A `callHandler` primitive to invoke registered
15
+ * handlers by key is the intended home for those side effects; it is not implemented yet.)
16
+ *
17
+ * @param actionDef - The action's `label`, `allowedStates` (where it may run) and `nextState` (where it lands)
18
+ * @param io - Backend read/write closures
19
+ * @returns Action result envelope with the resulting state in `data.state`
20
+ * @public
21
+ */
22
+ export async function applyGuardedTransition(actionDef, io) {
23
+ if (actionDef.nextState == null) {
24
+ return {
25
+ success: false,
26
+ data: null,
27
+ error: `Action "${actionDef.label ?? 'unknown'}" has no workflow transition and no server-side effect is wired — nothing was executed.`,
28
+ };
29
+ }
30
+ const currentState = (await io.readState()) ?? '';
31
+ if (!isActionAllowedInState(actionDef, currentState)) {
32
+ return {
33
+ success: false,
34
+ data: null,
35
+ error: `Action not allowed from state: ${currentState || '(unknown)'}`,
36
+ };
37
+ }
38
+ // NON-ATOMIC: the current state is read above and nextState written here as two
39
+ // separate statements. Two concurrent transitions on the same record can both pass
40
+ // the guard before either writes, so both "succeed" even when the workflow intended
41
+ // them to be mutually exclusive. Closing this requires guarding and writing in one
42
+ // statement — an atomic `UPDATE ... SET status = nextState WHERE status = ANY(allowedStates)`
43
+ // — and reporting "no rows updated" as a rejected transition. Deferred for now.
44
+ await io.writeState(actionDef.nextState);
45
+ return {
46
+ success: true,
47
+ data: { state: actionDef.nextState },
48
+ error: null,
49
+ };
50
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stonecrop/graphql-middleware",
3
- "version": "0.13.12",
3
+ "version": "0.13.14",
4
4
  "type": "module",
5
5
  "types": "./dist/graphql-middleware.d.ts",
6
6
  "exports": {
@@ -18,7 +18,7 @@
18
18
  "graphql": "^16.14.0",
19
19
  "pluralize": "^8.0.0",
20
20
  "postgraphile": "^5.0.3",
21
- "@stonecrop/schema": "0.13.12"
21
+ "@stonecrop/schema": "0.13.14"
22
22
  },
23
23
  "devDependencies": {
24
24
  "@electric-sql/pglite": "^0.4.5",