@stonecrop/graphql-middleware 0.13.14 → 0.15.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.
Files changed (40) hide show
  1. package/README.md +19 -18
  2. package/dist/graphql-middleware.d.ts +44 -74
  3. package/dist/index.js +314 -354
  4. package/dist/index.js.map +1 -1
  5. package/dist/src/dispatch/transition.d.ts +20 -8
  6. package/dist/src/dispatch/transition.d.ts.map +1 -1
  7. package/dist/src/dispatch/transition.js +33 -9
  8. package/dist/src/index.d.ts +2 -2
  9. package/dist/src/index.d.ts.map +1 -1
  10. package/dist/src/index.js +2 -2
  11. package/dist/src/plugin/postgraphile.d.ts +3 -3
  12. package/dist/src/plugin/postgraphile.d.ts.map +1 -1
  13. package/dist/src/plugin/postgraphile.js +48 -23
  14. package/dist/src/registry/doctypes.d.ts.map +1 -1
  15. package/dist/src/registry/doctypes.js +17 -5
  16. package/dist/src/typeDefs.d.ts.map +1 -1
  17. package/dist/src/typeDefs.js +15 -10
  18. package/package.json +11 -6
  19. package/dist/plugin/postgraphile.js +0 -583
  20. package/dist/preset.js +0 -45
  21. package/dist/registry/actions.js +0 -163
  22. package/dist/registry/doctypes.js +0 -161
  23. package/dist/src/registry/actions.d.ts +0 -36
  24. package/dist/src/registry/actions.d.ts.map +0 -1
  25. package/dist/src/registry/actions.js +0 -169
  26. package/dist/src/types/context.d.ts +0 -20
  27. package/dist/src/types/context.d.ts.map +0 -1
  28. package/dist/src/types/index.d.ts +0 -46
  29. package/dist/src/types/index.d.ts.map +0 -1
  30. package/dist/src/types/index.js +0 -3
  31. package/dist/src/types/preset.d.ts +0 -18
  32. package/dist/src/types/preset.d.ts.map +0 -1
  33. package/dist/src/types/query.d.ts +0 -132
  34. package/dist/src/types/query.d.ts.map +0 -1
  35. package/dist/src/types/query.js +0 -3
  36. package/dist/typeDefs.js +0 -79
  37. package/dist/types/context.js +0 -0
  38. package/dist/types/index.js +0 -3
  39. package/dist/types/preset.js +0 -0
  40. package/dist/types/query.js +0 -3
@@ -1,10 +1,10 @@
1
- import { camelToSnake, pascalToSnake } from '@stonecrop/schema';
1
+ import { camelToSnake, pascalToSnake, resolveLinkRenderMode } from '@stonecrop/schema';
2
2
  import { loadOneWithPgClient, sideEffectWithPgClient } from '@dataplan/pg';
3
3
  import { constant, lambda, object } from 'postgraphile/grafast';
4
4
  import { extendSchema } from 'postgraphile/utils';
5
- import { getHandler } from '../registry/actions';
6
5
  import { getFetchHandler } from '../registry/fetchHandlers';
7
6
  import { getMeta, getAllMeta } from '../registry/doctypes';
7
+ import { applyGuardedTransition } from '../dispatch/transition';
8
8
  import { typeDefs } from '../typeDefs';
9
9
  /**
10
10
  * Create a PostGraphile plugin that extends the GraphQL schema with Stonecrop functionality.
@@ -289,18 +289,38 @@ export const createStonecropPlugin = (options = {}) => {
289
289
  error: `Unknown action: ${spec.action} on ${spec.doctype}`,
290
290
  };
291
291
  }
292
- const handler = getHandler(actionDef.handler);
293
- if (!handler) {
292
+ const pkMeta = getPkMeta(meta);
293
+ if (!pkMeta) {
294
294
  return {
295
295
  success: false,
296
296
  data: null,
297
- error: `Handler not registered: ${actionDef.handler}`,
297
+ error: `No primary key for doctype: ${spec.doctype}`,
298
298
  };
299
299
  }
300
- const actionContext = { doctype: meta, pgClient };
300
+ const pkColumn = camelToSnake(pkMeta.fieldname);
301
+ const table = resolveTableName(meta.name, options.tables);
302
+ // Record envelope: [{ id, data }] — the transition keys off the record id.
303
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- spec.actionArgs is a Grafast runtime value; record envelope shape is the dispatch contract
304
+ const argList = (Array.isArray(spec.actionArgs) ? spec.actionArgs : []);
305
+ const recordId = argList[0]?.id;
301
306
  try {
302
- const result = await handler(spec.actionArgs ?? [], actionContext);
303
- return { success: true, data: result, error: null };
307
+ return await applyGuardedTransition(actionDef, {
308
+ readState: async () => {
309
+ if (recordId == null)
310
+ return undefined;
311
+ const { rows } = await debugSql(pgClient, {
312
+ text: `SELECT "status" FROM ${table} WHERE "${pkColumn}"::text = $1`,
313
+ values: [String(recordId)],
314
+ });
315
+ return rows[0]?.status == null ? undefined : rows[0].status;
316
+ },
317
+ writeState: async (nextState) => {
318
+ await debugSql(pgClient, {
319
+ text: `UPDATE ${table} SET "status" = $1 WHERE "${pkColumn}"::text = $2`,
320
+ values: [nextState, String(recordId)],
321
+ });
322
+ },
323
+ });
304
324
  }
305
325
  catch (err) {
306
326
  return {
@@ -339,18 +359,23 @@ function flattenFields(fields) {
339
359
  }
340
360
  /**
341
361
  * Derive quoted SQL column entries from a flat field array.
342
- * Skips non-scalar fields (kind !== 'field'), `fieldtype: 'Display'` fields
343
- * (computed/read-only fields with no backing DB column), and Link fields with
344
- * an explicit links declaration.
362
+ * Skips non-scalar fields (kind !== 'field'), `computed` fields
363
+ * (no backing DB column), and *expanding* links
364
+ * (relations fetched separately, not scalar columns on this table).
345
365
  */
346
- function collectColumns(fields, linkedFieldnames) {
366
+ function collectColumns(fields, links) {
347
367
  const columns = [];
348
368
  for (const f of flattenFields(fields)) {
349
369
  if (f.kind !== 'field')
350
370
  continue;
351
- if (f.fieldtype === 'Display')
371
+ // A computed field has no backing DB column.
372
+ if (f.computed)
352
373
  continue;
353
- if (f.fieldtype === 'Link' && linkedFieldnames.has(f.fieldname))
374
+ // Only an *expanding* link is a relation rather than a column. An inline link (a picker)
375
+ // keeps its FK on this table and must still be selected — `resolveLinkRenderMode` is the
376
+ // shared rule, also used by the client resolver; never re-derive it here.
377
+ const link = links.get(f.fieldname);
378
+ if (link && resolveLinkRenderMode(link, f.component) !== 'inline')
354
379
  continue;
355
380
  const col = camelToSnake(f.fieldname);
356
381
  columns.push(col !== f.fieldname ? `"${col}" AS "${f.fieldname}"` : `"${f.fieldname}"`);
@@ -361,27 +386,27 @@ function collectColumns(fields, linkedFieldnames) {
361
386
  * Derive a quoted SQL column list from doctype field definitions.
362
387
  * Applies camelToSnake to each fieldname to get the DB column name, then
363
388
  * aliases it back to the fieldname so result rows carry API-layer keys.
364
- * Excludes `fieldtype: 'Display'` fields (no backing DB column), Fieldset
365
- * containers (recursing into their children instead), and Link fields that have
366
- * an explicit `links` declaration (FK references, not scalar columns).
389
+ * Excludes `computed` fields (no backing DB column), Fieldset
390
+ * containers (recursing into their children instead), and *expanding* links
391
+ * (relations fetched separately). An inline link keeps its FK column here.
367
392
  *
368
393
  * Exported for unit testing (not re-exported from the package index).
369
394
  */
370
395
  export function getSqlColumns(meta) {
371
- const linkedFieldnames = new Set();
396
+ const links = new Map();
372
397
  if (meta.links) {
373
398
  for (const [key, link] of Object.entries(meta.links)) {
374
- linkedFieldnames.add(link.fieldname ?? key);
399
+ links.set(link.fieldname ?? key, link);
375
400
  }
376
401
  }
377
- return collectColumns(meta.fields, linkedFieldnames).join(', ');
402
+ return collectColumns(meta.fields, links).join(', ');
378
403
  }
379
404
  /**
380
- * Find the field declared with fieldtype 'PrimaryKey' in the doctype.
381
- * Returns undefined when no PrimaryKey field is declared (PK-less doctypes).
405
+ * Find the field marked `primaryKey` in the doctype.
406
+ * Returns undefined when none is marked (PK-less doctypes).
382
407
  */
383
408
  function getPkMeta(meta) {
384
- return meta.fields.find((f) => f.kind === 'field' && f.fieldtype === 'PrimaryKey');
409
+ return meta.fields.find((f) => f.kind === 'field' && Boolean(f.primaryKey));
385
410
  }
386
411
  /**
387
412
  * Resolve the PostgreSQL FROM clause target for a doctype.
@@ -1 +1 @@
1
- {"version":3,"file":"doctypes.d.ts","sourceRoot":"","sources":["../../../src/registry/doctypes.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAIrE;;;GAGG;AACH,qBAAa,sBAAuB,SAAQ,KAAK;IAE/C,4DAA4D;aAC5C,IAAI,EAAE,MAAM;IAC5B,sCAAsC;aACtB,MAAM,EAAE,eAAe,EAAE;;IAHzC,4DAA4D;IAC5C,IAAI,EAAE,MAAM;IAC5B,sCAAsC;IACtB,MA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mBAAmB;IACnC,2DAA2D;IAC3D,eAAe,CAAC,EAAE,OAAO,CAAA;IACzB,kEAAkE;IAClE,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,KAAK,IAAI,CAAA;CAC3D;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,mBAAwB,GAAG,IAAI,CAajF;AAsCD;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAE,mBAAwB,GAAG,IAAI,CAgBjH;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,SAAS,CAQ7D;AAED;;;GAGG;AACH,wBAAgB,UAAU,IAAI,WAAW,EAAE,CAE1C;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAE7C;AAED;;;GAGG;AACH,wBAAgB,aAAa,IAAI,IAAI,CAEpC;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,IAAI,eAAe,EAAE,CAsCtD"}
@@ -145,13 +145,25 @@ export function validateReferences() {
145
145
  message: `References unknown doctype: ${doctype.inherits}`,
146
146
  });
147
147
  }
148
- // Check Link field targets
148
+ // Check link field targets — `doctype` is both the link marker and its target.
149
149
  for (const field of doctype.fields) {
150
- if (field.kind === 'field' && field.fieldtype === 'Link' && typeof field.options === 'string') {
151
- if (!doctypeRegistry.has(field.options)) {
150
+ if (field.kind !== 'field')
151
+ continue;
152
+ const target = field.doctype;
153
+ if (target !== undefined && !doctypeRegistry.has(target)) {
154
+ errors.push({
155
+ path: [doctype.name, 'fields', field.fieldname, 'doctype'],
156
+ message: `Link references unknown doctype: ${target}`,
157
+ });
158
+ }
159
+ }
160
+ // Check link-declaration targets (component-primary links live in the `links` map)
161
+ if (doctype.links) {
162
+ for (const [key, link] of Object.entries(doctype.links)) {
163
+ if (!doctypeRegistry.has(link.target)) {
152
164
  errors.push({
153
- path: [doctype.name, 'fields', field.fieldname, 'options'],
154
- message: `Link references unknown doctype: ${field.options}`,
165
+ path: [doctype.name, 'links', key, 'target'],
166
+ message: `Link references unknown doctype: ${link.target}`,
155
167
  });
156
168
  }
157
169
  }
@@ -1 +1 @@
1
- {"version":3,"file":"typeDefs.d.ts","sourceRoot":"","sources":["../../src/typeDefs.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,eAAO,MAAM,QAAQ,gCA+EpB,CAAA"}
1
+ {"version":3,"file":"typeDefs.d.ts","sourceRoot":"","sources":["../../src/typeDefs.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,eAAO,MAAM,QAAQ,gCAoFpB,CAAA"}
@@ -8,31 +8,36 @@ export const typeDefs = gql `
8
8
  type StonecropFieldMeta {
9
9
  kind: String!
10
10
  fieldname: String!
11
- fieldtype: String!
12
11
  component: String
12
+ primaryKey: Boolean
13
+ computed: Boolean
14
+ language: String
15
+ doctype: String
13
16
  label: String
14
17
  width: String
15
18
  align: String
19
+ edit: Boolean
20
+ mask: String
21
+ format: String
22
+ mode: String
23
+ options: JSON
16
24
  required: Boolean
17
25
  readOnly: Boolean
18
- edit: Boolean
19
26
  hidden: Boolean
20
27
  default: JSON
21
- options: JSON
22
- mask: String
23
- precision: Int
24
- scale: Int
25
- mode: String
26
28
  validation: JSON
29
+ cardinality: String
30
+ source: String
27
31
  }
28
32
 
29
33
  type StonecropWorkflowAction {
30
34
  label: String!
31
- handler: String!
32
35
  requiredFields: [String!]
33
36
  allowedStates: [String!]
34
- confirm: Boolean
35
- args: JSON
37
+ nextState: String
38
+ stateless: Boolean
39
+ selfTransition: Boolean
40
+ clientHandler: String
36
41
  }
37
42
 
38
43
  type StonecropWorkflowMeta {
package/package.json CHANGED
@@ -1,24 +1,29 @@
1
1
  {
2
2
  "name": "@stonecrop/graphql-middleware",
3
- "version": "0.13.14",
3
+ "version": "0.15.0",
4
+ "description": "GraphQL middleware for PostGraphile and Stonecrop schema",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/agritheory/stonecrop",
8
+ "directory": "graphql_middleware"
9
+ },
4
10
  "type": "module",
5
- "types": "./dist/graphql-middleware.d.ts",
6
11
  "exports": {
7
12
  ".": {
8
13
  "types": "./dist/graphql-middleware.d.ts",
9
14
  "import": "./dist/index.js"
10
15
  }
11
16
  },
17
+ "types": "./dist/graphql-middleware.d.ts",
12
18
  "files": [
13
19
  "dist"
14
20
  ],
15
- "description": "GraphQL middleware for PostGraphile and Stonecrop schema",
16
21
  "dependencies": {
17
22
  "@dataplan/pg": "^1.0.3",
18
23
  "graphql": "^16.14.0",
19
24
  "pluralize": "^8.0.0",
20
25
  "postgraphile": "^5.0.3",
21
- "@stonecrop/schema": "0.13.14"
26
+ "@stonecrop/schema": "0.15.0"
22
27
  },
23
28
  "devDependencies": {
24
29
  "@electric-sql/pglite": "^0.4.5",
@@ -44,8 +49,8 @@
44
49
  "lint": "oxlint",
45
50
  "lint:fix": "oxlint --fix",
46
51
  "test": "vitest run --coverage.enabled false",
47
- "test:watch": "vitest watch",
48
52
  "test:coverage": "vitest run --coverage",
49
- "test:ui": "vitest --ui"
53
+ "test:ui": "vitest --ui",
54
+ "test:watch": "vitest watch"
50
55
  }
51
56
  }