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