@omnigraph/mongo 0.0.0-stage → 0.1.0

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.
@@ -0,0 +1,762 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadGraphQLSchemaFromMongo = loadGraphQLSchemaFromMongo;
4
+ const graphql_1 = require("graphql");
5
+ const graphql_compose_1 = require("graphql-compose");
6
+ const graphql_scalars_1 = require("graphql-scalars");
7
+ const cross_helpers_1 = require("@graphql-mesh/cross-helpers");
8
+ const utils_1 = require("@graphql-mesh/utils");
9
+ const directives_js_1 = require("./directives.js");
10
+ const naming_js_1 = require("./naming.js");
11
+ const type_map_js_1 = require("./type-map.js");
12
+ const FEDERATION_LINK_URL = 'https://specs.apollo.dev/federation/v2.6';
13
+ async function loadGraphQLSchemaFromMongo(subgraphName, opts) {
14
+ const naming = { ...(0, naming_js_1.createDefaultNaming)(), ...opts.naming };
15
+ const cwd = opts.cwd || cross_helpers_1.process.cwd();
16
+ const importFn = opts.importFn || utils_1.defaultImportFn;
17
+ const schemaComposer = new graphql_compose_1.SchemaComposer();
18
+ schemaComposer.add(graphql_scalars_1.GraphQLObjectID);
19
+ schemaComposer.add(graphql_scalars_1.GraphQLDateTime);
20
+ schemaComposer.add(graphql_scalars_1.GraphQLJSON);
21
+ schemaComposer.addDirective(directives_js_1.TransportDirective);
22
+ schemaComposer.addDirective(directives_js_1.MongoDirective);
23
+ schemaComposer.add(directives_js_1.MongoKindEnum);
24
+ ensureSharedFilterInputs(schemaComposer);
25
+ ensurePageInfo(schemaComposer);
26
+ const modelMeta = [];
27
+ for (const modelConfig of opts.models) {
28
+ const model = await resolveModel(modelConfig, cwd, importFn);
29
+ const collection = modelConfig.collection ||
30
+ model.collection?.collectionName ||
31
+ model.schema?.get('collection') ||
32
+ pluralizeFallback(modelConfig.name);
33
+ const typeName = naming.typeName(modelConfig.name);
34
+ const paths = introspectPaths(model.schema);
35
+ modelMeta.push({ typeName, collection, schema: model.schema, paths });
36
+ emitModelObjectType(schemaComposer, {
37
+ subgraphName,
38
+ typeName,
39
+ collection,
40
+ paths,
41
+ naming,
42
+ });
43
+ }
44
+ for (const meta of modelMeta) {
45
+ emitModelRootFields(schemaComposer, {
46
+ subgraphName,
47
+ typeName: meta.typeName,
48
+ collection: meta.collection,
49
+ paths: meta.paths,
50
+ naming,
51
+ });
52
+ emitRefFields(schemaComposer, {
53
+ subgraphName,
54
+ typeName: meta.typeName,
55
+ schema: meta.schema,
56
+ naming,
57
+ knownTypes: new Set(modelMeta.map(m => m.typeName)),
58
+ collectionsByType: new Map(modelMeta.map(m => [m.typeName, m.collection])),
59
+ });
60
+ }
61
+ const schema = schemaComposer.buildSchema();
62
+ const extensions = (schema.extensions ||= {});
63
+ extensions.directives ||= {};
64
+ extensions.directives.transport = {
65
+ subgraph: subgraphName,
66
+ kind: 'mongo',
67
+ location: opts.endpoint,
68
+ };
69
+ extensions.directives.link = [
70
+ {
71
+ url: FEDERATION_LINK_URL,
72
+ import: ['@key'],
73
+ },
74
+ ];
75
+ markMongoDirectiveStringsAsBlocks(schema);
76
+ return schema;
77
+ }
78
+ async function resolveModel(config, cwd, importFn) {
79
+ if (config.model)
80
+ return config.model;
81
+ if (config.schema) {
82
+ return {
83
+ schema: config.schema,
84
+ collection: { collectionName: config.collection || pluralizeFallback(config.name) },
85
+ };
86
+ }
87
+ if (!config.path) {
88
+ throw new Error(`Model ${config.name} needs path, model, or schema`);
89
+ }
90
+ const model = await (0, utils_1.loadFromModuleExportExpression)(config.path, {
91
+ defaultExportName: config.name,
92
+ cwd,
93
+ importFn,
94
+ });
95
+ if (!model) {
96
+ throw new Error(`Model ${config.name} cannot be imported from ${config.path}`);
97
+ }
98
+ return model;
99
+ }
100
+ function introspectPaths(schema) {
101
+ const out = [];
102
+ const paths = schema.paths || {};
103
+ for (const [name, schemaType] of Object.entries(paths)) {
104
+ const instance = schemaType.instance || schemaType.constructor?.name || 'Mixed';
105
+ const enumValues = schemaType.enumValues || schemaType.options?.enum;
106
+ const ref = schemaType.options?.ref ||
107
+ schemaType.caster?.options?.ref ||
108
+ schemaType.schema?.options?.ref;
109
+ out.push({
110
+ name,
111
+ instance: String(instance),
112
+ isArray: instance === 'Array' || Array.isArray(schemaType.options?.type),
113
+ enumValues: Array.isArray(enumValues) ? enumValues.map(String) : undefined,
114
+ ref: ref ? String(ref) : undefined,
115
+ });
116
+ }
117
+ return out;
118
+ }
119
+ function selectionForPaths(paths) {
120
+ const lines = ['id: _id'];
121
+ for (const path of paths) {
122
+ if (path.name === '_id' || path.name === '__v' || path.name.includes('.'))
123
+ continue;
124
+ if (path.ref) {
125
+ // Scalar id field kept; populated object is a sibling field.
126
+ lines.push(`${path.name}: ${path.name}`);
127
+ continue;
128
+ }
129
+ lines.push(`${path.name}: ${path.name}`);
130
+ }
131
+ return blockArg(lines.join('\n'));
132
+ }
133
+ function emitModelObjectType(schemaComposer, opts) {
134
+ const { subgraphName, typeName, collection, paths, naming } = opts;
135
+ const otc = schemaComposer.createObjectTC({ name: typeName, fields: {} });
136
+ const selection = selectionForPaths(paths);
137
+ otc.setField('id', { type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLID) });
138
+ for (const path of paths) {
139
+ if (path.name === '_id' || path.name === '__v' || path.name.includes('.'))
140
+ continue;
141
+ otc.setField(path.name, { type: mapPathToOutput(path) });
142
+ }
143
+ otc.setDirectives([
144
+ { name: 'key', args: { fields: 'id' } },
145
+ {
146
+ name: 'mongo',
147
+ args: {
148
+ subgraph: subgraphName,
149
+ collection,
150
+ kind: 'FIND_ONE',
151
+ filter: blockArg('_id: $this.id'),
152
+ selection,
153
+ },
154
+ },
155
+ ]);
156
+ createFilterSortAndInputs(schemaComposer, naming, typeName, paths);
157
+ createConnectionTypes(schemaComposer, naming, typeName);
158
+ createPayloadType(schemaComposer, naming, typeName);
159
+ }
160
+ function emitModelRootFields(schemaComposer, opts) {
161
+ const { subgraphName, typeName, collection, paths, naming } = opts;
162
+ const selection = selectionForPaths(paths);
163
+ const filterType = naming.filterInput(typeName);
164
+ const sortType = naming.sortInput(typeName);
165
+ const queryTC = schemaComposer.Query;
166
+ const mutationTC = schemaComposer.Mutation;
167
+ const mongo = (kind, extra = {}) => ({
168
+ name: 'mongo',
169
+ args: { subgraph: subgraphName, collection, kind, selection, ...extra },
170
+ });
171
+ queryTC.setField(naming.findByIdField(typeName), {
172
+ type: typeName,
173
+ args: { _id: { type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLID) } },
174
+ directives: [mongo('FIND_ONE', { filter: blockArg('_id: $args._id') })],
175
+ });
176
+ queryTC.setField(naming.dataLoaderField(typeName), {
177
+ type: typeName,
178
+ args: { _id: { type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLID) } },
179
+ directives: [mongo('FIND_ONE', { filter: blockArg('_id: $args._id') })],
180
+ });
181
+ queryTC.setField(naming.findByIdsField(typeName), {
182
+ type: `[${typeName}!]!`,
183
+ args: {
184
+ _ids: { type: new graphql_1.GraphQLNonNull(new graphql_1.GraphQLList(new graphql_1.GraphQLNonNull(graphql_1.GraphQLID))) },
185
+ limit: { type: graphql_1.GraphQLInt, defaultValue: 100 },
186
+ sort: { type: sortType },
187
+ },
188
+ directives: [
189
+ mongo('FIND_MANY', {
190
+ filter: blockArg('_id: $args._ids'),
191
+ limit: '$args.limit',
192
+ sort: '$args.sort',
193
+ }),
194
+ ],
195
+ });
196
+ queryTC.setField(naming.dataLoaderManyField(typeName), {
197
+ type: `[${typeName}]!`,
198
+ args: {
199
+ _ids: { type: new graphql_1.GraphQLNonNull(new graphql_1.GraphQLList(new graphql_1.GraphQLNonNull(graphql_1.GraphQLID))) },
200
+ },
201
+ directives: [mongo('FIND_MANY', { filter: blockArg('_id: $args._ids') })],
202
+ });
203
+ queryTC.setField(naming.findManyField(typeName), {
204
+ type: `[${typeName}!]!`,
205
+ args: {
206
+ filter: { type: filterType },
207
+ limit: { type: graphql_1.GraphQLInt, defaultValue: 100 },
208
+ skip: { type: graphql_1.GraphQLInt },
209
+ sort: { type: sortType },
210
+ },
211
+ directives: [
212
+ mongo('FIND_MANY', {
213
+ filter: '$args.filter',
214
+ sort: '$args.sort',
215
+ limit: '$args.limit',
216
+ skip: '$args.skip',
217
+ }),
218
+ ],
219
+ });
220
+ queryTC.setField(naming.findOneField(typeName), {
221
+ type: typeName,
222
+ args: {
223
+ filter: { type: filterType },
224
+ skip: { type: graphql_1.GraphQLInt },
225
+ sort: { type: sortType },
226
+ },
227
+ directives: [
228
+ mongo('FIND_ONE', {
229
+ filter: '$args.filter',
230
+ sort: '$args.sort',
231
+ skip: '$args.skip',
232
+ }),
233
+ ],
234
+ });
235
+ queryTC.setField(naming.countField(typeName), {
236
+ type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLInt),
237
+ args: { filter: { type: filterType } },
238
+ directives: [mongo('COUNT', { filter: '$args.filter', selection: blockArg('$') })],
239
+ });
240
+ queryTC.setField(naming.connectionField(typeName), {
241
+ type: naming.connectionType(typeName),
242
+ args: {
243
+ filter: { type: filterType },
244
+ first: { type: graphql_1.GraphQLInt },
245
+ after: { type: graphql_1.GraphQLString },
246
+ sort: { type: sortType },
247
+ },
248
+ directives: [
249
+ mongo('CONNECTION', {
250
+ filter: '$args.filter',
251
+ sort: '$args.sort',
252
+ first: '$args.first',
253
+ after: '$args.after',
254
+ selection: blockArg(`
255
+ nodes {
256
+ ${indent(selectionForPaths(paths).replace(/^\n/, ''), 1)}
257
+ }
258
+ edges {
259
+ cursor: $cursor
260
+ node {
261
+ ${indent(selectionForPaths(paths).replace(/^\n/, ''), 2)}
262
+ }
263
+ }
264
+ pageInfo {
265
+ hasNextPage: $hasNextPage
266
+ hasPreviousPage: $hasPreviousPage
267
+ startCursor: $startCursor
268
+ endCursor: $endCursor
269
+ }
270
+ totalCount: $totalCount
271
+ `),
272
+ }),
273
+ ],
274
+ });
275
+ queryTC.setField(naming.paginationField(typeName), {
276
+ type: naming.paginationType(typeName),
277
+ args: {
278
+ filter: { type: filterType },
279
+ page: { type: graphql_1.GraphQLInt },
280
+ perPage: { type: graphql_1.GraphQLInt, defaultValue: 20 },
281
+ sort: { type: sortType },
282
+ },
283
+ directives: [
284
+ mongo('PAGINATION', {
285
+ filter: '$args.filter',
286
+ sort: '$args.sort',
287
+ page: '$args.page',
288
+ perPage: '$args.perPage',
289
+ selection: blockArg(`
290
+ count: $count
291
+ items {
292
+ ${indent(selectionForPaths(paths).replace(/^\n/, ''), 1)}
293
+ }
294
+ pageInfo {
295
+ currentPage: $currentPage
296
+ perPage: $perPage
297
+ pageCount: $pageCount
298
+ itemCount: $itemCount
299
+ hasNextPage: $hasNextPage
300
+ hasPreviousPage: $hasPreviousPage
301
+ }
302
+ `),
303
+ }),
304
+ ],
305
+ });
306
+ const setBlock = blockArg(paths
307
+ .filter(p => p.name !== '_id' && p.name !== '__v' && !p.name.includes('.'))
308
+ .map(p => `${p.name}: $args.record.${p.name}`)
309
+ .join('\n'));
310
+ const payloadSelection = blockArg(`
311
+ record {
312
+ ${indent(selectionForPaths(paths).replace(/^\n/, ''), 1)}
313
+ }
314
+ recordId: $_id
315
+ `);
316
+ mutationTC.setField(naming.createOneField(typeName), {
317
+ type: naming.payloadType(typeName),
318
+ args: { record: { type: `${naming.createInput(typeName)}!` } },
319
+ directives: [mongo('INSERT_ONE', { set: setBlock, selection: payloadSelection })],
320
+ });
321
+ mutationTC.setField(naming.createManyField(typeName), {
322
+ type: naming.payloadType(typeName),
323
+ args: { records: { type: `[${naming.createInput(typeName)}!]!` } },
324
+ directives: [
325
+ mongo('INSERT_MANY', {
326
+ set: blockArg('records: $args.records'),
327
+ selection: blockArg(`
328
+ records {
329
+ ${indent(selectionForPaths(paths).replace(/^\n/, ''), 1)}
330
+ }
331
+ recordIds: $recordIds
332
+ createdCount: $createdCount
333
+ `),
334
+ }),
335
+ ],
336
+ });
337
+ mutationTC.setField(naming.updateByIdField(typeName), {
338
+ type: naming.payloadType(typeName),
339
+ args: {
340
+ _id: { type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLID) },
341
+ record: { type: `${naming.updateInput(typeName)}!` },
342
+ },
343
+ directives: [
344
+ mongo('UPDATE_ONE', {
345
+ filter: blockArg('_id: $args._id'),
346
+ set: setBlock,
347
+ selection: payloadSelection,
348
+ }),
349
+ ],
350
+ });
351
+ mutationTC.setField(naming.updateOneField(typeName), {
352
+ type: naming.payloadType(typeName),
353
+ args: {
354
+ filter: { type: filterType },
355
+ record: { type: `${naming.updateInput(typeName)}!` },
356
+ },
357
+ directives: [
358
+ mongo('UPDATE_ONE', {
359
+ filter: '$args.filter',
360
+ set: setBlock,
361
+ selection: payloadSelection,
362
+ }),
363
+ ],
364
+ });
365
+ mutationTC.setField(naming.updateManyField(typeName), {
366
+ type: naming.payloadType(typeName),
367
+ args: {
368
+ filter: { type: filterType },
369
+ record: { type: `${naming.updateInput(typeName)}!` },
370
+ },
371
+ directives: [
372
+ mongo('UPDATE_MANY', {
373
+ filter: '$args.filter',
374
+ set: setBlock,
375
+ selection: blockArg(`
376
+ modifiedCount: $modifiedCount
377
+ matchedCount: $matchedCount
378
+ `),
379
+ }),
380
+ ],
381
+ });
382
+ mutationTC.setField(naming.removeByIdField(typeName), {
383
+ type: naming.payloadType(typeName),
384
+ args: { _id: { type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLID) } },
385
+ directives: [
386
+ mongo('DELETE_ONE', {
387
+ filter: blockArg('_id: $args._id'),
388
+ selection: payloadSelection,
389
+ }),
390
+ ],
391
+ });
392
+ mutationTC.setField(naming.removeOneField(typeName), {
393
+ type: naming.payloadType(typeName),
394
+ args: { filter: { type: filterType } },
395
+ directives: [
396
+ mongo('DELETE_ONE', {
397
+ filter: '$args.filter',
398
+ selection: payloadSelection,
399
+ }),
400
+ ],
401
+ });
402
+ mutationTC.setField(naming.removeManyField(typeName), {
403
+ type: naming.payloadType(typeName),
404
+ args: { filter: { type: `${filterType}!` } },
405
+ directives: [
406
+ mongo('DELETE_MANY', {
407
+ filter: '$args.filter',
408
+ selection: blockArg('deletedCount: $deletedCount'),
409
+ }),
410
+ ],
411
+ });
412
+ }
413
+ function emitRefFields(schemaComposer, opts) {
414
+ const paths = introspectPaths(opts.schema);
415
+ const otc = schemaComposer.getOTC(opts.typeName);
416
+ for (const path of paths) {
417
+ if (!path.ref || !opts.knownTypes.has(path.ref))
418
+ continue;
419
+ const refCollection = opts.collectionsByType.get(path.ref);
420
+ if (!refCollection)
421
+ continue;
422
+ const refPaths = introspectPaths(
423
+ // best-effort: empty selection of id only if we cannot re-introspect
424
+ opts.schema);
425
+ void refPaths;
426
+ const fieldName = path.isArray ? pluralizeFallback(path.ref) : path.ref[0].toLowerCase() + path.ref.slice(1);
427
+ // Prefer path name without Id suffix when present (authorId -> author)
428
+ const pretty = path.name.endsWith('Id') || path.name.endsWith('Ids')
429
+ ? path.name.replace(/Ids?$/, '')
430
+ : path.name + 'Doc';
431
+ const finalName = otc.hasField(pretty) ? fieldName : pretty;
432
+ if (otc.hasField(finalName) && finalName !== path.name) {
433
+ // already set
434
+ }
435
+ const refSelection = blockArg('id: _id');
436
+ if (path.isArray) {
437
+ otc.setField(finalName, {
438
+ type: `[${path.ref}!]!`,
439
+ directives: [
440
+ {
441
+ name: 'mongo',
442
+ args: {
443
+ subgraph: opts.subgraphName,
444
+ collection: refCollection,
445
+ kind: 'FIND_MANY',
446
+ filter: blockArg(`_id: $this.${path.name}`),
447
+ selection: refSelection,
448
+ },
449
+ },
450
+ ],
451
+ });
452
+ }
453
+ else {
454
+ otc.setField(finalName, {
455
+ type: path.ref,
456
+ directives: [
457
+ {
458
+ name: 'mongo',
459
+ args: {
460
+ subgraph: opts.subgraphName,
461
+ collection: refCollection,
462
+ kind: 'FIND_ONE',
463
+ filter: blockArg(`_id: $this.${path.name}`),
464
+ selection: refSelection,
465
+ },
466
+ },
467
+ ],
468
+ });
469
+ }
470
+ }
471
+ }
472
+ function mapPathToOutput(path) {
473
+ if (path.enumValues?.length) {
474
+ return path.isArray ? new graphql_1.GraphQLList(graphql_1.GraphQLString) : graphql_1.GraphQLString;
475
+ }
476
+ if (path.ref) {
477
+ const idType = graphql_1.GraphQLID;
478
+ return path.isArray ? new graphql_1.GraphQLList(idType) : idType;
479
+ }
480
+ if (path.isArray && path.instance !== 'Array') {
481
+ return new graphql_1.GraphQLList((0, type_map_js_1.mapMongoPathToGraphQL)(path.instance));
482
+ }
483
+ return (0, type_map_js_1.mapMongoPathToGraphQL)(path.instance, { asId: path.name === '_id' });
484
+ }
485
+ function createFilterSortAndInputs(schemaComposer, naming, typeName, paths) {
486
+ const filterName = naming.filterInput(typeName);
487
+ if (!schemaComposer.has(filterName)) {
488
+ schemaComposer.createInputTC({
489
+ name: filterName,
490
+ fields: {
491
+ AND: { type: `[${filterName}!]` },
492
+ OR: { type: `[${filterName}!]` },
493
+ NOT: { type: filterName },
494
+ id: { type: 'IDFilter' },
495
+ ...Object.fromEntries(paths
496
+ .filter(p => p.name !== '_id' && p.name !== '__v' && !p.name.includes('.'))
497
+ .map(p => {
498
+ let filterType = 'StringFilter';
499
+ if (p.instance === 'Number' || p.instance === 'Decimal128')
500
+ filterType = 'FloatFilter';
501
+ else if (p.instance === 'Boolean')
502
+ filterType = 'BooleanFilter';
503
+ else if (p.instance === 'Date')
504
+ filterType = 'DateTimeFilter';
505
+ else if (p.ref || p.instance === 'ObjectID' || p.instance === 'ObjectId')
506
+ filterType = 'IDFilter';
507
+ return [p.name, { type: filterType }];
508
+ })),
509
+ },
510
+ });
511
+ }
512
+ if (!schemaComposer.has(naming.sortInput(typeName))) {
513
+ if (!schemaComposer.has('SortDirection')) {
514
+ schemaComposer.createEnumTC({
515
+ name: 'SortDirection',
516
+ values: { ASC: { value: 'ASC' }, DESC: { value: 'DESC' } },
517
+ });
518
+ }
519
+ schemaComposer.createInputTC({
520
+ name: naming.sortInput(typeName),
521
+ fields: {
522
+ id: 'SortDirection',
523
+ ...Object.fromEntries(paths
524
+ .filter(p => p.name !== '_id' && p.name !== '__v' && !p.name.includes('.'))
525
+ .map(p => [p.name, { type: 'SortDirection' }])),
526
+ },
527
+ });
528
+ }
529
+ const createFields = {};
530
+ const updateFields = {};
531
+ for (const path of paths) {
532
+ if (path.name === '_id' || path.name === '__v' || path.name.includes('.'))
533
+ continue;
534
+ const gqlType = mapPathToOutput(path);
535
+ createFields[path.name] = { type: gqlType };
536
+ updateFields[path.name] = { type: gqlType };
537
+ }
538
+ if (!schemaComposer.has(naming.createInput(typeName))) {
539
+ schemaComposer.createInputTC({ name: naming.createInput(typeName), fields: createFields });
540
+ }
541
+ if (!schemaComposer.has(naming.updateInput(typeName))) {
542
+ schemaComposer.createInputTC({ name: naming.updateInput(typeName), fields: updateFields });
543
+ }
544
+ }
545
+ function createConnectionTypes(schemaComposer, naming, typeName) {
546
+ if (!schemaComposer.has(naming.edgeType(typeName))) {
547
+ schemaComposer.createObjectTC({
548
+ name: naming.edgeType(typeName),
549
+ fields: { cursor: 'String!', node: `${typeName}!` },
550
+ });
551
+ }
552
+ if (!schemaComposer.has(naming.connectionType(typeName))) {
553
+ schemaComposer.createObjectTC({
554
+ name: naming.connectionType(typeName),
555
+ fields: {
556
+ edges: `[${naming.edgeType(typeName)}!]!`,
557
+ nodes: `[${typeName}!]!`,
558
+ pageInfo: 'PageInfo!',
559
+ totalCount: 'Int!',
560
+ },
561
+ });
562
+ }
563
+ if (!schemaComposer.has(naming.paginationType(typeName))) {
564
+ schemaComposer.createObjectTC({
565
+ name: naming.paginationType(typeName),
566
+ fields: {
567
+ count: 'Int',
568
+ items: `[${typeName}]`,
569
+ pageInfo: 'PaginationInfo',
570
+ },
571
+ });
572
+ }
573
+ }
574
+ function createPayloadType(schemaComposer, naming, typeName) {
575
+ if (!schemaComposer.has(naming.payloadType(typeName))) {
576
+ schemaComposer.createObjectTC({
577
+ name: naming.payloadType(typeName),
578
+ fields: {
579
+ record: typeName,
580
+ recordId: graphql_1.GraphQLID,
581
+ records: `[${typeName}!]`,
582
+ recordIds: '[ID!]',
583
+ createdCount: graphql_1.GraphQLInt,
584
+ modifiedCount: graphql_1.GraphQLInt,
585
+ matchedCount: graphql_1.GraphQLInt,
586
+ deletedCount: graphql_1.GraphQLInt,
587
+ },
588
+ });
589
+ }
590
+ }
591
+ function ensurePageInfo(schemaComposer) {
592
+ if (!schemaComposer.has('PageInfo')) {
593
+ schemaComposer.createObjectTC({
594
+ name: 'PageInfo',
595
+ fields: {
596
+ hasNextPage: 'Boolean!',
597
+ hasPreviousPage: 'Boolean!',
598
+ startCursor: 'String',
599
+ endCursor: 'String',
600
+ },
601
+ });
602
+ }
603
+ if (!schemaComposer.has('PaginationInfo')) {
604
+ schemaComposer.createObjectTC({
605
+ name: 'PaginationInfo',
606
+ fields: {
607
+ currentPage: 'Int!',
608
+ perPage: 'Int!',
609
+ pageCount: 'Int',
610
+ itemCount: 'Int',
611
+ hasNextPage: 'Boolean',
612
+ hasPreviousPage: 'Boolean',
613
+ },
614
+ });
615
+ }
616
+ }
617
+ function ensureSharedFilterInputs(schemaComposer) {
618
+ if (!schemaComposer.has('IDFilter')) {
619
+ schemaComposer.createInputTC({
620
+ name: 'IDFilter',
621
+ fields: {
622
+ eq: graphql_1.GraphQLID,
623
+ ne: graphql_1.GraphQLID,
624
+ in: { type: new graphql_1.GraphQLList(new graphql_1.GraphQLNonNull(graphql_1.GraphQLID)) },
625
+ nin: { type: new graphql_1.GraphQLList(new graphql_1.GraphQLNonNull(graphql_1.GraphQLID)) },
626
+ exists: 'Boolean',
627
+ },
628
+ });
629
+ }
630
+ if (!schemaComposer.has('StringFilter')) {
631
+ schemaComposer.createInputTC({
632
+ name: 'StringFilter',
633
+ fields: {
634
+ eq: graphql_1.GraphQLString,
635
+ ne: graphql_1.GraphQLString,
636
+ in: { type: '[String!]' },
637
+ nin: { type: '[String!]' },
638
+ regex: graphql_1.GraphQLString,
639
+ exists: 'Boolean',
640
+ },
641
+ });
642
+ }
643
+ if (!schemaComposer.has('FloatFilter')) {
644
+ schemaComposer.createInputTC({
645
+ name: 'FloatFilter',
646
+ fields: {
647
+ eq: graphql_1.GraphQLFloat,
648
+ ne: graphql_1.GraphQLFloat,
649
+ in: { type: '[Float!]' },
650
+ nin: { type: '[Float!]' },
651
+ gt: graphql_1.GraphQLFloat,
652
+ gte: graphql_1.GraphQLFloat,
653
+ lt: graphql_1.GraphQLFloat,
654
+ lte: graphql_1.GraphQLFloat,
655
+ exists: 'Boolean',
656
+ },
657
+ });
658
+ }
659
+ if (!schemaComposer.has('BooleanFilter')) {
660
+ schemaComposer.createInputTC({
661
+ name: 'BooleanFilter',
662
+ fields: { eq: 'Boolean', ne: 'Boolean', exists: 'Boolean' },
663
+ });
664
+ }
665
+ if (!schemaComposer.has('DateTimeFilter')) {
666
+ schemaComposer.createInputTC({
667
+ name: 'DateTimeFilter',
668
+ fields: {
669
+ eq: 'DateTime',
670
+ ne: 'DateTime',
671
+ in: { type: '[DateTime!]' },
672
+ nin: { type: '[DateTime!]' },
673
+ gt: 'DateTime',
674
+ gte: 'DateTime',
675
+ lt: 'DateTime',
676
+ lte: 'DateTime',
677
+ exists: 'Boolean',
678
+ },
679
+ });
680
+ }
681
+ }
682
+ function markMongoDirectiveStringsAsBlocks(schema) {
683
+ const markNode = (astNode) => {
684
+ const directives = astNode?.directives;
685
+ if (!directives)
686
+ return;
687
+ for (const directive of directives) {
688
+ if (directive.name.value !== 'mongo' || !directive.arguments)
689
+ continue;
690
+ for (const arg of directive.arguments) {
691
+ if (arg.value.kind !== 'StringValue')
692
+ continue;
693
+ if (arg.name.value !== 'filter' &&
694
+ arg.name.value !== 'where' &&
695
+ arg.name.value !== 'set' &&
696
+ arg.name.value !== 'selection' &&
697
+ arg.name.value !== 'pipeline') {
698
+ continue;
699
+ }
700
+ if (isBareBinder(arg.value.value))
701
+ continue;
702
+ arg.value.block = true;
703
+ }
704
+ }
705
+ };
706
+ for (const type of Object.values(schema.getTypeMap())) {
707
+ if (!(type instanceof graphql_1.GraphQLObjectType))
708
+ continue;
709
+ markNode(type.astNode);
710
+ for (const field of Object.values(type.getFields())) {
711
+ markNode(field.astNode);
712
+ }
713
+ }
714
+ }
715
+ function isBareBinder(value) {
716
+ const trimmed = value.trim();
717
+ if (!trimmed.startsWith('$') ||
718
+ trimmed.includes('\n') ||
719
+ trimmed.includes(':') ||
720
+ trimmed.includes(' ')) {
721
+ return false;
722
+ }
723
+ if (trimmed.includes('...'))
724
+ return false;
725
+ const segments = trimmed.slice(1).split('.');
726
+ if (!segments.length || segments.some(s => !s))
727
+ return false;
728
+ for (const seg of segments) {
729
+ const c0 = seg.charCodeAt(0);
730
+ const okStart = (c0 >= 65 && c0 <= 90) || (c0 >= 97 && c0 <= 122) || c0 === 95;
731
+ if (!okStart)
732
+ return false;
733
+ for (let i = 1; i < seg.length; i++) {
734
+ const c = seg.charCodeAt(i);
735
+ const ok = (c >= 65 && c <= 90) || (c >= 97 && c <= 122) || (c >= 48 && c <= 57) || c === 95;
736
+ if (!ok)
737
+ return false;
738
+ }
739
+ }
740
+ return true;
741
+ }
742
+ function blockArg(body) {
743
+ const trimmed = body.replace(/^\n+|\n+$/g, '');
744
+ const indented = trimmed
745
+ .split('\n')
746
+ .map(line => (line.length ? ` ${line}` : line))
747
+ .join('\n');
748
+ return indented.includes('\n') ? indented : `\n${indented}`;
749
+ }
750
+ function indent(body, levels) {
751
+ const pad = ' '.repeat(levels);
752
+ return body
753
+ .split('\n')
754
+ .map(line => (line.length ? pad + line : line))
755
+ .join('\n');
756
+ }
757
+ function pluralizeFallback(name) {
758
+ const lower = name.toLowerCase();
759
+ if (lower.endsWith('s'))
760
+ return lower;
761
+ return `${lower}s`;
762
+ }