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