@stonecrop/graphql-middleware 0.10.15 → 0.11.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/dist/index.js CHANGED
@@ -1,189 +1,182 @@
1
- import { readdirSync as B, statSync as x, readFileSync as E } from "node:fs";
2
- import { join as M } from "node:path";
3
- import { validateDoctype as R, parseDoctype as k, toPascalCase as O, snakeToCamel as F } from "@stonecrop/schema";
4
- import { loadOne as b, object as w, constant as j, lambda as q } from "postgraphile/grafast";
1
+ import { readdirSync as B, statSync as M, readFileSync as x } from "node:fs";
2
+ import { join as E } from "node:path";
3
+ import { validateDoctype as k, parseDoctype as O, toPascalCase as D, snakeToCamel as j } from "@stonecrop/schema";
4
+ import { loadOne as b, object as w, constant as F, lambda as q } from "postgraphile/grafast";
5
5
  import { extendSchema as I, gql as T } from "postgraphile/utils";
6
6
  import J from "pluralize";
7
7
  const u = /* @__PURE__ */ new Map();
8
- class D extends Error {
9
- constructor(e, r) {
10
- const o = r.map((n) => ` ${n.path.join(".")}: ${n.message}`).join(`
8
+ class R extends Error {
9
+ constructor(t, r) {
10
+ const i = r.map((n) => ` ${n.path.join(".")}: ${n.message}`).join(`
11
11
  `);
12
- super(`Invalid doctype definition in ${e}:
13
- ${o}`), this.file = e, this.errors = r, this.name = "DoctypeValidationError";
12
+ super(`Invalid doctype definition in ${t}:
13
+ ${i}`), this.file = t, this.errors = r, this.name = "DoctypeValidationError";
14
14
  }
15
15
  }
16
- function L(t, e = {}) {
17
- const r = B(t);
18
- for (const o of r) {
19
- const n = M(t, o);
20
- x(n).isDirectory() ? L(n, e) : o.endsWith(".json") && C(n, e);
16
+ function L(e, t = {}) {
17
+ const r = B(e);
18
+ for (const i of r) {
19
+ const n = E(e, i);
20
+ M(n).isDirectory() ? L(n, t) : i.endsWith(".json") && C(n, t);
21
21
  }
22
22
  }
23
- function C(t, e) {
24
- const r = E(t, "utf-8");
25
- let o;
23
+ function C(e, t) {
24
+ const r = x(e, "utf-8");
25
+ let i;
26
26
  try {
27
- o = JSON.parse(r);
28
- } catch (a) {
29
- const i = {
27
+ i = JSON.parse(r);
28
+ } catch (c) {
29
+ const o = {
30
30
  path: [],
31
- message: `Invalid JSON: ${a instanceof Error ? a.message : String(a)}`
31
+ message: `Invalid JSON: ${c instanceof Error ? c.message : String(c)}`
32
32
  };
33
- if (e.continueOnError) {
34
- e.onError?.(t, [i]);
33
+ if (t.continueOnError) {
34
+ t.onError?.(e, [o]);
35
35
  return;
36
36
  }
37
- throw new D(t, [i]);
37
+ throw new R(e, [o]);
38
38
  }
39
- const n = R(o);
39
+ const n = k(i);
40
40
  if (!n.success) {
41
- if (e.continueOnError) {
42
- e.onError?.(t, n.errors);
41
+ if (t.continueOnError) {
42
+ t.onError?.(e, n.errors);
43
43
  return;
44
44
  }
45
- throw new D(t, n.errors);
45
+ throw new R(e, n.errors);
46
46
  }
47
- const c = k(o);
48
- u.set(c.name, c);
47
+ const a = O(i);
48
+ u.set(a.name, a);
49
49
  }
50
- function le(t, e = {}) {
51
- for (const [r, o] of Object.entries(t)) {
52
- const n = { ...o, name: r }, c = R(n);
53
- if (!c.success) {
54
- if (e.continueOnError) {
55
- e.onError?.(r, c.errors);
50
+ function de(e, t = {}) {
51
+ for (const [r, i] of Object.entries(e)) {
52
+ const n = { ...i, name: r }, a = k(n);
53
+ if (!a.success) {
54
+ if (t.continueOnError) {
55
+ t.onError?.(r, a.errors);
56
56
  continue;
57
57
  }
58
- throw new D(r, c.errors);
58
+ throw new R(r, a.errors);
59
59
  }
60
- const a = k(n);
61
- u.set(a.name, a);
60
+ const c = O(n);
61
+ u.set(c.name, c);
62
62
  }
63
63
  }
64
- function h(t) {
65
- return u.get(t);
64
+ function h(e) {
65
+ return u.get(e);
66
66
  }
67
67
  function H() {
68
68
  return Array.from(u.values());
69
69
  }
70
- function fe(t) {
71
- return u.has(t);
70
+ function fe(e) {
71
+ return u.has(e);
72
72
  }
73
73
  function ue() {
74
74
  u.clear();
75
75
  }
76
76
  function ye() {
77
- const t = [];
78
- for (const e of u.values()) {
79
- e.inherits && !u.has(e.inherits) && t.push({
80
- path: [e.name, "inherits"],
81
- message: `References unknown doctype: ${e.inherits}`
82
- }), e.listDoctype && !u.has(e.listDoctype) && t.push({
83
- path: [e.name, "listDoctype"],
84
- message: `References unknown doctype: ${e.listDoctype}`
85
- }), e.parentDoctype && !u.has(e.parentDoctype) && t.push({
86
- path: [e.name, "parentDoctype"],
87
- message: `References unknown doctype: ${e.parentDoctype}`
77
+ const e = [];
78
+ for (const t of u.values()) {
79
+ t.inherits && !u.has(t.inherits) && e.push({
80
+ path: [t.name, "inherits"],
81
+ message: `References unknown doctype: ${t.inherits}`
88
82
  });
89
- for (const r of e.fields)
90
- r.fieldtype === "Link" && typeof r.options == "string" && (u.has(r.options) || t.push({
91
- path: [e.name, "fields", r.fieldname, "options"],
83
+ for (const r of t.fields)
84
+ r.fieldtype === "Link" && typeof r.options == "string" && (u.has(r.options) || e.push({
85
+ path: [t.name, "fields", r.fieldname, "options"],
92
86
  message: `Link references unknown doctype: ${r.options}`
93
87
  }));
94
88
  }
95
- return t;
89
+ return e;
96
90
  }
97
91
  const $ = /* @__PURE__ */ new Map();
98
- function _(t, e) {
99
- $.set(t, e);
92
+ function _(e, t) {
93
+ $.set(e, t);
100
94
  }
101
- function U(t) {
102
- return $.get(t);
95
+ function v(e) {
96
+ return $.get(e);
103
97
  }
104
- function pe(t) {
105
- return $.has(t);
98
+ function pe(e) {
99
+ return $.has(e);
106
100
  }
107
101
  function me() {
108
102
  $.clear();
109
103
  }
110
- const Q = async (t, e) => {
111
- const [r] = t, { doctype: o } = e, n = [];
112
- for (const c of o.fields)
113
- c.required && (r[c.fieldname] === void 0 || r[c.fieldname] === null) && n.push(c.label ?? c.fieldname);
104
+ const U = async (e, t) => {
105
+ const [r] = e, { doctype: i } = t, n = [];
106
+ for (const a of i.fields)
107
+ a.required && (r[a.fieldname] === void 0 || r[a.fieldname] === null) && n.push(a.label ?? a.fieldname);
114
108
  if (n.length > 0)
115
109
  throw new Error(`Missing required fields: ${n.join(", ")}`);
116
110
  return { valid: !0 };
117
- }, P = async (t, e) => {
118
- const [r] = t, { doctype: o } = e, n = [];
119
- for (const c of o.fields) {
120
- const a = r[c.fieldname];
121
- if (a == null) continue;
122
- const i = V(c, a);
123
- i && n.push(i);
111
+ }, Q = async (e, t) => {
112
+ const [r] = e, { doctype: i } = t, n = [];
113
+ for (const a of i.fields) {
114
+ const c = r[a.fieldname];
115
+ if (c == null) continue;
116
+ const o = P(a, c);
117
+ o && n.push(o);
124
118
  }
119
+ if (i.links)
120
+ for (const [a, c] of Object.entries(i.links)) {
121
+ const o = r[a];
122
+ if (o == null) continue;
123
+ c.cardinality === "noneOrMany" || c.cardinality === "atLeastOne" ? Array.isArray(o) || n.push(`${a}: expected array, got ${typeof o}`) : (typeof o != "object" || Array.isArray(o)) && n.push(`${a}: expected object, got ${typeof o}`);
124
+ }
125
125
  if (n.length > 0)
126
126
  throw new Error(`Field type validation failed:
127
127
  ${n.join(`
128
128
  `)}`);
129
129
  return { valid: !0 };
130
130
  };
131
- function V(t, e) {
132
- const { fieldname: r, fieldtype: o, cardinality: n } = t;
133
- switch (o) {
131
+ function P(e, t) {
132
+ const { fieldname: r, fieldtype: i, cardinality: n } = e;
133
+ switch (i) {
134
134
  case "Int":
135
- if (typeof e != "number" || !Number.isInteger(e))
136
- return `${r}: expected integer, got ${typeof e}`;
135
+ if (typeof t != "number" || !Number.isInteger(t))
136
+ return `${r}: expected integer, got ${typeof t}`;
137
137
  break;
138
138
  case "Float":
139
139
  case "Decimal":
140
140
  case "Currency":
141
141
  case "Quantity":
142
- if (typeof e != "number" && typeof e != "string")
143
- return `${r}: expected number, got ${typeof e}`;
142
+ if (typeof t != "number" && typeof t != "string")
143
+ return `${r}: expected number, got ${typeof t}`;
144
144
  break;
145
145
  case "Check":
146
- if (typeof e != "boolean")
147
- return `${r}: expected boolean, got ${typeof e}`;
146
+ if (typeof t != "boolean")
147
+ return `${r}: expected boolean, got ${typeof t}`;
148
148
  break;
149
149
  case "Data":
150
150
  case "Text":
151
151
  case "Code":
152
152
  case "Link":
153
- if (typeof e != "string")
154
- return `${r}: expected string, got ${typeof e}`;
153
+ if (typeof t != "string")
154
+ return `${r}: expected string, got ${typeof t}`;
155
155
  break;
156
156
  case "Date":
157
157
  case "Time":
158
158
  case "Datetime":
159
- if (typeof e != "string" && !(e instanceof Date))
160
- return `${r}: expected date string or Date, got ${typeof e}`;
159
+ if (typeof t != "string" && !(t instanceof Date))
160
+ return `${r}: expected date string or Date, got ${typeof t}`;
161
161
  break;
162
162
  case "JSON":
163
- if (typeof e != "object")
164
- return `${r}: expected object, got ${typeof e}`;
165
- break;
166
- case "Doctype":
167
- if (n === "many") {
168
- if (!Array.isArray(e))
169
- return `${r}: expected array, got ${typeof e}`;
170
- } else if (typeof e != "object" || Array.isArray(e))
171
- return `${r}: expected object, got ${typeof e}`;
163
+ if (typeof t != "object")
164
+ return `${r}: expected object, got ${typeof t}`;
172
165
  break;
173
166
  }
174
167
  return null;
175
168
  }
176
- const W = async () => ({ ok: !0 }), G = {
177
- validateRequiredFields: Q,
178
- validateFieldTypes: P,
179
- noop: W
169
+ const V = async () => ({ ok: !0 }), W = {
170
+ validateRequiredFields: U,
171
+ validateFieldTypes: Q,
172
+ noop: V
180
173
  };
181
174
  function ge() {
182
- for (const [t, e] of Object.entries(G))
183
- _(t, e);
175
+ for (const [e, t] of Object.entries(W))
176
+ _(e, t);
184
177
  }
185
- const Se = (t) => {
186
- const e = t.inflection?.recordFieldName ?? z, r = t.inflection?.connectionFieldName ?? Y, o = t.inflection?.orderByTypeName ?? K, n = t.inflection?.recordArgName ?? X, c = t.inflection?.recordArgType ?? Z;
178
+ const Se = (e) => {
179
+ const t = e.inflection?.recordFieldName ?? G, r = e.inflection?.connectionFieldName ?? z, i = e.inflection?.orderByTypeName ?? Y, n = e.inflection?.recordArgName ?? K, a = e.inflection?.recordArgType ?? X;
187
180
  return I(() => ({
188
181
  typeDefs: T`
189
182
  type StonecropFieldMeta {
@@ -227,8 +220,6 @@ const Se = (t) => {
227
220
  fields: [StonecropFieldMeta!]!
228
221
  workflow: StonecropWorkflowMeta
229
222
  inherits: String
230
- listDoctype: String
231
- parentDoctype: String
232
223
  }
233
224
 
234
225
  type StonecropRecordResult {
@@ -268,34 +259,34 @@ const Se = (t) => {
268
259
  objects: {
269
260
  Query: {
270
261
  plans: {
271
- stonecropMeta(a, { $doctype: i }) {
272
- return q(i, (l) => h(l) ?? null);
262
+ stonecropMeta(c, { $doctype: o }) {
263
+ return q(o, (l) => h(l) ?? null);
273
264
  },
274
265
  stonecropAllMeta() {
275
- return j(H());
266
+ return F(H());
276
267
  },
277
- stonecropRecord(a, { $doctype: i, $id: l }) {
278
- return b(w({ doctype: i, id: l }), async (y) => await Promise.all(
268
+ stonecropRecord(c, { $doctype: o, $id: l }) {
269
+ return b(w({ doctype: o, id: l }), async (y) => await Promise.all(
279
270
  y.map(async (f) => {
280
271
  const d = h(f.doctype);
281
272
  if (!d)
282
273
  throw new Error(`Unknown doctype: ${f.doctype}`);
283
274
  if (!d.tableName)
284
275
  throw new Error(`Doctype ${f.doctype} has no table mapping`);
285
- const p = ee(d, e, n, c), s = await t.executor.query(p, {
276
+ const p = ee(d, t, n, a), s = await e.executor.query(p, {
286
277
  [n(d.tableName)]: f.id
287
278
  });
288
279
  return {
289
- data: re(s, d, e),
280
+ data: re(s, d, t),
290
281
  doctype: f.doctype
291
282
  };
292
283
  })
293
284
  ));
294
285
  },
295
- stonecropRecords(a, { $doctype: i, $filters: l, $orderBy: y, $limit: f, $offset: d }) {
286
+ stonecropRecords(c, { $doctype: o, $filters: l, $orderBy: y, $limit: f, $offset: d }) {
296
287
  return b(
297
288
  w({
298
- doctype: i,
289
+ doctype: o,
299
290
  filters: l,
300
291
  orderBy: y,
301
292
  limit: f,
@@ -316,8 +307,8 @@ const Se = (t) => {
316
307
  orderBy: s.orderBy
317
308
  },
318
309
  r,
319
- o
320
- ), N = await t.executor.query(S, {
310
+ i
311
+ ), N = await e.executor.query(S, {
321
312
  limit: s.limit,
322
313
  offset: s.offset,
323
314
  orderBy: s.orderBy
@@ -335,10 +326,10 @@ const Se = (t) => {
335
326
  },
336
327
  Mutation: {
337
328
  plans: {
338
- stonecropAction(a, { $doctype: i, $action: l, $args: y }) {
329
+ stonecropAction(c, { $doctype: o, $action: l, $args: y }) {
339
330
  return b(
340
331
  w({
341
- doctype: i,
332
+ doctype: o,
342
333
  action: l,
343
334
  actionArgs: y
344
335
  }),
@@ -358,7 +349,7 @@ const Se = (t) => {
358
349
  data: null,
359
350
  error: `Unknown action: ${d.action} on ${d.doctype}`
360
351
  };
361
- const m = s.handler, S = U(m);
352
+ const m = s.handler, S = v(m);
362
353
  if (!S)
363
354
  return {
364
355
  success: !1,
@@ -367,7 +358,7 @@ const Se = (t) => {
367
358
  };
368
359
  const N = {
369
360
  doctype: p,
370
- executor: t.executor
361
+ executor: e.executor
371
362
  };
372
363
  try {
373
364
  return {
@@ -391,44 +382,44 @@ const Se = (t) => {
391
382
  }
392
383
  }));
393
384
  };
394
- function z(t) {
395
- const e = J.singular(t);
396
- return `${F(e)}ById`;
385
+ function G(e) {
386
+ const t = J.singular(e);
387
+ return `${j(t)}ById`;
397
388
  }
398
- function Y(t) {
399
- return `all${O(t)}`;
389
+ function z(e) {
390
+ return `all${D(e)}`;
400
391
  }
401
- function K(t) {
402
- return `${O(t)}OrderBy`;
392
+ function Y(e) {
393
+ return `${D(e)}OrderBy`;
403
394
  }
404
- function X(t) {
395
+ function K(e) {
405
396
  return "id";
406
397
  }
407
- function Z(t) {
398
+ function X(e) {
408
399
  return "UUID!";
409
400
  }
410
- const v = /* @__PURE__ */ new Set(["Link", "Doctype"]);
411
- function A(t) {
412
- return t.fields.filter((e) => !v.has(e.fieldtype)).map((e) => e.fieldname).join(`
401
+ const Z = /* @__PURE__ */ new Set(["Link"]);
402
+ function A(e) {
403
+ return e.fields.filter((t) => !Z.has(t.fieldtype)).map((t) => t.fieldname).join(`
413
404
  `);
414
405
  }
415
- function ee(t, e, r, o) {
416
- const n = A(t), c = e(t.tableName), a = r(t.tableName), i = o(t.tableName);
406
+ function ee(e, t, r, i) {
407
+ const n = A(e), a = t(e.tableName), c = r(e.tableName), o = i(e.tableName);
417
408
  return `
418
- query GetRecord($${a}: ${i}) {
419
- ${c}(${a}: $${a}) {
409
+ query GetRecord($${c}: ${o}) {
410
+ ${a}(${c}: $${c}) {
420
411
  ${n}
421
412
  }
422
413
  }
423
414
  `;
424
415
  }
425
- function te(t, e, r, o) {
426
- const n = A(t), c = r(t.tableName), a = o(t.tableName), i = [], l = [];
427
- e.limit && (i.push("$limit: Int"), l.push("first: $limit")), e.offset && (i.push("$offset: Int"), l.push("offset: $offset")), e.orderBy && (i.push(`$orderBy: [${a}!]`), l.push("orderBy: $orderBy"));
428
- const y = i.length > 0 ? `(${i.join(", ")})` : "", f = l.length > 0 ? `(${l.join(", ")})` : "";
416
+ function te(e, t, r, i) {
417
+ const n = A(e), a = r(e.tableName), c = i(e.tableName), o = [], l = [];
418
+ t.limit && (o.push("$limit: Int"), l.push("first: $limit")), t.offset && (o.push("$offset: Int"), l.push("offset: $offset")), t.orderBy && (o.push(`$orderBy: [${c}!]`), l.push("orderBy: $orderBy"));
419
+ const y = o.length > 0 ? `(${o.join(", ")})` : "", f = l.length > 0 ? `(${l.join(", ")})` : "";
429
420
  return `
430
421
  query GetRecords${y} {
431
- ${c}${f} {
422
+ ${a}${f} {
432
423
  nodes {
433
424
  ${n}
434
425
  }
@@ -436,35 +427,33 @@ function te(t, e, r, o) {
436
427
  }
437
428
  `;
438
429
  }
439
- function re(t, e, r) {
440
- const o = r(e.tableName);
441
- return t[o];
430
+ function re(e, t, r) {
431
+ const i = r(t.tableName);
432
+ return e[i];
442
433
  }
443
- function ne(t, e, r) {
444
- const o = r(e.tableName);
445
- return t[o]?.nodes ?? [];
434
+ function ne(e, t, r) {
435
+ const i = r(t.tableName);
436
+ return e[i]?.nodes ?? [];
446
437
  }
447
438
  export {
448
- D as DoctypeValidationError,
449
- v as RELATION_FIELDTYPES,
450
- te as buildListQuery,
451
- ee as buildRecordQuery,
452
- G as builtinHandlers,
439
+ R as DoctypeValidationError,
440
+ Z as RELATION_FIELDTYPES,
441
+ W as builtinHandlers,
453
442
  me as clearHandlers,
454
443
  ue as clearRegistry,
455
444
  Se as createStonecropPlugin,
456
- Y as defaultConnectionFieldName,
457
- K as defaultOrderByTypeName,
458
- X as defaultRecordArgName,
459
- Z as defaultRecordArgType,
460
- z as defaultRecordFieldName,
445
+ z as defaultConnectionFieldName,
446
+ Y as defaultOrderByTypeName,
447
+ K as defaultRecordArgName,
448
+ X as defaultRecordArgType,
449
+ G as defaultRecordFieldName,
461
450
  H as getAllMeta,
462
- U as getHandler,
451
+ v as getHandler,
463
452
  h as getMeta,
464
453
  pe as hasHandler,
465
454
  fe as hasMeta,
466
455
  L as loadDoctypes,
467
- le as loadDoctypesFromObject,
456
+ de as loadDoctypesFromObject,
468
457
  A as queryableFieldNames,
469
458
  ge as registerBuiltinHandlers,
470
459
  _ as registerHandler,