@stonecrop/graphql-middleware 0.13.4 → 0.13.6

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,12 +1,12 @@
1
- import { readdirSync as te, statSync as ne, readFileSync as oe } from "node:fs";
2
- import { join as re } from "node:path";
3
- import { validateDoctype as Z, parseDoctype as ee, camelToSnake as x, pascalToSnake as ie } from "@stonecrop/schema";
4
- import { sideEffectWithPgClient as se, loadOneWithPgClient as K } from "@dataplan/pg";
5
- import { object as Q, constant as ce, lambda as ae } from "postgraphile/grafast";
6
- import { gql as le, extendSchema as de, wrapPlans as fe } from "postgraphile/utils";
7
- import { PostGraphileAmberPreset as ue } from "postgraphile/presets/amber";
8
- import { makePgService as ze } from "postgraphile/adaptors/pg";
9
- const S = /* @__PURE__ */ new Map();
1
+ import { readdirSync as ne, statSync as oe, readFileSync as re } from "node:fs";
2
+ import { join as ie } from "node:path";
3
+ import { validateDoctype as ee, parseDoctype as te, camelToSnake as x, pascalToSnake as se } from "@stonecrop/schema";
4
+ import { sideEffectWithPgClient as ce, loadOneWithPgClient as X } from "@dataplan/pg";
5
+ import { object as Q, constant as ae, lambda as le } from "postgraphile/grafast";
6
+ import { gql as de, extendSchema as fe, wrapPlans as ue } from "postgraphile/utils";
7
+ import { PostGraphileAmberPreset as pe } from "postgraphile/presets/amber";
8
+ import { makePgService as Ke } from "postgraphile/adaptors/pg";
9
+ const $ = /* @__PURE__ */ new Map();
10
10
  class V extends Error {
11
11
  constructor(e, n) {
12
12
  const o = n.map((r) => ` ${r.path.join(".")}: ${r.message}`).join(`
@@ -17,15 +17,15 @@ ${o}`), this.file = e, this.errors = n, this.name = "DoctypeValidationError";
17
17
  file;
18
18
  errors;
19
19
  }
20
- function pe(t, e = {}) {
21
- const n = te(t);
20
+ function ye(t, e = {}) {
21
+ const n = ne(t);
22
22
  for (const o of n) {
23
- const r = re(t, o);
24
- ne(r).isDirectory() ? pe(r, e) : o.endsWith(".json") && ye(r, e);
23
+ const r = ie(t, o);
24
+ oe(r).isDirectory() ? ye(r, e) : o.endsWith(".json") && ge(r, e);
25
25
  }
26
26
  }
27
- function ye(t, e) {
28
- const n = oe(t, "utf-8");
27
+ function ge(t, e) {
28
+ const n = re(t, "utf-8");
29
29
  let o;
30
30
  try {
31
31
  o = JSON.parse(n);
@@ -40,7 +40,7 @@ function ye(t, e) {
40
40
  }
41
41
  throw new V(t, [a]);
42
42
  }
43
- const r = Z(o);
43
+ const r = ee(o);
44
44
  if (!r.success) {
45
45
  if (e.continueOnError) {
46
46
  e.onError?.(t, r.errors);
@@ -48,12 +48,12 @@ function ye(t, e) {
48
48
  }
49
49
  throw new V(t, r.errors);
50
50
  }
51
- const s = ee(o);
52
- S.set(s.name, s);
51
+ const s = te(o);
52
+ $.set(s.name, s);
53
53
  }
54
- function Ie(t, e = {}) {
54
+ function Le(t, e = {}) {
55
55
  for (const [n, o] of Object.entries(t)) {
56
- const r = { ...o, name: n }, s = Z(r);
56
+ const r = { ...typeof o == "object" && o !== null ? o : {}, name: n }, s = ee(r);
57
57
  if (!s.success) {
58
58
  if (e.continueOnError) {
59
59
  e.onError?.(n, s.errors);
@@ -61,66 +61,66 @@ function Ie(t, e = {}) {
61
61
  }
62
62
  throw new V(n, s.errors);
63
63
  }
64
- const c = ee(r);
65
- S.set(c.name, c);
64
+ const c = te(r);
65
+ $.set(c.name, c);
66
66
  }
67
67
  }
68
- function j(t) {
69
- const e = S.get(t);
68
+ function I(t) {
69
+ const e = $.get(t);
70
70
  if (e) return e;
71
- for (const n of S.values())
71
+ for (const n of $.values())
72
72
  if (n.slug === t) return n;
73
73
  }
74
- function ge() {
75
- return Array.from(S.values());
74
+ function me() {
75
+ return Array.from($.values());
76
76
  }
77
- function Be(t) {
78
- return S.has(t);
77
+ function Pe(t) {
78
+ return $.has(t);
79
79
  }
80
- function Le() {
81
- S.clear();
80
+ function Te() {
81
+ $.clear();
82
82
  }
83
- function Pe() {
83
+ function He() {
84
84
  const t = [];
85
- for (const e of S.values()) {
86
- e.inherits && !S.has(e.inherits) && t.push({
85
+ for (const e of $.values()) {
86
+ e.inherits && !$.has(e.inherits) && t.push({
87
87
  path: [e.name, "inherits"],
88
88
  message: `References unknown doctype: ${e.inherits}`
89
89
  });
90
90
  for (const n of e.fields)
91
- n.fieldtype === "Link" && typeof n.options == "string" && (S.has(n.options) || t.push({
91
+ n.fieldtype === "Link" && typeof n.options == "string" && ($.has(n.options) || t.push({
92
92
  path: [e.name, "fields", n.fieldname, "options"],
93
93
  message: `Link references unknown doctype: ${n.options}`
94
94
  }));
95
95
  }
96
96
  return t;
97
97
  }
98
- const H = /* @__PURE__ */ new Map();
99
- function me(t, e) {
100
- H.set(t, e);
98
+ const T = /* @__PURE__ */ new Map();
99
+ function he(t, e) {
100
+ T.set(t, e);
101
101
  }
102
- function he(t) {
103
- return H.get(t);
102
+ function Se(t) {
103
+ return T.get(t);
104
104
  }
105
- function Te(t) {
106
- return H.has(t);
105
+ function Je(t) {
106
+ return T.has(t);
107
107
  }
108
- function He() {
109
- H.clear();
108
+ function We() {
109
+ T.clear();
110
110
  }
111
- const Se = async (t, e) => {
111
+ const $e = async (t, e) => {
112
112
  const [n] = t, { doctype: o } = e, r = [];
113
113
  for (const s of o.fields)
114
114
  s.required && (n[s.fieldname] === void 0 || n[s.fieldname] === null) && r.push(s.label ?? s.fieldname);
115
115
  if (r.length > 0)
116
116
  throw new Error(`Missing required fields: ${r.join(", ")}`);
117
117
  return { valid: !0 };
118
- }, $e = async (t, e) => {
118
+ }, we = async (t, e) => {
119
119
  const [n] = t, { doctype: o } = e, r = [];
120
120
  for (const s of o.fields) {
121
121
  const c = n[s.fieldname];
122
122
  if (c == null) continue;
123
- const a = we(s, c);
123
+ const a = ke(s, c);
124
124
  a && r.push(a);
125
125
  }
126
126
  if (o.links)
@@ -135,7 +135,7 @@ ${r.join(`
135
135
  `)}`);
136
136
  return { valid: !0 };
137
137
  };
138
- function we(t, e) {
138
+ function ke(t, e) {
139
139
  const { fieldname: n, fieldtype: o } = t;
140
140
  switch (o) {
141
141
  case "Int":
@@ -173,26 +173,26 @@ function we(t, e) {
173
173
  }
174
174
  return null;
175
175
  }
176
- const ke = async () => ({ ok: !0 }), Ee = {
177
- validateRequiredFields: Se,
178
- validateFieldTypes: $e,
179
- noop: ke
176
+ const Ee = async () => ({ ok: !0 }), be = {
177
+ validateRequiredFields: $e,
178
+ validateFieldTypes: we,
179
+ noop: Ee
180
180
  };
181
- function Je() {
182
- for (const [t, e] of Object.entries(Ee))
183
- me(t, e);
181
+ function qe() {
182
+ for (const [t, e] of Object.entries(be))
183
+ he(t, e);
184
184
  }
185
185
  const Y = /* @__PURE__ */ new Map();
186
- function We(t, e) {
186
+ function _e(t, e) {
187
187
  Y.set(t, e);
188
188
  }
189
- function be(t) {
189
+ function Me(t) {
190
190
  return Y.get(t);
191
191
  }
192
- function qe() {
192
+ function Qe() {
193
193
  Y.clear();
194
194
  }
195
- const Me = le`
195
+ const Oe = de`
196
196
  type StonecropFieldMeta {
197
197
  fieldname: String!
198
198
  fieldtype: String!
@@ -270,101 +270,101 @@ const Me = le`
270
270
  extend type Mutation {
271
271
  stonecropAction(doctype: String!, action: String!, args: JSON): StonecropActionResult!
272
272
  }
273
- `, _e = (t = {}) => {
273
+ `, Ue = (t = {}) => {
274
274
  const e = t.debug ? (n, o) => (console.log(`[@stonecrop/graphql-middleware] ${o.text}`, o.values ?? []), n.query(o)) : (n, o) => n.query(o);
275
- return de((n) => {
275
+ return fe((n) => {
276
276
  const o = n.input?.pgRegistry?.pgExecutors, r = o ? Object.values(o)[0] : void 0;
277
277
  if (!r)
278
278
  throw new Error("StonecropPlugin: no pgExecutors found — ensure pgServices is configured");
279
279
  return {
280
- typeDefs: Me,
280
+ typeDefs: Oe,
281
281
  objects: {
282
282
  Query: {
283
283
  plans: {
284
284
  stonecropMeta(s, { $doctype: c }) {
285
- return ae(c, (a) => j(a) ?? null);
285
+ return le(c, (a) => I(String(a)) ?? null);
286
286
  },
287
287
  stonecropAllMeta() {
288
- return ce(ge());
288
+ return ae(me());
289
289
  },
290
- stonecropRecord(s, { $doctype: c, $id: a, $options: $ }) {
291
- return K(
290
+ stonecropRecord(s, { $doctype: c, $id: a, $options: w }) {
291
+ return X(
292
292
  r,
293
- Q({ doctype: c, id: a, options: $ }),
294
- async (E, y) => {
293
+ Q({ doctype: c, id: a, options: w }),
294
+ async (b, u) => {
295
295
  const g = /* @__PURE__ */ new Map();
296
- for (let i = 0; i < y.length; i++) {
297
- const l = y[i].doctype;
298
- g.has(l) || g.set(l, []), g.get(l).push(i);
296
+ for (let i = 0; i < u.length; i++) {
297
+ const p = String(u[i].doctype);
298
+ g.has(p) || g.set(p, []), g.get(p).push(i);
299
299
  }
300
- const m = new Array(y.length);
301
- for (const [i, l] of g) {
302
- const d = j(i);
300
+ const m = Array.from({ length: u.length });
301
+ for (const [i, p] of g) {
302
+ const d = I(i);
303
303
  if (!d) {
304
- for (const p of l) m[p] = { data: null, doctype: i };
304
+ for (const f of p) m[f] = { data: null, doctype: i };
305
305
  continue;
306
306
  }
307
- const D = X(d);
308
- if (!D) {
309
- for (const p of l) m[p] = { data: null, doctype: i };
307
+ const R = Z(d);
308
+ if (!R) {
309
+ for (const f of p) m[f] = { data: null, doctype: i };
310
310
  continue;
311
311
  }
312
- const w = x(D.fieldname), C = U(d), J = l.map((p) => y[p].id), { rows: B } = await e(E, {
313
- text: `SELECT ${C} FROM ${I(d.name, t.tables)} WHERE "${w}"::text = ANY($1::text[])`,
314
- values: [J]
315
- }), v = new Map(B.map((p) => [String(p[D.fieldname]), p]));
316
- for (const p of l) {
317
- const F = y[p].id, f = v.get(F);
318
- if (!f) {
319
- m[p] = { data: null, doctype: i };
312
+ const H = x(R.fieldname), k = U(d), D = p.map((f) => String(u[f].id)), { rows: J } = await e(b, {
313
+ text: `SELECT ${k} FROM ${B(d.name, t.tables)} WHERE "${H}"::text = ANY($1::text[])`,
314
+ values: [D]
315
+ }), L = new Map(J.map((f) => [String(f[R.fieldname]), f]));
316
+ for (const f of p) {
317
+ const C = String(u[f].id), v = L.get(C);
318
+ if (!v) {
319
+ m[f] = { data: null, doctype: i };
320
320
  continue;
321
321
  }
322
- const u = { ...f }, h = y[p].options ?? {}, R = h.includeNested === !0, b = Array.isArray(h.includeNested) ? new Set(h.includeNested) : null;
322
+ const l = { ...v }, y = u[f].options ?? {}, M = y.includeNested === !0, h = Array.isArray(y.includeNested) ? new Set(y.includeNested) : null;
323
323
  if (d.links)
324
- for (const [M, k] of Object.entries(d.links)) {
325
- const P = k.fetch, W = k.cardinality === "noneOrMany" || k.cardinality === "atLeastOne", q = P?.method ?? (W ? "sync" : "lazy"), z = P?.method === "sync" ? P.limit : W ? 50 : void 0;
326
- if (!(R || (b ? b.has(M) : q === "sync"))) continue;
327
- if (q === "custom") {
328
- const A = P.handler, O = be(A);
329
- O && (u[M] = await O(E, u, k));
324
+ for (const [S, E] of Object.entries(d.links)) {
325
+ const N = E.fetch, W = E.cardinality === "noneOrMany" || E.cardinality === "atLeastOne", q = N?.method ?? (W ? "sync" : "lazy"), G = N?.method === "sync" ? N.limit : W ? 50 : void 0;
326
+ if (!(M || (h ? h.has(S) : q === "sync"))) continue;
327
+ if (q === "custom" && N?.method === "custom") {
328
+ const A = N.handler, O = Me(A);
329
+ O && (l[S] = await O(b, l, E));
330
330
  continue;
331
331
  }
332
- if (q !== "sync" && !b?.has(M)) continue;
333
- const N = j(k.target);
334
- if (!N) continue;
335
- const G = U(N);
332
+ if (q !== "sync" && !h?.has(S)) continue;
333
+ const j = I(E.target);
334
+ if (!j) continue;
335
+ const K = U(j);
336
336
  if (W) {
337
- if (!k.backlink) continue;
338
- const A = x(k.backlink);
339
- let O = `SELECT ${G} FROM ${I(N.name, t.tables)} WHERE "${A}"::text = $1`;
340
- const T = [F];
341
- z != null && (O += " LIMIT $2", T.push(z));
342
- const { rows: _ } = await e(E, {
337
+ if (!E.backlink) continue;
338
+ const A = x(E.backlink);
339
+ let O = `SELECT ${K} FROM ${B(j.name, t.tables)} WHERE "${A}"::text = $1`;
340
+ const P = [C];
341
+ G != null && (O += " LIMIT $2", P.push(G));
342
+ const { rows: _ } = await e(b, {
343
343
  text: O,
344
- values: T
344
+ values: P
345
345
  });
346
- u[M] = _;
346
+ l[S] = _;
347
347
  } else {
348
- if (!k.fieldname) continue;
349
- const A = u[k.fieldname];
348
+ if (!E.fieldname) continue;
349
+ const A = l[E.fieldname];
350
350
  if (A == null) {
351
- u[M] = null;
351
+ l[S] = null;
352
352
  continue;
353
353
  }
354
- const O = X(N);
354
+ const O = Z(j);
355
355
  if (!O) continue;
356
- const T = x(O.fieldname), { rows: _ } = await e(E, {
357
- text: `SELECT ${G} FROM ${I(N.name, t.tables)} WHERE "${T}" = $1`,
356
+ const P = x(O.fieldname), { rows: _ } = await e(b, {
357
+ text: `SELECT ${K} FROM ${B(j.name, t.tables)} WHERE "${P}" = $1`,
358
358
  values: [A]
359
359
  });
360
- u[M] = _[0] ?? null;
360
+ l[S] = _[0] ?? null;
361
361
  }
362
362
  }
363
- const L = b && d.links ? [...b].filter((M) => !(M in d.links)) : void 0;
364
- m[p] = {
365
- data: u,
363
+ const F = h && d.links ? [...h].filter((S) => !(S in d.links)) : void 0;
364
+ m[f] = {
365
+ data: l,
366
366
  doctype: i,
367
- unknownLinks: L?.length ? L : void 0
367
+ unknownLinks: F?.length ? F : void 0
368
368
  };
369
369
  }
370
370
  }
@@ -372,61 +372,63 @@ const Me = le`
372
372
  }
373
373
  );
374
374
  },
375
- stonecropRecords(s, { $doctype: c, $filters: a, $orderBy: $, $limit: E, $offset: y }) {
376
- return K(
375
+ stonecropRecords(s, { $doctype: c, $filters: a, $orderBy: w, $limit: b, $offset: u }) {
376
+ return X(
377
377
  r,
378
378
  Q({
379
379
  doctype: c,
380
380
  filters: a,
381
- orderBy: $,
382
- limit: E,
383
- offset: y
381
+ orderBy: w,
382
+ limit: b,
383
+ offset: u
384
384
  }),
385
385
  async (g, m) => await Promise.all(
386
386
  m.map(async (i) => {
387
- const l = j(i.doctype);
388
- if (!l)
389
- return { data: [], doctype: i.doctype, count: 0 };
390
- const d = new Set(l.fields.map((f) => f.fieldname)), D = U(l), w = [], C = [];
387
+ const p = String(i.doctype), d = I(p);
388
+ if (!d)
389
+ return { data: [], doctype: p, count: 0 };
390
+ const R = new Set(z(d.fields).map((l) => l.fieldname)), H = U(d), k = [], D = [];
391
391
  if (i.filters != null)
392
- for (const [f, u] of Object.entries(i.filters)) {
393
- if (!d.has(f))
394
- throw new Error(`Unknown filter field: ${f} for doctype ${l.name}`);
395
- w.push(u), C.push(`"${x(f)}" = $${w.length}`);
392
+ for (const [l, y] of Object.entries(i.filters)) {
393
+ if (!R.has(l))
394
+ throw new Error(`Unknown filter field: ${l} for doctype ${d.name}`);
395
+ k.push(y), D.push(`"${x(l)}" = $${k.length}`);
396
396
  }
397
- const J = C.length > 0 ? ` WHERE ${C.join(" AND ")}` : "";
398
- let B = "";
397
+ const J = D.length > 0 ? ` WHERE ${D.join(" AND ")}` : "";
398
+ let L = "";
399
399
  if (i.orderBy != null) {
400
- const f = i.orderBy, u = f.lastIndexOf("_");
401
- if (u <= 0)
402
- throw new Error(`Invalid orderBy format: "${f}". Expected FIELD_ASC or FIELD_DESC`);
403
- const h = f.slice(u + 1).toUpperCase();
404
- if (h !== "ASC" && h !== "DESC")
405
- throw new Error(`Invalid orderBy direction: "${h}". Must be ASC or DESC`);
406
- const R = f.slice(0, u);
407
- if (!d.has(R))
408
- throw new Error(`Unknown orderBy field: "${R}" for doctype ${l.name}`);
409
- B = ` ORDER BY "${x(R)}" ${h}`;
400
+ if (typeof i.orderBy != "string")
401
+ throw new Error(`Invalid orderBy: expected string, got ${typeof i.orderBy}`);
402
+ const l = i.orderBy, y = l.lastIndexOf("_");
403
+ if (y <= 0)
404
+ throw new Error(`Invalid orderBy format: "${l}". Expected FIELD_ASC or FIELD_DESC`);
405
+ const M = l.slice(y + 1).toUpperCase();
406
+ if (M !== "ASC" && M !== "DESC")
407
+ throw new Error(`Invalid orderBy direction: "${M}". Must be ASC or DESC`);
408
+ const h = l.slice(0, y);
409
+ if (!R.has(h))
410
+ throw new Error(`Unknown orderBy field: "${h}" for doctype ${d.name}`);
411
+ L = ` ORDER BY "${x(h)}" ${M}`;
410
412
  }
411
- let v = "";
412
- i.limit != null && (w.push(i.limit), v += ` LIMIT $${w.length}`), i.offset != null && (w.push(i.offset), v += ` OFFSET $${w.length}`);
413
- const { rows: p } = await e(g, {
414
- text: `SELECT ${D} FROM ${I(l.name, t.tables)}${J}${B}${v}`,
415
- values: w
413
+ let f = "";
414
+ i.limit != null && (k.push(i.limit), f += ` LIMIT $${k.length}`), i.offset != null && (k.push(i.offset), f += ` OFFSET $${k.length}`);
415
+ const { rows: C } = await e(g, {
416
+ text: `SELECT ${H} FROM ${B(d.name, t.tables)}${J}${L}${f}`,
417
+ values: k
416
418
  });
417
- let F = p.length;
419
+ let v = C.length;
418
420
  if (i.limit != null || i.offset != null) {
419
- const f = [], u = [];
421
+ const l = [], y = [];
420
422
  if (i.filters != null)
421
- for (const [b, L] of Object.entries(i.filters))
422
- d.has(b) && (f.push(L), u.push(`"${x(b)}" = $${f.length}`));
423
- const h = u.length > 0 ? ` WHERE ${u.join(" AND ")}` : "", { rows: R } = await e(g, {
424
- text: `SELECT COUNT(*) AS __count FROM ${I(l.name, t.tables)}${h}`,
425
- values: f
423
+ for (const [F, S] of Object.entries(i.filters))
424
+ R.has(F) && (l.push(S), y.push(`"${x(F)}" = $${l.length}`));
425
+ const M = y.length > 0 ? ` WHERE ${y.join(" AND ")}` : "", { rows: h } = await e(g, {
426
+ text: `SELECT COUNT(*) AS row_count FROM ${B(d.name, t.tables)}${M}`,
427
+ values: l
426
428
  });
427
- F = parseInt(R[0]?.__count ?? "0", 10);
429
+ v = parseInt(h[0]?.row_count ?? "0", 10);
428
430
  }
429
- return { data: p, doctype: i.doctype, count: F };
431
+ return { data: C, doctype: p, count: v };
430
432
  })
431
433
  )
432
434
  );
@@ -435,35 +437,35 @@ const Me = le`
435
437
  },
436
438
  Mutation: {
437
439
  plans: {
438
- stonecropAction(s, { $doctype: c, $action: a, $args: $ }) {
439
- return se(
440
+ stonecropAction(s, { $doctype: c, $action: a, $args: w }) {
441
+ return ce(
440
442
  r,
441
- Q({ doctype: c, action: a, actionArgs: $ }),
442
- async (E, y) => {
443
- const g = j(y.doctype);
443
+ Q({ doctype: c, action: a, actionArgs: w }),
444
+ async (b, u) => {
445
+ const g = I(String(u.doctype));
444
446
  if (!g)
445
447
  return {
446
448
  success: !1,
447
449
  data: null,
448
- error: `Unknown doctype: ${y.doctype}`
450
+ error: `Unknown doctype: ${u.doctype}`
449
451
  };
450
- const m = g.workflow?.actions?.[y.action];
452
+ const m = g.workflow?.actions?.[u.action];
451
453
  if (!m)
452
454
  return {
453
455
  success: !1,
454
456
  data: null,
455
- error: `Unknown action: ${y.action} on ${y.doctype}`
457
+ error: `Unknown action: ${u.action} on ${u.doctype}`
456
458
  };
457
- const i = he(m.handler);
459
+ const i = Se(m.handler);
458
460
  if (!i)
459
461
  return {
460
462
  success: !1,
461
463
  data: null,
462
464
  error: `Handler not registered: ${m.handler}`
463
465
  };
464
- const l = { doctype: g, pgClient: E };
466
+ const p = { doctype: g, pgClient: b };
465
467
  try {
466
- return { success: !0, data: await i(y.actionArgs ?? [], l), error: null };
468
+ return { success: !0, data: await i(u.actionArgs ?? [], p), error: null };
467
469
  } catch (d) {
468
470
  return {
469
471
  success: !1,
@@ -480,37 +482,52 @@ const Me = le`
480
482
  };
481
483
  });
482
484
  };
483
- function U(t) {
484
- const e = /* @__PURE__ */ new Set();
485
- if (t.links)
486
- for (const [o, r] of Object.entries(t.links))
487
- e.add(r.fieldname ?? o);
485
+ function z(t) {
486
+ const e = [];
487
+ for (const n of t)
488
+ if (n.fieldtype === "Fieldset") {
489
+ const o = (n.schema ?? []).filter(
490
+ (r) => typeof r.fieldname == "string" && typeof r.fieldtype == "string"
491
+ );
492
+ e.push(...z(o));
493
+ } else
494
+ e.push(n);
495
+ return e;
496
+ }
497
+ function Re(t, e) {
488
498
  const n = [];
489
- for (const o of t.fields) {
499
+ for (const o of z(t)) {
490
500
  if (o.fieldtype === "Display" || o.fieldtype === "Link" && e.has(o.fieldname)) continue;
491
501
  const r = x(o.fieldname);
492
502
  n.push(r !== o.fieldname ? `"${r}" AS "${o.fieldname}"` : `"${o.fieldname}"`);
493
503
  }
494
- return n.join(", ");
504
+ return n;
505
+ }
506
+ function U(t) {
507
+ const e = /* @__PURE__ */ new Set();
508
+ if (t.links)
509
+ for (const [n, o] of Object.entries(t.links))
510
+ e.add(o.fieldname ?? n);
511
+ return Re(t.fields, e).join(", ");
495
512
  }
496
- function X(t) {
513
+ function Z(t) {
497
514
  return t.fields.find((e) => e.fieldtype === "PrimaryKey");
498
515
  }
499
- function I(t, e) {
500
- const n = e?.[t] ?? ie(t), o = n.indexOf(".");
516
+ function B(t, e) {
517
+ const n = e?.[t] ?? se(t), o = n.indexOf(".");
501
518
  return o > 0 ? `"${n.slice(0, o)}"."${n.slice(o + 1)}"` : `"${n}"`;
502
519
  }
503
- const Qe = (t = {}) => {
520
+ const Ve = (t = {}) => {
504
521
  const { logPlans: e = !0, logTiming: n = !1 } = t, o = (r, s) => ({
505
522
  plan: (c) => {
506
523
  e && console.log(`[@stonecrop/graphql-middleware] Building plan for ${r}.${s}`);
507
- const a = n ? Date.now() : 0, $ = c();
524
+ const a = n ? Date.now() : 0, w = c();
508
525
  return n && console.log(
509
526
  `[@stonecrop/graphql-middleware] Plan built for ${r}.${s} in ${Date.now() - a}ms`
510
- ), $;
527
+ ), w;
511
528
  }
512
529
  });
513
- return fe({
530
+ return ue({
514
531
  Query: {
515
532
  stonecropRecord: o("Query", "stonecropRecord"),
516
533
  stonecropRecords: o("Query", "stonecropRecords"),
@@ -521,7 +538,7 @@ const Qe = (t = {}) => {
521
538
  stonecropAction: o("Mutation", "stonecropAction")
522
539
  }
523
540
  });
524
- }, Oe = {
541
+ }, xe = {
525
542
  name: "StonecropFieldCasingPlugin",
526
543
  version: "0.0.0",
527
544
  inflection: {
@@ -531,33 +548,33 @@ const Qe = (t = {}) => {
531
548
  }
532
549
  }
533
550
  }
534
- }, Re = (t) => ({
535
- extends: [ue],
536
- plugins: t?.fieldCasing === "pascal" ? [Oe] : []
537
- }), Ue = Re();
551
+ }, Ae = (t) => ({
552
+ extends: [pe],
553
+ plugins: t?.fieldCasing === "pascal" ? [xe] : []
554
+ }), Ye = Ae();
538
555
  export {
539
556
  V as DoctypeValidationError,
540
- Ue as StonecropPreset,
541
- Ee as builtinHandlers,
542
- qe as clearFetchHandlers,
543
- He as clearHandlers,
544
- Le as clearRegistry,
545
- Qe as createDebugPlugin,
546
- _e as createStonecropPlugin,
547
- Re as createStonecropPreset,
548
- ge as getAllMeta,
549
- be as getFetchHandler,
550
- he as getHandler,
551
- j as getMeta,
552
- Te as hasHandler,
553
- Be as hasMeta,
554
- pe as loadDoctypes,
555
- Ie as loadDoctypesFromObject,
556
- ze as makePgService,
557
- Je as registerBuiltinHandlers,
558
- We as registerFetchHandler,
559
- me as registerHandler,
560
- Me as typeDefs,
561
- Pe as validateReferences
557
+ Ye as StonecropPreset,
558
+ be as builtinHandlers,
559
+ Qe as clearFetchHandlers,
560
+ We as clearHandlers,
561
+ Te as clearRegistry,
562
+ Ve as createDebugPlugin,
563
+ Ue as createStonecropPlugin,
564
+ Ae as createStonecropPreset,
565
+ me as getAllMeta,
566
+ Me as getFetchHandler,
567
+ Se as getHandler,
568
+ I as getMeta,
569
+ Je as hasHandler,
570
+ Pe as hasMeta,
571
+ ye as loadDoctypes,
572
+ Le as loadDoctypesFromObject,
573
+ Ke as makePgService,
574
+ qe as registerBuiltinHandlers,
575
+ _e as registerFetchHandler,
576
+ he as registerHandler,
577
+ Oe as typeDefs,
578
+ He as validateReferences
562
579
  };
563
580
  //# sourceMappingURL=index.js.map