@stonecrop/graphql-middleware 0.13.0 → 0.13.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/README.md +0 -1
- package/dist/graphql-middleware.d.ts +7 -5
- package/dist/index.js +290 -274
- package/dist/index.js.map +1 -1
- package/dist/src/plugin/postgraphile.d.ts +7 -5
- package/dist/src/plugin/postgraphile.d.ts.map +1 -1
- package/dist/src/plugin/postgraphile.js +59 -24
- package/dist/src/typeDefs.d.ts.map +1 -1
- package/dist/src/typeDefs.js +0 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,133 +1,133 @@
|
|
|
1
|
-
import { readdirSync as
|
|
2
|
-
import { join as
|
|
3
|
-
import { validateDoctype as
|
|
4
|
-
import { sideEffectWithPgClient as
|
|
5
|
-
import { object as
|
|
6
|
-
import { gql as
|
|
7
|
-
import { PostGraphileAmberPreset as
|
|
8
|
-
import { makePgService as
|
|
9
|
-
const
|
|
10
|
-
class
|
|
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();
|
|
10
|
+
class V extends Error {
|
|
11
11
|
constructor(e, n) {
|
|
12
|
-
const
|
|
12
|
+
const o = n.map((r) => ` ${r.path.join(".")}: ${r.message}`).join(`
|
|
13
13
|
`);
|
|
14
14
|
super(`Invalid doctype definition in ${e}:
|
|
15
|
-
${
|
|
15
|
+
${o}`), this.file = e, this.errors = n, this.name = "DoctypeValidationError";
|
|
16
16
|
}
|
|
17
17
|
file;
|
|
18
18
|
errors;
|
|
19
19
|
}
|
|
20
|
-
function
|
|
21
|
-
const n =
|
|
22
|
-
for (const
|
|
23
|
-
const r =
|
|
24
|
-
|
|
20
|
+
function pe(t, e = {}) {
|
|
21
|
+
const n = te(t);
|
|
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);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
-
function
|
|
28
|
-
const n =
|
|
29
|
-
let
|
|
27
|
+
function ye(t, e) {
|
|
28
|
+
const n = oe(t, "utf-8");
|
|
29
|
+
let o;
|
|
30
30
|
try {
|
|
31
|
-
|
|
32
|
-
} catch (
|
|
33
|
-
const
|
|
31
|
+
o = JSON.parse(n);
|
|
32
|
+
} catch (c) {
|
|
33
|
+
const a = {
|
|
34
34
|
path: [],
|
|
35
|
-
message: `Invalid JSON: ${
|
|
35
|
+
message: `Invalid JSON: ${c instanceof Error ? c.message : String(c)}`
|
|
36
36
|
};
|
|
37
37
|
if (e.continueOnError) {
|
|
38
|
-
e.onError?.(t, [
|
|
38
|
+
e.onError?.(t, [a]);
|
|
39
39
|
return;
|
|
40
40
|
}
|
|
41
|
-
throw new
|
|
41
|
+
throw new V(t, [a]);
|
|
42
42
|
}
|
|
43
|
-
const r =
|
|
43
|
+
const r = Z(o);
|
|
44
44
|
if (!r.success) {
|
|
45
45
|
if (e.continueOnError) {
|
|
46
46
|
e.onError?.(t, r.errors);
|
|
47
47
|
return;
|
|
48
48
|
}
|
|
49
|
-
throw new
|
|
49
|
+
throw new V(t, r.errors);
|
|
50
50
|
}
|
|
51
|
-
const
|
|
52
|
-
|
|
51
|
+
const s = ee(o);
|
|
52
|
+
S.set(s.name, s);
|
|
53
53
|
}
|
|
54
|
-
function
|
|
55
|
-
for (const [n,
|
|
56
|
-
const r = { ...
|
|
57
|
-
if (!
|
|
54
|
+
function Ie(t, e = {}) {
|
|
55
|
+
for (const [n, o] of Object.entries(t)) {
|
|
56
|
+
const r = { ...o, name: n }, s = Z(r);
|
|
57
|
+
if (!s.success) {
|
|
58
58
|
if (e.continueOnError) {
|
|
59
|
-
e.onError?.(n,
|
|
59
|
+
e.onError?.(n, s.errors);
|
|
60
60
|
continue;
|
|
61
61
|
}
|
|
62
|
-
throw new
|
|
62
|
+
throw new V(n, s.errors);
|
|
63
63
|
}
|
|
64
|
-
const
|
|
65
|
-
|
|
64
|
+
const c = ee(r);
|
|
65
|
+
S.set(c.name, c);
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
|
-
function
|
|
69
|
-
const e =
|
|
68
|
+
function j(t) {
|
|
69
|
+
const e = S.get(t);
|
|
70
70
|
if (e) return e;
|
|
71
|
-
for (const n of
|
|
71
|
+
for (const n of S.values())
|
|
72
72
|
if (n.slug === t) return n;
|
|
73
73
|
}
|
|
74
|
-
function
|
|
75
|
-
return Array.from(
|
|
74
|
+
function ge() {
|
|
75
|
+
return Array.from(S.values());
|
|
76
76
|
}
|
|
77
|
-
function
|
|
78
|
-
return
|
|
77
|
+
function Be(t) {
|
|
78
|
+
return S.has(t);
|
|
79
79
|
}
|
|
80
|
-
function
|
|
81
|
-
|
|
80
|
+
function Le() {
|
|
81
|
+
S.clear();
|
|
82
82
|
}
|
|
83
|
-
function
|
|
83
|
+
function Pe() {
|
|
84
84
|
const t = [];
|
|
85
|
-
for (const e of
|
|
86
|
-
e.inherits && !
|
|
85
|
+
for (const e of S.values()) {
|
|
86
|
+
e.inherits && !S.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" && (
|
|
91
|
+
n.fieldtype === "Link" && typeof n.options == "string" && (S.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
|
|
99
|
-
function
|
|
100
|
-
|
|
98
|
+
const H = /* @__PURE__ */ new Map();
|
|
99
|
+
function me(t, e) {
|
|
100
|
+
H.set(t, e);
|
|
101
101
|
}
|
|
102
|
-
function
|
|
103
|
-
return
|
|
102
|
+
function he(t) {
|
|
103
|
+
return H.get(t);
|
|
104
104
|
}
|
|
105
|
-
function
|
|
106
|
-
return
|
|
105
|
+
function Te(t) {
|
|
106
|
+
return H.has(t);
|
|
107
107
|
}
|
|
108
|
-
function
|
|
109
|
-
|
|
108
|
+
function He() {
|
|
109
|
+
H.clear();
|
|
110
110
|
}
|
|
111
|
-
const
|
|
112
|
-
const [n] = t, { doctype:
|
|
113
|
-
for (const
|
|
114
|
-
|
|
111
|
+
const Se = async (t, e) => {
|
|
112
|
+
const [n] = t, { doctype: o } = e, r = [];
|
|
113
|
+
for (const s of o.fields)
|
|
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
|
-
},
|
|
119
|
-
const [n] = t, { doctype:
|
|
120
|
-
for (const
|
|
121
|
-
const
|
|
122
|
-
if (
|
|
123
|
-
const
|
|
124
|
-
|
|
118
|
+
}, $e = async (t, e) => {
|
|
119
|
+
const [n] = t, { doctype: o } = e, r = [];
|
|
120
|
+
for (const s of o.fields) {
|
|
121
|
+
const c = n[s.fieldname];
|
|
122
|
+
if (c == null) continue;
|
|
123
|
+
const a = we(s, c);
|
|
124
|
+
a && r.push(a);
|
|
125
125
|
}
|
|
126
|
-
if (
|
|
127
|
-
for (const [
|
|
128
|
-
const
|
|
129
|
-
if (
|
|
130
|
-
|
|
126
|
+
if (o.links)
|
|
127
|
+
for (const [s, c] of Object.entries(o.links)) {
|
|
128
|
+
const a = n[s];
|
|
129
|
+
if (a == null) continue;
|
|
130
|
+
c.cardinality === "noneOrMany" || c.cardinality === "atLeastOne" ? Array.isArray(a) || r.push(`${s}: expected array, got ${typeof a}`) : (typeof a != "object" || Array.isArray(a)) && r.push(`${s}: expected object, got ${typeof a}`);
|
|
131
131
|
}
|
|
132
132
|
if (r.length > 0)
|
|
133
133
|
throw new Error(`Field type validation failed:
|
|
@@ -135,9 +135,9 @@ ${r.join(`
|
|
|
135
135
|
`)}`);
|
|
136
136
|
return { valid: !0 };
|
|
137
137
|
};
|
|
138
|
-
function
|
|
139
|
-
const { fieldname: n, fieldtype:
|
|
140
|
-
switch (
|
|
138
|
+
function we(t, e) {
|
|
139
|
+
const { fieldname: n, fieldtype: o } = t;
|
|
140
|
+
switch (o) {
|
|
141
141
|
case "Int":
|
|
142
142
|
if (typeof e != "number" || !Number.isInteger(e))
|
|
143
143
|
return `${n}: expected integer, got ${typeof e}`;
|
|
@@ -173,26 +173,26 @@ function ye(t, e) {
|
|
|
173
173
|
}
|
|
174
174
|
return null;
|
|
175
175
|
}
|
|
176
|
-
const
|
|
177
|
-
validateRequiredFields:
|
|
178
|
-
validateFieldTypes:
|
|
179
|
-
noop:
|
|
176
|
+
const ke = async () => ({ ok: !0 }), Ee = {
|
|
177
|
+
validateRequiredFields: Se,
|
|
178
|
+
validateFieldTypes: $e,
|
|
179
|
+
noop: ke
|
|
180
180
|
};
|
|
181
|
-
function
|
|
182
|
-
for (const [t, e] of Object.entries(
|
|
183
|
-
|
|
181
|
+
function Je() {
|
|
182
|
+
for (const [t, e] of Object.entries(Ee))
|
|
183
|
+
me(t, e);
|
|
184
184
|
}
|
|
185
|
-
const
|
|
186
|
-
function
|
|
187
|
-
|
|
185
|
+
const Y = /* @__PURE__ */ new Map();
|
|
186
|
+
function We(t, e) {
|
|
187
|
+
Y.set(t, e);
|
|
188
188
|
}
|
|
189
|
-
function
|
|
190
|
-
return
|
|
189
|
+
function be(t) {
|
|
190
|
+
return Y.get(t);
|
|
191
191
|
}
|
|
192
|
-
function
|
|
193
|
-
|
|
192
|
+
function qe() {
|
|
193
|
+
Y.clear();
|
|
194
194
|
}
|
|
195
|
-
const
|
|
195
|
+
const Me = le`
|
|
196
196
|
type StonecropFieldMeta {
|
|
197
197
|
fieldname: String!
|
|
198
198
|
fieldtype: String!
|
|
@@ -230,7 +230,6 @@ const Se = oe`
|
|
|
230
230
|
type StonecropDoctypeMeta {
|
|
231
231
|
name: String!
|
|
232
232
|
slug: String
|
|
233
|
-
tableName: String
|
|
234
233
|
fields: [StonecropFieldMeta!]!
|
|
235
234
|
workflow: StonecropWorkflowMeta
|
|
236
235
|
inherits: String
|
|
@@ -271,155 +270,163 @@ const Se = oe`
|
|
|
271
270
|
extend type Mutation {
|
|
272
271
|
stonecropAction(doctype: String!, action: String!, args: JSON): StonecropActionResult!
|
|
273
272
|
}
|
|
274
|
-
`,
|
|
275
|
-
const e = t.
|
|
276
|
-
return
|
|
277
|
-
const
|
|
278
|
-
if (!
|
|
273
|
+
`, _e = (t = {}) => {
|
|
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) => {
|
|
276
|
+
const o = n.input?.pgRegistry?.pgExecutors, r = o ? Object.values(o)[0] : void 0;
|
|
277
|
+
if (!r)
|
|
279
278
|
throw new Error("StonecropPlugin: no pgExecutors found — ensure pgServices is configured");
|
|
280
279
|
return {
|
|
281
|
-
typeDefs:
|
|
280
|
+
typeDefs: Me,
|
|
282
281
|
objects: {
|
|
283
282
|
Query: {
|
|
284
283
|
plans: {
|
|
285
|
-
stonecropMeta(
|
|
286
|
-
return
|
|
284
|
+
stonecropMeta(s, { $doctype: c }) {
|
|
285
|
+
return ae(c, (a) => j(a) ?? null);
|
|
287
286
|
},
|
|
288
287
|
stonecropAllMeta() {
|
|
289
|
-
return
|
|
288
|
+
return ce(ge());
|
|
290
289
|
},
|
|
291
|
-
stonecropRecord(
|
|
292
|
-
return
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
async (
|
|
290
|
+
stonecropRecord(s, { $doctype: c, $id: a, $options: $ }) {
|
|
291
|
+
return K(
|
|
292
|
+
r,
|
|
293
|
+
Q({ doctype: c, id: a, options: $ }),
|
|
294
|
+
async (E, y) => {
|
|
296
295
|
const g = /* @__PURE__ */ new Map();
|
|
297
|
-
for (let
|
|
298
|
-
const
|
|
299
|
-
g.has(
|
|
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);
|
|
300
299
|
}
|
|
301
|
-
const
|
|
302
|
-
for (const [
|
|
303
|
-
const
|
|
304
|
-
if (!
|
|
305
|
-
for (const p of
|
|
300
|
+
const m = new Array(y.length);
|
|
301
|
+
for (const [i, l] of g) {
|
|
302
|
+
const d = j(i);
|
|
303
|
+
if (!d) {
|
|
304
|
+
for (const p of l) m[p] = { data: null, doctype: i };
|
|
306
305
|
continue;
|
|
307
306
|
}
|
|
308
|
-
const
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
307
|
+
const D = X(d);
|
|
308
|
+
if (!D) {
|
|
309
|
+
for (const p of l) m[p] = { data: null, doctype: i };
|
|
310
|
+
continue;
|
|
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 };
|
|
316
320
|
continue;
|
|
317
321
|
}
|
|
318
|
-
const
|
|
319
|
-
if (
|
|
320
|
-
for (const [
|
|
321
|
-
const
|
|
322
|
-
if (!(
|
|
323
|
-
if (
|
|
324
|
-
const
|
|
325
|
-
|
|
322
|
+
const u = { ...f }, h = y[p].options ?? {}, R = h.includeNested === !0, b = Array.isArray(h.includeNested) ? new Set(h.includeNested) : null;
|
|
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));
|
|
326
330
|
continue;
|
|
327
331
|
}
|
|
328
|
-
if (
|
|
329
|
-
const
|
|
330
|
-
if (!
|
|
331
|
-
const
|
|
332
|
-
if (
|
|
333
|
-
if (!
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
332
|
+
if (q !== "sync" && !b?.has(M)) continue;
|
|
333
|
+
const N = j(k.target);
|
|
334
|
+
if (!N) continue;
|
|
335
|
+
const G = U(N);
|
|
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, {
|
|
343
|
+
text: O,
|
|
344
|
+
values: T
|
|
340
345
|
});
|
|
341
|
-
|
|
346
|
+
u[M] = _;
|
|
342
347
|
} else {
|
|
343
|
-
if (!
|
|
344
|
-
const
|
|
345
|
-
if (
|
|
346
|
-
|
|
348
|
+
if (!k.fieldname) continue;
|
|
349
|
+
const A = u[k.fieldname];
|
|
350
|
+
if (A == null) {
|
|
351
|
+
u[M] = null;
|
|
347
352
|
continue;
|
|
348
353
|
}
|
|
349
|
-
const
|
|
350
|
-
|
|
351
|
-
|
|
354
|
+
const O = X(N);
|
|
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`,
|
|
358
|
+
values: [A]
|
|
352
359
|
});
|
|
353
|
-
|
|
360
|
+
u[M] = _[0] ?? null;
|
|
354
361
|
}
|
|
355
362
|
}
|
|
356
|
-
const
|
|
357
|
-
|
|
358
|
-
data:
|
|
359
|
-
doctype:
|
|
360
|
-
unknownLinks:
|
|
363
|
+
const L = b && d.links ? [...b].filter((M) => !(M in d.links)) : void 0;
|
|
364
|
+
m[p] = {
|
|
365
|
+
data: u,
|
|
366
|
+
doctype: i,
|
|
367
|
+
unknownLinks: L?.length ? L : void 0
|
|
361
368
|
};
|
|
362
369
|
}
|
|
363
370
|
}
|
|
364
|
-
return
|
|
371
|
+
return m;
|
|
365
372
|
}
|
|
366
373
|
);
|
|
367
374
|
},
|
|
368
|
-
stonecropRecords(
|
|
369
|
-
return
|
|
370
|
-
|
|
371
|
-
|
|
375
|
+
stonecropRecords(s, { $doctype: c, $filters: a, $orderBy: $, $limit: E, $offset: y }) {
|
|
376
|
+
return K(
|
|
377
|
+
r,
|
|
378
|
+
Q({
|
|
372
379
|
doctype: c,
|
|
373
|
-
filters:
|
|
374
|
-
orderBy:
|
|
375
|
-
limit:
|
|
376
|
-
offset:
|
|
380
|
+
filters: a,
|
|
381
|
+
orderBy: $,
|
|
382
|
+
limit: E,
|
|
383
|
+
offset: y
|
|
377
384
|
}),
|
|
378
|
-
async (g,
|
|
379
|
-
|
|
380
|
-
const
|
|
381
|
-
if (!
|
|
382
|
-
return { data: [], doctype:
|
|
383
|
-
const
|
|
384
|
-
if (
|
|
385
|
-
for (const [
|
|
386
|
-
if (!
|
|
387
|
-
throw new Error(`Unknown filter field: ${
|
|
388
|
-
|
|
385
|
+
async (g, m) => await Promise.all(
|
|
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 = [];
|
|
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}`);
|
|
389
396
|
}
|
|
390
|
-
const
|
|
391
|
-
let
|
|
392
|
-
if (
|
|
393
|
-
const
|
|
394
|
-
if (
|
|
395
|
-
throw new Error(`Invalid orderBy format: "${
|
|
396
|
-
const
|
|
397
|
-
if (
|
|
398
|
-
throw new Error(`Invalid orderBy direction: "${
|
|
399
|
-
const
|
|
400
|
-
if (!
|
|
401
|
-
throw new Error(`Unknown orderBy field: "${
|
|
402
|
-
|
|
397
|
+
const J = C.length > 0 ? ` WHERE ${C.join(" AND ")}` : "";
|
|
398
|
+
let B = "";
|
|
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}`;
|
|
403
410
|
}
|
|
404
|
-
let
|
|
405
|
-
|
|
406
|
-
const { rows:
|
|
407
|
-
text: `SELECT ${
|
|
408
|
-
values:
|
|
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
|
|
409
416
|
});
|
|
410
|
-
let
|
|
411
|
-
if (
|
|
412
|
-
const
|
|
413
|
-
if (
|
|
414
|
-
for (const [
|
|
415
|
-
|
|
416
|
-
const
|
|
417
|
-
text: `SELECT COUNT(*) AS __count FROM
|
|
418
|
-
values:
|
|
417
|
+
let F = p.length;
|
|
418
|
+
if (i.limit != null || i.offset != null) {
|
|
419
|
+
const f = [], u = [];
|
|
420
|
+
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
|
|
419
426
|
});
|
|
420
|
-
|
|
427
|
+
F = parseInt(R[0]?.__count ?? "0", 10);
|
|
421
428
|
}
|
|
422
|
-
return { data:
|
|
429
|
+
return { data: p, doctype: i.doctype, count: F };
|
|
423
430
|
})
|
|
424
431
|
)
|
|
425
432
|
);
|
|
@@ -428,40 +435,40 @@ const Se = oe`
|
|
|
428
435
|
},
|
|
429
436
|
Mutation: {
|
|
430
437
|
plans: {
|
|
431
|
-
stonecropAction(
|
|
432
|
-
return
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
async (
|
|
436
|
-
const g =
|
|
438
|
+
stonecropAction(s, { $doctype: c, $action: a, $args: $ }) {
|
|
439
|
+
return se(
|
|
440
|
+
r,
|
|
441
|
+
Q({ doctype: c, action: a, actionArgs: $ }),
|
|
442
|
+
async (E, y) => {
|
|
443
|
+
const g = j(y.doctype);
|
|
437
444
|
if (!g)
|
|
438
445
|
return {
|
|
439
446
|
success: !1,
|
|
440
447
|
data: null,
|
|
441
|
-
error: `Unknown doctype: ${
|
|
448
|
+
error: `Unknown doctype: ${y.doctype}`
|
|
442
449
|
};
|
|
443
|
-
const
|
|
444
|
-
if (!
|
|
450
|
+
const m = g.workflow?.actions?.[y.action];
|
|
451
|
+
if (!m)
|
|
445
452
|
return {
|
|
446
453
|
success: !1,
|
|
447
454
|
data: null,
|
|
448
|
-
error: `Unknown action: ${
|
|
455
|
+
error: `Unknown action: ${y.action} on ${y.doctype}`
|
|
449
456
|
};
|
|
450
|
-
const
|
|
451
|
-
if (!
|
|
457
|
+
const i = he(m.handler);
|
|
458
|
+
if (!i)
|
|
452
459
|
return {
|
|
453
460
|
success: !1,
|
|
454
461
|
data: null,
|
|
455
|
-
error: `Handler not registered: ${
|
|
462
|
+
error: `Handler not registered: ${m.handler}`
|
|
456
463
|
};
|
|
457
|
-
const
|
|
464
|
+
const l = { doctype: g, pgClient: E };
|
|
458
465
|
try {
|
|
459
|
-
return { success: !0, data: await
|
|
460
|
-
} catch (
|
|
466
|
+
return { success: !0, data: await i(y.actionArgs ?? [], l), error: null };
|
|
467
|
+
} catch (d) {
|
|
461
468
|
return {
|
|
462
469
|
success: !1,
|
|
463
470
|
data: null,
|
|
464
|
-
error:
|
|
471
|
+
error: d instanceof Error ? d.message : String(d)
|
|
465
472
|
};
|
|
466
473
|
}
|
|
467
474
|
}
|
|
@@ -473,39 +480,48 @@ const Se = oe`
|
|
|
473
480
|
};
|
|
474
481
|
});
|
|
475
482
|
};
|
|
476
|
-
function
|
|
477
|
-
const
|
|
483
|
+
function U(t) {
|
|
484
|
+
const e = /* @__PURE__ */ new Set();
|
|
478
485
|
if (t.links)
|
|
479
|
-
for (const [o,
|
|
480
|
-
|
|
481
|
-
const
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
+
for (const [o, r] of Object.entries(t.links))
|
|
487
|
+
e.add(r.fieldname ?? o);
|
|
488
|
+
const n = [];
|
|
489
|
+
for (const o of t.fields) {
|
|
490
|
+
if (o.fieldtype === "Display" || o.fieldtype === "Link" && e.has(o.fieldname)) continue;
|
|
491
|
+
const r = x(o.fieldname);
|
|
492
|
+
n.push(r !== o.fieldname ? `"${r}" AS "${o.fieldname}"` : `"${o.fieldname}"`);
|
|
493
|
+
}
|
|
494
|
+
return n.join(", ");
|
|
495
|
+
}
|
|
496
|
+
function X(t) {
|
|
497
|
+
return t.fields.find((e) => e.fieldtype === "PrimaryKey");
|
|
498
|
+
}
|
|
499
|
+
function I(t, e) {
|
|
500
|
+
const n = e?.[t] ?? ie(t), o = n.indexOf(".");
|
|
501
|
+
return o > 0 ? `"${n.slice(0, o)}"."${n.slice(o + 1)}"` : `"${n}"`;
|
|
486
502
|
}
|
|
487
|
-
const
|
|
488
|
-
const { logPlans: e = !0, logTiming: n = !1 } = t,
|
|
489
|
-
plan: (
|
|
490
|
-
e && console.log(`[@stonecrop/graphql-middleware] Building plan for ${r}.${
|
|
491
|
-
const
|
|
503
|
+
const Qe = (t = {}) => {
|
|
504
|
+
const { logPlans: e = !0, logTiming: n = !1 } = t, o = (r, s) => ({
|
|
505
|
+
plan: (c) => {
|
|
506
|
+
e && console.log(`[@stonecrop/graphql-middleware] Building plan for ${r}.${s}`);
|
|
507
|
+
const a = n ? Date.now() : 0, $ = c();
|
|
492
508
|
return n && console.log(
|
|
493
|
-
`[@stonecrop/graphql-middleware] Plan built for ${r}.${
|
|
509
|
+
`[@stonecrop/graphql-middleware] Plan built for ${r}.${s} in ${Date.now() - a}ms`
|
|
494
510
|
), $;
|
|
495
511
|
}
|
|
496
512
|
});
|
|
497
|
-
return
|
|
513
|
+
return fe({
|
|
498
514
|
Query: {
|
|
499
|
-
stonecropRecord:
|
|
500
|
-
stonecropRecords:
|
|
501
|
-
stonecropMeta:
|
|
502
|
-
stonecropAllMeta:
|
|
515
|
+
stonecropRecord: o("Query", "stonecropRecord"),
|
|
516
|
+
stonecropRecords: o("Query", "stonecropRecords"),
|
|
517
|
+
stonecropMeta: o("Query", "stonecropMeta"),
|
|
518
|
+
stonecropAllMeta: o("Query", "stonecropAllMeta")
|
|
503
519
|
},
|
|
504
520
|
Mutation: {
|
|
505
|
-
stonecropAction:
|
|
521
|
+
stonecropAction: o("Mutation", "stonecropAction")
|
|
506
522
|
}
|
|
507
523
|
});
|
|
508
|
-
},
|
|
524
|
+
}, Oe = {
|
|
509
525
|
name: "StonecropFieldCasingPlugin",
|
|
510
526
|
version: "0.0.0",
|
|
511
527
|
inflection: {
|
|
@@ -515,33 +531,33 @@ const He = (t = {}) => {
|
|
|
515
531
|
}
|
|
516
532
|
}
|
|
517
533
|
}
|
|
518
|
-
},
|
|
519
|
-
extends: [
|
|
520
|
-
plugins: t?.fieldCasing === "pascal" ? [
|
|
521
|
-
}),
|
|
534
|
+
}, Re = (t) => ({
|
|
535
|
+
extends: [ue],
|
|
536
|
+
plugins: t?.fieldCasing === "pascal" ? [Oe] : []
|
|
537
|
+
}), Ue = Re();
|
|
522
538
|
export {
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
539
|
+
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
|
|
546
562
|
};
|
|
547
563
|
//# sourceMappingURL=index.js.map
|