@kyro-cms/core 0.9.4 → 0.9.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/api-handler-graphql.cjs +10 -10
- package/dist/api-handler-graphql.js +6 -6
- package/dist/api-handler-trpc.cjs +8 -8
- package/dist/api-handler-trpc.js +6 -6
- package/dist/api-handler.cjs +9 -9
- package/dist/api-handler.js +6 -6
- package/dist/{chunk-YFAVQQTU.js → chunk-AX2TZRQJ.js} +3 -3
- package/dist/{chunk-YFAVQQTU.js.map → chunk-AX2TZRQJ.js.map} +1 -1
- package/dist/{chunk-5H3MWQJS.js → chunk-CMXVTUYV.js} +12 -12
- package/dist/chunk-CMXVTUYV.js.map +1 -0
- package/dist/{chunk-E2763JUP.cjs → chunk-DRVOUQMT.cjs} +27 -27
- package/dist/chunk-DRVOUQMT.cjs.map +1 -0
- package/dist/{chunk-4M7X5HAB.cjs → chunk-FKKQUMXR.cjs} +109 -3
- package/dist/chunk-FKKQUMXR.cjs.map +1 -0
- package/dist/{chunk-PV2I2KMI.cjs → chunk-HVCUIII2.cjs} +21 -75
- package/dist/chunk-HVCUIII2.cjs.map +1 -0
- package/dist/{chunk-CJONKRHJ.js → chunk-NZEUU7QB.js} +108 -3
- package/dist/chunk-NZEUU7QB.js.map +1 -0
- package/dist/{chunk-NWUEVLQT.cjs → chunk-OZ3CCTTA.cjs} +5 -5
- package/dist/{chunk-NWUEVLQT.cjs.map → chunk-OZ3CCTTA.cjs.map} +1 -1
- package/dist/chunk-PONTBXR5.js +842 -0
- package/dist/chunk-PONTBXR5.js.map +1 -0
- package/dist/{chunk-CNKT4PME.cjs → chunk-QVJNSAQL.cjs} +71 -149
- package/dist/chunk-QVJNSAQL.cjs.map +1 -0
- package/dist/{chunk-OHC6UHFY.js → chunk-QX3WNQ7V.js} +18 -72
- package/dist/chunk-QX3WNQ7V.js.map +1 -0
- package/dist/chunk-RRKCIAPU.cjs +848 -0
- package/dist/chunk-RRKCIAPU.cjs.map +1 -0
- package/dist/{chunk-NI5D3F7H.cjs → chunk-U3VVLYXG.cjs} +55 -7
- package/dist/chunk-U3VVLYXG.cjs.map +1 -0
- package/dist/{chunk-IPTZM3VE.js → chunk-VLK5SJRI.js} +56 -134
- package/dist/chunk-VLK5SJRI.js.map +1 -0
- package/dist/{chunk-NFKRKH3P.js → chunk-X4BPPD6K.js} +52 -4
- package/dist/chunk-X4BPPD6K.js.map +1 -0
- package/dist/graphql/index.cjs +8 -4
- package/dist/graphql/index.d.cts +4 -1
- package/dist/graphql/index.d.ts +4 -1
- package/dist/graphql/index.js +2 -2
- package/dist/index.cjs +59 -59
- package/dist/index.js +7 -7
- package/dist/integration.cjs +2 -2
- package/dist/integration.js +1 -1
- package/dist/rest/index.cjs +4 -4
- package/dist/rest/index.js +2 -2
- package/dist/trpc/index.cjs +11 -11
- package/dist/trpc/index.js +2 -2
- package/package.json +2 -2
- package/dist/chunk-3HR772HI.cjs +0 -555
- package/dist/chunk-3HR772HI.cjs.map +0 -1
- package/dist/chunk-4M7X5HAB.cjs.map +0 -1
- package/dist/chunk-5H3MWQJS.js.map +0 -1
- package/dist/chunk-CJONKRHJ.js.map +0 -1
- package/dist/chunk-CNKT4PME.cjs.map +0 -1
- package/dist/chunk-E2763JUP.cjs.map +0 -1
- package/dist/chunk-IPTZM3VE.js.map +0 -1
- package/dist/chunk-L5UKKZQN.js +0 -552
- package/dist/chunk-L5UKKZQN.js.map +0 -1
- package/dist/chunk-NFKRKH3P.js.map +0 -1
- package/dist/chunk-NI5D3F7H.cjs.map +0 -1
- package/dist/chunk-OHC6UHFY.js.map +0 -1
- package/dist/chunk-PV2I2KMI.cjs.map +0 -1
|
@@ -0,0 +1,848 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkFKKQUMXR_cjs = require('./chunk-FKKQUMXR.cjs');
|
|
4
|
+
var graphql = require('graphql');
|
|
5
|
+
|
|
6
|
+
var RelationLoader = class {
|
|
7
|
+
db;
|
|
8
|
+
tenantID;
|
|
9
|
+
user;
|
|
10
|
+
caches = /* @__PURE__ */ new Map();
|
|
11
|
+
pending = /* @__PURE__ */ new Map();
|
|
12
|
+
constructor(opts) {
|
|
13
|
+
this.db = opts.db;
|
|
14
|
+
this.tenantID = opts.tenantID;
|
|
15
|
+
this.user = opts.user;
|
|
16
|
+
}
|
|
17
|
+
getCacheKey(collection) {
|
|
18
|
+
return collection;
|
|
19
|
+
}
|
|
20
|
+
async load(collection, id) {
|
|
21
|
+
const key = this.getCacheKey(collection);
|
|
22
|
+
let cache = this.caches.get(key);
|
|
23
|
+
if (!cache) {
|
|
24
|
+
cache = /* @__PURE__ */ new Map();
|
|
25
|
+
this.caches.set(key, cache);
|
|
26
|
+
}
|
|
27
|
+
if (cache.has(id)) {
|
|
28
|
+
return cache.get(id);
|
|
29
|
+
}
|
|
30
|
+
let pendingSet = this.pending.get(key);
|
|
31
|
+
if (!pendingSet) {
|
|
32
|
+
pendingSet = /* @__PURE__ */ new Set();
|
|
33
|
+
this.pending.set(key, pendingSet);
|
|
34
|
+
}
|
|
35
|
+
pendingSet.add(id);
|
|
36
|
+
cache.set(id, void 0);
|
|
37
|
+
return void 0;
|
|
38
|
+
}
|
|
39
|
+
async flushAll() {
|
|
40
|
+
for (const [key, ids] of this.pending) {
|
|
41
|
+
if (ids.size === 0) continue;
|
|
42
|
+
const cache = this.caches.get(key);
|
|
43
|
+
const docs = await this.db.find({
|
|
44
|
+
collection: key,
|
|
45
|
+
where: { id: { in: Array.from(ids) } },
|
|
46
|
+
limit: ids.size,
|
|
47
|
+
tenantID: this.tenantID,
|
|
48
|
+
draft: !!this.user
|
|
49
|
+
});
|
|
50
|
+
for (const doc of docs.docs || []) {
|
|
51
|
+
if (doc && doc.id) {
|
|
52
|
+
cache.set(doc.id, doc);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
this.pending.clear();
|
|
57
|
+
}
|
|
58
|
+
resolveOne(collection, id) {
|
|
59
|
+
return this.caches.get(this.getCacheKey(collection))?.get(id);
|
|
60
|
+
}
|
|
61
|
+
static async withBatch(loader, fn) {
|
|
62
|
+
const result = await fn();
|
|
63
|
+
await loader.flushAll();
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
function gqlError(message, code) {
|
|
68
|
+
const err = new Error(message);
|
|
69
|
+
err.extensions = { code };
|
|
70
|
+
return err;
|
|
71
|
+
}
|
|
72
|
+
async function checkAccess(config, operation, context) {
|
|
73
|
+
const result = await chunkFKKQUMXR_cjs.checkCollectionAccess(config, operation, context);
|
|
74
|
+
if (!result.allowed) {
|
|
75
|
+
throw gqlError(result.error || "Access denied", "FORBIDDEN");
|
|
76
|
+
}
|
|
77
|
+
return result.extraWhere;
|
|
78
|
+
}
|
|
79
|
+
async function checkGqlGlobalAccess(config, operation, context) {
|
|
80
|
+
const result = await chunkFKKQUMXR_cjs.checkGlobalAccess(config, operation, context);
|
|
81
|
+
if (!result.allowed) {
|
|
82
|
+
throw gqlError(result.error || "Access denied", "FORBIDDEN");
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function fieldToGraphQLType(field, registry, collectionTypes, isInputType = false, loader) {
|
|
86
|
+
switch (field.type) {
|
|
87
|
+
case "text":
|
|
88
|
+
case "email":
|
|
89
|
+
case "password":
|
|
90
|
+
case "textarea":
|
|
91
|
+
case "color":
|
|
92
|
+
case "code":
|
|
93
|
+
case "markdown":
|
|
94
|
+
case "date":
|
|
95
|
+
case "select":
|
|
96
|
+
case "radio":
|
|
97
|
+
case "upload":
|
|
98
|
+
return graphql.GraphQLString;
|
|
99
|
+
case "number":
|
|
100
|
+
return field.integer ? graphql.GraphQLInt : graphql.GraphQLFloat;
|
|
101
|
+
case "checkbox":
|
|
102
|
+
return graphql.GraphQLBoolean;
|
|
103
|
+
case "json":
|
|
104
|
+
case "richtext":
|
|
105
|
+
return graphql.GraphQLString;
|
|
106
|
+
case "relationship":
|
|
107
|
+
if (typeof field.relationTo === "string") {
|
|
108
|
+
if (isInputType) {
|
|
109
|
+
if (field.hasMany) {
|
|
110
|
+
return new graphql.GraphQLList(graphql.GraphQLString);
|
|
111
|
+
}
|
|
112
|
+
return graphql.GraphQLString;
|
|
113
|
+
}
|
|
114
|
+
if (collectionTypes?.[field.relationTo]) {
|
|
115
|
+
const refType = collectionTypes[field.relationTo];
|
|
116
|
+
if (field.hasMany) {
|
|
117
|
+
return new graphql.GraphQLList(refType);
|
|
118
|
+
}
|
|
119
|
+
return refType;
|
|
120
|
+
}
|
|
121
|
+
const relatedCollection = registry.getCollection(field.relationTo);
|
|
122
|
+
if (relatedCollection) {
|
|
123
|
+
const refName = `${field.relationTo.replace(/-/g, "_")}_ref`;
|
|
124
|
+
const refType = new graphql.GraphQLObjectType({
|
|
125
|
+
name: refName,
|
|
126
|
+
fields: () => ({
|
|
127
|
+
id: { type: graphql.GraphQLString },
|
|
128
|
+
...buildFieldsFromCollection(relatedCollection, registry, collectionTypes)
|
|
129
|
+
})
|
|
130
|
+
});
|
|
131
|
+
if (collectionTypes) {
|
|
132
|
+
collectionTypes[field.relationTo] = refType;
|
|
133
|
+
}
|
|
134
|
+
if (field.hasMany) {
|
|
135
|
+
return new graphql.GraphQLList(refType);
|
|
136
|
+
}
|
|
137
|
+
return refType;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return field.hasMany ? new graphql.GraphQLList(graphql.GraphQLString) : graphql.GraphQLString;
|
|
141
|
+
case "array": {
|
|
142
|
+
if (isInputType) return graphql.GraphQLString;
|
|
143
|
+
const arrFields = field.fields;
|
|
144
|
+
if (arrFields && arrFields.length > 0) {
|
|
145
|
+
const arrTypeName = `${field.name?.replace(/-/g, "_") || "array"}_item`;
|
|
146
|
+
const itemType = fieldsToGraphQLObjectType(arrTypeName, arrFields, registry, collectionTypes, loader);
|
|
147
|
+
return new graphql.GraphQLList(itemType);
|
|
148
|
+
}
|
|
149
|
+
return new graphql.GraphQLList(graphql.GraphQLString);
|
|
150
|
+
}
|
|
151
|
+
case "group": {
|
|
152
|
+
if (isInputType) return graphql.GraphQLString;
|
|
153
|
+
const groupFields = field.fields;
|
|
154
|
+
if (groupFields && groupFields.length > 0) {
|
|
155
|
+
const groupTypeName = `${field.name?.replace(/-/g, "_") || "group"}_group`;
|
|
156
|
+
return fieldsToGraphQLObjectType(groupTypeName, groupFields, registry, collectionTypes, loader);
|
|
157
|
+
}
|
|
158
|
+
return graphql.GraphQLString;
|
|
159
|
+
}
|
|
160
|
+
case "blocks": {
|
|
161
|
+
if (isInputType) return graphql.GraphQLString;
|
|
162
|
+
const blocks = field.blocks;
|
|
163
|
+
if (blocks && blocks.length > 0) {
|
|
164
|
+
const blockTypes = [];
|
|
165
|
+
for (const block of blocks) {
|
|
166
|
+
const blockTypeName = `Block_${block.slug.replace(/-/g, "_")}`;
|
|
167
|
+
const bType = new graphql.GraphQLObjectType({
|
|
168
|
+
name: blockTypeName,
|
|
169
|
+
fields: () => {
|
|
170
|
+
const out = {
|
|
171
|
+
blockType: { type: new graphql.GraphQLNonNull(graphql.GraphQLString) },
|
|
172
|
+
id: { type: graphql.GraphQLString }
|
|
173
|
+
};
|
|
174
|
+
if (block.fields) {
|
|
175
|
+
for (const bf of block.fields) {
|
|
176
|
+
if (!bf.name) continue;
|
|
177
|
+
const bfConfig = buildFieldConfig(bf, registry, collectionTypes, loader);
|
|
178
|
+
if (bfConfig) out[bf.name] = bfConfig;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return out;
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
blockTypes.push(bType);
|
|
185
|
+
}
|
|
186
|
+
if (blockTypes.length > 0) {
|
|
187
|
+
const unionName = `${field.name?.replace(/-/g, "_") || "blocks"}_union`;
|
|
188
|
+
return new graphql.GraphQLUnionType({
|
|
189
|
+
name: unionName,
|
|
190
|
+
types: blockTypes,
|
|
191
|
+
resolveType(value) {
|
|
192
|
+
const blockName = `Block_${(value?.blockType || "").replace(/-/g, "_")}`;
|
|
193
|
+
const blockType = blockTypes.find(
|
|
194
|
+
(bt) => bt.name === blockName
|
|
195
|
+
);
|
|
196
|
+
return blockType?.name || void 0;
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return new graphql.GraphQLList(graphql.GraphQLString);
|
|
202
|
+
}
|
|
203
|
+
case "row":
|
|
204
|
+
case "collapsible":
|
|
205
|
+
case "tabs":
|
|
206
|
+
return graphql.GraphQLString;
|
|
207
|
+
default:
|
|
208
|
+
return graphql.GraphQLString;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function buildFieldsFromCollection(config, registry, collectionTypes, loader) {
|
|
212
|
+
const fields = {};
|
|
213
|
+
for (const field of config.fields) {
|
|
214
|
+
if (field.name && field.admin?.hidden !== true) {
|
|
215
|
+
const fc = buildFieldConfig(field, registry, collectionTypes, loader);
|
|
216
|
+
if (fc) fields[field.name] = fc;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return fields;
|
|
220
|
+
}
|
|
221
|
+
function buildSortEnum(config) {
|
|
222
|
+
const sortFields = {
|
|
223
|
+
id: { value: "id" },
|
|
224
|
+
createdAt: { value: "createdAt" },
|
|
225
|
+
updatedAt: { value: "updatedAt" }
|
|
226
|
+
};
|
|
227
|
+
for (const field of config.fields) {
|
|
228
|
+
if (field.name) {
|
|
229
|
+
sortFields[field.name] = { value: field.name };
|
|
230
|
+
sortFields[`${field.name}_asc`] = { value: `${field.name}` };
|
|
231
|
+
sortFields[`${field.name}_desc`] = { value: `-${field.name}` };
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return new graphql.GraphQLEnumType({
|
|
235
|
+
name: `${config.slug.replace(/-/g, "_")}_sort`,
|
|
236
|
+
values: sortFields
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
function buildFieldFilterType(field, namePrefix) {
|
|
240
|
+
const safeName = `${namePrefix}_${field.name?.replace(/-/g, "_") || "field"}_filter`;
|
|
241
|
+
const stringOps = {
|
|
242
|
+
equals: { type: graphql.GraphQLString },
|
|
243
|
+
not_equals: { type: graphql.GraphQLString },
|
|
244
|
+
contains: { type: graphql.GraphQLString },
|
|
245
|
+
not_contains: { type: graphql.GraphQLString },
|
|
246
|
+
in: { type: new graphql.GraphQLList(graphql.GraphQLString) },
|
|
247
|
+
not_in: { type: new graphql.GraphQLList(graphql.GraphQLString) },
|
|
248
|
+
exists: { type: graphql.GraphQLBoolean }
|
|
249
|
+
};
|
|
250
|
+
const numberOps = {
|
|
251
|
+
equals: { type: graphql.GraphQLFloat },
|
|
252
|
+
not_equals: { type: graphql.GraphQLFloat },
|
|
253
|
+
greater_than: { type: graphql.GraphQLFloat },
|
|
254
|
+
less_than: { type: graphql.GraphQLFloat },
|
|
255
|
+
greater_than_equal: { type: graphql.GraphQLFloat },
|
|
256
|
+
less_than_equal: { type: graphql.GraphQLFloat },
|
|
257
|
+
in: { type: new graphql.GraphQLList(graphql.GraphQLFloat) },
|
|
258
|
+
not_in: { type: new graphql.GraphQLList(graphql.GraphQLFloat) },
|
|
259
|
+
exists: { type: graphql.GraphQLBoolean }
|
|
260
|
+
};
|
|
261
|
+
const boolOps = {
|
|
262
|
+
equals: { type: graphql.GraphQLBoolean },
|
|
263
|
+
exists: { type: graphql.GraphQLBoolean }
|
|
264
|
+
};
|
|
265
|
+
const dateOps = {
|
|
266
|
+
equals: { type: graphql.GraphQLString },
|
|
267
|
+
not_equals: { type: graphql.GraphQLString },
|
|
268
|
+
greater_than: { type: graphql.GraphQLString },
|
|
269
|
+
less_than: { type: graphql.GraphQLString },
|
|
270
|
+
exists: { type: graphql.GraphQLBoolean }
|
|
271
|
+
};
|
|
272
|
+
const relationOps = {
|
|
273
|
+
equals: { type: graphql.GraphQLString },
|
|
274
|
+
not_equals: { type: graphql.GraphQLString },
|
|
275
|
+
in: { type: new graphql.GraphQLList(graphql.GraphQLString) },
|
|
276
|
+
not_in: { type: new graphql.GraphQLList(graphql.GraphQLString) },
|
|
277
|
+
exists: { type: graphql.GraphQLBoolean }
|
|
278
|
+
};
|
|
279
|
+
const jsonOps = {
|
|
280
|
+
exists: { type: graphql.GraphQLBoolean }
|
|
281
|
+
};
|
|
282
|
+
let ops;
|
|
283
|
+
switch (field.type) {
|
|
284
|
+
case "text":
|
|
285
|
+
case "email":
|
|
286
|
+
case "password":
|
|
287
|
+
case "textarea":
|
|
288
|
+
case "color":
|
|
289
|
+
case "code":
|
|
290
|
+
case "markdown":
|
|
291
|
+
case "select":
|
|
292
|
+
case "radio":
|
|
293
|
+
case "upload":
|
|
294
|
+
ops = stringOps;
|
|
295
|
+
break;
|
|
296
|
+
case "number":
|
|
297
|
+
ops = numberOps;
|
|
298
|
+
break;
|
|
299
|
+
case "checkbox":
|
|
300
|
+
ops = boolOps;
|
|
301
|
+
break;
|
|
302
|
+
case "date":
|
|
303
|
+
ops = dateOps;
|
|
304
|
+
break;
|
|
305
|
+
case "relationship":
|
|
306
|
+
ops = relationOps;
|
|
307
|
+
break;
|
|
308
|
+
case "json":
|
|
309
|
+
case "richtext":
|
|
310
|
+
ops = jsonOps;
|
|
311
|
+
break;
|
|
312
|
+
default:
|
|
313
|
+
ops = { exists: { type: graphql.GraphQLBoolean } };
|
|
314
|
+
}
|
|
315
|
+
return new graphql.GraphQLInputObjectType({
|
|
316
|
+
name: safeName,
|
|
317
|
+
fields: ops
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
function buildCollectionFilterType(config, namePrefix) {
|
|
321
|
+
const fields = {
|
|
322
|
+
AND: { type: new graphql.GraphQLList(graphql.GraphQLString) },
|
|
323
|
+
OR: { type: new graphql.GraphQLList(graphql.GraphQLString) }
|
|
324
|
+
};
|
|
325
|
+
for (const field of config.fields) {
|
|
326
|
+
if (!field.name) continue;
|
|
327
|
+
const filterType = buildFieldFilterType(field, namePrefix);
|
|
328
|
+
if (filterType) {
|
|
329
|
+
fields[field.name] = { type: filterType };
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return new graphql.GraphQLInputObjectType({
|
|
333
|
+
name: `${namePrefix}_filter`,
|
|
334
|
+
fields
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
function translateFilter(filter) {
|
|
338
|
+
if (!filter || typeof filter !== "object") return filter || {};
|
|
339
|
+
const result = {};
|
|
340
|
+
for (const [key, value] of Object.entries(filter)) {
|
|
341
|
+
if (key === "AND" || key === "OR") continue;
|
|
342
|
+
result[key] = value;
|
|
343
|
+
}
|
|
344
|
+
return result;
|
|
345
|
+
}
|
|
346
|
+
function fieldsToGraphQLObjectType(name, fields, registry, collectionTypes, loader) {
|
|
347
|
+
return new graphql.GraphQLObjectType({
|
|
348
|
+
name,
|
|
349
|
+
fields: () => {
|
|
350
|
+
const out = {};
|
|
351
|
+
for (const field of fields) {
|
|
352
|
+
if (!field.name) continue;
|
|
353
|
+
if (field.type === "row" || field.type === "collapsible") {
|
|
354
|
+
const subFields = field.fields;
|
|
355
|
+
if (subFields) {
|
|
356
|
+
for (const sf of subFields) {
|
|
357
|
+
if (!sf.name) continue;
|
|
358
|
+
const fc = buildFieldConfig(sf, registry, collectionTypes, loader);
|
|
359
|
+
if (fc) out[sf.name] = fc;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
} else if (field.type === "tabs") {
|
|
363
|
+
const tabs = field.tabs;
|
|
364
|
+
if (tabs) {
|
|
365
|
+
for (const tab of tabs) {
|
|
366
|
+
if (tab.fields) {
|
|
367
|
+
for (const sf of tab.fields) {
|
|
368
|
+
if (!sf.name) continue;
|
|
369
|
+
const fc = buildFieldConfig(sf, registry, collectionTypes, loader);
|
|
370
|
+
if (fc) out[sf.name] = fc;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
} else {
|
|
376
|
+
const fc = buildFieldConfig(field, registry, collectionTypes, loader);
|
|
377
|
+
if (fc) out[field.name] = fc;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return out;
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
function buildFieldConfig(field, registry, collectionTypes, loader) {
|
|
385
|
+
if (!field.name) return null;
|
|
386
|
+
if (field.type === "relationship" && !field.hasMany) {
|
|
387
|
+
const gqlType = fieldToGraphQLType(field, registry, collectionTypes, false, loader);
|
|
388
|
+
return {
|
|
389
|
+
type: field.required ? new graphql.GraphQLNonNull(gqlType) : gqlType,
|
|
390
|
+
description: field.admin?.description || field.label,
|
|
391
|
+
resolve: (source) => {
|
|
392
|
+
const relField = field;
|
|
393
|
+
const id = source?.[relField.name];
|
|
394
|
+
if (!id) return null;
|
|
395
|
+
if (loader && typeof id === "string") {
|
|
396
|
+
const cached = loader.resolveOne(relField.relationTo, id);
|
|
397
|
+
if (cached) return cached;
|
|
398
|
+
loader.load(relField.relationTo, id);
|
|
399
|
+
}
|
|
400
|
+
return { id };
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
if (field.type === "relationship" && field.hasMany) {
|
|
405
|
+
const gqlType = fieldToGraphQLType(field, registry, collectionTypes, false, loader);
|
|
406
|
+
return {
|
|
407
|
+
type: gqlType,
|
|
408
|
+
description: field.admin?.description || field.label,
|
|
409
|
+
resolve: (source) => {
|
|
410
|
+
const relField = field;
|
|
411
|
+
const ids = source?.[relField.name] || [];
|
|
412
|
+
if (!ids.length) return [];
|
|
413
|
+
if (loader) {
|
|
414
|
+
const results = [];
|
|
415
|
+
for (const id of ids) {
|
|
416
|
+
const cached = loader.resolveOne(relField.relationTo, id);
|
|
417
|
+
if (cached) results.push(cached);
|
|
418
|
+
else {
|
|
419
|
+
loader.load(relField.relationTo, id);
|
|
420
|
+
results.push({ id });
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
return results;
|
|
424
|
+
}
|
|
425
|
+
return ids.map((id) => ({ id }));
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
return {
|
|
430
|
+
type: field.required ? new graphql.GraphQLNonNull(fieldToGraphQLType(field, registry, collectionTypes, false, loader)) : fieldToGraphQLType(field, registry, collectionTypes, false, loader),
|
|
431
|
+
description: field.admin?.description || field.label
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
function buildGraphQLSchema(options) {
|
|
435
|
+
const { registry, db, user, req, tenantID, apiKey, settings } = options;
|
|
436
|
+
const apiAccess = settings?.access?.apiAccess;
|
|
437
|
+
if (apiAccess?.graphqlEnabled === false) {
|
|
438
|
+
throw new Error("GraphQL API is disabled");
|
|
439
|
+
}
|
|
440
|
+
const collections = registry.getCollections();
|
|
441
|
+
const globals = registry.getGlobals();
|
|
442
|
+
const relationLoader = new RelationLoader({ db, tenantID, user });
|
|
443
|
+
const collectionTypes = {};
|
|
444
|
+
const collectionInputTypes = {};
|
|
445
|
+
for (const collection of collections) {
|
|
446
|
+
collectionTypes[collection.slug] = new graphql.GraphQLObjectType({
|
|
447
|
+
name: `${collection.slug.replace(/-/g, "_")}_type`,
|
|
448
|
+
fields: () => ({
|
|
449
|
+
id: { type: graphql.GraphQLString },
|
|
450
|
+
...buildFieldsFromCollection(collection, registry, collectionTypes, relationLoader),
|
|
451
|
+
...collection.timestamps ? {
|
|
452
|
+
createdAt: { type: graphql.GraphQLString },
|
|
453
|
+
updatedAt: { type: graphql.GraphQLString }
|
|
454
|
+
} : {},
|
|
455
|
+
...collection.tenantScoped ? {
|
|
456
|
+
tenantID: { type: graphql.GraphQLString }
|
|
457
|
+
} : {}
|
|
458
|
+
})
|
|
459
|
+
});
|
|
460
|
+
const inputFields = {};
|
|
461
|
+
for (const field of collection.fields) {
|
|
462
|
+
if (field.name && field.name !== "id") {
|
|
463
|
+
inputFields[field.name] = {
|
|
464
|
+
type: fieldToGraphQLType(field, registry, collectionTypes, true)
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
collectionInputTypes[collection.slug] = new graphql.GraphQLInputObjectType({
|
|
469
|
+
name: `${collection.slug.replace(/-/g, "_")}_input`,
|
|
470
|
+
fields: () => inputFields
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
const globalTypes = {};
|
|
474
|
+
const globalInputTypes = {};
|
|
475
|
+
for (const global of globals) {
|
|
476
|
+
globalTypes[global.slug] = new graphql.GraphQLObjectType({
|
|
477
|
+
name: `${global.slug.replace(/-/g, "_")}_global_type`,
|
|
478
|
+
fields: () => ({
|
|
479
|
+
id: { type: graphql.GraphQLString },
|
|
480
|
+
...buildFieldsFromCollection(
|
|
481
|
+
{ slug: global.slug, fields: global.fields },
|
|
482
|
+
registry,
|
|
483
|
+
collectionTypes,
|
|
484
|
+
relationLoader
|
|
485
|
+
)
|
|
486
|
+
})
|
|
487
|
+
});
|
|
488
|
+
globalInputTypes[global.slug] = new graphql.GraphQLInputObjectType({
|
|
489
|
+
name: `${global.slug.replace(/-/g, "_")}_global_input`,
|
|
490
|
+
fields: () => {
|
|
491
|
+
const inputFields = {};
|
|
492
|
+
for (const field of global.fields) {
|
|
493
|
+
if (field.name && field.name !== "id") {
|
|
494
|
+
inputFields[field.name] = {
|
|
495
|
+
type: fieldToGraphQLType(field, registry, collectionTypes, true)
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
return inputFields;
|
|
500
|
+
}
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
const queryFields = {};
|
|
504
|
+
for (const collection of collections) {
|
|
505
|
+
const type = collectionTypes[collection.slug];
|
|
506
|
+
if (!type) continue;
|
|
507
|
+
const filterType = buildCollectionFilterType(collection, collection.slug.replace(/-/g, "_"));
|
|
508
|
+
const sortEnum = buildSortEnum(collection);
|
|
509
|
+
queryFields[`${collection.slug.replace(/-/g, "_")}Find`] = {
|
|
510
|
+
type: new graphql.GraphQLObjectType({
|
|
511
|
+
name: `${collection.slug.replace(/-/g, "_")}_find_result`,
|
|
512
|
+
fields: {
|
|
513
|
+
docs: { type: new graphql.GraphQLList(type) },
|
|
514
|
+
totalDocs: { type: graphql.GraphQLInt },
|
|
515
|
+
page: { type: graphql.GraphQLInt },
|
|
516
|
+
totalPages: { type: graphql.GraphQLInt },
|
|
517
|
+
hasNextPage: { type: graphql.GraphQLBoolean },
|
|
518
|
+
hasPrevPage: { type: graphql.GraphQLBoolean }
|
|
519
|
+
}
|
|
520
|
+
}),
|
|
521
|
+
args: {
|
|
522
|
+
where: { type: filterType },
|
|
523
|
+
sort: { type: sortEnum || graphql.GraphQLString },
|
|
524
|
+
limit: { type: graphql.GraphQLInt },
|
|
525
|
+
page: { type: graphql.GraphQLInt },
|
|
526
|
+
draft: { type: graphql.GraphQLBoolean }
|
|
527
|
+
},
|
|
528
|
+
resolve: async (_, args, ctx) => {
|
|
529
|
+
const extraWhere = await checkAccess(collection, "read", {
|
|
530
|
+
user,
|
|
531
|
+
req,
|
|
532
|
+
tenantID,
|
|
533
|
+
apiKey
|
|
534
|
+
});
|
|
535
|
+
if (tenantID) {
|
|
536
|
+
db.setTenantContext({ tenantId: tenantID, userId: user?.id ?? "", role: user?.role, isSuperAdmin: user?.role === "super_admin" });
|
|
537
|
+
}
|
|
538
|
+
let where = {};
|
|
539
|
+
if (args.where) {
|
|
540
|
+
if (typeof args.where === "string") {
|
|
541
|
+
try {
|
|
542
|
+
where = JSON.parse(args.where);
|
|
543
|
+
} catch {
|
|
544
|
+
}
|
|
545
|
+
} else {
|
|
546
|
+
where = translateFilter(args.where);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
if (extraWhere) {
|
|
550
|
+
where = { ...where, ...extraWhere };
|
|
551
|
+
}
|
|
552
|
+
const isDraft = args.draft ?? !!user;
|
|
553
|
+
return RelationLoader.withBatch(relationLoader, async () => {
|
|
554
|
+
return db.find({
|
|
555
|
+
collection: collection.slug,
|
|
556
|
+
where,
|
|
557
|
+
sort: args.sort,
|
|
558
|
+
limit: args.limit || 10,
|
|
559
|
+
page: args.page || 1,
|
|
560
|
+
tenantID,
|
|
561
|
+
draft: isDraft
|
|
562
|
+
});
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
queryFields[`${collection.slug.replace(/-/g, "_")}FindByID`] = {
|
|
567
|
+
type,
|
|
568
|
+
args: {
|
|
569
|
+
id: { type: new graphql.GraphQLNonNull(graphql.GraphQLString) },
|
|
570
|
+
draft: { type: graphql.GraphQLBoolean }
|
|
571
|
+
},
|
|
572
|
+
resolve: async (_, args, ctx) => {
|
|
573
|
+
await checkAccess(collection, "read", {
|
|
574
|
+
user,
|
|
575
|
+
req,
|
|
576
|
+
tenantID,
|
|
577
|
+
apiKey
|
|
578
|
+
});
|
|
579
|
+
if (tenantID) {
|
|
580
|
+
db.setTenantContext({ tenantId: tenantID, userId: user?.id ?? "", role: user?.role, isSuperAdmin: user?.role === "super_admin" });
|
|
581
|
+
}
|
|
582
|
+
const isDraft = args.draft ?? !!user;
|
|
583
|
+
return RelationLoader.withBatch(relationLoader, async () => {
|
|
584
|
+
const doc = await db.findByID({
|
|
585
|
+
collection: collection.slug,
|
|
586
|
+
id: args.id,
|
|
587
|
+
tenantID,
|
|
588
|
+
draft: isDraft
|
|
589
|
+
});
|
|
590
|
+
return doc;
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
};
|
|
594
|
+
queryFields[`${collection.slug.replace(/-/g, "_")}Count`] = {
|
|
595
|
+
type: new graphql.GraphQLObjectType({
|
|
596
|
+
name: `${collection.slug.replace(/-/g, "_")}_count`,
|
|
597
|
+
fields: {
|
|
598
|
+
totalDocs: { type: graphql.GraphQLInt }
|
|
599
|
+
}
|
|
600
|
+
}),
|
|
601
|
+
args: {
|
|
602
|
+
where: { type: graphql.GraphQLString }
|
|
603
|
+
},
|
|
604
|
+
resolve: async (_, args, ctx) => {
|
|
605
|
+
await checkAccess(collection, "read", {
|
|
606
|
+
user,
|
|
607
|
+
req,
|
|
608
|
+
tenantID,
|
|
609
|
+
apiKey
|
|
610
|
+
});
|
|
611
|
+
let where = {};
|
|
612
|
+
if (args.where) {
|
|
613
|
+
if (typeof args.where === "string") {
|
|
614
|
+
try {
|
|
615
|
+
where = JSON.parse(args.where);
|
|
616
|
+
} catch {
|
|
617
|
+
}
|
|
618
|
+
} else {
|
|
619
|
+
where = translateFilter(args.where);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
const count = await db.count({
|
|
623
|
+
collection: collection.slug,
|
|
624
|
+
where,
|
|
625
|
+
tenantID
|
|
626
|
+
});
|
|
627
|
+
return { totalDocs: count };
|
|
628
|
+
}
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
for (const global of globals) {
|
|
632
|
+
const type = globalTypes[global.slug];
|
|
633
|
+
if (!type) continue;
|
|
634
|
+
queryFields[`${global.slug.replace(/-/g, "_")}Get`] = {
|
|
635
|
+
type,
|
|
636
|
+
resolve: async () => {
|
|
637
|
+
await checkGqlGlobalAccess(global, "read", {
|
|
638
|
+
user,
|
|
639
|
+
req,
|
|
640
|
+
tenantID
|
|
641
|
+
});
|
|
642
|
+
if (tenantID) {
|
|
643
|
+
db.setTenantContext({ tenantId: tenantID, userId: user?.id ?? "", role: user?.role, isSuperAdmin: user?.role === "super_admin" });
|
|
644
|
+
}
|
|
645
|
+
return db.findOne({
|
|
646
|
+
collection: `_globals_${global.slug}`,
|
|
647
|
+
where: {},
|
|
648
|
+
tenantID
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
const Query = new graphql.GraphQLObjectType({
|
|
654
|
+
name: "Query",
|
|
655
|
+
fields: queryFields
|
|
656
|
+
});
|
|
657
|
+
const mutationFields = {};
|
|
658
|
+
for (const collection of collections) {
|
|
659
|
+
const type = collectionTypes[collection.slug];
|
|
660
|
+
const inputType = collectionInputTypes[collection.slug];
|
|
661
|
+
if (!type || !inputType) continue;
|
|
662
|
+
mutationFields[`${collection.slug.replace(/-/g, "_")}Create`] = {
|
|
663
|
+
type: new graphql.GraphQLObjectType({
|
|
664
|
+
name: `${collection.slug.replace(/-/g, "_")}_create_result`,
|
|
665
|
+
fields: {
|
|
666
|
+
doc: { type },
|
|
667
|
+
message: { type: graphql.GraphQLString }
|
|
668
|
+
}
|
|
669
|
+
}),
|
|
670
|
+
args: {
|
|
671
|
+
data: { type: new graphql.GraphQLNonNull(inputType) },
|
|
672
|
+
draft: { type: graphql.GraphQLBoolean }
|
|
673
|
+
},
|
|
674
|
+
resolve: async (_, args, ctx) => {
|
|
675
|
+
await checkAccess(collection, "create", {
|
|
676
|
+
user,
|
|
677
|
+
req,
|
|
678
|
+
tenantID,
|
|
679
|
+
apiKey
|
|
680
|
+
});
|
|
681
|
+
if (tenantID) {
|
|
682
|
+
db.setTenantContext({ tenantId: tenantID, userId: user?.id ?? "", role: user?.role, isSuperAdmin: user?.role === "super_admin" });
|
|
683
|
+
}
|
|
684
|
+
const schema = registry.getCreateZodSchema(collection.slug);
|
|
685
|
+
const validated = schema.parse(args.data);
|
|
686
|
+
const doc = await db.create({
|
|
687
|
+
collection: collection.slug,
|
|
688
|
+
data: validated,
|
|
689
|
+
tenantID
|
|
690
|
+
});
|
|
691
|
+
return { doc, message: "Created successfully" };
|
|
692
|
+
}
|
|
693
|
+
};
|
|
694
|
+
mutationFields[`${collection.slug.replace(/-/g, "_")}Update`] = {
|
|
695
|
+
type: new graphql.GraphQLObjectType({
|
|
696
|
+
name: `${collection.slug.replace(/-/g, "_")}_update_result`,
|
|
697
|
+
fields: {
|
|
698
|
+
doc: { type },
|
|
699
|
+
message: { type: graphql.GraphQLString }
|
|
700
|
+
}
|
|
701
|
+
}),
|
|
702
|
+
args: {
|
|
703
|
+
id: { type: new graphql.GraphQLNonNull(graphql.GraphQLString) },
|
|
704
|
+
data: { type: new graphql.GraphQLNonNull(inputType) },
|
|
705
|
+
baseUpdatedAt: { type: graphql.GraphQLString },
|
|
706
|
+
draft: { type: graphql.GraphQLBoolean }
|
|
707
|
+
},
|
|
708
|
+
resolve: async (_, args, ctx) => {
|
|
709
|
+
await checkAccess(collection, "update", {
|
|
710
|
+
user,
|
|
711
|
+
req,
|
|
712
|
+
tenantID,
|
|
713
|
+
apiKey
|
|
714
|
+
});
|
|
715
|
+
if (tenantID) {
|
|
716
|
+
db.setTenantContext({ tenantId: tenantID, userId: user?.id ?? "", role: user?.role, isSuperAdmin: user?.role === "super_admin" });
|
|
717
|
+
}
|
|
718
|
+
if (args.baseUpdatedAt) {
|
|
719
|
+
const originalDoc = await db.findByID({
|
|
720
|
+
collection: collection.slug,
|
|
721
|
+
id: args.id,
|
|
722
|
+
tenantID,
|
|
723
|
+
draft: true
|
|
724
|
+
});
|
|
725
|
+
if (originalDoc && originalDoc.updatedAt && args.baseUpdatedAt !== originalDoc.updatedAt) {
|
|
726
|
+
throw gqlError(
|
|
727
|
+
`Revision conflict: document has changed since ${args.baseUpdatedAt}. Current updatedAt: ${originalDoc.updatedAt}`,
|
|
728
|
+
"CONFLICT"
|
|
729
|
+
);
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
const schema = registry.getUpdateZodSchema(collection.slug);
|
|
733
|
+
const validated = schema.parse(args.data);
|
|
734
|
+
const doc = await db.update({
|
|
735
|
+
collection: collection.slug,
|
|
736
|
+
id: args.id,
|
|
737
|
+
data: validated,
|
|
738
|
+
tenantID
|
|
739
|
+
});
|
|
740
|
+
return { doc, message: "Updated successfully" };
|
|
741
|
+
}
|
|
742
|
+
};
|
|
743
|
+
mutationFields[`${collection.slug.replace(/-/g, "_")}Delete`] = {
|
|
744
|
+
type: new graphql.GraphQLObjectType({
|
|
745
|
+
name: `${collection.slug.replace(/-/g, "_")}_delete_result`,
|
|
746
|
+
fields: {
|
|
747
|
+
doc: { type },
|
|
748
|
+
message: { type: graphql.GraphQLString }
|
|
749
|
+
}
|
|
750
|
+
}),
|
|
751
|
+
args: {
|
|
752
|
+
id: { type: new graphql.GraphQLNonNull(graphql.GraphQLString) }
|
|
753
|
+
},
|
|
754
|
+
resolve: async (_, args, ctx) => {
|
|
755
|
+
await checkAccess(collection, "delete", {
|
|
756
|
+
user,
|
|
757
|
+
req,
|
|
758
|
+
tenantID,
|
|
759
|
+
apiKey
|
|
760
|
+
});
|
|
761
|
+
if (tenantID) {
|
|
762
|
+
db.setTenantContext({ tenantId: tenantID, userId: user?.id ?? "", role: user?.role, isSuperAdmin: user?.role === "super_admin" });
|
|
763
|
+
}
|
|
764
|
+
const doc = await db.delete({
|
|
765
|
+
collection: collection.slug,
|
|
766
|
+
id: args.id,
|
|
767
|
+
tenantID
|
|
768
|
+
});
|
|
769
|
+
return { doc, message: "Deleted successfully" };
|
|
770
|
+
}
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
for (const global of globals) {
|
|
774
|
+
const inputType = globalInputTypes[global.slug];
|
|
775
|
+
if (!inputType) continue;
|
|
776
|
+
mutationFields[`${global.slug.replace(/-/g, "_")}Update`] = {
|
|
777
|
+
type: new graphql.GraphQLObjectType({
|
|
778
|
+
name: `${global.slug.replace(/-/g, "_")}_update_result`,
|
|
779
|
+
fields: {
|
|
780
|
+
doc: { type: globalTypes[global.slug] || graphql.GraphQLString },
|
|
781
|
+
message: { type: graphql.GraphQLString }
|
|
782
|
+
}
|
|
783
|
+
}),
|
|
784
|
+
args: {
|
|
785
|
+
data: { type: new graphql.GraphQLNonNull(inputType) }
|
|
786
|
+
},
|
|
787
|
+
resolve: async (_, args, ctx) => {
|
|
788
|
+
await checkGqlGlobalAccess(global, "update", {
|
|
789
|
+
user,
|
|
790
|
+
req,
|
|
791
|
+
tenantID
|
|
792
|
+
});
|
|
793
|
+
if (tenantID) {
|
|
794
|
+
db.setTenantContext({ tenantId: tenantID, userId: user?.id ?? "", role: user?.role, isSuperAdmin: user?.role === "super_admin" });
|
|
795
|
+
}
|
|
796
|
+
const doc = await db.findOne({
|
|
797
|
+
collection: `_globals_${global.slug}`,
|
|
798
|
+
where: {},
|
|
799
|
+
tenantID
|
|
800
|
+
});
|
|
801
|
+
let result;
|
|
802
|
+
if (doc) {
|
|
803
|
+
result = await db.update({
|
|
804
|
+
collection: `_globals_${global.slug}`,
|
|
805
|
+
id: doc.id,
|
|
806
|
+
data: args.data,
|
|
807
|
+
tenantID
|
|
808
|
+
});
|
|
809
|
+
} else {
|
|
810
|
+
result = await db.create({
|
|
811
|
+
collection: `_globals_${global.slug}`,
|
|
812
|
+
data: args.data,
|
|
813
|
+
tenantID
|
|
814
|
+
});
|
|
815
|
+
}
|
|
816
|
+
return { doc: result, message: "Updated successfully" };
|
|
817
|
+
}
|
|
818
|
+
};
|
|
819
|
+
}
|
|
820
|
+
const Mutation = new graphql.GraphQLObjectType({
|
|
821
|
+
name: "Mutation",
|
|
822
|
+
fields: mutationFields
|
|
823
|
+
});
|
|
824
|
+
return new graphql.GraphQLSchema({
|
|
825
|
+
query: Query,
|
|
826
|
+
mutation: Mutation
|
|
827
|
+
});
|
|
828
|
+
}
|
|
829
|
+
function createGraphQLSchema(registry, db, options) {
|
|
830
|
+
const schema = buildGraphQLSchema({
|
|
831
|
+
registry,
|
|
832
|
+
db,
|
|
833
|
+
user: options?.user,
|
|
834
|
+
req: options?.req,
|
|
835
|
+
tenantID: options?.tenantID,
|
|
836
|
+
apiKey: options?.apiKey
|
|
837
|
+
});
|
|
838
|
+
return Object.assign(schema, { sdl: graphql.printSchema(schema) });
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
Object.defineProperty(exports, "printSchema", {
|
|
842
|
+
enumerable: true,
|
|
843
|
+
get: function () { return graphql.printSchema; }
|
|
844
|
+
});
|
|
845
|
+
exports.buildGraphQLSchema = buildGraphQLSchema;
|
|
846
|
+
exports.createGraphQLSchema = createGraphQLSchema;
|
|
847
|
+
//# sourceMappingURL=chunk-RRKCIAPU.cjs.map
|
|
848
|
+
//# sourceMappingURL=chunk-RRKCIAPU.cjs.map
|