@stonecrop/graphql-middleware 0.11.1 → 0.11.2

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,184 +1,184 @@
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 R, parseDoctype as O, toPascalCase as A, snakeToCamel as j } from "@stonecrop/schema";
4
- import { loadOne as b, object as w, constant as F, lambda as q } from "postgraphile/grafast";
5
- import { extendSchema as I, gql as T } from "postgraphile/utils";
6
- import J from "pluralize";
7
- const u = /* @__PURE__ */ new Map();
8
- class k extends Error {
9
- constructor(t, r) {
10
- const i = r.map((n) => ` ${n.path.join(".")}: ${n.message}`).join(`
1
+ import { readdirSync as C, statSync as _, readFileSync as P } from "node:fs";
2
+ import { join as U } from "node:path";
3
+ import { validateDoctype as E, parseDoctype as I, toPascalCase as A, snakeToCamel as H } from "@stonecrop/schema";
4
+ import { loadOne as M, object as R, constant as Q, lambda as V } from "postgraphile/grafast";
5
+ import { extendSchema as z, gql as J } from "postgraphile/utils";
6
+ import j from "pluralize";
7
+ const k = /* @__PURE__ */ new Map();
8
+ class O extends Error {
9
+ constructor(t, n) {
10
+ const i = n.map((o) => ` ${o.path.join(".")}: ${o.message}`).join(`
11
11
  `);
12
12
  super(`Invalid doctype definition in ${t}:
13
- ${i}`), this.file = t, this.errors = r, this.name = "DoctypeValidationError";
13
+ ${i}`), this.file = t, this.errors = n, this.name = "DoctypeValidationError";
14
14
  }
15
15
  }
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);
16
+ function G(e, t = {}) {
17
+ const n = C(e);
18
+ for (const i of n) {
19
+ const o = U(e, i);
20
+ _(o).isDirectory() ? G(o, t) : i.endsWith(".json") && Y(o, t);
21
21
  }
22
22
  }
23
- function C(e, t) {
24
- const r = x(e, "utf-8");
23
+ function Y(e, t) {
24
+ const n = P(e, "utf-8");
25
25
  let i;
26
26
  try {
27
- i = JSON.parse(r);
28
- } catch (c) {
29
- const o = {
27
+ i = JSON.parse(n);
28
+ } catch (s) {
29
+ const c = {
30
30
  path: [],
31
- message: `Invalid JSON: ${c instanceof Error ? c.message : String(c)}`
31
+ message: `Invalid JSON: ${s instanceof Error ? s.message : String(s)}`
32
32
  };
33
33
  if (t.continueOnError) {
34
- t.onError?.(e, [o]);
34
+ t.onError?.(e, [c]);
35
35
  return;
36
36
  }
37
- throw new k(e, [o]);
37
+ throw new O(e, [c]);
38
38
  }
39
- const n = R(i);
40
- if (!n.success) {
39
+ const o = E(i);
40
+ if (!o.success) {
41
41
  if (t.continueOnError) {
42
- t.onError?.(e, n.errors);
42
+ t.onError?.(e, o.errors);
43
43
  return;
44
44
  }
45
- throw new k(e, n.errors);
45
+ throw new O(e, o.errors);
46
46
  }
47
- const a = O(i);
48
- u.set(a.name, a);
47
+ const r = I(i);
48
+ k.set(r.name, r);
49
49
  }
50
- function de(e, t = {}) {
51
- for (const [r, i] of Object.entries(e)) {
52
- const n = { ...i, name: r }, a = R(n);
53
- if (!a.success) {
50
+ function be(e, t = {}) {
51
+ for (const [n, i] of Object.entries(e)) {
52
+ const o = { ...i, name: n }, r = E(o);
53
+ if (!r.success) {
54
54
  if (t.continueOnError) {
55
- t.onError?.(r, a.errors);
55
+ t.onError?.(n, r.errors);
56
56
  continue;
57
57
  }
58
- throw new k(r, a.errors);
58
+ throw new O(n, r.errors);
59
59
  }
60
- const c = O(n);
61
- u.set(c.name, c);
60
+ const s = I(o);
61
+ k.set(s.name, s);
62
62
  }
63
63
  }
64
- function h(e) {
65
- return u.get(e);
64
+ function N(e) {
65
+ return k.get(e);
66
66
  }
67
- function H() {
68
- return Array.from(u.values());
67
+ function K() {
68
+ return Array.from(k.values());
69
69
  }
70
- function fe(e) {
71
- return u.has(e);
70
+ function Me(e) {
71
+ return k.has(e);
72
72
  }
73
- function ue() {
74
- u.clear();
73
+ function Re() {
74
+ k.clear();
75
75
  }
76
- function ye() {
76
+ function Oe() {
77
77
  const e = [];
78
- for (const t of u.values()) {
79
- t.inherits && !u.has(t.inherits) && e.push({
78
+ for (const t of k.values()) {
79
+ t.inherits && !k.has(t.inherits) && e.push({
80
80
  path: [t.name, "inherits"],
81
81
  message: `References unknown doctype: ${t.inherits}`
82
82
  });
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"],
86
- message: `Link references unknown doctype: ${r.options}`
83
+ for (const n of t.fields)
84
+ n.fieldtype === "Link" && typeof n.options == "string" && (k.has(n.options) || e.push({
85
+ path: [t.name, "fields", n.fieldname, "options"],
86
+ message: `Link references unknown doctype: ${n.options}`
87
87
  }));
88
88
  }
89
89
  return e;
90
90
  }
91
- const $ = /* @__PURE__ */ new Map();
92
- function _(e, t) {
93
- $.set(e, t);
91
+ const b = /* @__PURE__ */ new Map();
92
+ function X(e, t) {
93
+ b.set(e, t);
94
94
  }
95
- function v(e) {
96
- return $.get(e);
95
+ function Z(e) {
96
+ return b.get(e);
97
97
  }
98
- function pe(e) {
99
- return $.has(e);
98
+ function Ae(e) {
99
+ return b.has(e);
100
100
  }
101
- function me() {
102
- $.clear();
101
+ function Be() {
102
+ b.clear();
103
103
  }
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);
108
- if (n.length > 0)
109
- throw new Error(`Missing required fields: ${n.join(", ")}`);
104
+ const ee = async (e, t) => {
105
+ const [n] = e, { doctype: i } = t, o = [];
106
+ for (const r of i.fields)
107
+ r.required && (n[r.fieldname] === void 0 || n[r.fieldname] === null) && o.push(r.label ?? r.fieldname);
108
+ if (o.length > 0)
109
+ throw new Error(`Missing required fields: ${o.join(", ")}`);
110
110
  return { valid: !0 };
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 = W(a, c);
117
- o && n.push(o);
111
+ }, te = async (e, t) => {
112
+ const [n] = e, { doctype: i } = t, o = [];
113
+ for (const r of i.fields) {
114
+ const s = n[r.fieldname];
115
+ if (s == null) continue;
116
+ const c = ne(r, s);
117
+ c && o.push(c);
118
118
  }
119
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}`);
120
+ for (const [r, s] of Object.entries(i.links)) {
121
+ const c = n[r];
122
+ if (c == null) continue;
123
+ s.cardinality === "noneOrMany" || s.cardinality === "atLeastOne" ? Array.isArray(c) || o.push(`${r}: expected array, got ${typeof c}`) : (typeof c != "object" || Array.isArray(c)) && o.push(`${r}: expected object, got ${typeof c}`);
124
124
  }
125
- if (n.length > 0)
125
+ if (o.length > 0)
126
126
  throw new Error(`Field type validation failed:
127
- ${n.join(`
127
+ ${o.join(`
128
128
  `)}`);
129
129
  return { valid: !0 };
130
130
  };
131
- function W(e, t) {
132
- const { fieldname: r, fieldtype: i, cardinality: n } = e;
131
+ function ne(e, t) {
132
+ const { fieldname: n, fieldtype: i, cardinality: o } = e;
133
133
  switch (i) {
134
134
  case "Int":
135
135
  if (typeof t != "number" || !Number.isInteger(t))
136
- return `${r}: expected integer, got ${typeof t}`;
136
+ return `${n}: 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
142
  if (typeof t != "number" && typeof t != "string")
143
- return `${r}: expected number, got ${typeof t}`;
143
+ return `${n}: expected number, got ${typeof t}`;
144
144
  break;
145
145
  case "Check":
146
146
  if (typeof t != "boolean")
147
- return `${r}: expected boolean, got ${typeof t}`;
147
+ return `${n}: 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
153
  if (typeof t != "string")
154
- return `${r}: expected string, got ${typeof t}`;
154
+ return `${n}: expected string, got ${typeof t}`;
155
155
  break;
156
156
  case "Date":
157
157
  case "Time":
158
158
  case "Datetime":
159
159
  if (typeof t != "string" && !(t instanceof Date))
160
- return `${r}: expected date string or Date, got ${typeof t}`;
160
+ return `${n}: expected date string or Date, got ${typeof t}`;
161
161
  break;
162
162
  case "JSON":
163
163
  if (typeof t != "object")
164
- return `${r}: expected object, got ${typeof t}`;
164
+ return `${n}: expected object, got ${typeof t}`;
165
165
  break;
166
166
  }
167
167
  return null;
168
168
  }
169
- const P = async () => ({ ok: !0 }), V = {
170
- validateRequiredFields: U,
171
- validateFieldTypes: Q,
172
- noop: P
169
+ const oe = async () => ({ ok: !0 }), re = {
170
+ validateRequiredFields: ee,
171
+ validateFieldTypes: te,
172
+ noop: oe
173
173
  };
174
- function ge() {
175
- for (const [e, t] of Object.entries(V))
176
- _(e, t);
174
+ function De() {
175
+ for (const [e, t] of Object.entries(re))
176
+ X(e, t);
177
177
  }
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;
180
- return I(() => ({
181
- typeDefs: T`
178
+ const Fe = (e) => {
179
+ const t = e.inflection?.recordFieldName ?? ie, n = e.inflection?.connectionFieldName ?? ce, i = e.inflection?.orderByTypeName ?? se, o = e.inflection?.recordArgName ?? ae, r = e.inflection?.recordArgType ?? le, s = e.inflection?.reverseConnectionName ?? B;
180
+ return z(() => ({
181
+ typeDefs: J`
182
182
  type StonecropFieldMeta {
183
183
  fieldname: String!
184
184
  fieldtype: String!
@@ -225,6 +225,7 @@ const Se = (e) => {
225
225
  type StonecropRecordResult {
226
226
  data: JSON
227
227
  doctype: String!
228
+ unknownLinks: [String!]
228
229
  }
229
230
 
230
231
  type StonecropRecordsResult {
@@ -242,7 +243,7 @@ const Se = (e) => {
242
243
  extend type Query {
243
244
  stonecropMeta(doctype: String!): StonecropDoctypeMeta
244
245
  stonecropAllMeta: [StonecropDoctypeMeta!]!
245
- stonecropRecord(doctype: String!, id: String!): StonecropRecordResult
246
+ stonecropRecord(doctype: String!, id: String!, options: JSON): StonecropRecordResult
246
247
  stonecropRecords(
247
248
  doctype: String!
248
249
  filters: JSON
@@ -259,64 +260,82 @@ const Se = (e) => {
259
260
  objects: {
260
261
  Query: {
261
262
  plans: {
262
- stonecropMeta(c, { $doctype: o }) {
263
- return q(o, (l) => h(l) ?? null);
263
+ stonecropMeta(c, { $doctype: l }) {
264
+ return V(l, (y) => N(y) ?? null);
264
265
  },
265
266
  stonecropAllMeta() {
266
- return F(H());
267
+ return Q(K());
267
268
  },
268
- stonecropRecord(c, { $doctype: o, $id: l }) {
269
- return b(w({ doctype: o, id: l }), async (y) => await Promise.all(
270
- y.map(async (f) => {
271
- const d = h(f.doctype);
272
- if (!d)
273
- throw new Error(`Unknown doctype: ${f.doctype}`);
274
- if (!d.tableName)
275
- throw new Error(`Doctype ${f.doctype} has no table mapping`);
276
- const p = ee(d, t, n, a), s = await e.executor.query(p, {
277
- [n(d.tableName)]: f.id
278
- });
279
- return {
280
- data: re(s, d, t),
281
- doctype: f.doctype
282
- };
283
- })
284
- ));
269
+ stonecropRecord(c, { $doctype: l, $id: y, $options: p }) {
270
+ return M(
271
+ R({ doctype: l, id: y, options: p }),
272
+ async (d) => await Promise.all(
273
+ d.map(async (u) => {
274
+ const f = N(u.doctype);
275
+ if (!f)
276
+ throw new Error(`Unknown doctype: ${u.doctype}`);
277
+ if (!f.tableName)
278
+ throw new Error(`Doctype ${u.doctype} has no table mapping`);
279
+ const a = u.options ?? {}, h = ue(
280
+ f,
281
+ t,
282
+ o,
283
+ r,
284
+ N,
285
+ a.includeNested ? {
286
+ includeNested: a.includeNested,
287
+ maxDepth: a.maxDepth
288
+ } : void 0,
289
+ s
290
+ ), g = await e.executor.query(h, {
291
+ [o(f.tableName)]: u.id
292
+ });
293
+ let S = ge({ result: g, meta: f, recordFieldName: t });
294
+ a.includeNested && S && f.links && (S = ye({ record: S, meta: f, getMeta: N }));
295
+ const m = Array.isArray(a.includeNested) && f.links ? a.includeNested.filter((w) => !(w in f.links)) : void 0;
296
+ return {
297
+ data: S,
298
+ doctype: u.doctype,
299
+ ...m ? { unknownLinks: m } : {}
300
+ };
301
+ })
302
+ )
303
+ );
285
304
  },
286
- stonecropRecords(c, { $doctype: o, $filters: l, $orderBy: y, $limit: f, $offset: d }) {
287
- return b(
288
- w({
289
- doctype: o,
290
- filters: l,
291
- orderBy: y,
292
- limit: f,
293
- offset: d
305
+ stonecropRecords(c, { $doctype: l, $filters: y, $orderBy: p, $limit: d, $offset: u }) {
306
+ return M(
307
+ R({
308
+ doctype: l,
309
+ filters: y,
310
+ orderBy: p,
311
+ limit: d,
312
+ offset: u
294
313
  }),
295
- async (p) => await Promise.all(
296
- p.map(async (s) => {
297
- const m = h(s.doctype);
298
- if (!m)
299
- throw new Error(`Unknown doctype: ${s.doctype}`);
300
- if (!m.tableName)
301
- throw new Error(`Doctype ${s.doctype} has no table mapping`);
302
- const S = te(
303
- m,
314
+ async (f) => await Promise.all(
315
+ f.map(async (a) => {
316
+ const h = N(a.doctype);
317
+ if (!h)
318
+ throw new Error(`Unknown doctype: ${a.doctype}`);
319
+ if (!h.tableName)
320
+ throw new Error(`Doctype ${a.doctype} has no table mapping`);
321
+ const g = pe(
322
+ h,
304
323
  {
305
- limit: s.limit,
306
- offset: s.offset,
307
- orderBy: s.orderBy
324
+ limit: a.limit,
325
+ offset: a.offset,
326
+ orderBy: a.orderBy
308
327
  },
309
- r,
328
+ n,
310
329
  i
311
- ), N = await e.executor.query(S, {
312
- limit: s.limit,
313
- offset: s.offset,
314
- orderBy: s.orderBy
315
- }), g = ne(N, m, r);
330
+ ), S = await e.executor.query(g, {
331
+ limit: a.limit,
332
+ offset: a.offset,
333
+ orderBy: a.orderBy
334
+ }), m = me({ result: S, meta: h, connectionFieldName: n });
316
335
  return {
317
- data: g,
318
- doctype: s.doctype,
319
- count: g.length
336
+ data: m,
337
+ doctype: a.doctype,
338
+ count: m.length
320
339
  };
321
340
  })
322
341
  )
@@ -326,51 +345,51 @@ const Se = (e) => {
326
345
  },
327
346
  Mutation: {
328
347
  plans: {
329
- stonecropAction(c, { $doctype: o, $action: l, $args: y }) {
330
- return b(
331
- w({
332
- doctype: o,
333
- action: l,
334
- actionArgs: y
348
+ stonecropAction(c, { $doctype: l, $action: y, $args: p }) {
349
+ return M(
350
+ R({
351
+ doctype: l,
352
+ action: y,
353
+ actionArgs: p
335
354
  }),
336
- async (f) => await Promise.all(
337
- f.map(async (d) => {
338
- const p = h(d.doctype);
339
- if (!p)
355
+ async (d) => await Promise.all(
356
+ d.map(async (u) => {
357
+ const f = N(u.doctype);
358
+ if (!f)
340
359
  return {
341
360
  success: !1,
342
361
  data: null,
343
- error: `Unknown doctype: ${d.doctype}`
362
+ error: `Unknown doctype: ${u.doctype}`
344
363
  };
345
- const s = p.workflow?.actions?.[d.action];
346
- if (!s)
364
+ const a = f.workflow?.actions?.[u.action];
365
+ if (!a)
347
366
  return {
348
367
  success: !1,
349
368
  data: null,
350
- error: `Unknown action: ${d.action} on ${d.doctype}`
369
+ error: `Unknown action: ${u.action} on ${u.doctype}`
351
370
  };
352
- const m = s.handler, S = v(m);
353
- if (!S)
371
+ const h = a.handler, g = Z(h);
372
+ if (!g)
354
373
  return {
355
374
  success: !1,
356
375
  data: null,
357
- error: `Handler not registered: ${m}`
376
+ error: `Handler not registered: ${h}`
358
377
  };
359
- const N = {
360
- doctype: p,
378
+ const S = {
379
+ doctype: f,
361
380
  executor: e.executor
362
381
  };
363
382
  try {
364
383
  return {
365
384
  success: !0,
366
- data: await S(d.actionArgs ?? [], N),
385
+ data: await g(u.actionArgs ?? [], S),
367
386
  error: null
368
387
  };
369
- } catch (g) {
388
+ } catch (m) {
370
389
  return {
371
390
  success: !1,
372
391
  data: null,
373
- error: g instanceof Error ? g.message : String(g)
392
+ error: m instanceof Error ? m.message : String(m)
374
393
  };
375
394
  }
376
395
  })
@@ -382,81 +401,259 @@ const Se = (e) => {
382
401
  }
383
402
  }));
384
403
  };
385
- function G(e) {
386
- const t = J.singular(e);
387
- return `${j(t)}ById`;
404
+ function ie(e) {
405
+ const t = j.singular(e);
406
+ return `${H(t)}ById`;
388
407
  }
389
- function z(e) {
408
+ function ce(e) {
390
409
  return `all${A(e)}`;
391
410
  }
392
- function Y(e) {
411
+ function se(e) {
393
412
  return `${A(e)}OrderBy`;
394
413
  }
395
- function K(e) {
414
+ function ae(e) {
396
415
  return "id";
397
416
  }
398
- function X(e) {
417
+ function le(e) {
399
418
  return "UUID!";
400
419
  }
401
- const Z = /* @__PURE__ */ new Set(["Link"]);
420
+ function B(e) {
421
+ const t = j.plural(e.target.replace(/-/g, "_")), n = A(t), i = e.backlink || e.doctype, o = i.charAt(0).toUpperCase() + i.slice(1);
422
+ return `${n}By${o}Id`;
423
+ }
402
424
  function D(e) {
403
- return e.fields.filter((t) => !Z.has(t.fieldtype)).map((t) => t.fieldname).join(`
425
+ return e === "noneOrMany" || e === "atLeastOne";
426
+ }
427
+ const q = 50, de = /* @__PURE__ */ new Set(["Link"]);
428
+ function F(e) {
429
+ return e.fields.filter((t) => !de.has(t.fieldtype)).map((t) => t.fieldname).join(`
404
430
  `);
405
431
  }
406
- function ee(e, t, r, i) {
407
- const n = D(e), a = t(e.tableName), c = r(e.tableName), o = i(e.tableName);
432
+ function T(e) {
433
+ return e.fetch !== void 0 ? e.fetch : D(e.cardinality) ? { method: "sync", limit: q } : { method: "lazy" };
434
+ }
435
+ function fe(e) {
436
+ return e.blockWorkflows !== void 0 ? e.blockWorkflows : T(e).method === "sync";
437
+ }
438
+ function L(e) {
439
+ const { links: t, meta: n, includeSet: i, getMeta: o, seen: r, depth: s, maxDepth: c, reverseConnectionNameFn: l } = e;
440
+ if (c !== void 0 && s >= c) return "";
441
+ const y = [];
442
+ for (const [p, d] of Object.entries(t)) {
443
+ if (c !== void 0 && s >= c) break;
444
+ const u = fe(d), f = d.blockWorkflows === !0;
445
+ if (i && !i.has(p) && !f)
446
+ continue;
447
+ const a = T(d);
448
+ if (u === !1 || a.method !== "sync" && !f)
449
+ continue;
450
+ const g = o(d.target);
451
+ if (!g) continue;
452
+ const S = r.has(d.target);
453
+ S || r.add(d.target);
454
+ const m = F(g);
455
+ let w = "";
456
+ if (!S && g.links && g.tableName && (c === void 0 || s + 1 < c)) {
457
+ const $ = L({
458
+ links: g.links,
459
+ meta: g,
460
+ includeSet: null,
461
+ getMeta: o,
462
+ seen: r,
463
+ depth: s + 1,
464
+ maxDepth: c,
465
+ reverseConnectionNameFn: l
466
+ });
467
+ $ && (w = `
468
+ ` + $), r.delete(d.target);
469
+ }
470
+ const x = m + w;
471
+ if (D(d.cardinality)) {
472
+ const $ = {
473
+ doctype: n.slug || n.name,
474
+ linkName: p,
475
+ backlink: d.backlink,
476
+ target: d.target
477
+ }, W = l ? l($) : B($), v = a.method === "sync" && a.limit !== void 0 ? `first: ${a.limit}` : a.method === "sync" ? `first: ${q}` : "";
478
+ y.push(`
479
+ ${W}${v ? `(${v})` : ""} {
480
+ nodes {
481
+ ${x}
482
+ }
483
+ }`);
484
+ } else
485
+ y.push(`
486
+ ${p} {
487
+ ${x}
488
+ }`);
489
+ }
490
+ return y.join("");
491
+ }
492
+ function ue(e, t, n, i, o, r, s) {
493
+ const c = t(e.tableName), l = n(e.tableName), y = i(e.tableName), p = /* @__PURE__ */ new Set([e.slug || e.name]);
494
+ let d = F(e);
495
+ if (r?.includeNested && e.links) {
496
+ const u = Array.isArray(r.includeNested) ? new Set(r.includeNested) : null, f = L({
497
+ links: e.links,
498
+ meta: e,
499
+ includeSet: u,
500
+ getMeta: o,
501
+ seen: p,
502
+ depth: 0,
503
+ maxDepth: r.maxDepth,
504
+ reverseConnectionNameFn: s
505
+ });
506
+ f && (d += `
507
+ ` + f);
508
+ }
408
509
  return `
409
- query GetRecord($${c}: ${o}) {
410
- ${a}(${c}: $${c}) {
411
- ${n}
510
+ query GetRecord($${l}: ${y}) {
511
+ ${c}(${l}: $${l}) {
512
+ ${d}
412
513
  }
413
514
  }
414
515
  `;
415
516
  }
416
- function te(e, t, r, i) {
417
- const n = D(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(", ")})` : "";
517
+ function pe(e, t, n, i) {
518
+ const o = F(e), r = n(e.tableName), s = i(e.tableName), c = [], l = [];
519
+ t.limit && (c.push("$limit: Int"), l.push("first: $limit")), t.offset && (c.push("$offset: Int"), l.push("offset: $offset")), t.orderBy && (c.push(`$orderBy: [${s}!]`), l.push("orderBy: $orderBy"));
520
+ const y = c.length > 0 ? `(${c.join(", ")})` : "", p = l.length > 0 ? `(${l.join(", ")})` : "";
420
521
  return `
421
522
  query GetRecords${y} {
422
- ${a}${f} {
523
+ ${r}${p} {
423
524
  nodes {
424
- ${n}
525
+ ${o}
425
526
  }
426
527
  }
427
528
  }
428
529
  `;
429
530
  }
430
- function re(e, t, r) {
431
- const i = r(t.tableName);
432
- return e[i];
531
+ function ye(e) {
532
+ const { record: t, meta: n, getMeta: i } = e;
533
+ if (!n.links) return t;
534
+ const o = { ...t };
535
+ for (const [r, s] of Object.entries(n.links))
536
+ if (D(s.cardinality)) {
537
+ if (!i(s.target)) continue;
538
+ const y = {
539
+ doctype: n.slug || n.name,
540
+ backlink: s.backlink || r,
541
+ target: s.target
542
+ }, p = B(y), d = o[p];
543
+ d?.nodes ? (o[r] = d.nodes, delete o[p]) : (o[r] = [], delete o[p]);
544
+ }
545
+ return o;
546
+ }
547
+ function ge(e) {
548
+ const { result: t, meta: n, recordFieldName: i } = e, o = i(n.tableName);
549
+ return t[o];
433
550
  }
434
- function ne(e, t, r) {
435
- const i = r(t.tableName);
436
- return e[i]?.nodes ?? [];
551
+ function me(e) {
552
+ const { result: t, meta: n, connectionFieldName: i } = e, o = i(n.tableName);
553
+ return t[o]?.nodes ?? [];
437
554
  }
555
+ const xe = J`
556
+ type StonecropFieldMeta {
557
+ fieldname: String!
558
+ fieldtype: String!
559
+ component: String
560
+ label: String
561
+ width: String
562
+ align: String
563
+ required: Boolean
564
+ readOnly: Boolean
565
+ edit: Boolean
566
+ hidden: Boolean
567
+ default: JSON
568
+ options: JSON
569
+ mask: String
570
+ precision: Int
571
+ scale: Int
572
+ mode: String
573
+ validation: JSON
574
+ }
575
+
576
+ type StonecropWorkflowAction {
577
+ label: String!
578
+ handler: String!
579
+ requiredFields: [String!]
580
+ allowedStates: [String!]
581
+ confirm: Boolean
582
+ args: JSON
583
+ }
584
+
585
+ type StonecropWorkflowMeta {
586
+ states: [String!]
587
+ actions: [StonecropWorkflowAction!]
588
+ }
589
+
590
+ type StonecropDoctypeMeta {
591
+ name: String!
592
+ slug: String
593
+ tableName: String
594
+ fields: [StonecropFieldMeta!]!
595
+ workflow: StonecropWorkflowMeta
596
+ inherits: String
597
+ }
598
+
599
+ type StonecropRecordResult {
600
+ data: JSON
601
+ doctype: String!
602
+ unknownLinks: [String!]
603
+ }
604
+
605
+ type StonecropRecordsResult {
606
+ data: [JSON!]!
607
+ doctype: String!
608
+ count: Int!
609
+ }
610
+
611
+ type StonecropActionResult {
612
+ success: Boolean!
613
+ data: JSON
614
+ error: String
615
+ }
616
+
617
+ extend type Query {
618
+ stonecropMeta(doctype: String!): StonecropDoctypeMeta
619
+ stonecropAllMeta: [StonecropDoctypeMeta!]!
620
+ stonecropRecord(doctype: String!, id: String!, options: JSON): StonecropRecordResult
621
+ stonecropRecords(doctype: String!, filters: JSON, orderBy: String, limit: Int, offset: Int): StonecropRecordsResult
622
+ }
623
+
624
+ extend type Mutation {
625
+ stonecropAction(doctype: String!, action: String!, args: JSON): StonecropActionResult!
626
+ }
627
+ `;
438
628
  export {
439
- k as DoctypeValidationError,
440
- Z as RELATION_FIELDTYPES,
441
- V as builtinHandlers,
442
- me as clearHandlers,
443
- ue as clearRegistry,
444
- Se as createStonecropPlugin,
445
- z as defaultConnectionFieldName,
446
- Y as defaultOrderByTypeName,
447
- K as defaultRecordArgName,
448
- X as defaultRecordArgType,
449
- G as defaultRecordFieldName,
450
- H as getAllMeta,
451
- v as getHandler,
452
- h as getMeta,
453
- pe as hasHandler,
454
- fe as hasMeta,
455
- L as loadDoctypes,
456
- de as loadDoctypesFromObject,
457
- D as queryableFieldNames,
458
- ge as registerBuiltinHandlers,
459
- _ as registerHandler,
460
- ye as validateReferences
629
+ O as DoctypeValidationError,
630
+ de as RELATION_FIELDTYPES,
631
+ pe as buildListQuery,
632
+ ue as buildRecordQuery,
633
+ re as builtinHandlers,
634
+ Be as clearHandlers,
635
+ Re as clearRegistry,
636
+ Fe as createStonecropPlugin,
637
+ ce as defaultConnectionFieldName,
638
+ se as defaultOrderByTypeName,
639
+ ae as defaultRecordArgName,
640
+ le as defaultRecordArgType,
641
+ ie as defaultRecordFieldName,
642
+ B as defaultReverseConnectionName,
643
+ me as extractListResult,
644
+ ge as extractSingleResult,
645
+ K as getAllMeta,
646
+ Z as getHandler,
647
+ N as getMeta,
648
+ Ae as hasHandler,
649
+ Me as hasMeta,
650
+ G as loadDoctypes,
651
+ be as loadDoctypesFromObject,
652
+ ye as mergeNestedResults,
653
+ F as queryableFieldNames,
654
+ De as registerBuiltinHandlers,
655
+ X as registerHandler,
656
+ xe as typeDefs,
657
+ Oe as validateReferences
461
658
  };
462
659
  //# sourceMappingURL=index.js.map