@kyro-cms/core 0.9.8 → 0.9.9
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/api-handler-graphql.cjs +18 -13
- package/dist/api-handler-graphql.cjs.map +1 -1
- package/dist/api-handler-graphql.js +14 -9
- package/dist/api-handler-graphql.js.map +1 -1
- package/dist/api-handler-trpc.cjs +9 -9
- package/dist/api-handler-trpc.js +7 -7
- package/dist/api-handler.cjs +10 -10
- package/dist/api-handler.js +7 -7
- package/dist/{chunk-2GXALYKV.js → chunk-3AKCVYDB.js} +4 -109
- package/dist/chunk-3AKCVYDB.js.map +1 -0
- package/dist/{chunk-KGJRJ7CU.cjs → chunk-4OCMVGEY.cjs} +4 -110
- package/dist/chunk-4OCMVGEY.cjs.map +1 -0
- package/dist/{chunk-HA4MGAZJ.js → chunk-5RFGYEQ3.js} +109 -28
- package/dist/chunk-5RFGYEQ3.js.map +1 -0
- package/dist/chunk-5RPRLTTN.js +861 -0
- package/dist/chunk-5RPRLTTN.js.map +1 -0
- package/dist/{chunk-EQTMBMDR.js → chunk-BVUDFKKS.js} +16 -3
- package/dist/chunk-BVUDFKKS.js.map +1 -0
- package/dist/{chunk-OVZO4KB6.cjs → chunk-E4JZ73FY.cjs} +34 -25
- package/dist/chunk-E4JZ73FY.cjs.map +1 -0
- package/dist/{chunk-2CBCU5ML.cjs → chunk-IEIHABVR.cjs} +129 -48
- package/dist/chunk-IEIHABVR.cjs.map +1 -0
- package/dist/{chunk-Z7OH3MWT.cjs → chunk-LNTFRAHF.cjs} +110 -2
- package/dist/chunk-LNTFRAHF.cjs.map +1 -0
- package/dist/chunk-N4GN6Q3V.cjs +869 -0
- package/dist/chunk-N4GN6Q3V.cjs.map +1 -0
- package/dist/{chunk-65DSXNQL.js → chunk-OX6GOIPH.js} +4 -4
- package/dist/{chunk-65DSXNQL.js.map → chunk-OX6GOIPH.js.map} +1 -1
- package/dist/{chunk-PJ7F4TCC.cjs → chunk-OXYSOU6Z.cjs} +18 -5
- package/dist/chunk-OXYSOU6Z.cjs.map +1 -0
- package/dist/{chunk-ZFHTJ2YX.js → chunk-OYCICW4B.js} +16 -7
- package/dist/chunk-OYCICW4B.js.map +1 -0
- package/dist/{chunk-W3GT55TY.cjs → chunk-TQG7G7YI.cjs} +13 -13
- package/dist/{chunk-W3GT55TY.cjs.map → chunk-TQG7G7YI.cjs.map} +1 -1
- package/dist/{chunk-2Z7YAJBS.js → chunk-WDTTHKDS.js} +109 -3
- package/dist/chunk-WDTTHKDS.js.map +1 -0
- package/dist/cli/index.cjs +148 -14
- package/dist/cli/index.cjs.map +1 -1
- package/dist/cli/index.js +148 -14
- package/dist/cli/index.js.map +1 -1
- package/dist/graphql/index.cjs +7 -7
- package/dist/graphql/index.d.cts +21 -13
- package/dist/graphql/index.d.ts +21 -13
- package/dist/graphql/index.js +2 -2
- package/dist/index.cjs +70 -70
- package/dist/index.d.cts +6 -5
- package/dist/index.d.ts +6 -5
- package/dist/index.js +7 -7
- package/dist/rest/index.cjs +5 -5
- package/dist/rest/index.js +3 -3
- package/dist/trpc/index.cjs +12 -12
- package/dist/trpc/index.js +3 -3
- package/package.json +1 -1
- package/dist/chunk-2CBCU5ML.cjs.map +0 -1
- package/dist/chunk-2GXALYKV.js.map +0 -1
- package/dist/chunk-2Z7YAJBS.js.map +0 -1
- package/dist/chunk-6DURKQ6W.js +0 -1072
- package/dist/chunk-6DURKQ6W.js.map +0 -1
- package/dist/chunk-EQTMBMDR.js.map +0 -1
- package/dist/chunk-HA4MGAZJ.js.map +0 -1
- package/dist/chunk-KGJRJ7CU.cjs.map +0 -1
- package/dist/chunk-OVZO4KB6.cjs.map +0 -1
- package/dist/chunk-PF642V5O.cjs +0 -1080
- package/dist/chunk-PF642V5O.cjs.map +0 -1
- package/dist/chunk-PJ7F4TCC.cjs.map +0 -1
- package/dist/chunk-Z7OH3MWT.cjs.map +0 -1
- package/dist/chunk-ZFHTJ2YX.js.map +0 -1
package/dist/chunk-PF642V5O.cjs
DELETED
|
@@ -1,1080 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var chunkKGJRJ7CU_cjs = require('./chunk-KGJRJ7CU.cjs');
|
|
4
|
-
var graphql = require('graphql');
|
|
5
|
-
|
|
6
|
-
function toPascalCase(slug) {
|
|
7
|
-
return slug.split(/[-_]/).map((s) => s.charAt(0).toUpperCase() + s.slice(1)).join("");
|
|
8
|
-
}
|
|
9
|
-
function toCamelCase(slug) {
|
|
10
|
-
const p = toPascalCase(slug);
|
|
11
|
-
return p.charAt(0).toLowerCase() + p.slice(1);
|
|
12
|
-
}
|
|
13
|
-
function toPascalSingular(slug) {
|
|
14
|
-
const p = toPascalCase(slug);
|
|
15
|
-
if (p.endsWith("s") && p.length > 1) return p.slice(0, -1);
|
|
16
|
-
return p;
|
|
17
|
-
}
|
|
18
|
-
function toCamelSingular(slug) {
|
|
19
|
-
return toCamelCase(slug).endsWith("s") && slug.length > 1 ? toCamelCase(slug).slice(0, -1) : toCamelCase(slug);
|
|
20
|
-
}
|
|
21
|
-
var RelationLoader = class {
|
|
22
|
-
db;
|
|
23
|
-
tenantID;
|
|
24
|
-
user;
|
|
25
|
-
caches = /* @__PURE__ */ new Map();
|
|
26
|
-
pending = /* @__PURE__ */ new Map();
|
|
27
|
-
constructor(opts) {
|
|
28
|
-
this.db = opts.db;
|
|
29
|
-
this.tenantID = opts.tenantID;
|
|
30
|
-
this.user = opts.user;
|
|
31
|
-
}
|
|
32
|
-
getCacheKey(collection) {
|
|
33
|
-
return collection;
|
|
34
|
-
}
|
|
35
|
-
async load(collection, id) {
|
|
36
|
-
const key = this.getCacheKey(collection);
|
|
37
|
-
let cache = this.caches.get(key);
|
|
38
|
-
if (!cache) {
|
|
39
|
-
cache = /* @__PURE__ */ new Map();
|
|
40
|
-
this.caches.set(key, cache);
|
|
41
|
-
}
|
|
42
|
-
if (cache.has(id)) {
|
|
43
|
-
return cache.get(id);
|
|
44
|
-
}
|
|
45
|
-
let pendingSet = this.pending.get(key);
|
|
46
|
-
if (!pendingSet) {
|
|
47
|
-
pendingSet = /* @__PURE__ */ new Set();
|
|
48
|
-
this.pending.set(key, pendingSet);
|
|
49
|
-
}
|
|
50
|
-
pendingSet.add(id);
|
|
51
|
-
cache.set(id, void 0);
|
|
52
|
-
return void 0;
|
|
53
|
-
}
|
|
54
|
-
async flushAll() {
|
|
55
|
-
for (const [key, ids] of this.pending) {
|
|
56
|
-
if (ids.size === 0) continue;
|
|
57
|
-
const cache = this.caches.get(key);
|
|
58
|
-
const docs = await this.db.find({
|
|
59
|
-
collection: key,
|
|
60
|
-
where: { id: { in: Array.from(ids) } },
|
|
61
|
-
limit: ids.size,
|
|
62
|
-
tenantID: this.tenantID,
|
|
63
|
-
draft: !!this.user
|
|
64
|
-
});
|
|
65
|
-
for (const doc of docs.docs || []) {
|
|
66
|
-
if (doc && doc.id) {
|
|
67
|
-
cache.set(doc.id, doc);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
this.pending.clear();
|
|
72
|
-
}
|
|
73
|
-
resolveOne(collection, id) {
|
|
74
|
-
return this.caches.get(this.getCacheKey(collection))?.get(id);
|
|
75
|
-
}
|
|
76
|
-
static async withBatch(loader, fn) {
|
|
77
|
-
const result = await fn();
|
|
78
|
-
await loader.flushAll();
|
|
79
|
-
return result;
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
function gqlError(message, code) {
|
|
83
|
-
const err = new Error(message);
|
|
84
|
-
err.extensions = { code };
|
|
85
|
-
return err;
|
|
86
|
-
}
|
|
87
|
-
function depthLimit(maxDepth) {
|
|
88
|
-
return (validationContext) => {
|
|
89
|
-
let depth = 0;
|
|
90
|
-
return {
|
|
91
|
-
Field() {
|
|
92
|
-
depth++;
|
|
93
|
-
if (depth > maxDepth) {
|
|
94
|
-
validationContext.reportError(
|
|
95
|
-
new graphql.GraphQLError(`Query exceeds max depth of ${maxDepth}`)
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
},
|
|
99
|
-
leaveField() {
|
|
100
|
-
depth--;
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
async function checkAccess(config, operation, context) {
|
|
106
|
-
const result = await chunkKGJRJ7CU_cjs.checkCollectionAccess(config, operation, context);
|
|
107
|
-
if (!result.allowed) {
|
|
108
|
-
throw gqlError(result.error || "Access denied", "FORBIDDEN");
|
|
109
|
-
}
|
|
110
|
-
return result.extraWhere;
|
|
111
|
-
}
|
|
112
|
-
async function checkGqlGlobalAccess(config, operation, context) {
|
|
113
|
-
const result = await chunkKGJRJ7CU_cjs.checkGlobalAccess(config, operation, context);
|
|
114
|
-
if (!result.allowed) {
|
|
115
|
-
throw gqlError(result.error || "Access denied", "FORBIDDEN");
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
function fieldToGraphQLType(field, registry, collectionTypes, isInputType = false) {
|
|
119
|
-
switch (field.type) {
|
|
120
|
-
case "text":
|
|
121
|
-
case "email":
|
|
122
|
-
case "password":
|
|
123
|
-
case "textarea":
|
|
124
|
-
case "color":
|
|
125
|
-
case "code":
|
|
126
|
-
case "markdown":
|
|
127
|
-
case "date":
|
|
128
|
-
case "select":
|
|
129
|
-
case "radio":
|
|
130
|
-
case "upload":
|
|
131
|
-
return graphql.GraphQLString;
|
|
132
|
-
case "number":
|
|
133
|
-
return field.integer ? graphql.GraphQLInt : graphql.GraphQLFloat;
|
|
134
|
-
case "checkbox":
|
|
135
|
-
return graphql.GraphQLBoolean;
|
|
136
|
-
case "json":
|
|
137
|
-
case "richtext":
|
|
138
|
-
return graphql.GraphQLString;
|
|
139
|
-
case "relationship":
|
|
140
|
-
if (typeof field.relationTo === "string") {
|
|
141
|
-
if (isInputType) {
|
|
142
|
-
if (field.hasMany) {
|
|
143
|
-
return new graphql.GraphQLList(graphql.GraphQLString);
|
|
144
|
-
}
|
|
145
|
-
return graphql.GraphQLString;
|
|
146
|
-
}
|
|
147
|
-
if (collectionTypes?.[field.relationTo]) {
|
|
148
|
-
const refType = collectionTypes[field.relationTo];
|
|
149
|
-
if (field.hasMany) {
|
|
150
|
-
return new graphql.GraphQLList(refType);
|
|
151
|
-
}
|
|
152
|
-
return refType;
|
|
153
|
-
}
|
|
154
|
-
const relatedCollection = registry.getCollection(field.relationTo);
|
|
155
|
-
if (relatedCollection) {
|
|
156
|
-
const refType = new graphql.GraphQLObjectType({
|
|
157
|
-
name: toPascalSingular(field.relationTo),
|
|
158
|
-
fields: () => ({
|
|
159
|
-
id: { type: graphql.GraphQLString },
|
|
160
|
-
...buildFieldsFromCollection(relatedCollection, registry, collectionTypes)
|
|
161
|
-
})
|
|
162
|
-
});
|
|
163
|
-
if (collectionTypes) {
|
|
164
|
-
collectionTypes[field.relationTo] = refType;
|
|
165
|
-
}
|
|
166
|
-
if (field.hasMany) {
|
|
167
|
-
return new graphql.GraphQLList(refType);
|
|
168
|
-
}
|
|
169
|
-
return refType;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
return field.hasMany ? new graphql.GraphQLList(graphql.GraphQLString) : graphql.GraphQLString;
|
|
173
|
-
case "array": {
|
|
174
|
-
if (isInputType) {
|
|
175
|
-
const arrFields2 = field.fields;
|
|
176
|
-
if (arrFields2 && arrFields2.length > 0) {
|
|
177
|
-
const arrTypeName = `${field.name?.replace(/-/g, "_") || "array"}_input_item`;
|
|
178
|
-
return new graphql.GraphQLList(fieldsToGraphQLInputObjectType(arrTypeName, arrFields2, registry));
|
|
179
|
-
}
|
|
180
|
-
return new graphql.GraphQLList(graphql.GraphQLString);
|
|
181
|
-
}
|
|
182
|
-
const arrFields = field.fields;
|
|
183
|
-
if (arrFields && arrFields.length > 0) {
|
|
184
|
-
const arrTypeName = `${field.name?.replace(/-/g, "_") || "array"}_item`;
|
|
185
|
-
const itemType = fieldsToGraphQLObjectType(arrTypeName, arrFields, registry, collectionTypes);
|
|
186
|
-
return new graphql.GraphQLList(itemType);
|
|
187
|
-
}
|
|
188
|
-
return new graphql.GraphQLList(graphql.GraphQLString);
|
|
189
|
-
}
|
|
190
|
-
case "group": {
|
|
191
|
-
if (isInputType) {
|
|
192
|
-
const groupFields2 = field.fields;
|
|
193
|
-
if (groupFields2 && groupFields2.length > 0) {
|
|
194
|
-
const groupTypeName = `${field.name?.replace(/-/g, "_") || "group"}_input_group`;
|
|
195
|
-
return fieldsToGraphQLInputObjectType(groupTypeName, groupFields2, registry);
|
|
196
|
-
}
|
|
197
|
-
return graphql.GraphQLString;
|
|
198
|
-
}
|
|
199
|
-
const groupFields = field.fields;
|
|
200
|
-
if (groupFields && groupFields.length > 0) {
|
|
201
|
-
const groupTypeName = `${field.name?.replace(/-/g, "_") || "group"}_group`;
|
|
202
|
-
return fieldsToGraphQLObjectType(groupTypeName, groupFields, registry, collectionTypes);
|
|
203
|
-
}
|
|
204
|
-
return graphql.GraphQLString;
|
|
205
|
-
}
|
|
206
|
-
case "blocks": {
|
|
207
|
-
if (isInputType) return graphql.GraphQLString;
|
|
208
|
-
const blocks = field.blocks;
|
|
209
|
-
if (blocks && blocks.length > 0) {
|
|
210
|
-
const blockTypes = [];
|
|
211
|
-
for (const block of blocks) {
|
|
212
|
-
const blockTypeName = `Block${toPascalCase(block.slug)}`;
|
|
213
|
-
const bType = new graphql.GraphQLObjectType({
|
|
214
|
-
name: blockTypeName,
|
|
215
|
-
fields: () => {
|
|
216
|
-
const out = {
|
|
217
|
-
blockType: { type: new graphql.GraphQLNonNull(graphql.GraphQLString) },
|
|
218
|
-
id: { type: graphql.GraphQLString }
|
|
219
|
-
};
|
|
220
|
-
if (block.fields) {
|
|
221
|
-
for (const bf of block.fields) {
|
|
222
|
-
if (!bf.name) continue;
|
|
223
|
-
const bfConfig = buildFieldConfig(bf, registry, collectionTypes);
|
|
224
|
-
if (bfConfig) out[bf.name] = bfConfig;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
return out;
|
|
228
|
-
}
|
|
229
|
-
});
|
|
230
|
-
blockTypes.push(bType);
|
|
231
|
-
}
|
|
232
|
-
if (blockTypes.length > 0) {
|
|
233
|
-
const unionName = `${field.name?.replace(/-/g, "_") || "blocks"}_union`;
|
|
234
|
-
return new graphql.GraphQLUnionType({
|
|
235
|
-
name: unionName,
|
|
236
|
-
types: blockTypes,
|
|
237
|
-
resolveType(value) {
|
|
238
|
-
const blockName = `Block${value?.blockType ? toPascalCase(value.blockType) : ""}`;
|
|
239
|
-
const blockType = blockTypes.find(
|
|
240
|
-
(bt) => bt.name === blockName
|
|
241
|
-
);
|
|
242
|
-
return blockType?.name || void 0;
|
|
243
|
-
}
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
return new graphql.GraphQLList(graphql.GraphQLString);
|
|
248
|
-
}
|
|
249
|
-
case "row":
|
|
250
|
-
case "collapsible":
|
|
251
|
-
case "tabs":
|
|
252
|
-
return graphql.GraphQLString;
|
|
253
|
-
default:
|
|
254
|
-
return graphql.GraphQLString;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
function buildFieldsFromCollection(config, registry, collectionTypes) {
|
|
258
|
-
const fields = {};
|
|
259
|
-
for (const field of config.fields) {
|
|
260
|
-
if (field.name && field.admin?.hidden !== true) {
|
|
261
|
-
const fc = buildFieldConfig(field, registry, collectionTypes);
|
|
262
|
-
if (fc) fields[field.name] = fc;
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
return fields;
|
|
266
|
-
}
|
|
267
|
-
function buildSortEnum(config) {
|
|
268
|
-
const sortFields = {
|
|
269
|
-
id: { value: "id" },
|
|
270
|
-
createdAt: { value: "createdAt" },
|
|
271
|
-
updatedAt: { value: "updatedAt" }
|
|
272
|
-
};
|
|
273
|
-
for (const field of config.fields) {
|
|
274
|
-
if (field.name) {
|
|
275
|
-
sortFields[field.name] = { value: field.name };
|
|
276
|
-
sortFields[`${field.name}_asc`] = { value: `${field.name}` };
|
|
277
|
-
sortFields[`${field.name}_desc`] = { value: `-${field.name}` };
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
return new graphql.GraphQLEnumType({
|
|
281
|
-
name: `${toPascalSingular(config.slug)}Sort`,
|
|
282
|
-
values: sortFields
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
function buildFieldFilterType(field, namePrefix) {
|
|
286
|
-
const safeName = `${namePrefix}_${field.name?.replace(/-/g, "_") || "field"}_filter`;
|
|
287
|
-
const stringOps = {
|
|
288
|
-
equals: { type: graphql.GraphQLString },
|
|
289
|
-
not_equals: { type: graphql.GraphQLString },
|
|
290
|
-
contains: { type: graphql.GraphQLString },
|
|
291
|
-
not_contains: { type: graphql.GraphQLString },
|
|
292
|
-
in: { type: new graphql.GraphQLList(graphql.GraphQLString) },
|
|
293
|
-
not_in: { type: new graphql.GraphQLList(graphql.GraphQLString) },
|
|
294
|
-
exists: { type: graphql.GraphQLBoolean }
|
|
295
|
-
};
|
|
296
|
-
const numberOps = {
|
|
297
|
-
equals: { type: graphql.GraphQLFloat },
|
|
298
|
-
not_equals: { type: graphql.GraphQLFloat },
|
|
299
|
-
greater_than: { type: graphql.GraphQLFloat },
|
|
300
|
-
less_than: { type: graphql.GraphQLFloat },
|
|
301
|
-
greater_than_equal: { type: graphql.GraphQLFloat },
|
|
302
|
-
less_than_equal: { type: graphql.GraphQLFloat },
|
|
303
|
-
in: { type: new graphql.GraphQLList(graphql.GraphQLFloat) },
|
|
304
|
-
not_in: { type: new graphql.GraphQLList(graphql.GraphQLFloat) },
|
|
305
|
-
exists: { type: graphql.GraphQLBoolean }
|
|
306
|
-
};
|
|
307
|
-
const boolOps = {
|
|
308
|
-
equals: { type: graphql.GraphQLBoolean },
|
|
309
|
-
exists: { type: graphql.GraphQLBoolean }
|
|
310
|
-
};
|
|
311
|
-
const dateOps = {
|
|
312
|
-
equals: { type: graphql.GraphQLString },
|
|
313
|
-
not_equals: { type: graphql.GraphQLString },
|
|
314
|
-
greater_than: { type: graphql.GraphQLString },
|
|
315
|
-
less_than: { type: graphql.GraphQLString },
|
|
316
|
-
exists: { type: graphql.GraphQLBoolean }
|
|
317
|
-
};
|
|
318
|
-
const relationOps = {
|
|
319
|
-
equals: { type: graphql.GraphQLString },
|
|
320
|
-
not_equals: { type: graphql.GraphQLString },
|
|
321
|
-
in: { type: new graphql.GraphQLList(graphql.GraphQLString) },
|
|
322
|
-
not_in: { type: new graphql.GraphQLList(graphql.GraphQLString) },
|
|
323
|
-
exists: { type: graphql.GraphQLBoolean }
|
|
324
|
-
};
|
|
325
|
-
const jsonOps = {
|
|
326
|
-
exists: { type: graphql.GraphQLBoolean }
|
|
327
|
-
};
|
|
328
|
-
let ops;
|
|
329
|
-
switch (field.type) {
|
|
330
|
-
case "text":
|
|
331
|
-
case "email":
|
|
332
|
-
case "password":
|
|
333
|
-
case "textarea":
|
|
334
|
-
case "color":
|
|
335
|
-
case "code":
|
|
336
|
-
case "markdown":
|
|
337
|
-
case "select":
|
|
338
|
-
case "radio":
|
|
339
|
-
case "upload":
|
|
340
|
-
ops = stringOps;
|
|
341
|
-
break;
|
|
342
|
-
case "number":
|
|
343
|
-
ops = numberOps;
|
|
344
|
-
break;
|
|
345
|
-
case "checkbox":
|
|
346
|
-
ops = boolOps;
|
|
347
|
-
break;
|
|
348
|
-
case "date":
|
|
349
|
-
ops = dateOps;
|
|
350
|
-
break;
|
|
351
|
-
case "relationship":
|
|
352
|
-
ops = relationOps;
|
|
353
|
-
break;
|
|
354
|
-
case "json":
|
|
355
|
-
case "richtext":
|
|
356
|
-
ops = jsonOps;
|
|
357
|
-
break;
|
|
358
|
-
default:
|
|
359
|
-
ops = { exists: { type: graphql.GraphQLBoolean } };
|
|
360
|
-
}
|
|
361
|
-
return new graphql.GraphQLInputObjectType({
|
|
362
|
-
name: safeName,
|
|
363
|
-
fields: ops
|
|
364
|
-
});
|
|
365
|
-
}
|
|
366
|
-
function buildCollectionFilterType(config, namePrefix) {
|
|
367
|
-
const fields = {
|
|
368
|
-
AND: { type: new graphql.GraphQLList(graphql.GraphQLString) },
|
|
369
|
-
OR: { type: new graphql.GraphQLList(graphql.GraphQLString) }
|
|
370
|
-
};
|
|
371
|
-
for (const field of config.fields) {
|
|
372
|
-
if (!field.name) continue;
|
|
373
|
-
const filterType = buildFieldFilterType(field, namePrefix);
|
|
374
|
-
if (filterType) {
|
|
375
|
-
fields[field.name] = { type: filterType };
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
return new graphql.GraphQLInputObjectType({
|
|
379
|
-
name: `${namePrefix}Filter`,
|
|
380
|
-
fields
|
|
381
|
-
});
|
|
382
|
-
}
|
|
383
|
-
function translateFilter(filter) {
|
|
384
|
-
if (!filter || typeof filter !== "object") return filter || {};
|
|
385
|
-
const result = {};
|
|
386
|
-
for (const [key, value] of Object.entries(filter)) {
|
|
387
|
-
if (key === "AND" || key === "OR") continue;
|
|
388
|
-
result[key] = value;
|
|
389
|
-
}
|
|
390
|
-
return result;
|
|
391
|
-
}
|
|
392
|
-
function fieldsToGraphQLInputObjectType(name, fields, registry) {
|
|
393
|
-
return new graphql.GraphQLInputObjectType({
|
|
394
|
-
name,
|
|
395
|
-
fields: () => {
|
|
396
|
-
const out = {};
|
|
397
|
-
for (const field of fields) {
|
|
398
|
-
if (!field.name) continue;
|
|
399
|
-
if (field.type === "row" || field.type === "collapsible") {
|
|
400
|
-
const subFields = field.fields;
|
|
401
|
-
if (subFields) {
|
|
402
|
-
for (const sf of subFields) {
|
|
403
|
-
if (!sf.name) continue;
|
|
404
|
-
out[sf.name] = { type: fieldToGraphQLType(sf, registry, void 0, true) };
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
} else if (field.type === "tabs") {
|
|
408
|
-
const tabs = field.tabs;
|
|
409
|
-
if (tabs) {
|
|
410
|
-
for (const tab of tabs) {
|
|
411
|
-
if (tab.fields) {
|
|
412
|
-
for (const sf of tab.fields) {
|
|
413
|
-
if (!sf.name) continue;
|
|
414
|
-
out[sf.name] = { type: fieldToGraphQLType(sf, registry, void 0, true) };
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
} else {
|
|
420
|
-
out[field.name] = { type: fieldToGraphQLType(field, registry, void 0, true) };
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
return out;
|
|
424
|
-
}
|
|
425
|
-
});
|
|
426
|
-
}
|
|
427
|
-
function fieldsToGraphQLObjectType(name, fields, registry, collectionTypes) {
|
|
428
|
-
return new graphql.GraphQLObjectType({
|
|
429
|
-
name,
|
|
430
|
-
fields: () => {
|
|
431
|
-
const out = {};
|
|
432
|
-
for (const field of fields) {
|
|
433
|
-
if (!field.name) continue;
|
|
434
|
-
if (field.type === "row" || field.type === "collapsible") {
|
|
435
|
-
const subFields = field.fields;
|
|
436
|
-
if (subFields) {
|
|
437
|
-
for (const sf of subFields) {
|
|
438
|
-
if (!sf.name) continue;
|
|
439
|
-
const fc = buildFieldConfig(sf, registry, collectionTypes);
|
|
440
|
-
if (fc) out[sf.name] = fc;
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
} else if (field.type === "tabs") {
|
|
444
|
-
const tabs = field.tabs;
|
|
445
|
-
if (tabs) {
|
|
446
|
-
for (const tab of tabs) {
|
|
447
|
-
if (tab.fields) {
|
|
448
|
-
for (const sf of tab.fields) {
|
|
449
|
-
if (!sf.name) continue;
|
|
450
|
-
const fc = buildFieldConfig(sf, registry, collectionTypes);
|
|
451
|
-
if (fc) out[sf.name] = fc;
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
} else {
|
|
457
|
-
const fc = buildFieldConfig(field, registry, collectionTypes);
|
|
458
|
-
if (fc) out[field.name] = fc;
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
return out;
|
|
462
|
-
}
|
|
463
|
-
});
|
|
464
|
-
}
|
|
465
|
-
function buildFieldConfig(field, registry, collectionTypes) {
|
|
466
|
-
if (!field.name) return null;
|
|
467
|
-
const deprecationReason = field.deprecated || void 0;
|
|
468
|
-
if (field.type === "relationship" && !field.hasMany) {
|
|
469
|
-
const gqlType = fieldToGraphQLType(field, registry, collectionTypes, false);
|
|
470
|
-
return {
|
|
471
|
-
type: field.required ? new graphql.GraphQLNonNull(gqlType) : gqlType,
|
|
472
|
-
description: field.admin?.description || field.label,
|
|
473
|
-
deprecationReason,
|
|
474
|
-
resolve: (source, args, ctx) => {
|
|
475
|
-
const loader = ctx?.relationLoader;
|
|
476
|
-
const relField = field;
|
|
477
|
-
const id = source?.[relField.name];
|
|
478
|
-
if (!id) return null;
|
|
479
|
-
if (loader && typeof id === "string") {
|
|
480
|
-
const cached = loader.resolveOne(relField.relationTo, id);
|
|
481
|
-
if (cached) return cached;
|
|
482
|
-
loader.load(relField.relationTo, id);
|
|
483
|
-
}
|
|
484
|
-
return { id };
|
|
485
|
-
}
|
|
486
|
-
};
|
|
487
|
-
}
|
|
488
|
-
if (field.type === "relationship" && field.hasMany) {
|
|
489
|
-
const gqlType = fieldToGraphQLType(field, registry, collectionTypes, false);
|
|
490
|
-
return {
|
|
491
|
-
type: gqlType,
|
|
492
|
-
description: field.admin?.description || field.label,
|
|
493
|
-
deprecationReason,
|
|
494
|
-
resolve: (source, args, ctx) => {
|
|
495
|
-
const loader = ctx?.relationLoader;
|
|
496
|
-
const relField = field;
|
|
497
|
-
const ids = source?.[relField.name] || [];
|
|
498
|
-
if (!ids.length) return [];
|
|
499
|
-
if (loader) {
|
|
500
|
-
const results = [];
|
|
501
|
-
for (const id of ids) {
|
|
502
|
-
const cached = loader.resolveOne(relField.relationTo, id);
|
|
503
|
-
if (cached) results.push(cached);
|
|
504
|
-
else {
|
|
505
|
-
loader.load(relField.relationTo, id);
|
|
506
|
-
results.push({ id });
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
return results;
|
|
510
|
-
}
|
|
511
|
-
return ids.map((id) => ({ id }));
|
|
512
|
-
}
|
|
513
|
-
};
|
|
514
|
-
}
|
|
515
|
-
return {
|
|
516
|
-
type: field.required ? new graphql.GraphQLNonNull(fieldToGraphQLType(field, registry, collectionTypes, false)) : fieldToGraphQLType(field, registry, collectionTypes, false),
|
|
517
|
-
description: field.admin?.description || field.label,
|
|
518
|
-
deprecationReason
|
|
519
|
-
};
|
|
520
|
-
}
|
|
521
|
-
function buildGraphQLSchema(options) {
|
|
522
|
-
const { registry, db, settings } = options;
|
|
523
|
-
const apiAccess = settings?.access?.apiAccess;
|
|
524
|
-
if (apiAccess?.graphqlEnabled === false) {
|
|
525
|
-
throw new Error("GraphQL API is disabled");
|
|
526
|
-
}
|
|
527
|
-
const collections = registry.getCollections();
|
|
528
|
-
const globals = registry.getGlobals();
|
|
529
|
-
const collectionTypes = {};
|
|
530
|
-
const collectionInputTypes = {};
|
|
531
|
-
for (const collection of collections) {
|
|
532
|
-
collectionTypes[collection.slug] = new graphql.GraphQLObjectType({
|
|
533
|
-
name: toPascalSingular(collection.slug),
|
|
534
|
-
fields: () => ({
|
|
535
|
-
id: { type: new graphql.GraphQLNonNull(graphql.GraphQLString) },
|
|
536
|
-
...buildFieldsFromCollection(collection, registry, collectionTypes),
|
|
537
|
-
...collection.timestamps ? {
|
|
538
|
-
createdAt: { type: graphql.GraphQLString },
|
|
539
|
-
updatedAt: { type: graphql.GraphQLString }
|
|
540
|
-
} : {},
|
|
541
|
-
...collection.tenantScoped ? {
|
|
542
|
-
tenantID: { type: graphql.GraphQLString }
|
|
543
|
-
} : {}
|
|
544
|
-
})
|
|
545
|
-
});
|
|
546
|
-
const inputFields = {};
|
|
547
|
-
for (const field of collection.fields) {
|
|
548
|
-
if (field.name && field.name !== "id") {
|
|
549
|
-
inputFields[field.name] = {
|
|
550
|
-
type: fieldToGraphQLType(field, registry, collectionTypes, true)
|
|
551
|
-
};
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
collectionInputTypes[collection.slug] = new graphql.GraphQLInputObjectType({
|
|
555
|
-
name: `${toPascalSingular(collection.slug)}Input`,
|
|
556
|
-
fields: () => inputFields
|
|
557
|
-
});
|
|
558
|
-
}
|
|
559
|
-
const globalTypes = {};
|
|
560
|
-
const globalInputTypes = {};
|
|
561
|
-
for (const global of globals) {
|
|
562
|
-
globalTypes[global.slug] = new graphql.GraphQLObjectType({
|
|
563
|
-
name: toPascalCase(global.slug),
|
|
564
|
-
fields: () => ({
|
|
565
|
-
id: { type: graphql.GraphQLString },
|
|
566
|
-
...buildFieldsFromCollection(
|
|
567
|
-
{ slug: global.slug, fields: global.fields },
|
|
568
|
-
registry,
|
|
569
|
-
collectionTypes
|
|
570
|
-
)
|
|
571
|
-
})
|
|
572
|
-
});
|
|
573
|
-
globalInputTypes[global.slug] = new graphql.GraphQLInputObjectType({
|
|
574
|
-
name: `${toPascalCase(global.slug)}Input`,
|
|
575
|
-
fields: () => {
|
|
576
|
-
const inputFields = {};
|
|
577
|
-
for (const field of global.fields) {
|
|
578
|
-
if (field.name && field.name !== "id") {
|
|
579
|
-
inputFields[field.name] = {
|
|
580
|
-
type: fieldToGraphQLType(field, registry, collectionTypes, true)
|
|
581
|
-
};
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
return inputFields;
|
|
585
|
-
}
|
|
586
|
-
});
|
|
587
|
-
}
|
|
588
|
-
const pageInfoType = new graphql.GraphQLObjectType({
|
|
589
|
-
name: "PageInfo",
|
|
590
|
-
fields: {
|
|
591
|
-
hasNextPage: { type: new graphql.GraphQLNonNull(graphql.GraphQLBoolean) },
|
|
592
|
-
hasPreviousPage: { type: new graphql.GraphQLNonNull(graphql.GraphQLBoolean) },
|
|
593
|
-
startCursor: { type: graphql.GraphQLString },
|
|
594
|
-
endCursor: { type: graphql.GraphQLString }
|
|
595
|
-
}
|
|
596
|
-
});
|
|
597
|
-
const queryFields = {};
|
|
598
|
-
for (const collection of collections) {
|
|
599
|
-
const type = collectionTypes[collection.slug];
|
|
600
|
-
if (!type) continue;
|
|
601
|
-
const filterType = buildCollectionFilterType(collection, toPascalSingular(collection.slug));
|
|
602
|
-
const sortEnum = buildSortEnum(collection);
|
|
603
|
-
queryFields[toCamelCase(collection.slug)] = {
|
|
604
|
-
type: new graphql.GraphQLObjectType({
|
|
605
|
-
name: `${toPascalSingular(collection.slug)}FindResult`,
|
|
606
|
-
fields: {
|
|
607
|
-
docs: { type: new graphql.GraphQLList(type) },
|
|
608
|
-
totalDocs: { type: graphql.GraphQLInt },
|
|
609
|
-
page: { type: graphql.GraphQLInt },
|
|
610
|
-
totalPages: { type: graphql.GraphQLInt },
|
|
611
|
-
hasNextPage: { type: graphql.GraphQLBoolean },
|
|
612
|
-
hasPrevPage: { type: graphql.GraphQLBoolean }
|
|
613
|
-
}
|
|
614
|
-
}),
|
|
615
|
-
args: {
|
|
616
|
-
where: { type: filterType },
|
|
617
|
-
sort: { type: sortEnum || graphql.GraphQLString },
|
|
618
|
-
limit: { type: graphql.GraphQLInt },
|
|
619
|
-
page: { type: graphql.GraphQLInt },
|
|
620
|
-
draft: { type: graphql.GraphQLBoolean }
|
|
621
|
-
},
|
|
622
|
-
resolve: async (_, args, ctx) => {
|
|
623
|
-
const { user: ctxUser, req: ctxReq, tenantID: ctxTenant, apiKey: ctxApiKey, db: ctxDb, relationLoader: ctxLoader } = ctx;
|
|
624
|
-
const extraWhere = await checkAccess(collection, "read", {
|
|
625
|
-
user: ctxUser,
|
|
626
|
-
req: ctxReq,
|
|
627
|
-
tenantID: ctxTenant,
|
|
628
|
-
apiKey: ctxApiKey
|
|
629
|
-
});
|
|
630
|
-
if (ctxTenant) {
|
|
631
|
-
ctxDb.setTenantContext({ tenantId: ctxTenant, userId: ctxUser?.id ?? "", role: ctxUser?.role, isSuperAdmin: ctxUser?.role === "super_admin" });
|
|
632
|
-
}
|
|
633
|
-
let where = {};
|
|
634
|
-
if (args.where) {
|
|
635
|
-
if (typeof args.where === "string") {
|
|
636
|
-
try {
|
|
637
|
-
where = JSON.parse(args.where);
|
|
638
|
-
} catch {
|
|
639
|
-
}
|
|
640
|
-
} else {
|
|
641
|
-
where = translateFilter(args.where);
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
if (extraWhere) {
|
|
645
|
-
where = { ...where, ...extraWhere };
|
|
646
|
-
}
|
|
647
|
-
const isDraft = args.draft ?? !!ctxUser;
|
|
648
|
-
return RelationLoader.withBatch(ctxLoader, async () => {
|
|
649
|
-
return ctxDb.find({
|
|
650
|
-
collection: collection.slug,
|
|
651
|
-
where,
|
|
652
|
-
sort: args.sort,
|
|
653
|
-
limit: args.limit || 10,
|
|
654
|
-
page: args.page || 1,
|
|
655
|
-
tenantID: ctxTenant,
|
|
656
|
-
draft: isDraft
|
|
657
|
-
});
|
|
658
|
-
});
|
|
659
|
-
}
|
|
660
|
-
};
|
|
661
|
-
queryFields[toCamelSingular(collection.slug)] = {
|
|
662
|
-
type,
|
|
663
|
-
args: {
|
|
664
|
-
id: { type: new graphql.GraphQLNonNull(graphql.GraphQLString) },
|
|
665
|
-
draft: { type: graphql.GraphQLBoolean }
|
|
666
|
-
},
|
|
667
|
-
resolve: async (_, args, ctx) => {
|
|
668
|
-
const { user: ctxUser, req: ctxReq, tenantID: ctxTenant, apiKey: ctxApiKey, db: ctxDb, relationLoader: ctxLoader } = ctx;
|
|
669
|
-
await checkAccess(collection, "read", {
|
|
670
|
-
user: ctxUser,
|
|
671
|
-
req: ctxReq,
|
|
672
|
-
tenantID: ctxTenant,
|
|
673
|
-
apiKey: ctxApiKey
|
|
674
|
-
});
|
|
675
|
-
if (ctxTenant) {
|
|
676
|
-
ctxDb.setTenantContext({ tenantId: ctxTenant, userId: ctxUser?.id ?? "", role: ctxUser?.role, isSuperAdmin: ctxUser?.role === "super_admin" });
|
|
677
|
-
}
|
|
678
|
-
const isDraft = args.draft ?? !!ctxUser;
|
|
679
|
-
return RelationLoader.withBatch(ctxLoader, async () => {
|
|
680
|
-
const doc = await ctxDb.findByID({
|
|
681
|
-
collection: collection.slug,
|
|
682
|
-
id: args.id,
|
|
683
|
-
tenantID: ctxTenant,
|
|
684
|
-
draft: isDraft
|
|
685
|
-
});
|
|
686
|
-
return doc;
|
|
687
|
-
});
|
|
688
|
-
}
|
|
689
|
-
};
|
|
690
|
-
queryFields[`count${toPascalSingular(collection.slug)}s`] = {
|
|
691
|
-
type: new graphql.GraphQLObjectType({
|
|
692
|
-
name: `${toPascalSingular(collection.slug)}Count`,
|
|
693
|
-
fields: {
|
|
694
|
-
totalDocs: { type: graphql.GraphQLInt }
|
|
695
|
-
}
|
|
696
|
-
}),
|
|
697
|
-
args: {
|
|
698
|
-
where: { type: graphql.GraphQLString }
|
|
699
|
-
},
|
|
700
|
-
resolve: async (_, args, ctx) => {
|
|
701
|
-
const { user: ctxUser, req: ctxReq, tenantID: ctxTenant, apiKey: ctxApiKey, db: ctxDb } = ctx;
|
|
702
|
-
await checkAccess(collection, "read", {
|
|
703
|
-
user: ctxUser,
|
|
704
|
-
req: ctxReq,
|
|
705
|
-
tenantID: ctxTenant,
|
|
706
|
-
apiKey: ctxApiKey
|
|
707
|
-
});
|
|
708
|
-
let where = {};
|
|
709
|
-
if (args.where) {
|
|
710
|
-
if (typeof args.where === "string") {
|
|
711
|
-
try {
|
|
712
|
-
where = JSON.parse(args.where);
|
|
713
|
-
} catch {
|
|
714
|
-
}
|
|
715
|
-
} else {
|
|
716
|
-
where = translateFilter(args.where);
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
const count = await ctxDb.count({
|
|
720
|
-
collection: collection.slug,
|
|
721
|
-
where,
|
|
722
|
-
tenantID: ctxTenant
|
|
723
|
-
});
|
|
724
|
-
return { totalDocs: count };
|
|
725
|
-
}
|
|
726
|
-
};
|
|
727
|
-
const edgeType = new graphql.GraphQLObjectType({
|
|
728
|
-
name: `${toPascalSingular(collection.slug)}Edge`,
|
|
729
|
-
fields: {
|
|
730
|
-
node: { type },
|
|
731
|
-
cursor: { type: new graphql.GraphQLNonNull(graphql.GraphQLString) }
|
|
732
|
-
}
|
|
733
|
-
});
|
|
734
|
-
const connectionType = new graphql.GraphQLObjectType({
|
|
735
|
-
name: `${toPascalSingular(collection.slug)}Connection`,
|
|
736
|
-
fields: {
|
|
737
|
-
edges: { type: new graphql.GraphQLList(edgeType) },
|
|
738
|
-
pageInfo: { type: new graphql.GraphQLNonNull(pageInfoType) },
|
|
739
|
-
totalCount: { type: graphql.GraphQLInt }
|
|
740
|
-
}
|
|
741
|
-
});
|
|
742
|
-
queryFields[`${toCamelCase(collection.slug)}Connection`] = {
|
|
743
|
-
type: connectionType,
|
|
744
|
-
args: {
|
|
745
|
-
first: { type: graphql.GraphQLInt },
|
|
746
|
-
after: { type: graphql.GraphQLString },
|
|
747
|
-
last: { type: graphql.GraphQLInt },
|
|
748
|
-
before: { type: graphql.GraphQLString },
|
|
749
|
-
where: { type: filterType },
|
|
750
|
-
sort: { type: sortEnum || graphql.GraphQLString },
|
|
751
|
-
draft: { type: graphql.GraphQLBoolean }
|
|
752
|
-
},
|
|
753
|
-
resolve: async (_, args, ctx) => {
|
|
754
|
-
const { user: ctxUser, req: ctxReq, tenantID: ctxTenant, apiKey: ctxApiKey, db: ctxDb, relationLoader: ctxLoader } = ctx;
|
|
755
|
-
const extraWhere = await checkAccess(collection, "read", {
|
|
756
|
-
user: ctxUser,
|
|
757
|
-
req: ctxReq,
|
|
758
|
-
tenantID: ctxTenant,
|
|
759
|
-
apiKey: ctxApiKey
|
|
760
|
-
});
|
|
761
|
-
if (ctxTenant) {
|
|
762
|
-
ctxDb.setTenantContext({ tenantId: ctxTenant, userId: ctxUser?.id ?? "", role: ctxUser?.role, isSuperAdmin: ctxUser?.role === "super_admin" });
|
|
763
|
-
}
|
|
764
|
-
let where = {};
|
|
765
|
-
if (args.where) {
|
|
766
|
-
if (typeof args.where === "string") {
|
|
767
|
-
try {
|
|
768
|
-
where = JSON.parse(args.where);
|
|
769
|
-
} catch {
|
|
770
|
-
}
|
|
771
|
-
} else {
|
|
772
|
-
where = translateFilter(args.where);
|
|
773
|
-
}
|
|
774
|
-
}
|
|
775
|
-
if (extraWhere) where = { ...where, ...extraWhere };
|
|
776
|
-
const isDraft = args.draft ?? !!ctxUser;
|
|
777
|
-
return RelationLoader.withBatch(ctxLoader, async () => {
|
|
778
|
-
let page = 1;
|
|
779
|
-
let limit = args.first || args.last || 10;
|
|
780
|
-
if (args.after) {
|
|
781
|
-
try {
|
|
782
|
-
const afterIdx = parseInt(Buffer.from(args.after, "base64").toString("utf-8"), 10);
|
|
783
|
-
if (!isNaN(afterIdx)) page = Math.floor(afterIdx / limit) + 1;
|
|
784
|
-
} catch {
|
|
785
|
-
}
|
|
786
|
-
} else if (args.before) {
|
|
787
|
-
try {
|
|
788
|
-
const beforeIdx = parseInt(Buffer.from(args.before, "base64").toString("utf-8"), 10);
|
|
789
|
-
if (!isNaN(beforeIdx)) page = Math.max(1, Math.floor(beforeIdx / limit));
|
|
790
|
-
} catch {
|
|
791
|
-
}
|
|
792
|
-
}
|
|
793
|
-
const result = await ctxDb.find({
|
|
794
|
-
collection: collection.slug,
|
|
795
|
-
where,
|
|
796
|
-
sort: args.sort,
|
|
797
|
-
limit,
|
|
798
|
-
page,
|
|
799
|
-
tenantID: ctxTenant,
|
|
800
|
-
draft: isDraft
|
|
801
|
-
});
|
|
802
|
-
const edges = (result.docs || []).map((doc, idx) => {
|
|
803
|
-
const cursorIdx = (page - 1) * limit + idx;
|
|
804
|
-
return {
|
|
805
|
-
node: doc,
|
|
806
|
-
cursor: Buffer.from(String(cursorIdx)).toString("base64")
|
|
807
|
-
};
|
|
808
|
-
});
|
|
809
|
-
return {
|
|
810
|
-
edges,
|
|
811
|
-
pageInfo: {
|
|
812
|
-
hasNextPage: result.hasNextPage || false,
|
|
813
|
-
hasPreviousPage: result.hasPrevPage || false,
|
|
814
|
-
startCursor: edges.length > 0 ? edges[0].cursor : null,
|
|
815
|
-
endCursor: edges.length > 0 ? edges[edges.length - 1].cursor : null
|
|
816
|
-
},
|
|
817
|
-
totalCount: result.totalDocs
|
|
818
|
-
};
|
|
819
|
-
});
|
|
820
|
-
}
|
|
821
|
-
};
|
|
822
|
-
}
|
|
823
|
-
for (const global of globals) {
|
|
824
|
-
const type = globalTypes[global.slug];
|
|
825
|
-
if (!type) continue;
|
|
826
|
-
queryFields[toCamelCase(global.slug)] = {
|
|
827
|
-
type,
|
|
828
|
-
resolve: async (_, args, ctx) => {
|
|
829
|
-
const { user: ctxUser, req: ctxReq, tenantID: ctxTenant, db: ctxDb } = ctx;
|
|
830
|
-
await checkGqlGlobalAccess(global, "read", {
|
|
831
|
-
user: ctxUser,
|
|
832
|
-
req: ctxReq,
|
|
833
|
-
tenantID: ctxTenant
|
|
834
|
-
});
|
|
835
|
-
if (ctxTenant) {
|
|
836
|
-
ctxDb.setTenantContext({ tenantId: ctxTenant, userId: ctxUser?.id ?? "", role: ctxUser?.role, isSuperAdmin: ctxUser?.role === "super_admin" });
|
|
837
|
-
}
|
|
838
|
-
return ctxDb.findOne({
|
|
839
|
-
collection: `_globals_${global.slug}`,
|
|
840
|
-
where: {},
|
|
841
|
-
tenantID: ctxTenant
|
|
842
|
-
});
|
|
843
|
-
}
|
|
844
|
-
};
|
|
845
|
-
}
|
|
846
|
-
const Query = new graphql.GraphQLObjectType({
|
|
847
|
-
name: "Query",
|
|
848
|
-
fields: queryFields
|
|
849
|
-
});
|
|
850
|
-
const mutationFields = {};
|
|
851
|
-
for (const collection of collections) {
|
|
852
|
-
const type = collectionTypes[collection.slug];
|
|
853
|
-
const inputType = collectionInputTypes[collection.slug];
|
|
854
|
-
if (!type || !inputType) continue;
|
|
855
|
-
mutationFields[`create${toPascalSingular(collection.slug)}`] = {
|
|
856
|
-
type: new graphql.GraphQLObjectType({
|
|
857
|
-
name: `${toPascalSingular(collection.slug)}CreateResult`,
|
|
858
|
-
fields: {
|
|
859
|
-
doc: { type },
|
|
860
|
-
message: { type: graphql.GraphQLString }
|
|
861
|
-
}
|
|
862
|
-
}),
|
|
863
|
-
args: {
|
|
864
|
-
data: { type: new graphql.GraphQLNonNull(inputType) },
|
|
865
|
-
draft: { type: graphql.GraphQLBoolean }
|
|
866
|
-
},
|
|
867
|
-
resolve: async (_, args, ctx) => {
|
|
868
|
-
const { user: ctxUser, req: ctxReq, tenantID: ctxTenant, apiKey: ctxApiKey, db: ctxDb, registry: ctxRegistry } = ctx;
|
|
869
|
-
await checkAccess(collection, "create", {
|
|
870
|
-
user: ctxUser,
|
|
871
|
-
req: ctxReq,
|
|
872
|
-
tenantID: ctxTenant,
|
|
873
|
-
apiKey: ctxApiKey
|
|
874
|
-
});
|
|
875
|
-
if (ctxTenant) {
|
|
876
|
-
ctxDb.setTenantContext({ tenantId: ctxTenant, userId: ctxUser?.id ?? "", role: ctxUser?.role, isSuperAdmin: ctxUser?.role === "super_admin" });
|
|
877
|
-
}
|
|
878
|
-
const createSchema = ctxRegistry.getCreateZodSchema(collection.slug);
|
|
879
|
-
let validated;
|
|
880
|
-
try {
|
|
881
|
-
validated = createSchema.parse(args.data);
|
|
882
|
-
} catch (err) {
|
|
883
|
-
if (err?.issues) {
|
|
884
|
-
const msg = err.issues.map((i) => `${i.path?.join(".")}: ${i.message}`).join("; ");
|
|
885
|
-
throw gqlError(msg, "BAD_USER_INPUT");
|
|
886
|
-
}
|
|
887
|
-
throw err;
|
|
888
|
-
}
|
|
889
|
-
const doc = await ctxDb.create({
|
|
890
|
-
collection: collection.slug,
|
|
891
|
-
data: validated,
|
|
892
|
-
tenantID: ctxTenant
|
|
893
|
-
});
|
|
894
|
-
return { doc, message: "Created successfully" };
|
|
895
|
-
}
|
|
896
|
-
};
|
|
897
|
-
mutationFields[`update${toPascalSingular(collection.slug)}`] = {
|
|
898
|
-
type: new graphql.GraphQLObjectType({
|
|
899
|
-
name: `${toPascalSingular(collection.slug)}UpdateResult`,
|
|
900
|
-
fields: {
|
|
901
|
-
doc: { type },
|
|
902
|
-
message: { type: graphql.GraphQLString }
|
|
903
|
-
}
|
|
904
|
-
}),
|
|
905
|
-
args: {
|
|
906
|
-
id: { type: new graphql.GraphQLNonNull(graphql.GraphQLString) },
|
|
907
|
-
data: { type: new graphql.GraphQLNonNull(inputType) },
|
|
908
|
-
baseUpdatedAt: { type: graphql.GraphQLString },
|
|
909
|
-
draft: { type: graphql.GraphQLBoolean }
|
|
910
|
-
},
|
|
911
|
-
resolve: async (_, args, ctx) => {
|
|
912
|
-
const { user: ctxUser, req: ctxReq, tenantID: ctxTenant, apiKey: ctxApiKey, db: ctxDb, registry: ctxRegistry } = ctx;
|
|
913
|
-
await checkAccess(collection, "update", {
|
|
914
|
-
user: ctxUser,
|
|
915
|
-
req: ctxReq,
|
|
916
|
-
tenantID: ctxTenant,
|
|
917
|
-
apiKey: ctxApiKey
|
|
918
|
-
});
|
|
919
|
-
if (ctxTenant) {
|
|
920
|
-
ctxDb.setTenantContext({ tenantId: ctxTenant, userId: ctxUser?.id ?? "", role: ctxUser?.role, isSuperAdmin: ctxUser?.role === "super_admin" });
|
|
921
|
-
}
|
|
922
|
-
if (args.baseUpdatedAt) {
|
|
923
|
-
const originalDoc = await ctxDb.findByID({
|
|
924
|
-
collection: collection.slug,
|
|
925
|
-
id: args.id,
|
|
926
|
-
tenantID: ctxTenant,
|
|
927
|
-
draft: true
|
|
928
|
-
});
|
|
929
|
-
if (originalDoc && originalDoc.updatedAt && args.baseUpdatedAt !== originalDoc.updatedAt) {
|
|
930
|
-
throw gqlError(
|
|
931
|
-
`Revision conflict: document has changed since ${args.baseUpdatedAt}. Current updatedAt: ${originalDoc.updatedAt}`,
|
|
932
|
-
"CONFLICT"
|
|
933
|
-
);
|
|
934
|
-
}
|
|
935
|
-
}
|
|
936
|
-
const updateSchema = ctxRegistry.getUpdateZodSchema(collection.slug);
|
|
937
|
-
let validated;
|
|
938
|
-
try {
|
|
939
|
-
validated = updateSchema.parse(args.data);
|
|
940
|
-
} catch (err) {
|
|
941
|
-
if (err?.issues) {
|
|
942
|
-
const msg = err.issues.map((i) => `${i.path?.join(".")}: ${i.message}`).join("; ");
|
|
943
|
-
throw gqlError(msg, "BAD_USER_INPUT");
|
|
944
|
-
}
|
|
945
|
-
throw err;
|
|
946
|
-
}
|
|
947
|
-
const doc = await ctxDb.update({
|
|
948
|
-
collection: collection.slug,
|
|
949
|
-
id: args.id,
|
|
950
|
-
data: validated,
|
|
951
|
-
tenantID: ctxTenant
|
|
952
|
-
});
|
|
953
|
-
return { doc, message: "Updated successfully" };
|
|
954
|
-
}
|
|
955
|
-
};
|
|
956
|
-
mutationFields[`delete${toPascalSingular(collection.slug)}`] = {
|
|
957
|
-
type: new graphql.GraphQLObjectType({
|
|
958
|
-
name: `${toPascalSingular(collection.slug)}DeleteResult`,
|
|
959
|
-
fields: {
|
|
960
|
-
doc: { type },
|
|
961
|
-
message: { type: graphql.GraphQLString }
|
|
962
|
-
}
|
|
963
|
-
}),
|
|
964
|
-
args: {
|
|
965
|
-
id: { type: new graphql.GraphQLNonNull(graphql.GraphQLString) }
|
|
966
|
-
},
|
|
967
|
-
resolve: async (_, args, ctx) => {
|
|
968
|
-
const { user: ctxUser, req: ctxReq, tenantID: ctxTenant, apiKey: ctxApiKey, db: ctxDb } = ctx;
|
|
969
|
-
await checkAccess(collection, "delete", {
|
|
970
|
-
user: ctxUser,
|
|
971
|
-
req: ctxReq,
|
|
972
|
-
tenantID: ctxTenant,
|
|
973
|
-
apiKey: ctxApiKey
|
|
974
|
-
});
|
|
975
|
-
if (ctxTenant) {
|
|
976
|
-
ctxDb.setTenantContext({ tenantId: ctxTenant, userId: ctxUser?.id ?? "", role: ctxUser?.role, isSuperAdmin: ctxUser?.role === "super_admin" });
|
|
977
|
-
}
|
|
978
|
-
const doc = await ctxDb.delete({
|
|
979
|
-
collection: collection.slug,
|
|
980
|
-
id: args.id,
|
|
981
|
-
tenantID: ctxTenant
|
|
982
|
-
});
|
|
983
|
-
return { doc, message: "Deleted successfully" };
|
|
984
|
-
}
|
|
985
|
-
};
|
|
986
|
-
}
|
|
987
|
-
for (const global of globals) {
|
|
988
|
-
const inputType = globalInputTypes[global.slug];
|
|
989
|
-
if (!inputType) continue;
|
|
990
|
-
mutationFields[`update${toPascalCase(global.slug)}`] = {
|
|
991
|
-
type: new graphql.GraphQLObjectType({
|
|
992
|
-
name: `${toPascalCase(global.slug)}UpdateResult`,
|
|
993
|
-
fields: {
|
|
994
|
-
doc: { type: globalTypes[global.slug] || graphql.GraphQLString },
|
|
995
|
-
message: { type: graphql.GraphQLString }
|
|
996
|
-
}
|
|
997
|
-
}),
|
|
998
|
-
args: {
|
|
999
|
-
data: { type: new graphql.GraphQLNonNull(inputType) }
|
|
1000
|
-
},
|
|
1001
|
-
resolve: async (_, args, ctx) => {
|
|
1002
|
-
const { user: ctxUser, req: ctxReq, tenantID: ctxTenant, db: ctxDb } = ctx;
|
|
1003
|
-
await checkGqlGlobalAccess(global, "update", {
|
|
1004
|
-
user: ctxUser,
|
|
1005
|
-
req: ctxReq,
|
|
1006
|
-
tenantID: ctxTenant
|
|
1007
|
-
});
|
|
1008
|
-
if (ctxTenant) {
|
|
1009
|
-
ctxDb.setTenantContext({ tenantId: ctxTenant, userId: ctxUser?.id ?? "", role: ctxUser?.role, isSuperAdmin: ctxUser?.role === "super_admin" });
|
|
1010
|
-
}
|
|
1011
|
-
const doc = await ctxDb.findOne({
|
|
1012
|
-
collection: `_globals_${global.slug}`,
|
|
1013
|
-
where: {},
|
|
1014
|
-
tenantID: ctxTenant
|
|
1015
|
-
});
|
|
1016
|
-
let result;
|
|
1017
|
-
if (doc) {
|
|
1018
|
-
result = await ctxDb.update({
|
|
1019
|
-
collection: `_globals_${global.slug}`,
|
|
1020
|
-
id: doc.id,
|
|
1021
|
-
data: args.data,
|
|
1022
|
-
tenantID: ctxTenant
|
|
1023
|
-
});
|
|
1024
|
-
} else {
|
|
1025
|
-
result = await ctxDb.create({
|
|
1026
|
-
collection: `_globals_${global.slug}`,
|
|
1027
|
-
data: args.data,
|
|
1028
|
-
tenantID: ctxTenant
|
|
1029
|
-
});
|
|
1030
|
-
}
|
|
1031
|
-
return { doc: result, message: "Updated successfully" };
|
|
1032
|
-
}
|
|
1033
|
-
};
|
|
1034
|
-
}
|
|
1035
|
-
const Mutation = new graphql.GraphQLObjectType({
|
|
1036
|
-
name: "Mutation",
|
|
1037
|
-
fields: mutationFields
|
|
1038
|
-
});
|
|
1039
|
-
const keyDirective = new graphql.GraphQLDirective({
|
|
1040
|
-
name: "key",
|
|
1041
|
-
locations: [graphql.DirectiveLocation.OBJECT, graphql.DirectiveLocation.INTERFACE],
|
|
1042
|
-
args: {
|
|
1043
|
-
fields: { type: new graphql.GraphQLNonNull(graphql.GraphQLString) }
|
|
1044
|
-
}
|
|
1045
|
-
});
|
|
1046
|
-
const serviceSdl = graphql.printSchema(new graphql.GraphQLSchema({
|
|
1047
|
-
query: new graphql.GraphQLObjectType({ name: "Query", fields: { ...queryFields } }),
|
|
1048
|
-
mutation: Mutation,
|
|
1049
|
-
directives: [keyDirective]
|
|
1050
|
-
}));
|
|
1051
|
-
queryFields._service = {
|
|
1052
|
-
type: new graphql.GraphQLObjectType({
|
|
1053
|
-
name: "_Service",
|
|
1054
|
-
fields: {
|
|
1055
|
-
sdl: { type: graphql.GraphQLString }
|
|
1056
|
-
}
|
|
1057
|
-
}),
|
|
1058
|
-
resolve: () => ({ sdl: serviceSdl })
|
|
1059
|
-
};
|
|
1060
|
-
return new graphql.GraphQLSchema({
|
|
1061
|
-
query: Query,
|
|
1062
|
-
mutation: Mutation,
|
|
1063
|
-
directives: [keyDirective]
|
|
1064
|
-
});
|
|
1065
|
-
}
|
|
1066
|
-
function createGraphQLSchema(registry, db) {
|
|
1067
|
-
const schema = buildGraphQLSchema({ registry, db });
|
|
1068
|
-
return Object.assign(schema, { sdl: graphql.printSchema(schema) });
|
|
1069
|
-
}
|
|
1070
|
-
|
|
1071
|
-
Object.defineProperty(exports, "printSchema", {
|
|
1072
|
-
enumerable: true,
|
|
1073
|
-
get: function () { return graphql.printSchema; }
|
|
1074
|
-
});
|
|
1075
|
-
exports.RelationLoader = RelationLoader;
|
|
1076
|
-
exports.buildGraphQLSchema = buildGraphQLSchema;
|
|
1077
|
-
exports.createGraphQLSchema = createGraphQLSchema;
|
|
1078
|
-
exports.depthLimit = depthLimit;
|
|
1079
|
-
//# sourceMappingURL=chunk-PF642V5O.cjs.map
|
|
1080
|
-
//# sourceMappingURL=chunk-PF642V5O.cjs.map
|