@stonecrop/graphql-middleware 0.8.9 → 0.8.10

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,9 +1,10 @@
1
- import { validateDoctype as N, parseDoctype as D } from "@stonecrop/schema";
2
- import { ActionDefinition as ue, DoctypeMeta as fe, FieldMeta as me, FieldOptions as ge, FieldValidation as he, GQL_SCALAR_MAP as Se, StonecropFieldType as $e, TYPE_MAP as we, WELL_KNOWN_SCALARS as be, WorkflowMeta as Ne, camelToLabel as De, camelToSnake as Re, classifyFieldType as Me, convertGraphQLSchema as ke, defaultIsEntityField as Ae, defaultIsEntityType as qe, getDefaultComponent as Oe, parseDoctype as Fe, parseField as Be, pascalToSnake as Ce, snakeToCamel as Ee, snakeToLabel as je, toPascalCase as xe, toSlug as Te, validateDoctype as Ie, validateField as Je } from "@stonecrop/schema";
3
- import { readdirSync as k, statSync as A, readFileSync as q } from "node:fs";
4
- import { join as O } from "node:path";
5
- import { extendSchema as F, gql as B } from "postgraphile/utils";
6
- import { loadOne as S, object as $, constant as C, lambda as E } from "postgraphile/grafast";
1
+ import { validateDoctype as N, parseDoctype as D, toPascalCase as R, snakeToCamel as k } from "@stonecrop/schema";
2
+ import { ActionDefinition as he, DoctypeMeta as Se, FieldMeta as $e, FieldOptions as we, FieldValidation as be, GQL_SCALAR_MAP as Ne, StonecropFieldType as De, TYPE_MAP as Re, WELL_KNOWN_SCALARS as Me, WorkflowMeta as ke, camelToLabel as Ae, camelToSnake as qe, classifyFieldType as Oe, convertGraphQLSchema as Fe, defaultIsEntityField as Ee, defaultIsEntityType as Be, getDefaultComponent as Te, parseDoctype as Ce, parseField as je, pascalToSnake as xe, snakeToCamel as Ie, snakeToLabel as Le, toPascalCase as Je, toSlug as _e, validateDoctype as Ge, validateField as Pe } from "@stonecrop/schema";
3
+ import { readdirSync as A, statSync as q, readFileSync as O } from "node:fs";
4
+ import { join as F } from "node:path";
5
+ import { extendSchema as E, gql as B } from "postgraphile/utils";
6
+ import { loadOne as S, object as $, constant as T, lambda as C } from "postgraphile/grafast";
7
+ import j from "pluralize";
7
8
  const p = /* @__PURE__ */ new Map();
8
9
  class w extends Error {
9
10
  constructor(e, r) {
@@ -13,28 +14,28 @@ class w extends Error {
13
14
  ${n}`), this.file = e, this.errors = r, this.name = "DoctypeValidationError";
14
15
  }
15
16
  }
16
- function j(t, e = {}) {
17
- const r = k(t);
17
+ function x(t, e = {}) {
18
+ const r = A(t);
18
19
  for (const n of r) {
19
- const o = O(t, n);
20
- A(o).isDirectory() ? j(o, e) : n.endsWith(".json") && x(o, e);
20
+ const o = F(t, n);
21
+ q(o).isDirectory() ? x(o, e) : n.endsWith(".json") && I(o, e);
21
22
  }
22
23
  }
23
- function x(t, e) {
24
- const r = q(t, "utf-8");
24
+ function I(t, e) {
25
+ const r = O(t, "utf-8");
25
26
  let n;
26
27
  try {
27
28
  n = JSON.parse(r);
28
- } catch (c) {
29
- const s = {
29
+ } catch (s) {
30
+ const c = {
30
31
  path: [],
31
- message: `Invalid JSON: ${c instanceof Error ? c.message : String(c)}`
32
+ message: `Invalid JSON: ${s instanceof Error ? s.message : String(s)}`
32
33
  };
33
34
  if (e.continueOnError) {
34
- e.onError?.(t, [s]);
35
+ e.onError?.(t, [c]);
35
36
  return;
36
37
  }
37
- throw new w(t, [s]);
38
+ throw new w(t, [c]);
38
39
  }
39
40
  const o = N(n);
40
41
  if (!o.success) {
@@ -47,7 +48,7 @@ function x(t, e) {
47
48
  const a = D(n);
48
49
  p.set(a.name, a);
49
50
  }
50
- function re(t, e = {}) {
51
+ function se(t, e = {}) {
51
52
  for (const [r, n] of Object.entries(t)) {
52
53
  const o = { ...n, name: r }, a = N(o);
53
54
  if (!a.success) {
@@ -57,23 +58,23 @@ function re(t, e = {}) {
57
58
  }
58
59
  throw new w(r, a.errors);
59
60
  }
60
- const c = D(o);
61
- p.set(c.name, c);
61
+ const s = D(o);
62
+ p.set(s.name, s);
62
63
  }
63
64
  }
64
65
  function m(t) {
65
66
  return p.get(t);
66
67
  }
67
- function T() {
68
+ function L() {
68
69
  return Array.from(p.values());
69
70
  }
70
- function oe(t) {
71
+ function ce(t) {
71
72
  return p.has(t);
72
73
  }
73
- function ne() {
74
+ function ie() {
74
75
  p.clear();
75
76
  }
76
- function ae() {
77
+ function de() {
77
78
  const t = [];
78
79
  for (const e of p.values()) {
79
80
  e.inherits && !p.has(e.inherits) && t.push({
@@ -95,32 +96,32 @@ function ae() {
95
96
  return t;
96
97
  }
97
98
  const g = /* @__PURE__ */ new Map();
98
- function I(t, e) {
99
+ function J(t, e) {
99
100
  g.set(t, e);
100
101
  }
101
- function J(t) {
102
+ function _(t) {
102
103
  return g.get(t);
103
104
  }
104
- function ce(t) {
105
+ function le(t) {
105
106
  return g.has(t);
106
107
  }
107
- function se() {
108
+ function pe() {
108
109
  g.clear();
109
110
  }
110
- const L = async (t, e) => {
111
+ const G = async (t, e) => {
111
112
  const [r] = t, { doctype: n } = e, o = [];
112
113
  for (const a of n.fields)
113
114
  a.required && (r[a.fieldname] === void 0 || r[a.fieldname] === null) && o.push(a.label ?? a.fieldname);
114
115
  if (o.length > 0)
115
116
  throw new Error(`Missing required fields: ${o.join(", ")}`);
116
117
  return { valid: !0 };
117
- }, _ = async (t, e) => {
118
+ }, P = async (t, e) => {
118
119
  const [r] = t, { doctype: n } = e, o = [];
119
120
  for (const a of n.fields) {
120
- const c = r[a.fieldname];
121
- if (c == null) continue;
122
- const s = G(a.fieldname, c, a.fieldtype);
123
- s && o.push(s);
121
+ const s = r[a.fieldname];
122
+ if (s == null) continue;
123
+ const c = H(a.fieldname, s, a.fieldtype);
124
+ c && o.push(c);
124
125
  }
125
126
  if (o.length > 0)
126
127
  throw new Error(`Field type validation failed:
@@ -128,7 +129,7 @@ ${o.join(`
128
129
  `)}`);
129
130
  return { valid: !0 };
130
131
  };
131
- function G(t, e, r) {
132
+ function H(t, e, r) {
132
133
  switch (r) {
133
134
  case "Int":
134
135
  if (typeof e != "number" || !Number.isInteger(e))
@@ -169,18 +170,18 @@ function G(t, e, r) {
169
170
  }
170
171
  return null;
171
172
  }
172
- const U = async () => ({ ok: !0 }), H = {
173
- validateRequiredFields: L,
174
- validateFieldTypes: _,
175
- noop: U
173
+ const Q = async () => ({ ok: !0 }), U = {
174
+ validateRequiredFields: G,
175
+ validateFieldTypes: P,
176
+ noop: Q
176
177
  };
177
- function ie() {
178
- for (const [t, e] of Object.entries(H))
179
- I(t, e);
178
+ function ye() {
179
+ for (const [t, e] of Object.entries(U))
180
+ J(t, e);
180
181
  }
181
- const de = (t) => {
182
- const e = t.inflection?.recordFieldName ?? P, r = t.inflection?.connectionFieldName ?? Q, n = t.inflection?.orderByTypeName ?? W;
183
- return F(() => ({
182
+ const ue = (t) => {
183
+ const e = t.inflection?.recordFieldName ?? W, r = t.inflection?.connectionFieldName ?? v, n = t.inflection?.orderByTypeName ?? V;
184
+ return E(() => ({
184
185
  typeDefs: B`
185
186
  type StonecropFieldMeta {
186
187
  fieldname: String!
@@ -251,35 +252,35 @@ const de = (t) => {
251
252
  Query: {
252
253
  plans: {
253
254
  stonecropMeta(o, { $doctype: a }) {
254
- return E(a, (c) => m(c) ?? null);
255
+ return C(a, (s) => m(s) ?? null);
255
256
  },
256
257
  stonecropAllMeta() {
257
- return C(T());
258
+ return T(L());
258
259
  },
259
- stonecropRecord(o, { $doctype: a, $id: c }) {
260
- return S($({ doctype: a, id: c }), async (s) => await Promise.all(
261
- s.map(async (l) => {
262
- const d = m(l.doctype);
263
- if (!d)
264
- throw new Error(`Unknown doctype: ${l.doctype}`);
265
- if (!d.tableName)
266
- throw new Error(`Doctype ${l.doctype} has no table mapping`);
267
- const y = V(d, e), i = await t.executor.query(y, { id: l.id });
260
+ stonecropRecord(o, { $doctype: a, $id: s }) {
261
+ return S($({ doctype: a, id: s }), async (c) => await Promise.all(
262
+ c.map(async (d) => {
263
+ const l = m(d.doctype);
264
+ if (!l)
265
+ throw new Error(`Unknown doctype: ${d.doctype}`);
266
+ if (!l.tableName)
267
+ throw new Error(`Doctype ${d.doctype} has no table mapping`);
268
+ const y = z(l, e), i = await t.executor.query(y, { id: d.id });
268
269
  return {
269
- data: z(i, d, e),
270
- doctype: l.doctype
270
+ data: X(i, l, e),
271
+ doctype: d.doctype
271
272
  };
272
273
  })
273
274
  ));
274
275
  },
275
- stonecropRecords(o, { $doctype: a, $filters: c, $orderBy: s, $limit: l, $offset: d }) {
276
+ stonecropRecords(o, { $doctype: a, $filters: s, $orderBy: c, $limit: d, $offset: l }) {
276
277
  return S(
277
278
  $({
278
279
  doctype: a,
279
- filters: c,
280
- orderBy: s,
281
- limit: l,
282
- offset: d
280
+ filters: s,
281
+ orderBy: c,
282
+ limit: d,
283
+ offset: l
283
284
  }),
284
285
  async (y) => await Promise.all(
285
286
  y.map(async (i) => {
@@ -288,7 +289,7 @@ const de = (t) => {
288
289
  throw new Error(`Unknown doctype: ${i.doctype}`);
289
290
  if (!u.tableName)
290
291
  throw new Error(`Doctype ${i.doctype} has no table mapping`);
291
- const h = v(
292
+ const h = K(
292
293
  u,
293
294
  {
294
295
  limit: i.limit,
@@ -301,7 +302,7 @@ const de = (t) => {
301
302
  limit: i.limit,
302
303
  offset: i.offset,
303
304
  orderBy: i.orderBy
304
- }), b = K(f, u, r);
305
+ }), b = Z(f, u, r);
305
306
  return {
306
307
  data: b,
307
308
  doctype: i.doctype,
@@ -315,30 +316,30 @@ const de = (t) => {
315
316
  },
316
317
  Mutation: {
317
318
  plans: {
318
- stonecropAction(o, { $doctype: a, $action: c, $args: s }) {
319
+ stonecropAction(o, { $doctype: a, $action: s, $args: c }) {
319
320
  return S(
320
321
  $({
321
322
  doctype: a,
322
- action: c,
323
- actionArgs: s
323
+ action: s,
324
+ actionArgs: c
324
325
  }),
325
- async (l) => await Promise.all(
326
- l.map(async (d) => {
327
- const y = m(d.doctype);
326
+ async (d) => await Promise.all(
327
+ d.map(async (l) => {
328
+ const y = m(l.doctype);
328
329
  if (!y)
329
330
  return {
330
331
  success: !1,
331
332
  data: null,
332
- error: `Unknown doctype: ${d.doctype}`
333
+ error: `Unknown doctype: ${l.doctype}`
333
334
  };
334
- const i = y.workflow?.actions?.[d.action];
335
+ const i = y.workflow?.actions?.[l.action];
335
336
  if (!i)
336
337
  return {
337
338
  success: !1,
338
339
  data: null,
339
- error: `Unknown action: ${d.action} on ${d.doctype}`
340
+ error: `Unknown action: ${l.action} on ${l.doctype}`
340
341
  };
341
- const u = J(i.handler);
342
+ const u = _(i.handler);
342
343
  if (!u)
343
344
  return {
344
345
  success: !1,
@@ -352,7 +353,7 @@ const de = (t) => {
352
353
  try {
353
354
  return {
354
355
  success: !0,
355
- data: await u(d.actionArgs ?? [], h),
356
+ data: await u(l.actionArgs ?? [], h),
356
357
  error: null
357
358
  };
358
359
  } catch (f) {
@@ -371,26 +372,23 @@ const de = (t) => {
371
372
  }
372
373
  }));
373
374
  };
374
- function R(t) {
375
- return t.replace(/_([a-z])/g, (e, r) => r.toUpperCase());
376
- }
377
- function M(t) {
378
- const e = R(t);
379
- return e.charAt(0).toUpperCase() + e.slice(1);
380
- }
381
- function P(t) {
382
- const e = t.replace(/s$/, "");
383
- return `${R(e)}ById`;
375
+ function W(t) {
376
+ const e = j.singular(t);
377
+ return `${k(e)}ById`;
384
378
  }
385
- function Q(t) {
386
- return `all${M(t)}`;
379
+ function v(t) {
380
+ return `all${R(t)}`;
387
381
  }
388
- function W(t) {
389
- return `${M(t)}OrderBy`;
382
+ function V(t) {
383
+ return `${R(t)}OrderBy`;
390
384
  }
391
- function V(t, e) {
392
- const r = t.fields.map((o) => o.fieldname).join(`
385
+ const Y = /* @__PURE__ */ new Set(["Link", "Doctype"]);
386
+ function M(t) {
387
+ return t.fields.filter((e) => !Y.has(e.fieldtype)).map((e) => e.fieldname).join(`
393
388
  `);
389
+ }
390
+ function z(t, e) {
391
+ const r = M(t);
394
392
  return `
395
393
  query GetRecord($id: UUID!) {
396
394
  ${e(t.tableName)}(id: $id) {
@@ -399,14 +397,13 @@ function V(t, e) {
399
397
  }
400
398
  `;
401
399
  }
402
- function v(t, e, r, n) {
403
- const o = t.fields.map((d) => d.fieldname).join(`
404
- `), a = r(t.tableName), c = n(t.tableName), s = [];
405
- e.limit && s.push("first: $limit"), e.offset && s.push("offset: $offset"), e.orderBy && s.push("orderBy: $orderBy");
406
- const l = s.length > 0 ? `(${s.join(", ")})` : "";
400
+ function K(t, e, r, n) {
401
+ const o = M(t), a = r(t.tableName), s = n(t.tableName), c = [], d = [];
402
+ e.limit && (c.push("$limit: Int"), d.push("first: $limit")), e.offset && (c.push("$offset: Int"), d.push("offset: $offset")), e.orderBy && (c.push(`$orderBy: [${s}!]`), d.push("orderBy: $orderBy"));
403
+ const l = c.length > 0 ? `(${c.join(", ")})` : "", y = d.length > 0 ? `(${d.join(", ")})` : "";
407
404
  return `
408
- query GetRecords($limit: Int, $offset: Int, $orderBy: [${c}!]) {
409
- ${a}${l} {
405
+ query GetRecords${l} {
406
+ ${a}${y} {
410
407
  nodes {
411
408
  ${o}
412
409
  }
@@ -414,15 +411,15 @@ function v(t, e, r, n) {
414
411
  }
415
412
  `;
416
413
  }
417
- function z(t, e, r) {
414
+ function X(t, e, r) {
418
415
  const n = r(e.tableName);
419
416
  return t[n];
420
417
  }
421
- function K(t, e, r) {
418
+ function Z(t, e, r) {
422
419
  const n = r(e.tableName);
423
420
  return t[n]?.nodes ?? [];
424
421
  }
425
- class le {
422
+ class fe {
426
423
  endpoint;
427
424
  headers;
428
425
  metaCache = /* @__PURE__ */ new Map();
@@ -613,47 +610,54 @@ class le {
613
610
  }
614
611
  }
615
612
  export {
616
- ue as ActionDefinition,
617
- fe as DoctypeMeta,
613
+ he as ActionDefinition,
614
+ Se as DoctypeMeta,
618
615
  w as DoctypeValidationError,
619
- me as FieldMeta,
620
- ge as FieldOptions,
621
- he as FieldValidation,
622
- Se as GQL_SCALAR_MAP,
623
- le as StonecropClient,
624
- $e as StonecropFieldType,
625
- we as TYPE_MAP,
626
- be as WELL_KNOWN_SCALARS,
627
- Ne as WorkflowMeta,
628
- H as builtinHandlers,
629
- De as camelToLabel,
630
- Re as camelToSnake,
631
- Me as classifyFieldType,
632
- se as clearHandlers,
633
- ne as clearRegistry,
634
- ke as convertGraphQLSchema,
635
- de as createStonecropPlugin,
636
- Ae as defaultIsEntityField,
637
- qe as defaultIsEntityType,
638
- T as getAllMeta,
639
- Oe as getDefaultComponent,
640
- J as getHandler,
616
+ $e as FieldMeta,
617
+ we as FieldOptions,
618
+ be as FieldValidation,
619
+ Ne as GQL_SCALAR_MAP,
620
+ Y as RELATION_FIELDTYPES,
621
+ fe as StonecropClient,
622
+ De as StonecropFieldType,
623
+ Re as TYPE_MAP,
624
+ Me as WELL_KNOWN_SCALARS,
625
+ ke as WorkflowMeta,
626
+ K as buildListQuery,
627
+ z as buildRecordQuery,
628
+ U as builtinHandlers,
629
+ Ae as camelToLabel,
630
+ qe as camelToSnake,
631
+ Oe as classifyFieldType,
632
+ pe as clearHandlers,
633
+ ie as clearRegistry,
634
+ Fe as convertGraphQLSchema,
635
+ ue as createStonecropPlugin,
636
+ v as defaultConnectionFieldName,
637
+ Ee as defaultIsEntityField,
638
+ Be as defaultIsEntityType,
639
+ V as defaultOrderByTypeName,
640
+ W as defaultRecordFieldName,
641
+ L as getAllMeta,
642
+ Te as getDefaultComponent,
643
+ _ as getHandler,
641
644
  m as getMeta,
642
- ce as hasHandler,
643
- oe as hasMeta,
644
- j as loadDoctypes,
645
- re as loadDoctypesFromObject,
646
- Fe as parseDoctype,
647
- Be as parseField,
648
- Ce as pascalToSnake,
649
- ie as registerBuiltinHandlers,
650
- I as registerHandler,
651
- Ee as snakeToCamel,
652
- je as snakeToLabel,
653
- xe as toPascalCase,
654
- Te as toSlug,
655
- Ie as validateDoctype,
656
- Je as validateField,
657
- ae as validateReferences
645
+ le as hasHandler,
646
+ ce as hasMeta,
647
+ x as loadDoctypes,
648
+ se as loadDoctypesFromObject,
649
+ Ce as parseDoctype,
650
+ je as parseField,
651
+ xe as pascalToSnake,
652
+ M as queryableFieldNames,
653
+ ye as registerBuiltinHandlers,
654
+ J as registerHandler,
655
+ Ie as snakeToCamel,
656
+ Le as snakeToLabel,
657
+ Je as toPascalCase,
658
+ _e as toSlug,
659
+ Ge as validateDoctype,
660
+ Pe as validateField,
661
+ de as validateReferences
658
662
  };
659
663
  //# sourceMappingURL=index.js.map