@stonecrop/graphql-middleware 0.11.1 → 0.11.3

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 W, 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 N = /* @__PURE__ */ new Map();
8
+ class O extends Error {
9
+ constructor(t, n) {
10
+ const c = 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
+ ${c}`), 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 = W(e);
18
+ for (const c of n) {
19
+ const o = U(e, c);
20
+ _(o).isDirectory() ? G(o, t) : c.endsWith(".json") && Y(o, t);
21
21
  }
22
22
  }
23
- function C(e, t) {
24
- const r = x(e, "utf-8");
25
- let i;
23
+ function Y(e, t) {
24
+ const n = P(e, "utf-8");
25
+ let c;
26
26
  try {
27
- i = JSON.parse(r);
28
- } catch (c) {
29
- const o = {
27
+ c = JSON.parse(n);
28
+ } catch (s) {
29
+ const i = {
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, [i]);
35
35
  return;
36
36
  }
37
- throw new k(e, [o]);
37
+ throw new O(e, [i]);
38
38
  }
39
- const n = R(i);
40
- if (!n.success) {
39
+ const o = E(c);
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(c);
48
+ N.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, c] of Object.entries(e)) {
52
+ const o = { ...c, 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
+ N.set(s.name, s);
62
62
  }
63
63
  }
64
- function h(e) {
65
- return u.get(e);
64
+ function k(e) {
65
+ return N.get(e);
66
66
  }
67
- function H() {
68
- return Array.from(u.values());
67
+ function K() {
68
+ return Array.from(N.values());
69
69
  }
70
- function fe(e) {
71
- return u.has(e);
70
+ function Me(e) {
71
+ return N.has(e);
72
72
  }
73
- function ue() {
74
- u.clear();
73
+ function Re() {
74
+ N.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 N.values()) {
79
+ t.inherits && !N.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" && (N.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 Fe() {
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: c } = t, o = [];
106
+ for (const r of c.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: c } = t, o = [];
113
+ for (const r of c.fields) {
114
+ const s = n[r.fieldname];
115
+ if (s == null) continue;
116
+ const i = ne(r, s);
117
+ i && o.push(i);
118
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}`);
119
+ if (c.links)
120
+ for (const [r, s] of Object.entries(c.links)) {
121
+ const i = n[r];
122
+ if (i == null) continue;
123
+ s.cardinality === "noneOrMany" || s.cardinality === "atLeastOne" ? Array.isArray(i) || o.push(`${r}: expected array, got ${typeof i}`) : (typeof i != "object" || Array.isArray(i)) && o.push(`${r}: expected object, got ${typeof i}`);
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;
133
- switch (i) {
131
+ function ne(e, t) {
132
+ const { fieldname: n, fieldtype: c, cardinality: o } = e;
133
+ switch (c) {
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 Be() {
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 De = (e) => {
179
+ const t = e.inflection?.recordFieldName ?? ie, n = e.inflection?.connectionFieldName ?? ce, c = e.inflection?.orderByTypeName ?? se, o = e.inflection?.recordArgName ?? ae, r = e.inflection?.recordArgType ?? le, s = e.inflection?.reverseConnectionName ?? F;
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,87 @@ 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(i, { $doctype: l }) {
264
+ return V(l, (y) => k(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(i, { $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 (f) => {
274
+ const u = k(f.doctype);
275
+ if (!u)
276
+ throw new Error(`Unknown doctype: ${f.doctype}`);
277
+ if (!u.tableName)
278
+ throw new Error(`Doctype ${f.doctype} has no table mapping`);
279
+ const a = f.options ?? {}, h = ue(
280
+ u,
281
+ t,
282
+ o,
283
+ r,
284
+ k,
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(u.tableName)]: f.id
292
+ });
293
+ let S = ge({ result: g, meta: u, recordFieldName: t });
294
+ a.includeNested && S && u.links && (S = ye({
295
+ record: S,
296
+ meta: u,
297
+ getMeta: k,
298
+ reverseConnectionNameFn: s
299
+ }));
300
+ const m = Array.isArray(a.includeNested) && u.links ? a.includeNested.filter((w) => !(w in u.links)) : void 0;
301
+ return {
302
+ data: S,
303
+ doctype: f.doctype,
304
+ ...m ? { unknownLinks: m } : {}
305
+ };
306
+ })
307
+ )
308
+ );
285
309
  },
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
310
+ stonecropRecords(i, { $doctype: l, $filters: y, $orderBy: p, $limit: d, $offset: f }) {
311
+ return M(
312
+ R({
313
+ doctype: l,
314
+ filters: y,
315
+ orderBy: p,
316
+ limit: d,
317
+ offset: f
294
318
  }),
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,
319
+ async (u) => await Promise.all(
320
+ u.map(async (a) => {
321
+ const h = k(a.doctype);
322
+ if (!h)
323
+ throw new Error(`Unknown doctype: ${a.doctype}`);
324
+ if (!h.tableName)
325
+ throw new Error(`Doctype ${a.doctype} has no table mapping`);
326
+ const g = pe(
327
+ h,
304
328
  {
305
- limit: s.limit,
306
- offset: s.offset,
307
- orderBy: s.orderBy
329
+ limit: a.limit,
330
+ offset: a.offset,
331
+ orderBy: a.orderBy
308
332
  },
309
- r,
310
- 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);
333
+ n,
334
+ c
335
+ ), S = await e.executor.query(g, {
336
+ limit: a.limit,
337
+ offset: a.offset,
338
+ orderBy: a.orderBy
339
+ }), m = me({ result: S, meta: h, connectionFieldName: n });
316
340
  return {
317
- data: g,
318
- doctype: s.doctype,
319
- count: g.length
341
+ data: m,
342
+ doctype: a.doctype,
343
+ count: m.length
320
344
  };
321
345
  })
322
346
  )
@@ -326,51 +350,51 @@ const Se = (e) => {
326
350
  },
327
351
  Mutation: {
328
352
  plans: {
329
- stonecropAction(c, { $doctype: o, $action: l, $args: y }) {
330
- return b(
331
- w({
332
- doctype: o,
333
- action: l,
334
- actionArgs: y
353
+ stonecropAction(i, { $doctype: l, $action: y, $args: p }) {
354
+ return M(
355
+ R({
356
+ doctype: l,
357
+ action: y,
358
+ actionArgs: p
335
359
  }),
336
- async (f) => await Promise.all(
337
- f.map(async (d) => {
338
- const p = h(d.doctype);
339
- if (!p)
360
+ async (d) => await Promise.all(
361
+ d.map(async (f) => {
362
+ const u = k(f.doctype);
363
+ if (!u)
340
364
  return {
341
365
  success: !1,
342
366
  data: null,
343
- error: `Unknown doctype: ${d.doctype}`
367
+ error: `Unknown doctype: ${f.doctype}`
344
368
  };
345
- const s = p.workflow?.actions?.[d.action];
346
- if (!s)
369
+ const a = u.workflow?.actions?.[f.action];
370
+ if (!a)
347
371
  return {
348
372
  success: !1,
349
373
  data: null,
350
- error: `Unknown action: ${d.action} on ${d.doctype}`
374
+ error: `Unknown action: ${f.action} on ${f.doctype}`
351
375
  };
352
- const m = s.handler, S = v(m);
353
- if (!S)
376
+ const h = a.handler, g = Z(h);
377
+ if (!g)
354
378
  return {
355
379
  success: !1,
356
380
  data: null,
357
- error: `Handler not registered: ${m}`
381
+ error: `Handler not registered: ${h}`
358
382
  };
359
- const N = {
360
- doctype: p,
383
+ const S = {
384
+ doctype: u,
361
385
  executor: e.executor
362
386
  };
363
387
  try {
364
388
  return {
365
389
  success: !0,
366
- data: await S(d.actionArgs ?? [], N),
390
+ data: await g(f.actionArgs ?? [], S),
367
391
  error: null
368
392
  };
369
- } catch (g) {
393
+ } catch (m) {
370
394
  return {
371
395
  success: !1,
372
396
  data: null,
373
- error: g instanceof Error ? g.message : String(g)
397
+ error: m instanceof Error ? m.message : String(m)
374
398
  };
375
399
  }
376
400
  })
@@ -382,81 +406,260 @@ const Se = (e) => {
382
406
  }
383
407
  }));
384
408
  };
385
- function G(e) {
386
- const t = J.singular(e);
387
- return `${j(t)}ById`;
409
+ function ie(e) {
410
+ const t = j.singular(e);
411
+ return `${H(t)}ById`;
388
412
  }
389
- function z(e) {
413
+ function ce(e) {
390
414
  return `all${A(e)}`;
391
415
  }
392
- function Y(e) {
416
+ function se(e) {
393
417
  return `${A(e)}OrderBy`;
394
418
  }
395
- function K(e) {
419
+ function ae(e) {
396
420
  return "id";
397
421
  }
398
- function X(e) {
422
+ function le(e) {
399
423
  return "UUID!";
400
424
  }
401
- const Z = /* @__PURE__ */ new Set(["Link"]);
425
+ function F(e) {
426
+ const t = j.plural(e.target.replace(/-/g, "_")), n = A(t), c = e.backlink || e.doctype, o = c.charAt(0).toUpperCase() + c.slice(1);
427
+ return `${n}By${o}Id`;
428
+ }
429
+ function B(e) {
430
+ return e === "noneOrMany" || e === "atLeastOne";
431
+ }
432
+ const q = 50, de = /* @__PURE__ */ new Set(["Link"]);
402
433
  function D(e) {
403
- return e.fields.filter((t) => !Z.has(t.fieldtype)).map((t) => t.fieldname).join(`
434
+ return e.fields.filter((t) => !de.has(t.fieldtype)).map((t) => t.fieldname).join(`
404
435
  `);
405
436
  }
406
- function ee(e, t, r, i) {
407
- const n = D(e), a = t(e.tableName), c = r(e.tableName), o = i(e.tableName);
437
+ function T(e) {
438
+ return e.fetch !== void 0 ? e.fetch : B(e.cardinality) ? { method: "sync", limit: q } : { method: "lazy" };
439
+ }
440
+ function fe(e) {
441
+ return e.blockWorkflows !== void 0 ? e.blockWorkflows : T(e).method === "sync";
442
+ }
443
+ function C(e) {
444
+ const { links: t, meta: n, includeSet: c, getMeta: o, seen: r, depth: s, maxDepth: i, reverseConnectionNameFn: l } = e;
445
+ if (i !== void 0 && s >= i) return "";
446
+ const y = [];
447
+ for (const [p, d] of Object.entries(t)) {
448
+ if (i !== void 0 && s >= i) break;
449
+ const f = fe(d), u = d.blockWorkflows === !0;
450
+ if (c && !c.has(p) && !u)
451
+ continue;
452
+ const a = T(d);
453
+ if (f === !1 || a.method !== "sync" && !u)
454
+ continue;
455
+ const g = o(d.target);
456
+ if (!g) continue;
457
+ const S = r.has(d.target);
458
+ S || r.add(d.target);
459
+ const m = D(g);
460
+ let w = "";
461
+ if (!S && g.links && g.tableName && (i === void 0 || s + 1 < i)) {
462
+ const $ = C({
463
+ links: g.links,
464
+ meta: g,
465
+ includeSet: null,
466
+ getMeta: o,
467
+ seen: r,
468
+ depth: s + 1,
469
+ maxDepth: i,
470
+ reverseConnectionNameFn: l
471
+ });
472
+ $ && (w = `
473
+ ` + $), r.delete(d.target);
474
+ }
475
+ const v = m + w;
476
+ if (B(d.cardinality)) {
477
+ const $ = {
478
+ doctype: n.slug || n.name,
479
+ linkName: p,
480
+ backlink: d.backlink,
481
+ target: d.target
482
+ }, L = l ? l($) : F($), x = a.method === "sync" && a.limit !== void 0 ? `first: ${a.limit}` : a.method === "sync" ? `first: ${q}` : "";
483
+ y.push(`
484
+ ${L}${x ? `(${x})` : ""} {
485
+ nodes {
486
+ ${v}
487
+ }
488
+ }`);
489
+ } else
490
+ y.push(`
491
+ ${p} {
492
+ ${v}
493
+ }`);
494
+ }
495
+ return y.join("");
496
+ }
497
+ function ue(e, t, n, c, o, r, s) {
498
+ const i = t(e.tableName), l = n(e.tableName), y = c(e.tableName), p = /* @__PURE__ */ new Set([e.slug || e.name]);
499
+ let d = D(e);
500
+ if (r?.includeNested && e.links) {
501
+ const f = Array.isArray(r.includeNested) ? new Set(r.includeNested) : null, u = C({
502
+ links: e.links,
503
+ meta: e,
504
+ includeSet: f,
505
+ getMeta: o,
506
+ seen: p,
507
+ depth: 0,
508
+ maxDepth: r.maxDepth,
509
+ reverseConnectionNameFn: s
510
+ });
511
+ u && (d += `
512
+ ` + u);
513
+ }
408
514
  return `
409
- query GetRecord($${c}: ${o}) {
410
- ${a}(${c}: $${c}) {
411
- ${n}
515
+ query GetRecord($${l}: ${y}) {
516
+ ${i}(${l}: $${l}) {
517
+ ${d}
412
518
  }
413
519
  }
414
520
  `;
415
521
  }
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(", ")})` : "";
522
+ function pe(e, t, n, c) {
523
+ const o = D(e), r = n(e.tableName), s = c(e.tableName), i = [], l = [];
524
+ t.limit && (i.push("$limit: Int"), l.push("first: $limit")), t.offset && (i.push("$offset: Int"), l.push("offset: $offset")), t.orderBy && (i.push(`$orderBy: [${s}!]`), l.push("orderBy: $orderBy"));
525
+ const y = i.length > 0 ? `(${i.join(", ")})` : "", p = l.length > 0 ? `(${l.join(", ")})` : "";
420
526
  return `
421
527
  query GetRecords${y} {
422
- ${a}${f} {
528
+ ${r}${p} {
423
529
  nodes {
424
- ${n}
530
+ ${o}
425
531
  }
426
532
  }
427
533
  }
428
534
  `;
429
535
  }
430
- function re(e, t, r) {
431
- const i = r(t.tableName);
432
- return e[i];
536
+ function ye(e) {
537
+ const { record: t, meta: n, getMeta: c, reverseConnectionNameFn: o } = e;
538
+ if (!n.links) return t;
539
+ const r = { ...t };
540
+ for (const [s, i] of Object.entries(n.links))
541
+ if (B(i.cardinality)) {
542
+ if (!c(i.target)) continue;
543
+ const p = {
544
+ doctype: n.slug || n.name,
545
+ linkName: s,
546
+ backlink: i.backlink || s,
547
+ target: i.target
548
+ }, d = o ? o(p) : F(p), f = r[d];
549
+ f?.nodes ? (r[s] = f.nodes, delete r[d]) : (r[s] = [], delete r[d]);
550
+ }
551
+ return r;
552
+ }
553
+ function ge(e) {
554
+ const { result: t, meta: n, recordFieldName: c } = e, o = c(n.tableName);
555
+ return t[o];
433
556
  }
434
- function ne(e, t, r) {
435
- const i = r(t.tableName);
436
- return e[i]?.nodes ?? [];
557
+ function me(e) {
558
+ const { result: t, meta: n, connectionFieldName: c } = e, o = c(n.tableName);
559
+ return t[o]?.nodes ?? [];
437
560
  }
561
+ const ve = J`
562
+ type StonecropFieldMeta {
563
+ fieldname: String!
564
+ fieldtype: String!
565
+ component: String
566
+ label: String
567
+ width: String
568
+ align: String
569
+ required: Boolean
570
+ readOnly: Boolean
571
+ edit: Boolean
572
+ hidden: Boolean
573
+ default: JSON
574
+ options: JSON
575
+ mask: String
576
+ precision: Int
577
+ scale: Int
578
+ mode: String
579
+ validation: JSON
580
+ }
581
+
582
+ type StonecropWorkflowAction {
583
+ label: String!
584
+ handler: String!
585
+ requiredFields: [String!]
586
+ allowedStates: [String!]
587
+ confirm: Boolean
588
+ args: JSON
589
+ }
590
+
591
+ type StonecropWorkflowMeta {
592
+ states: [String!]
593
+ actions: [StonecropWorkflowAction!]
594
+ }
595
+
596
+ type StonecropDoctypeMeta {
597
+ name: String!
598
+ slug: String
599
+ tableName: String
600
+ fields: [StonecropFieldMeta!]!
601
+ workflow: StonecropWorkflowMeta
602
+ inherits: String
603
+ }
604
+
605
+ type StonecropRecordResult {
606
+ data: JSON
607
+ doctype: String!
608
+ unknownLinks: [String!]
609
+ }
610
+
611
+ type StonecropRecordsResult {
612
+ data: [JSON!]!
613
+ doctype: String!
614
+ count: Int!
615
+ }
616
+
617
+ type StonecropActionResult {
618
+ success: Boolean!
619
+ data: JSON
620
+ error: String
621
+ }
622
+
623
+ extend type Query {
624
+ stonecropMeta(doctype: String!): StonecropDoctypeMeta
625
+ stonecropAllMeta: [StonecropDoctypeMeta!]!
626
+ stonecropRecord(doctype: String!, id: String!, options: JSON): StonecropRecordResult
627
+ stonecropRecords(doctype: String!, filters: JSON, orderBy: String, limit: Int, offset: Int): StonecropRecordsResult
628
+ }
629
+
630
+ extend type Mutation {
631
+ stonecropAction(doctype: String!, action: String!, args: JSON): StonecropActionResult!
632
+ }
633
+ `;
438
634
  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,
635
+ O as DoctypeValidationError,
636
+ de as RELATION_FIELDTYPES,
637
+ pe as buildListQuery,
638
+ ue as buildRecordQuery,
639
+ re as builtinHandlers,
640
+ Fe as clearHandlers,
641
+ Re as clearRegistry,
642
+ De as createStonecropPlugin,
643
+ ce as defaultConnectionFieldName,
644
+ se as defaultOrderByTypeName,
645
+ ae as defaultRecordArgName,
646
+ le as defaultRecordArgType,
647
+ ie as defaultRecordFieldName,
648
+ F as defaultReverseConnectionName,
649
+ me as extractListResult,
650
+ ge as extractSingleResult,
651
+ K as getAllMeta,
652
+ Z as getHandler,
653
+ k as getMeta,
654
+ Ae as hasHandler,
655
+ Me as hasMeta,
656
+ G as loadDoctypes,
657
+ be as loadDoctypesFromObject,
658
+ ye as mergeNestedResults,
457
659
  D as queryableFieldNames,
458
- ge as registerBuiltinHandlers,
459
- _ as registerHandler,
460
- ye as validateReferences
660
+ Be as registerBuiltinHandlers,
661
+ X as registerHandler,
662
+ ve as typeDefs,
663
+ Oe as validateReferences
461
664
  };
462
665
  //# sourceMappingURL=index.js.map