drizzle-graphql-plus 0.8.8 → 0.8.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.cjs ADDED
@@ -0,0 +1,2125 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc2) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc2 = __getOwnPropDesc(from, key)) || desc2.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ buildSchema: () => buildSchema
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+ var import_drizzle_orm7 = require("drizzle-orm");
27
+ var import_mysql_core3 = require("drizzle-orm/mysql-core");
28
+ var import_pg_core3 = require("drizzle-orm/pg-core");
29
+ var import_sqlite_core3 = require("drizzle-orm/sqlite-core");
30
+ var import_graphql7 = require("graphql");
31
+
32
+ // src/util/builders/mysql.ts
33
+ var import_drizzle_orm4 = require("drizzle-orm");
34
+ var import_mysql_core2 = require("drizzle-orm/mysql-core");
35
+ var import_graphql4 = require("graphql");
36
+
37
+ // src/util/builders/common.ts
38
+ var import_drizzle_orm3 = require("drizzle-orm");
39
+ var import_graphql3 = require("graphql");
40
+
41
+ // src/util/case-ops/index.ts
42
+ var uncapitalize = (input) => input.length ? `${input[0].toLocaleLowerCase()}${input.length > 1 ? input.slice(1, input.length) : ""}` : input;
43
+ var capitalize = (input) => input.length ? `${input[0].toLocaleUpperCase()}${input.length > 1 ? input.slice(1, input.length) : ""}` : input;
44
+
45
+ // src/util/data-mappers/index.ts
46
+ var import_drizzle_orm = require("drizzle-orm");
47
+ var import_graphql = require("graphql");
48
+ var remapToGraphQLCore = (key, value, tableName, column, relationMap) => {
49
+ if (value instanceof Date)
50
+ return value.toISOString();
51
+ if (value instanceof Buffer)
52
+ return Array.from(value);
53
+ if (typeof value === "bigint")
54
+ return value.toString();
55
+ if (Array.isArray(value)) {
56
+ const relations = relationMap?.[tableName];
57
+ if (relations?.[key]) {
58
+ return remapToGraphQLArrayOutput(
59
+ value,
60
+ relations[key].targetTableName,
61
+ relations[key].relation.referencedTable,
62
+ relationMap
63
+ );
64
+ }
65
+ if (column.columnType === "PgGeometry" || column.columnType === "PgVector")
66
+ return value;
67
+ return value.map((arrVal) => remapToGraphQLCore(key, arrVal, tableName, column, relationMap));
68
+ }
69
+ if (typeof value === "object") {
70
+ const relations = relationMap?.[tableName];
71
+ if (relations?.[key]) {
72
+ return remapToGraphQLSingleOutput(
73
+ value,
74
+ relations[key].targetTableName,
75
+ relations[key].relation.referencedTable,
76
+ relationMap
77
+ );
78
+ }
79
+ if (column.columnType === "PgGeometryObject")
80
+ return value;
81
+ return JSON.stringify(value);
82
+ }
83
+ return value;
84
+ };
85
+ var remapToGraphQLSingleOutput = (queryOutput, tableName, table, relationMap) => {
86
+ for (const [key, value] of Object.entries(queryOutput)) {
87
+ if (value === void 0 || value === null) {
88
+ delete queryOutput[key];
89
+ } else {
90
+ queryOutput[key] = remapToGraphQLCore(key, value, tableName, table[key], relationMap);
91
+ }
92
+ }
93
+ return queryOutput;
94
+ };
95
+ var remapToGraphQLArrayOutput = (queryOutput, tableName, table, relationMap) => {
96
+ for (const entry of queryOutput) {
97
+ remapToGraphQLSingleOutput(entry, tableName, table, relationMap);
98
+ }
99
+ return queryOutput;
100
+ };
101
+ var remapFromGraphQLCore = (value, column, columnName) => {
102
+ switch (column.dataType) {
103
+ case "date": {
104
+ const formatted = new Date(value);
105
+ if (Number.isNaN(formatted.getTime()))
106
+ throw new import_graphql.GraphQLError(`Field '${columnName}' is not a valid date!`);
107
+ return formatted;
108
+ }
109
+ case "buffer": {
110
+ if (!Array.isArray(value)) {
111
+ throw new import_graphql.GraphQLError(`Field '${columnName}' is not an array!`);
112
+ }
113
+ return Buffer.from(value);
114
+ }
115
+ case "json": {
116
+ if (column.columnType === "PgGeometryObject")
117
+ return value;
118
+ try {
119
+ return JSON.parse(value);
120
+ } catch (e) {
121
+ throw new import_graphql.GraphQLError(
122
+ `Invalid JSON in field '${columnName}':
123
+ ${e instanceof Error ? e.message : "Unknown error"}`
124
+ );
125
+ }
126
+ }
127
+ case "array": {
128
+ if (!Array.isArray(value)) {
129
+ throw new import_graphql.GraphQLError(`Field '${columnName}' is not an array!`);
130
+ }
131
+ if (column.columnType === "PgGeometry" && value.length !== 2) {
132
+ throw new import_graphql.GraphQLError(
133
+ `Invalid float tuple in field '${columnName}': expected array with length of 2, received ${value.length}`
134
+ );
135
+ }
136
+ return value;
137
+ }
138
+ case "bigint": {
139
+ try {
140
+ return BigInt(value);
141
+ } catch (error) {
142
+ throw new import_graphql.GraphQLError(`Field '${columnName}' is not a BigInt!`);
143
+ }
144
+ }
145
+ default: {
146
+ return value;
147
+ }
148
+ }
149
+ };
150
+ var remapFromGraphQLSingleInput = (queryInput, table) => {
151
+ for (const [key, value] of Object.entries(queryInput)) {
152
+ if (value === void 0) {
153
+ delete queryInput[key];
154
+ } else {
155
+ const column = (0, import_drizzle_orm.getTableColumns)(table)[key];
156
+ if (!column)
157
+ throw new import_graphql.GraphQLError(`Unknown column: ${key}`);
158
+ if (value === null && column.notNull) {
159
+ delete queryInput[key];
160
+ continue;
161
+ }
162
+ queryInput[key] = remapFromGraphQLCore(value, column, key);
163
+ }
164
+ }
165
+ return queryInput;
166
+ };
167
+ var remapFromGraphQLArrayInput = (queryInput, table) => {
168
+ for (const entry of queryInput)
169
+ remapFromGraphQLSingleInput(entry, table);
170
+ return queryInput;
171
+ };
172
+
173
+ // src/util/type-converter/index.ts
174
+ var import_drizzle_orm2 = require("drizzle-orm");
175
+ var import_mysql_core = require("drizzle-orm/mysql-core");
176
+ var import_pg_core = require("drizzle-orm/pg-core");
177
+ var import_sqlite_core = require("drizzle-orm/sqlite-core");
178
+ var import_graphql2 = require("graphql");
179
+ var allowedNameChars = /^[a-zA-Z0-9_]+$/;
180
+ var enumMap = /* @__PURE__ */ new WeakMap();
181
+ var generateEnumCached = (column, columnName, tableName) => {
182
+ if (enumMap.has(column))
183
+ return enumMap.get(column);
184
+ const gqlEnum = new import_graphql2.GraphQLEnumType({
185
+ name: `${capitalize(tableName)}${capitalize(columnName)}Enum`,
186
+ values: Object.fromEntries(column.enumValues.map((e, index) => [allowedNameChars.test(e) ? e : `Option${index}`, {
187
+ value: e,
188
+ description: `Value: ${e}`
189
+ }]))
190
+ });
191
+ enumMap.set(column, gqlEnum);
192
+ return gqlEnum;
193
+ };
194
+ var geoXyType = new import_graphql2.GraphQLObjectType({
195
+ name: "PgGeometryObject",
196
+ fields: {
197
+ x: { type: import_graphql2.GraphQLFloat },
198
+ y: { type: import_graphql2.GraphQLFloat }
199
+ }
200
+ });
201
+ var geoXyInputType = new import_graphql2.GraphQLInputObjectType({
202
+ name: "PgGeometryObjectInput",
203
+ fields: {
204
+ x: { type: import_graphql2.GraphQLFloat },
205
+ y: { type: import_graphql2.GraphQLFloat }
206
+ }
207
+ });
208
+ var columnToGraphQLCore = (column, columnName, tableName, isInput) => {
209
+ switch (column.dataType) {
210
+ case "boolean":
211
+ return { type: import_graphql2.GraphQLBoolean, description: "Boolean" };
212
+ case "json":
213
+ return column.columnType === "PgGeometryObject" ? {
214
+ type: isInput ? geoXyInputType : geoXyType,
215
+ description: "Geometry points XY"
216
+ } : { type: import_graphql2.GraphQLString, description: "JSON" };
217
+ case "date":
218
+ return { type: import_graphql2.GraphQLString, description: "Date" };
219
+ case "string":
220
+ if (column.enumValues?.length)
221
+ return { type: generateEnumCached(column, columnName, tableName) };
222
+ return { type: import_graphql2.GraphQLString, description: "String" };
223
+ case "bigint":
224
+ return { type: import_graphql2.GraphQLString, description: "BigInt" };
225
+ case "number":
226
+ return (0, import_drizzle_orm2.is)(column, import_pg_core.PgInteger) || (0, import_drizzle_orm2.is)(column, import_pg_core.PgSerial) || (0, import_drizzle_orm2.is)(column, import_mysql_core.MySqlInt) || (0, import_drizzle_orm2.is)(column, import_mysql_core.MySqlSerial) || (0, import_drizzle_orm2.is)(column, import_sqlite_core.SQLiteInteger) ? { type: import_graphql2.GraphQLInt, description: "Integer" } : { type: import_graphql2.GraphQLFloat, description: "Float" };
227
+ case "buffer":
228
+ return { type: new import_graphql2.GraphQLList(new import_graphql2.GraphQLNonNull(import_graphql2.GraphQLInt)), description: "Buffer" };
229
+ case "array": {
230
+ if (column.columnType === "PgVector") {
231
+ return {
232
+ type: new import_graphql2.GraphQLList(new import_graphql2.GraphQLNonNull(import_graphql2.GraphQLFloat)),
233
+ description: "Array<Float>"
234
+ };
235
+ }
236
+ if (column.columnType === "PgGeometry") {
237
+ return {
238
+ type: new import_graphql2.GraphQLList(new import_graphql2.GraphQLNonNull(import_graphql2.GraphQLFloat)),
239
+ description: "Tuple<[Float, Float]>"
240
+ };
241
+ }
242
+ const innerType = columnToGraphQLCore(
243
+ column.baseColumn,
244
+ columnName,
245
+ tableName,
246
+ isInput
247
+ );
248
+ return {
249
+ type: new import_graphql2.GraphQLList(new import_graphql2.GraphQLNonNull(innerType.type)),
250
+ description: `Array<${innerType.description}>`
251
+ };
252
+ }
253
+ case "custom":
254
+ default:
255
+ throw new Error(`Drizzle-GraphQL Error: Type ${column.dataType} is not implemented!`);
256
+ }
257
+ };
258
+ var drizzleColumnToGraphQLType = (column, columnName, tableName, forceNullable = false, defaultIsNullable = false, isInput = false) => {
259
+ const typeDesc = columnToGraphQLCore(column, columnName, tableName, isInput);
260
+ const noDesc = ["string", "boolean", "number"];
261
+ if (noDesc.find((e) => e === column.dataType))
262
+ delete typeDesc.description;
263
+ if (forceNullable)
264
+ return typeDesc;
265
+ if (column.notNull && !(defaultIsNullable && (column.hasDefault || column.defaultFn))) {
266
+ return {
267
+ type: new import_graphql2.GraphQLNonNull(typeDesc.type),
268
+ description: typeDesc.description
269
+ };
270
+ }
271
+ return typeDesc;
272
+ };
273
+
274
+ // src/util/builders/common.ts
275
+ var rqbCrashTypes = ["SQLiteBigInt", "SQLiteBlobJson", "SQLiteBlobBuffer"];
276
+ var extractSelectedColumnsFromTree = (tree, table) => {
277
+ const tableColumns = (0, import_drizzle_orm3.getTableColumns)(table);
278
+ const treeEntries = Object.entries(tree);
279
+ const selectedColumns = [];
280
+ for (const [fieldName, fieldData] of treeEntries) {
281
+ if (!tableColumns[fieldData.name])
282
+ continue;
283
+ selectedColumns.push([fieldData.name, true]);
284
+ }
285
+ if (!selectedColumns.length) {
286
+ const columnKeys = Object.entries(tableColumns);
287
+ const columnName = columnKeys.find(
288
+ (e) => rqbCrashTypes.find((haram) => e[1].columnType !== haram)
289
+ )?.[0] ?? columnKeys[0][0];
290
+ selectedColumns.push([columnName, true]);
291
+ }
292
+ return Object.fromEntries(selectedColumns);
293
+ };
294
+ var extractSelectedColumnsFromTreeSQLFormat = (tree, table) => {
295
+ const tableColumns = (0, import_drizzle_orm3.getTableColumns)(table);
296
+ const treeEntries = Object.entries(tree);
297
+ const selectedColumns = [];
298
+ for (const [fieldName, fieldData] of treeEntries) {
299
+ if (!tableColumns[fieldData.name])
300
+ continue;
301
+ selectedColumns.push([fieldData.name, tableColumns[fieldData.name]]);
302
+ }
303
+ if (!selectedColumns.length) {
304
+ const columnKeys = Object.entries(tableColumns);
305
+ const columnName = columnKeys.find(
306
+ (e) => rqbCrashTypes.find((haram) => e[1].columnType !== haram)
307
+ )?.[0] ?? columnKeys[0][0];
308
+ selectedColumns.push([columnName, tableColumns[columnName]]);
309
+ }
310
+ return Object.fromEntries(selectedColumns);
311
+ };
312
+ var innerOrder = new import_graphql3.GraphQLInputObjectType({
313
+ name: "InnerOrder",
314
+ fields: {
315
+ direction: {
316
+ type: new import_graphql3.GraphQLNonNull(
317
+ new import_graphql3.GraphQLEnumType({
318
+ name: "OrderDirection",
319
+ description: "Order by direction",
320
+ values: {
321
+ asc: {
322
+ value: "asc",
323
+ description: "Ascending order"
324
+ },
325
+ desc: {
326
+ value: "desc",
327
+ description: "Descending order"
328
+ }
329
+ }
330
+ })
331
+ )
332
+ },
333
+ priority: {
334
+ type: new import_graphql3.GraphQLNonNull(import_graphql3.GraphQLInt),
335
+ description: "Priority of current field"
336
+ }
337
+ }
338
+ });
339
+ var generateColumnFilterValues = (column, tableName, columnName) => {
340
+ const columnGraphQLType = drizzleColumnToGraphQLType(
341
+ column,
342
+ columnName,
343
+ tableName,
344
+ true,
345
+ false,
346
+ true
347
+ );
348
+ const columnArr = new import_graphql3.GraphQLList(new import_graphql3.GraphQLNonNull(columnGraphQLType.type));
349
+ const baseFields = {
350
+ eq: {
351
+ type: columnGraphQLType.type,
352
+ description: columnGraphQLType.description
353
+ },
354
+ ne: {
355
+ type: columnGraphQLType.type,
356
+ description: columnGraphQLType.description
357
+ },
358
+ lt: {
359
+ type: columnGraphQLType.type,
360
+ description: columnGraphQLType.description
361
+ },
362
+ lte: {
363
+ type: columnGraphQLType.type,
364
+ description: columnGraphQLType.description
365
+ },
366
+ gt: {
367
+ type: columnGraphQLType.type,
368
+ description: columnGraphQLType.description
369
+ },
370
+ gte: {
371
+ type: columnGraphQLType.type,
372
+ description: columnGraphQLType.description
373
+ },
374
+ like: { type: import_graphql3.GraphQLString },
375
+ notLike: { type: import_graphql3.GraphQLString },
376
+ ilike: { type: import_graphql3.GraphQLString },
377
+ notIlike: { type: import_graphql3.GraphQLString },
378
+ inArray: {
379
+ type: columnArr,
380
+ description: `Array<${columnGraphQLType.description}>`
381
+ },
382
+ notInArray: {
383
+ type: columnArr,
384
+ description: `Array<${columnGraphQLType.description}>`
385
+ },
386
+ isNull: { type: import_graphql3.GraphQLBoolean },
387
+ isNotNull: { type: import_graphql3.GraphQLBoolean }
388
+ };
389
+ const type = new import_graphql3.GraphQLInputObjectType({
390
+ name: `${capitalize(tableName)}${capitalize(columnName)}Filters`,
391
+ fields: {
392
+ ...baseFields,
393
+ OR: {
394
+ type: new import_graphql3.GraphQLList(
395
+ new import_graphql3.GraphQLNonNull(
396
+ new import_graphql3.GraphQLInputObjectType({
397
+ name: `${capitalize(tableName)}${capitalize(
398
+ columnName
399
+ )}filtersOr`,
400
+ fields: {
401
+ ...baseFields
402
+ }
403
+ })
404
+ )
405
+ )
406
+ }
407
+ }
408
+ });
409
+ return type;
410
+ };
411
+ var orderMap = /* @__PURE__ */ new WeakMap();
412
+ var generateTableOrderCached = (table) => {
413
+ if (orderMap.has(table))
414
+ return orderMap.get(table);
415
+ const columns = (0, import_drizzle_orm3.getTableColumns)(table);
416
+ const columnEntries = Object.entries(columns);
417
+ const remapped = Object.fromEntries(
418
+ columnEntries.map(([columnName, columnDescription]) => [
419
+ columnName,
420
+ { type: innerOrder }
421
+ ])
422
+ );
423
+ orderMap.set(table, remapped);
424
+ return remapped;
425
+ };
426
+ var filterMap = /* @__PURE__ */ new WeakMap();
427
+ var generateTableFilterValuesCached = (table, tableName) => {
428
+ if (filterMap.has(table))
429
+ return filterMap.get(table);
430
+ const columns = (0, import_drizzle_orm3.getTableColumns)(table);
431
+ const columnEntries = Object.entries(columns);
432
+ const remapped = Object.fromEntries(
433
+ columnEntries.map(([columnName, columnDescription]) => [
434
+ columnName,
435
+ {
436
+ type: generateColumnFilterValues(
437
+ columnDescription,
438
+ tableName,
439
+ columnName
440
+ )
441
+ }
442
+ ])
443
+ );
444
+ filterMap.set(table, remapped);
445
+ return remapped;
446
+ };
447
+ var fieldMap = /* @__PURE__ */ new WeakMap();
448
+ var generateTableSelectTypeFieldsCached = (table, tableName) => {
449
+ if (fieldMap.has(table))
450
+ return fieldMap.get(table);
451
+ const columns = (0, import_drizzle_orm3.getTableColumns)(table);
452
+ const columnEntries = Object.entries(columns);
453
+ const remapped = Object.fromEntries(
454
+ columnEntries.map(([columnName, columnDescription]) => [
455
+ columnName,
456
+ drizzleColumnToGraphQLType(columnDescription, columnName, tableName)
457
+ ])
458
+ );
459
+ fieldMap.set(table, remapped);
460
+ return remapped;
461
+ };
462
+ var orderTypeMap = /* @__PURE__ */ new WeakMap();
463
+ var generateTableOrderTypeCached = (table, tableName) => {
464
+ if (orderTypeMap.has(table))
465
+ return orderTypeMap.get(table);
466
+ const orderColumns = generateTableOrderCached(table);
467
+ const order = new import_graphql3.GraphQLInputObjectType({
468
+ name: `${capitalize(tableName)}OrderBy`,
469
+ fields: orderColumns
470
+ });
471
+ orderTypeMap.set(table, order);
472
+ return order;
473
+ };
474
+ var filterTypeMap = /* @__PURE__ */ new WeakMap();
475
+ var generateTableFilterTypeCached = (table, tableName) => {
476
+ if (filterTypeMap.has(table))
477
+ return filterTypeMap.get(table);
478
+ const filterColumns = generateTableFilterValuesCached(table, tableName);
479
+ const filters = new import_graphql3.GraphQLInputObjectType({
480
+ name: `${capitalize(tableName)}Filters`,
481
+ fields: {
482
+ ...filterColumns,
483
+ OR: {
484
+ type: new import_graphql3.GraphQLList(
485
+ new import_graphql3.GraphQLNonNull(
486
+ new import_graphql3.GraphQLInputObjectType({
487
+ name: `${capitalize(tableName)}FiltersOr`,
488
+ fields: filterColumns
489
+ })
490
+ )
491
+ )
492
+ }
493
+ }
494
+ });
495
+ filterTypeMap.set(table, filters);
496
+ return filters;
497
+ };
498
+ var generateSelectFields = (tables, tableName, relationMap, typeName, withOrder, relationsDepthLimit, currentDepth = 0, usedTables = /* @__PURE__ */ new Set()) => {
499
+ const relations = relationMap[tableName];
500
+ const relationEntries = relations ? Object.entries(relations) : [];
501
+ const table = tables[tableName];
502
+ const order = withOrder ? generateTableOrderTypeCached(table, tableName) : void 0;
503
+ const filters = generateTableFilterTypeCached(table, tableName);
504
+ const tableFields = generateTableSelectTypeFieldsCached(table, tableName);
505
+ if (usedTables.has(tableName) || typeof relationsDepthLimit === "number" && currentDepth >= relationsDepthLimit || !relationEntries.length) {
506
+ return {
507
+ order,
508
+ filters,
509
+ tableFields,
510
+ relationFields: {}
511
+ };
512
+ }
513
+ const rawRelationFields = [];
514
+ const updatedUsedTables = new Set(usedTables).add(tableName);
515
+ const newDepth = currentDepth + 1;
516
+ for (const [relationName, { targetTableName, relation }] of relationEntries) {
517
+ const relTypeName = `${typeName}${capitalize(relationName)}Relation`;
518
+ const isOne = (0, import_drizzle_orm3.is)(relation, import_drizzle_orm3.One);
519
+ const relData = generateSelectFields(
520
+ tables,
521
+ targetTableName,
522
+ relationMap,
523
+ relTypeName,
524
+ !isOne,
525
+ relationsDepthLimit,
526
+ newDepth,
527
+ updatedUsedTables
528
+ );
529
+ const relType = new import_graphql3.GraphQLObjectType({
530
+ name: relTypeName,
531
+ fields: { ...relData.tableFields, ...relData.relationFields }
532
+ });
533
+ if (isOne) {
534
+ rawRelationFields.push([
535
+ relationName,
536
+ {
537
+ type: relType,
538
+ args: {
539
+ where: { type: relData.filters }
540
+ }
541
+ }
542
+ ]);
543
+ continue;
544
+ }
545
+ rawRelationFields.push([
546
+ relationName,
547
+ {
548
+ type: new import_graphql3.GraphQLNonNull(new import_graphql3.GraphQLList(new import_graphql3.GraphQLNonNull(relType))),
549
+ args: {
550
+ where: { type: relData.filters },
551
+ orderBy: { type: relData.order },
552
+ offset: { type: import_graphql3.GraphQLInt },
553
+ limit: { type: import_graphql3.GraphQLInt }
554
+ }
555
+ }
556
+ ]);
557
+ }
558
+ const relationFields = Object.fromEntries(rawRelationFields);
559
+ return {
560
+ order,
561
+ filters,
562
+ tableFields,
563
+ relationFields
564
+ };
565
+ };
566
+ var generateTableTypes = (tableName, tables, relationMap, withReturning, relationsDepthLimit) => {
567
+ const stylizedName = capitalize(tableName);
568
+ const { tableFields, relationFields, filters, order } = generateSelectFields(
569
+ tables,
570
+ tableName,
571
+ relationMap,
572
+ stylizedName,
573
+ true,
574
+ relationsDepthLimit
575
+ );
576
+ const table = tables[tableName];
577
+ const columns = (0, import_drizzle_orm3.getTableColumns)(table);
578
+ const columnEntries = Object.entries(columns);
579
+ const insertFields = Object.fromEntries(
580
+ columnEntries.map(([columnName, columnDescription]) => [
581
+ columnName,
582
+ drizzleColumnToGraphQLType(
583
+ columnDescription,
584
+ columnName,
585
+ tableName,
586
+ false,
587
+ true,
588
+ true
589
+ )
590
+ ])
591
+ );
592
+ const updateFields = Object.fromEntries(
593
+ columnEntries.map(([columnName, columnDescription]) => [
594
+ columnName,
595
+ drizzleColumnToGraphQLType(
596
+ columnDescription,
597
+ columnName,
598
+ tableName,
599
+ true,
600
+ false,
601
+ true
602
+ )
603
+ ])
604
+ );
605
+ const insertInput = new import_graphql3.GraphQLInputObjectType({
606
+ name: `${stylizedName}InsertInput`,
607
+ fields: insertFields
608
+ });
609
+ const tableFieldsInterface = new import_graphql3.GraphQLInterfaceType({
610
+ name: `${stylizedName}Fields`,
611
+ fields: tableFields
612
+ });
613
+ const selectSingleOutput = new import_graphql3.GraphQLObjectType({
614
+ name: `${stylizedName}SelectItem`,
615
+ fields: { ...tableFields, ...relationFields },
616
+ interfaces: [tableFieldsInterface]
617
+ });
618
+ const selectArrOutput = new import_graphql3.GraphQLNonNull(
619
+ new import_graphql3.GraphQLList(new import_graphql3.GraphQLNonNull(selectSingleOutput))
620
+ );
621
+ const singleTableItemOutput = withReturning ? new import_graphql3.GraphQLObjectType({
622
+ name: `${stylizedName}Item`,
623
+ fields: tableFields,
624
+ interfaces: [tableFieldsInterface]
625
+ }) : void 0;
626
+ const arrTableItemOutput = withReturning ? new import_graphql3.GraphQLNonNull(
627
+ new import_graphql3.GraphQLList(new import_graphql3.GraphQLNonNull(singleTableItemOutput))
628
+ ) : void 0;
629
+ const updateInput = new import_graphql3.GraphQLInputObjectType({
630
+ name: `${stylizedName}UpdateInput`,
631
+ fields: updateFields
632
+ });
633
+ const inputs = {
634
+ insertInput,
635
+ updateInput,
636
+ tableOrder: order,
637
+ tableFilters: filters
638
+ };
639
+ const outputs = withReturning ? {
640
+ selectSingleOutput,
641
+ selectArrOutput,
642
+ singleTableItemOutput,
643
+ arrTableItemOutput,
644
+ tableFieldsInterface
645
+ } : {
646
+ selectSingleOutput,
647
+ selectArrOutput,
648
+ tableFieldsInterface
649
+ };
650
+ return {
651
+ inputs,
652
+ outputs
653
+ };
654
+ };
655
+ var extractOrderBy = (table, orderArgs) => {
656
+ const res = [];
657
+ for (const [column, config] of Object.entries(orderArgs).sort(
658
+ (a, b) => (b[1]?.priority ?? 0) - (a[1]?.priority ?? 0)
659
+ )) {
660
+ if (!config)
661
+ continue;
662
+ const { direction } = config;
663
+ res.push(
664
+ direction === "asc" ? (0, import_drizzle_orm3.asc)((0, import_drizzle_orm3.getTableColumns)(table)[column]) : (0, import_drizzle_orm3.desc)((0, import_drizzle_orm3.getTableColumns)(table)[column])
665
+ );
666
+ }
667
+ return res;
668
+ };
669
+ var extractFiltersColumn = (column, columnName, operators) => {
670
+ if (!operators.OR?.length)
671
+ delete operators.OR;
672
+ const entries = Object.entries(
673
+ operators
674
+ );
675
+ if (operators.OR) {
676
+ if (entries.length > 1) {
677
+ throw new import_graphql3.GraphQLError(
678
+ `WHERE ${columnName}: Cannot specify both fields and 'OR' in column operators!`
679
+ );
680
+ }
681
+ const variants2 = [];
682
+ for (const variant of operators.OR) {
683
+ const extracted = extractFiltersColumn(column, columnName, variant);
684
+ if (extracted)
685
+ variants2.push(extracted);
686
+ }
687
+ return variants2.length ? variants2.length > 1 ? (0, import_drizzle_orm3.or)(...variants2) : variants2[0] : void 0;
688
+ }
689
+ const variants = [];
690
+ for (const [operatorName, operatorValue] of entries) {
691
+ if (operatorValue === null || operatorValue === false)
692
+ continue;
693
+ let operator;
694
+ switch (operatorName) {
695
+ case "eq":
696
+ operator = operator ?? import_drizzle_orm3.eq;
697
+ case "ne":
698
+ operator = operator ?? import_drizzle_orm3.ne;
699
+ case "gt":
700
+ operator = operator ?? import_drizzle_orm3.gt;
701
+ case "gte":
702
+ operator = operator ?? import_drizzle_orm3.gte;
703
+ case "lt":
704
+ operator = operator ?? import_drizzle_orm3.lt;
705
+ case "lte":
706
+ operator = operator ?? import_drizzle_orm3.lte;
707
+ const singleValue = remapFromGraphQLCore(
708
+ operatorValue,
709
+ column,
710
+ columnName
711
+ );
712
+ variants.push(operator(column, singleValue));
713
+ break;
714
+ case "like":
715
+ operator = operator ?? import_drizzle_orm3.like;
716
+ case "notLike":
717
+ operator = operator ?? import_drizzle_orm3.notLike;
718
+ case "ilike":
719
+ operator = operator ?? import_drizzle_orm3.ilike;
720
+ case "notIlike":
721
+ operator = operator ?? import_drizzle_orm3.notIlike;
722
+ variants.push(operator(column, operatorValue));
723
+ break;
724
+ case "inArray":
725
+ operator = operator ?? import_drizzle_orm3.inArray;
726
+ case "notInArray":
727
+ operator = operator ?? import_drizzle_orm3.notInArray;
728
+ if (!operatorValue.length) {
729
+ throw new import_graphql3.GraphQLError(
730
+ `WHERE ${columnName}: Unable to use operator ${operatorName} with an empty array!`
731
+ );
732
+ }
733
+ const arrayValue = operatorValue.map(
734
+ (val) => remapFromGraphQLCore(val, column, columnName)
735
+ );
736
+ variants.push(operator(column, arrayValue));
737
+ break;
738
+ case "isNull":
739
+ operator = operator ?? import_drizzle_orm3.isNull;
740
+ case "isNotNull":
741
+ operator = operator ?? import_drizzle_orm3.isNotNull;
742
+ variants.push(operator(column));
743
+ }
744
+ }
745
+ return variants.length ? variants.length > 1 ? (0, import_drizzle_orm3.and)(...variants) : variants[0] : void 0;
746
+ };
747
+ var extractFilters = (table, tableName, filters) => {
748
+ if (!filters.OR?.length)
749
+ delete filters.OR;
750
+ const entries = Object.entries(filters);
751
+ if (!entries.length)
752
+ return;
753
+ if (filters.OR) {
754
+ if (entries.length > 1) {
755
+ throw new import_graphql3.GraphQLError(
756
+ `WHERE ${tableName}: Cannot specify both fields and 'OR' in table filters!`
757
+ );
758
+ }
759
+ const variants2 = [];
760
+ for (const variant of filters.OR) {
761
+ const extracted = extractFilters(table, tableName, variant);
762
+ if (extracted)
763
+ variants2.push(extracted);
764
+ }
765
+ return variants2.length ? variants2.length > 1 ? (0, import_drizzle_orm3.or)(...variants2) : variants2[0] : void 0;
766
+ }
767
+ const variants = [];
768
+ for (const [columnName, operators] of entries) {
769
+ if (operators === null)
770
+ continue;
771
+ const column = (0, import_drizzle_orm3.getTableColumns)(table)[columnName];
772
+ variants.push(extractFiltersColumn(column, columnName, operators));
773
+ }
774
+ return variants.length ? variants.length > 1 ? (0, import_drizzle_orm3.and)(...variants) : variants[0] : void 0;
775
+ };
776
+ var extractRelationsParamsInner = (relationMap, tables, tableName, typeName, originField, isInitial = false) => {
777
+ const relations = relationMap[tableName];
778
+ if (!relations)
779
+ return void 0;
780
+ const baseField = Object.entries(originField.fieldsByTypeName).find(
781
+ ([key, value]) => key === typeName
782
+ )?.[1];
783
+ if (!baseField)
784
+ return void 0;
785
+ const args = {};
786
+ for (const [relName, { targetTableName, relation }] of Object.entries(
787
+ relations
788
+ )) {
789
+ const relTypeName = `${isInitial ? capitalize(tableName) : typeName}${capitalize(relName)}Relation`;
790
+ const relFieldSelection = Object.values(baseField).find(
791
+ (field) => field.name === relName
792
+ )?.fieldsByTypeName[relTypeName];
793
+ if (!relFieldSelection)
794
+ continue;
795
+ const columns = extractSelectedColumnsFromTree(
796
+ relFieldSelection,
797
+ tables[targetTableName]
798
+ );
799
+ const thisRecord = {};
800
+ thisRecord.columns = columns;
801
+ const relationField = Object.values(baseField).find(
802
+ (e) => e.name === relName
803
+ );
804
+ const relationArgs = relationField?.args;
805
+ const orderBy = relationArgs?.orderBy ? extractOrderBy(tables[targetTableName], relationArgs.orderBy) : void 0;
806
+ const where = relationArgs?.where ? extractFilters(tables[targetTableName], relName, relationArgs?.where) : void 0;
807
+ const offset = relationArgs?.offset ?? void 0;
808
+ const limit = relationArgs?.limit ?? void 0;
809
+ thisRecord.orderBy = orderBy;
810
+ thisRecord.where = where;
811
+ thisRecord.offset = offset;
812
+ thisRecord.limit = limit;
813
+ const relWith = relationField ? extractRelationsParamsInner(
814
+ relationMap,
815
+ tables,
816
+ targetTableName,
817
+ relTypeName,
818
+ relationField
819
+ ) : void 0;
820
+ thisRecord.with = relWith;
821
+ args[relName] = thisRecord;
822
+ }
823
+ return args;
824
+ };
825
+ var extractRelationsParams = (relationMap, tables, tableName, info, typeName) => {
826
+ if (!info)
827
+ return void 0;
828
+ return extractRelationsParamsInner(
829
+ relationMap,
830
+ tables,
831
+ tableName,
832
+ typeName,
833
+ info,
834
+ true
835
+ );
836
+ };
837
+
838
+ // src/util/builders/mysql.ts
839
+ var import_graphql_parse_resolve_info = require("graphql-parse-resolve-info");
840
+ var generateSelectArray = (db, tableName, tables, relationMap, orderArgs, filterArgs) => {
841
+ const queryName = `${uncapitalize(tableName)}`;
842
+ const queryBase = db.query[tableName];
843
+ if (!queryBase) {
844
+ throw new Error(
845
+ `Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
846
+ );
847
+ }
848
+ const queryArgs = {
849
+ offset: {
850
+ type: import_graphql4.GraphQLInt
851
+ },
852
+ limit: {
853
+ type: import_graphql4.GraphQLInt
854
+ },
855
+ orderBy: {
856
+ type: orderArgs
857
+ },
858
+ where: {
859
+ type: filterArgs
860
+ }
861
+ };
862
+ const typeName = `${capitalize(tableName)}SelectItem`;
863
+ const table = tables[tableName];
864
+ return {
865
+ name: queryName,
866
+ resolver: async (source, args, context, info) => {
867
+ try {
868
+ const { offset, limit, orderBy, where } = args;
869
+ const parsedInfo = (0, import_graphql_parse_resolve_info.parseResolveInfo)(info, {
870
+ deep: true
871
+ });
872
+ const query = queryBase.findMany({
873
+ columns: extractSelectedColumnsFromTree(
874
+ parsedInfo.fieldsByTypeName[typeName],
875
+ table
876
+ ),
877
+ offset,
878
+ limit,
879
+ orderBy: orderBy ? extractOrderBy(table, orderBy) : void 0,
880
+ where: where ? extractFilters(table, tableName, where) : void 0,
881
+ with: relationMap[tableName] ? extractRelationsParams(
882
+ relationMap,
883
+ tables,
884
+ tableName,
885
+ parsedInfo,
886
+ typeName
887
+ ) : void 0
888
+ });
889
+ const result = await query;
890
+ return remapToGraphQLArrayOutput(result, tableName, table, relationMap);
891
+ } catch (e) {
892
+ if (typeof e === "object" && typeof e.message === "string") {
893
+ throw new import_graphql4.GraphQLError(e.message);
894
+ }
895
+ throw e;
896
+ }
897
+ },
898
+ args: queryArgs
899
+ };
900
+ };
901
+ var generateSelectSingle = (db, tableName, tables, relationMap, orderArgs, filterArgs) => {
902
+ const queryName = `${uncapitalize(tableName)}Single`;
903
+ const queryBase = db.query[tableName];
904
+ if (!queryBase) {
905
+ throw new Error(
906
+ `Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
907
+ );
908
+ }
909
+ const queryArgs = {
910
+ offset: {
911
+ type: import_graphql4.GraphQLInt
912
+ },
913
+ orderBy: {
914
+ type: orderArgs
915
+ },
916
+ where: {
917
+ type: filterArgs
918
+ }
919
+ };
920
+ const typeName = `${capitalize(tableName)}SelectItem`;
921
+ const table = tables[tableName];
922
+ return {
923
+ name: queryName,
924
+ resolver: async (source, args, context, info) => {
925
+ try {
926
+ const { offset, orderBy, where } = args;
927
+ const parsedInfo = (0, import_graphql_parse_resolve_info.parseResolveInfo)(info, {
928
+ deep: true
929
+ });
930
+ const query = queryBase.findFirst({
931
+ columns: extractSelectedColumnsFromTree(
932
+ parsedInfo.fieldsByTypeName[typeName],
933
+ table
934
+ ),
935
+ offset,
936
+ orderBy: orderBy ? extractOrderBy(table, orderBy) : void 0,
937
+ where: where ? extractFilters(table, tableName, where) : void 0,
938
+ with: relationMap[tableName] ? extractRelationsParams(
939
+ relationMap,
940
+ tables,
941
+ tableName,
942
+ parsedInfo,
943
+ typeName
944
+ ) : void 0
945
+ });
946
+ const result = await query;
947
+ if (!result)
948
+ return void 0;
949
+ return remapToGraphQLSingleOutput(
950
+ result,
951
+ tableName,
952
+ table,
953
+ relationMap
954
+ );
955
+ } catch (e) {
956
+ if (typeof e === "object" && typeof e.message === "string") {
957
+ throw new import_graphql4.GraphQLError(e.message);
958
+ }
959
+ throw e;
960
+ }
961
+ },
962
+ args: queryArgs
963
+ };
964
+ };
965
+ var generateInsertArray = (db, tableName, table, baseType) => {
966
+ const queryName = `insertInto${capitalize(tableName)}`;
967
+ const queryArgs = {
968
+ values: {
969
+ type: new import_graphql4.GraphQLNonNull(new import_graphql4.GraphQLList(new import_graphql4.GraphQLNonNull(baseType)))
970
+ }
971
+ };
972
+ return {
973
+ name: queryName,
974
+ resolver: async (source, args, context, info) => {
975
+ try {
976
+ const input = remapFromGraphQLArrayInput(args.values, table);
977
+ if (!input.length)
978
+ throw new import_graphql4.GraphQLError("No values were provided!");
979
+ await db.insert(table).values(input);
980
+ return { isSuccess: true };
981
+ } catch (e) {
982
+ if (typeof e === "object" && typeof e.message === "string") {
983
+ throw new import_graphql4.GraphQLError(e.message);
984
+ }
985
+ throw e;
986
+ }
987
+ },
988
+ args: queryArgs
989
+ };
990
+ };
991
+ var generateInsertSingle = (db, tableName, table, baseType) => {
992
+ const queryName = `insertInto${capitalize(tableName)}Single`;
993
+ const queryArgs = {
994
+ values: {
995
+ type: new import_graphql4.GraphQLNonNull(baseType)
996
+ }
997
+ };
998
+ return {
999
+ name: queryName,
1000
+ resolver: async (source, args, context, info) => {
1001
+ try {
1002
+ const input = remapFromGraphQLSingleInput(args.values, table);
1003
+ await db.insert(table).values(input);
1004
+ return { isSuccess: true };
1005
+ } catch (e) {
1006
+ if (typeof e === "object" && typeof e.message === "string") {
1007
+ throw new import_graphql4.GraphQLError(e.message);
1008
+ }
1009
+ throw e;
1010
+ }
1011
+ },
1012
+ args: queryArgs
1013
+ };
1014
+ };
1015
+ var generateUpdate = (db, tableName, table, setArgs, filterArgs) => {
1016
+ const queryName = `update${capitalize(tableName)}`;
1017
+ const queryArgs = {
1018
+ set: {
1019
+ type: new import_graphql4.GraphQLNonNull(setArgs)
1020
+ },
1021
+ where: {
1022
+ type: filterArgs
1023
+ }
1024
+ };
1025
+ return {
1026
+ name: queryName,
1027
+ resolver: async (source, args, context, info) => {
1028
+ try {
1029
+ const { where, set } = args;
1030
+ const input = remapFromGraphQLSingleInput(set, table);
1031
+ if (!Object.keys(input).length)
1032
+ throw new import_graphql4.GraphQLError("Unable to update with no values specified!");
1033
+ let query = db.update(table).set(input);
1034
+ if (where) {
1035
+ const filters = extractFilters(table, tableName, where);
1036
+ query = query.where(filters);
1037
+ }
1038
+ await query;
1039
+ return { isSuccess: true };
1040
+ } catch (e) {
1041
+ if (typeof e === "object" && typeof e.message === "string") {
1042
+ throw new import_graphql4.GraphQLError(e.message);
1043
+ }
1044
+ throw e;
1045
+ }
1046
+ },
1047
+ args: queryArgs
1048
+ };
1049
+ };
1050
+ var generateDelete = (db, tableName, table, filterArgs) => {
1051
+ const queryName = `deleteFrom${tableName}`;
1052
+ const queryArgs = {
1053
+ where: {
1054
+ type: filterArgs
1055
+ }
1056
+ };
1057
+ return {
1058
+ name: queryName,
1059
+ resolver: async (source, args, context, info) => {
1060
+ try {
1061
+ const { where } = args;
1062
+ let query = db.delete(table);
1063
+ if (where) {
1064
+ const filters = extractFilters(table, tableName, where);
1065
+ query = query.where(filters);
1066
+ }
1067
+ await query;
1068
+ return { isSuccess: true };
1069
+ } catch (e) {
1070
+ if (typeof e === "object" && typeof e.message === "string") {
1071
+ throw new import_graphql4.GraphQLError(e.message);
1072
+ }
1073
+ throw e;
1074
+ }
1075
+ },
1076
+ args: queryArgs
1077
+ };
1078
+ };
1079
+ var generateSchemaData = (db, schema, relationsDepthLimit) => {
1080
+ const rawSchema = schema;
1081
+ const schemaEntries = Object.entries(rawSchema);
1082
+ const tableEntries = schemaEntries.filter(
1083
+ ([key, value]) => (0, import_drizzle_orm4.is)(value, import_mysql_core2.MySqlTable)
1084
+ );
1085
+ const tables = Object.fromEntries(tableEntries);
1086
+ if (!tableEntries.length) {
1087
+ throw new Error(
1088
+ "Drizzle-GraphQL Error: No tables detected in Drizzle-ORM's database instance. Did you forget to pass schema to drizzle constructor?"
1089
+ );
1090
+ }
1091
+ const rawRelations = schemaEntries.filter(([key, value]) => (0, import_drizzle_orm4.is)(value, import_drizzle_orm4.Relations)).map(([key, value]) => [
1092
+ tableEntries.find(
1093
+ ([tableName, tableValue]) => tableValue === value.table
1094
+ )[0],
1095
+ value
1096
+ ]).map(([tableName, relValue]) => [
1097
+ tableName,
1098
+ relValue.config((0, import_drizzle_orm4.createTableRelationsHelpers)(tables[tableName]))
1099
+ ]);
1100
+ const namedRelations = Object.fromEntries(
1101
+ rawRelations.map(([relName, config]) => {
1102
+ const namedConfig = Object.fromEntries(
1103
+ Object.entries(config).map(([innerRelName, innerRelValue]) => [
1104
+ innerRelName,
1105
+ {
1106
+ relation: innerRelValue,
1107
+ targetTableName: tableEntries.find(
1108
+ ([tableName, tableValue]) => tableValue === innerRelValue.referencedTable
1109
+ )[0]
1110
+ }
1111
+ ])
1112
+ );
1113
+ return [relName, namedConfig];
1114
+ })
1115
+ );
1116
+ const queries = {};
1117
+ const mutations = {};
1118
+ const gqlSchemaTypes = Object.fromEntries(
1119
+ Object.entries(tables).map(([tableName, table]) => [
1120
+ tableName,
1121
+ generateTableTypes(
1122
+ tableName,
1123
+ tables,
1124
+ namedRelations,
1125
+ false,
1126
+ relationsDepthLimit
1127
+ )
1128
+ ])
1129
+ );
1130
+ const mutationReturnType = new import_graphql4.GraphQLObjectType({
1131
+ name: `MutationReturn`,
1132
+ fields: {
1133
+ isSuccess: {
1134
+ type: new import_graphql4.GraphQLNonNull(import_graphql4.GraphQLBoolean)
1135
+ }
1136
+ }
1137
+ });
1138
+ const inputs = {};
1139
+ const interfaces = {};
1140
+ const outputs = {
1141
+ MutationReturn: mutationReturnType
1142
+ };
1143
+ for (const [tableName, tableTypes] of Object.entries(gqlSchemaTypes)) {
1144
+ const { insertInput, updateInput, tableFilters, tableOrder } = tableTypes.inputs;
1145
+ const { selectSingleOutput, selectArrOutput, tableFieldsInterface } = tableTypes.outputs;
1146
+ const selectArrGenerated = generateSelectArray(
1147
+ db,
1148
+ tableName,
1149
+ tables,
1150
+ namedRelations,
1151
+ tableOrder,
1152
+ tableFilters
1153
+ );
1154
+ const selectSingleGenerated = generateSelectSingle(
1155
+ db,
1156
+ tableName,
1157
+ tables,
1158
+ namedRelations,
1159
+ tableOrder,
1160
+ tableFilters
1161
+ );
1162
+ const insertArrGenerated = generateInsertArray(
1163
+ db,
1164
+ tableName,
1165
+ schema[tableName],
1166
+ insertInput
1167
+ );
1168
+ const insertSingleGenerated = generateInsertSingle(
1169
+ db,
1170
+ tableName,
1171
+ schema[tableName],
1172
+ insertInput
1173
+ );
1174
+ const updateGenerated = generateUpdate(
1175
+ db,
1176
+ tableName,
1177
+ schema[tableName],
1178
+ updateInput,
1179
+ tableFilters
1180
+ );
1181
+ const deleteGenerated = generateDelete(
1182
+ db,
1183
+ tableName,
1184
+ schema[tableName],
1185
+ tableFilters
1186
+ );
1187
+ queries[selectArrGenerated.name] = {
1188
+ type: selectArrOutput,
1189
+ args: selectArrGenerated.args,
1190
+ resolve: selectArrGenerated.resolver
1191
+ };
1192
+ queries[selectSingleGenerated.name] = {
1193
+ type: selectSingleOutput,
1194
+ args: selectSingleGenerated.args,
1195
+ resolve: selectSingleGenerated.resolver
1196
+ };
1197
+ mutations[insertArrGenerated.name] = {
1198
+ type: mutationReturnType,
1199
+ args: insertArrGenerated.args,
1200
+ resolve: insertArrGenerated.resolver
1201
+ };
1202
+ mutations[insertSingleGenerated.name] = {
1203
+ type: mutationReturnType,
1204
+ args: insertSingleGenerated.args,
1205
+ resolve: insertSingleGenerated.resolver
1206
+ };
1207
+ mutations[updateGenerated.name] = {
1208
+ type: mutationReturnType,
1209
+ args: updateGenerated.args,
1210
+ resolve: updateGenerated.resolver
1211
+ };
1212
+ mutations[deleteGenerated.name] = {
1213
+ type: mutationReturnType,
1214
+ args: deleteGenerated.args,
1215
+ resolve: deleteGenerated.resolver
1216
+ };
1217
+ [insertInput, updateInput, tableFilters, tableOrder].forEach(
1218
+ (e) => inputs[e.name] = e
1219
+ );
1220
+ outputs[selectSingleOutput.name] = selectSingleOutput;
1221
+ interfaces[tableFieldsInterface.name] = tableFieldsInterface;
1222
+ }
1223
+ return { queries, mutations, inputs, interfaces, types: outputs };
1224
+ };
1225
+
1226
+ // src/util/builders/pg.ts
1227
+ var import_drizzle_orm5 = require("drizzle-orm");
1228
+ var import_pg_core2 = require("drizzle-orm/pg-core");
1229
+ var import_graphql5 = require("graphql");
1230
+ var import_graphql_parse_resolve_info2 = require("graphql-parse-resolve-info");
1231
+ var generateSelectArray2 = (db, tableName, tables, relationMap, orderArgs, filterArgs) => {
1232
+ const queryName = `${uncapitalize(tableName)}`;
1233
+ const queryBase = db.query[tableName];
1234
+ if (!queryBase) {
1235
+ throw new Error(
1236
+ `Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
1237
+ );
1238
+ }
1239
+ const queryArgs = {
1240
+ offset: {
1241
+ type: import_graphql5.GraphQLInt
1242
+ },
1243
+ limit: {
1244
+ type: import_graphql5.GraphQLInt
1245
+ },
1246
+ orderBy: {
1247
+ type: orderArgs
1248
+ },
1249
+ where: {
1250
+ type: filterArgs
1251
+ }
1252
+ };
1253
+ const typeName = `${capitalize(tableName)}SelectItem`;
1254
+ const table = tables[tableName];
1255
+ return {
1256
+ name: queryName,
1257
+ resolver: async (source, args, context, info) => {
1258
+ try {
1259
+ const { offset, limit, orderBy, where } = args;
1260
+ const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, {
1261
+ deep: true
1262
+ });
1263
+ const query = queryBase.findMany({
1264
+ columns: extractSelectedColumnsFromTree(
1265
+ parsedInfo.fieldsByTypeName[typeName],
1266
+ table
1267
+ ),
1268
+ offset,
1269
+ limit,
1270
+ orderBy: orderBy ? extractOrderBy(table, orderBy) : void 0,
1271
+ where: where ? extractFilters(table, tableName, where) : void 0,
1272
+ with: relationMap[tableName] ? extractRelationsParams(
1273
+ relationMap,
1274
+ tables,
1275
+ tableName,
1276
+ parsedInfo,
1277
+ typeName
1278
+ ) : void 0
1279
+ });
1280
+ const result = await query;
1281
+ return remapToGraphQLArrayOutput(result, tableName, table, relationMap);
1282
+ } catch (e) {
1283
+ if (typeof e === "object" && typeof e.message === "string") {
1284
+ throw new import_graphql5.GraphQLError(e.message);
1285
+ }
1286
+ throw e;
1287
+ }
1288
+ },
1289
+ args: queryArgs
1290
+ };
1291
+ };
1292
+ var generateSelectSingle2 = (db, tableName, tables, relationMap, orderArgs, filterArgs) => {
1293
+ const queryName = `${uncapitalize(tableName)}Single`;
1294
+ const queryBase = db.query[tableName];
1295
+ if (!queryBase) {
1296
+ throw new Error(
1297
+ `Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
1298
+ );
1299
+ }
1300
+ const queryArgs = {
1301
+ offset: {
1302
+ type: import_graphql5.GraphQLInt
1303
+ },
1304
+ orderBy: {
1305
+ type: orderArgs
1306
+ },
1307
+ where: {
1308
+ type: filterArgs
1309
+ }
1310
+ };
1311
+ const typeName = `${capitalize(tableName)}SelectItem`;
1312
+ const table = tables[tableName];
1313
+ return {
1314
+ name: queryName,
1315
+ resolver: async (source, args, context, info) => {
1316
+ try {
1317
+ const { offset, orderBy, where } = args;
1318
+ const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, {
1319
+ deep: true
1320
+ });
1321
+ const query = queryBase.findFirst({
1322
+ columns: extractSelectedColumnsFromTree(
1323
+ parsedInfo.fieldsByTypeName[typeName],
1324
+ table
1325
+ ),
1326
+ offset,
1327
+ orderBy: orderBy ? extractOrderBy(table, orderBy) : void 0,
1328
+ where: where ? extractFilters(table, tableName, where) : void 0,
1329
+ with: relationMap[tableName] ? extractRelationsParams(
1330
+ relationMap,
1331
+ tables,
1332
+ tableName,
1333
+ parsedInfo,
1334
+ typeName
1335
+ ) : void 0
1336
+ });
1337
+ const result = await query;
1338
+ if (!result)
1339
+ return void 0;
1340
+ return remapToGraphQLSingleOutput(
1341
+ result,
1342
+ tableName,
1343
+ table,
1344
+ relationMap
1345
+ );
1346
+ } catch (e) {
1347
+ if (typeof e === "object" && typeof e.message === "string") {
1348
+ throw new import_graphql5.GraphQLError(e.message);
1349
+ }
1350
+ throw e;
1351
+ }
1352
+ },
1353
+ args: queryArgs
1354
+ };
1355
+ };
1356
+ var generateInsertArray2 = (db, tableName, table, baseType) => {
1357
+ const queryName = `insertInto${capitalize(tableName)}`;
1358
+ const typeName = `${capitalize(tableName)}Item`;
1359
+ const queryArgs = {
1360
+ values: {
1361
+ type: new import_graphql5.GraphQLNonNull(new import_graphql5.GraphQLList(new import_graphql5.GraphQLNonNull(baseType)))
1362
+ }
1363
+ };
1364
+ return {
1365
+ name: queryName,
1366
+ resolver: async (source, args, context, info) => {
1367
+ try {
1368
+ const input = remapFromGraphQLArrayInput(args.values, table);
1369
+ if (!input.length)
1370
+ throw new import_graphql5.GraphQLError("No values were provided!");
1371
+ const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, {
1372
+ deep: true
1373
+ });
1374
+ const columns = extractSelectedColumnsFromTreeSQLFormat(
1375
+ parsedInfo.fieldsByTypeName[typeName],
1376
+ table
1377
+ );
1378
+ const result = await db.insert(table).values(input).returning(columns).onConflictDoNothing();
1379
+ return remapToGraphQLArrayOutput(result, tableName, table);
1380
+ } catch (e) {
1381
+ if (typeof e === "object" && typeof e.message === "string") {
1382
+ throw new import_graphql5.GraphQLError(e.message);
1383
+ }
1384
+ throw e;
1385
+ }
1386
+ },
1387
+ args: queryArgs
1388
+ };
1389
+ };
1390
+ var generateInsertSingle2 = (db, tableName, table, baseType) => {
1391
+ const queryName = `insertInto${capitalize(tableName)}Single`;
1392
+ const typeName = `${capitalize(tableName)}Item`;
1393
+ const queryArgs = {
1394
+ values: {
1395
+ type: new import_graphql5.GraphQLNonNull(baseType)
1396
+ }
1397
+ };
1398
+ return {
1399
+ name: queryName,
1400
+ resolver: async (source, args, context, info) => {
1401
+ try {
1402
+ const input = remapFromGraphQLSingleInput(args.values, table);
1403
+ const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, {
1404
+ deep: true
1405
+ });
1406
+ const columns = extractSelectedColumnsFromTreeSQLFormat(
1407
+ parsedInfo.fieldsByTypeName[typeName],
1408
+ table
1409
+ );
1410
+ const result = await db.insert(table).values(input).returning(columns).onConflictDoNothing();
1411
+ if (!result[0])
1412
+ return void 0;
1413
+ return remapToGraphQLSingleOutput(result[0], tableName, table);
1414
+ } catch (e) {
1415
+ if (typeof e === "object" && typeof e.message === "string") {
1416
+ throw new import_graphql5.GraphQLError(e.message);
1417
+ }
1418
+ throw e;
1419
+ }
1420
+ },
1421
+ args: queryArgs
1422
+ };
1423
+ };
1424
+ var generateUpdate2 = (db, tableName, table, setArgs, filterArgs) => {
1425
+ const queryName = `update${capitalize(tableName)}`;
1426
+ const typeName = `${capitalize(tableName)}Item`;
1427
+ const queryArgs = {
1428
+ set: {
1429
+ type: new import_graphql5.GraphQLNonNull(setArgs)
1430
+ },
1431
+ where: {
1432
+ type: filterArgs
1433
+ }
1434
+ };
1435
+ return {
1436
+ name: queryName,
1437
+ resolver: async (source, args, context, info) => {
1438
+ try {
1439
+ const { where, set } = args;
1440
+ const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, {
1441
+ deep: true
1442
+ });
1443
+ const columns = extractSelectedColumnsFromTreeSQLFormat(
1444
+ parsedInfo.fieldsByTypeName[typeName],
1445
+ table
1446
+ );
1447
+ const input = remapFromGraphQLSingleInput(set, table);
1448
+ if (!Object.keys(input).length)
1449
+ throw new import_graphql5.GraphQLError("Unable to update with no values specified!");
1450
+ let query = db.update(table).set(input);
1451
+ if (where) {
1452
+ const filters = extractFilters(table, tableName, where);
1453
+ query = query.where(filters);
1454
+ }
1455
+ query = query.returning(columns);
1456
+ const result = await query;
1457
+ return remapToGraphQLArrayOutput(result, tableName, table);
1458
+ } catch (e) {
1459
+ if (typeof e === "object" && typeof e.message === "string") {
1460
+ throw new import_graphql5.GraphQLError(e.message);
1461
+ }
1462
+ throw e;
1463
+ }
1464
+ },
1465
+ args: queryArgs
1466
+ };
1467
+ };
1468
+ var generateDelete2 = (db, tableName, table, filterArgs) => {
1469
+ const queryName = `deleteFrom${capitalize(tableName)}`;
1470
+ const typeName = `${capitalize(tableName)}Item`;
1471
+ const queryArgs = {
1472
+ where: {
1473
+ type: filterArgs
1474
+ }
1475
+ };
1476
+ return {
1477
+ name: queryName,
1478
+ resolver: async (source, args, context, info) => {
1479
+ try {
1480
+ const { where } = args;
1481
+ const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, {
1482
+ deep: true
1483
+ });
1484
+ const columns = extractSelectedColumnsFromTreeSQLFormat(
1485
+ parsedInfo.fieldsByTypeName[typeName],
1486
+ table
1487
+ );
1488
+ let query = db.delete(table);
1489
+ if (where) {
1490
+ const filters = extractFilters(table, tableName, where);
1491
+ query = query.where(filters);
1492
+ }
1493
+ query = query.returning(columns);
1494
+ const result = await query;
1495
+ return remapToGraphQLArrayOutput(result, tableName, table);
1496
+ } catch (e) {
1497
+ if (typeof e === "object" && typeof e.message === "string") {
1498
+ throw new import_graphql5.GraphQLError(e.message);
1499
+ }
1500
+ throw e;
1501
+ }
1502
+ },
1503
+ args: queryArgs
1504
+ };
1505
+ };
1506
+ var generateSchemaData2 = (db, schema, relationsDepthLimit) => {
1507
+ const rawSchema = schema;
1508
+ const schemaEntries = Object.entries(rawSchema);
1509
+ const tableEntries = schemaEntries.filter(
1510
+ ([key, value]) => (0, import_drizzle_orm5.is)(value, import_pg_core2.PgTable)
1511
+ );
1512
+ const tables = Object.fromEntries(tableEntries);
1513
+ if (!tableEntries.length) {
1514
+ throw new Error(
1515
+ "Drizzle-GraphQL Error: No tables detected in Drizzle-ORM's database instance. Did you forget to pass schema to drizzle constructor?"
1516
+ );
1517
+ }
1518
+ const rawRelations = schemaEntries.filter(([key, value]) => (0, import_drizzle_orm5.is)(value, import_drizzle_orm5.Relations)).map(([key, value]) => [
1519
+ tableEntries.find(
1520
+ ([tableName, tableValue]) => tableValue === value.table
1521
+ )[0],
1522
+ value
1523
+ ]).map(([tableName, relValue]) => [
1524
+ tableName,
1525
+ relValue.config((0, import_drizzle_orm5.createTableRelationsHelpers)(tables[tableName]))
1526
+ ]);
1527
+ const namedRelations = Object.fromEntries(
1528
+ rawRelations.map(([relName, config]) => {
1529
+ const namedConfig = Object.fromEntries(
1530
+ Object.entries(config).map(([innerRelName, innerRelValue]) => [
1531
+ innerRelName,
1532
+ {
1533
+ relation: innerRelValue,
1534
+ targetTableName: tableEntries.find(
1535
+ ([tableName, tableValue]) => tableValue === innerRelValue.referencedTable
1536
+ )[0]
1537
+ }
1538
+ ])
1539
+ );
1540
+ return [relName, namedConfig];
1541
+ })
1542
+ );
1543
+ const queries = {};
1544
+ const mutations = {};
1545
+ const gqlSchemaTypes = Object.fromEntries(
1546
+ Object.entries(tables).map(([tableName, table]) => [
1547
+ tableName,
1548
+ generateTableTypes(
1549
+ tableName,
1550
+ tables,
1551
+ namedRelations,
1552
+ true,
1553
+ relationsDepthLimit
1554
+ )
1555
+ ])
1556
+ );
1557
+ const inputs = {};
1558
+ const interfaces = {};
1559
+ const outputs = {};
1560
+ for (const [tableName, tableTypes] of Object.entries(gqlSchemaTypes)) {
1561
+ const { insertInput, updateInput, tableFilters, tableOrder } = tableTypes.inputs;
1562
+ const {
1563
+ selectSingleOutput,
1564
+ selectArrOutput,
1565
+ singleTableItemOutput,
1566
+ arrTableItemOutput,
1567
+ tableFieldsInterface
1568
+ } = tableTypes.outputs;
1569
+ const selectArrGenerated = generateSelectArray2(
1570
+ db,
1571
+ tableName,
1572
+ tables,
1573
+ namedRelations,
1574
+ tableOrder,
1575
+ tableFilters
1576
+ );
1577
+ const selectSingleGenerated = generateSelectSingle2(
1578
+ db,
1579
+ tableName,
1580
+ tables,
1581
+ namedRelations,
1582
+ tableOrder,
1583
+ tableFilters
1584
+ );
1585
+ const insertArrGenerated = generateInsertArray2(
1586
+ db,
1587
+ tableName,
1588
+ schema[tableName],
1589
+ insertInput
1590
+ );
1591
+ const insertSingleGenerated = generateInsertSingle2(
1592
+ db,
1593
+ tableName,
1594
+ schema[tableName],
1595
+ insertInput
1596
+ );
1597
+ const updateGenerated = generateUpdate2(
1598
+ db,
1599
+ tableName,
1600
+ schema[tableName],
1601
+ updateInput,
1602
+ tableFilters
1603
+ );
1604
+ const deleteGenerated = generateDelete2(
1605
+ db,
1606
+ tableName,
1607
+ schema[tableName],
1608
+ tableFilters
1609
+ );
1610
+ queries[selectArrGenerated.name] = {
1611
+ type: selectArrOutput,
1612
+ args: selectArrGenerated.args,
1613
+ resolve: selectArrGenerated.resolver
1614
+ };
1615
+ queries[selectSingleGenerated.name] = {
1616
+ type: selectSingleOutput,
1617
+ args: selectSingleGenerated.args,
1618
+ resolve: selectSingleGenerated.resolver
1619
+ };
1620
+ mutations[insertArrGenerated.name] = {
1621
+ type: arrTableItemOutput,
1622
+ args: insertArrGenerated.args,
1623
+ resolve: insertArrGenerated.resolver
1624
+ };
1625
+ mutations[insertSingleGenerated.name] = {
1626
+ type: singleTableItemOutput,
1627
+ args: insertSingleGenerated.args,
1628
+ resolve: insertSingleGenerated.resolver
1629
+ };
1630
+ mutations[updateGenerated.name] = {
1631
+ type: arrTableItemOutput,
1632
+ args: updateGenerated.args,
1633
+ resolve: updateGenerated.resolver
1634
+ };
1635
+ mutations[deleteGenerated.name] = {
1636
+ type: arrTableItemOutput,
1637
+ args: deleteGenerated.args,
1638
+ resolve: deleteGenerated.resolver
1639
+ };
1640
+ [insertInput, updateInput, tableFilters, tableOrder].forEach(
1641
+ (e) => inputs[e.name] = e
1642
+ );
1643
+ outputs[selectSingleOutput.name] = selectSingleOutput;
1644
+ outputs[singleTableItemOutput.name] = singleTableItemOutput;
1645
+ interfaces[tableFieldsInterface.name] = tableFieldsInterface;
1646
+ }
1647
+ return { queries, mutations, inputs, interfaces, types: outputs };
1648
+ };
1649
+
1650
+ // src/util/builders/sqlite.ts
1651
+ var import_drizzle_orm6 = require("drizzle-orm");
1652
+ var import_sqlite_core2 = require("drizzle-orm/sqlite-core");
1653
+ var import_graphql6 = require("graphql");
1654
+ var import_graphql_parse_resolve_info3 = require("graphql-parse-resolve-info");
1655
+ var generateSelectArray3 = (db, tableName, tables, relationMap, orderArgs, filterArgs) => {
1656
+ const queryName = `${uncapitalize(tableName)}`;
1657
+ const queryBase = db.query[tableName];
1658
+ if (!queryBase) {
1659
+ throw new Error(
1660
+ `Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
1661
+ );
1662
+ }
1663
+ const queryArgs = {
1664
+ offset: {
1665
+ type: import_graphql6.GraphQLInt
1666
+ },
1667
+ limit: {
1668
+ type: import_graphql6.GraphQLInt
1669
+ },
1670
+ orderBy: {
1671
+ type: orderArgs
1672
+ },
1673
+ where: {
1674
+ type: filterArgs
1675
+ }
1676
+ };
1677
+ const typeName = `${capitalize(tableName)}SelectItem`;
1678
+ const table = tables[tableName];
1679
+ return {
1680
+ name: queryName,
1681
+ resolver: async (source, args, context, info) => {
1682
+ try {
1683
+ const { offset, limit, orderBy, where } = args;
1684
+ const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
1685
+ deep: true
1686
+ });
1687
+ const query = queryBase.findMany({
1688
+ columns: extractSelectedColumnsFromTree(
1689
+ parsedInfo.fieldsByTypeName[typeName],
1690
+ table
1691
+ ),
1692
+ offset,
1693
+ limit,
1694
+ orderBy: orderBy ? extractOrderBy(table, orderBy) : void 0,
1695
+ where: where ? extractFilters(table, tableName, where) : void 0,
1696
+ with: relationMap[tableName] ? extractRelationsParams(
1697
+ relationMap,
1698
+ tables,
1699
+ tableName,
1700
+ parsedInfo,
1701
+ typeName
1702
+ ) : void 0
1703
+ });
1704
+ const result = await query;
1705
+ return remapToGraphQLArrayOutput(result, tableName, table, relationMap);
1706
+ } catch (e) {
1707
+ if (typeof e === "object" && typeof e.message === "string") {
1708
+ throw new import_graphql6.GraphQLError(e.message);
1709
+ }
1710
+ throw e;
1711
+ }
1712
+ },
1713
+ args: queryArgs
1714
+ };
1715
+ };
1716
+ var generateSelectSingle3 = (db, tableName, tables, relationMap, orderArgs, filterArgs) => {
1717
+ const queryName = `${uncapitalize(tableName)}Single`;
1718
+ const queryBase = db.query[tableName];
1719
+ if (!queryBase) {
1720
+ throw new Error(
1721
+ `Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
1722
+ );
1723
+ }
1724
+ const queryArgs = {
1725
+ offset: {
1726
+ type: import_graphql6.GraphQLInt
1727
+ },
1728
+ orderBy: {
1729
+ type: orderArgs
1730
+ },
1731
+ where: {
1732
+ type: filterArgs
1733
+ }
1734
+ };
1735
+ const typeName = `${capitalize(tableName)}SelectItem`;
1736
+ const table = tables[tableName];
1737
+ return {
1738
+ name: queryName,
1739
+ resolver: async (source, args, context, info) => {
1740
+ try {
1741
+ const { offset, orderBy, where } = args;
1742
+ const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
1743
+ deep: true
1744
+ });
1745
+ const query = queryBase.findFirst({
1746
+ columns: extractSelectedColumnsFromTree(
1747
+ parsedInfo.fieldsByTypeName[typeName],
1748
+ table
1749
+ ),
1750
+ offset,
1751
+ orderBy: orderBy ? extractOrderBy(table, orderBy) : void 0,
1752
+ where: where ? extractFilters(table, tableName, where) : void 0,
1753
+ with: relationMap[tableName] ? extractRelationsParams(
1754
+ relationMap,
1755
+ tables,
1756
+ tableName,
1757
+ parsedInfo,
1758
+ typeName
1759
+ ) : void 0
1760
+ });
1761
+ const result = await query;
1762
+ if (!result)
1763
+ return void 0;
1764
+ return remapToGraphQLSingleOutput(
1765
+ result,
1766
+ tableName,
1767
+ table,
1768
+ relationMap
1769
+ );
1770
+ } catch (e) {
1771
+ if (typeof e === "object" && typeof e.message === "string") {
1772
+ throw new import_graphql6.GraphQLError(e.message);
1773
+ }
1774
+ throw e;
1775
+ }
1776
+ },
1777
+ args: queryArgs
1778
+ };
1779
+ };
1780
+ var generateInsertArray3 = (db, tableName, table, baseType) => {
1781
+ const queryName = `insertInto${capitalize(tableName)}`;
1782
+ const typeName = `${capitalize(tableName)}Item`;
1783
+ const queryArgs = {
1784
+ values: {
1785
+ type: new import_graphql6.GraphQLNonNull(new import_graphql6.GraphQLList(new import_graphql6.GraphQLNonNull(baseType)))
1786
+ }
1787
+ };
1788
+ return {
1789
+ name: queryName,
1790
+ resolver: async (source, args, context, info) => {
1791
+ try {
1792
+ const input = remapFromGraphQLArrayInput(args.values, table);
1793
+ if (!input.length)
1794
+ throw new import_graphql6.GraphQLError("No values were provided!");
1795
+ const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
1796
+ deep: true
1797
+ });
1798
+ const columns = extractSelectedColumnsFromTreeSQLFormat(
1799
+ parsedInfo.fieldsByTypeName[typeName],
1800
+ table
1801
+ );
1802
+ const result = await db.insert(table).values(input).returning(columns).onConflictDoNothing();
1803
+ return remapToGraphQLArrayOutput(result, tableName, table);
1804
+ } catch (e) {
1805
+ if (typeof e === "object" && typeof e.message === "string") {
1806
+ throw new import_graphql6.GraphQLError(e.message);
1807
+ }
1808
+ throw e;
1809
+ }
1810
+ },
1811
+ args: queryArgs
1812
+ };
1813
+ };
1814
+ var generateInsertSingle3 = (db, tableName, table, baseType) => {
1815
+ const queryName = `insertInto${capitalize(tableName)}Single`;
1816
+ const typeName = `${capitalize(tableName)}Item`;
1817
+ const queryArgs = {
1818
+ values: {
1819
+ type: new import_graphql6.GraphQLNonNull(baseType)
1820
+ }
1821
+ };
1822
+ return {
1823
+ name: queryName,
1824
+ resolver: async (source, args, context, info) => {
1825
+ try {
1826
+ const input = remapFromGraphQLSingleInput(args.values, table);
1827
+ const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
1828
+ deep: true
1829
+ });
1830
+ const columns = extractSelectedColumnsFromTreeSQLFormat(
1831
+ parsedInfo.fieldsByTypeName[typeName],
1832
+ table
1833
+ );
1834
+ const result = await db.insert(table).values(input).returning(columns).onConflictDoNothing();
1835
+ if (!result[0])
1836
+ return void 0;
1837
+ return remapToGraphQLSingleOutput(result[0], tableName, table);
1838
+ } catch (e) {
1839
+ if (typeof e === "object" && typeof e.message === "string") {
1840
+ throw new import_graphql6.GraphQLError(e.message);
1841
+ }
1842
+ throw e;
1843
+ }
1844
+ },
1845
+ args: queryArgs
1846
+ };
1847
+ };
1848
+ var generateUpdate3 = (db, tableName, table, setArgs, filterArgs) => {
1849
+ const queryName = `update${capitalize(tableName)}`;
1850
+ const typeName = `${capitalize(tableName)}Item`;
1851
+ const queryArgs = {
1852
+ set: {
1853
+ type: new import_graphql6.GraphQLNonNull(setArgs)
1854
+ },
1855
+ where: {
1856
+ type: filterArgs
1857
+ }
1858
+ };
1859
+ return {
1860
+ name: queryName,
1861
+ resolver: async (source, args, context, info) => {
1862
+ try {
1863
+ const { where, set } = args;
1864
+ const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
1865
+ deep: true
1866
+ });
1867
+ const columns = extractSelectedColumnsFromTreeSQLFormat(
1868
+ parsedInfo.fieldsByTypeName[typeName],
1869
+ table
1870
+ );
1871
+ const input = remapFromGraphQLSingleInput(set, table);
1872
+ if (!Object.keys(input).length)
1873
+ throw new import_graphql6.GraphQLError("Unable to update with no values specified!");
1874
+ let query = db.update(table).set(input);
1875
+ if (where) {
1876
+ const filters = extractFilters(table, tableName, where);
1877
+ query = query.where(filters);
1878
+ }
1879
+ query = query.returning(columns);
1880
+ const result = await query;
1881
+ return remapToGraphQLArrayOutput(result, tableName, table);
1882
+ } catch (e) {
1883
+ if (typeof e === "object" && typeof e.message === "string") {
1884
+ throw new import_graphql6.GraphQLError(e.message);
1885
+ }
1886
+ throw e;
1887
+ }
1888
+ },
1889
+ args: queryArgs
1890
+ };
1891
+ };
1892
+ var generateDelete3 = (db, tableName, table, filterArgs) => {
1893
+ const queryName = `deleteFrom${capitalize(tableName)}`;
1894
+ const typeName = `${capitalize(tableName)}Item`;
1895
+ const queryArgs = {
1896
+ where: {
1897
+ type: filterArgs
1898
+ }
1899
+ };
1900
+ return {
1901
+ name: queryName,
1902
+ resolver: async (source, args, context, info) => {
1903
+ try {
1904
+ const { where } = args;
1905
+ const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
1906
+ deep: true
1907
+ });
1908
+ const columns = extractSelectedColumnsFromTreeSQLFormat(
1909
+ parsedInfo.fieldsByTypeName[typeName],
1910
+ table
1911
+ );
1912
+ let query = db.delete(table);
1913
+ if (where) {
1914
+ const filters = extractFilters(table, tableName, where);
1915
+ query = query.where(filters);
1916
+ }
1917
+ query = query.returning(columns);
1918
+ const result = await query;
1919
+ return remapToGraphQLArrayOutput(result, tableName, table);
1920
+ } catch (e) {
1921
+ if (typeof e === "object" && typeof e.message === "string") {
1922
+ throw new import_graphql6.GraphQLError(e.message);
1923
+ }
1924
+ throw e;
1925
+ }
1926
+ },
1927
+ args: queryArgs
1928
+ };
1929
+ };
1930
+ var generateSchemaData3 = (db, schema, relationsDepthLimit) => {
1931
+ const rawSchema = schema;
1932
+ const schemaEntries = Object.entries(rawSchema);
1933
+ const tableEntries = schemaEntries.filter(
1934
+ ([key, value]) => (0, import_drizzle_orm6.is)(value, import_sqlite_core2.SQLiteTable)
1935
+ );
1936
+ const tables = Object.fromEntries(tableEntries);
1937
+ if (!tableEntries.length) {
1938
+ throw new Error(
1939
+ "Drizzle-GraphQL Error: No tables detected in Drizzle-ORM's database instance. Did you forget to pass schema to drizzle constructor?"
1940
+ );
1941
+ }
1942
+ const rawRelations = schemaEntries.filter(([key, value]) => (0, import_drizzle_orm6.is)(value, import_drizzle_orm6.Relations)).map(([key, value]) => [
1943
+ tableEntries.find(
1944
+ ([tableName, tableValue]) => tableValue === value.table
1945
+ )[0],
1946
+ value
1947
+ ]).map(([tableName, relValue]) => [
1948
+ tableName,
1949
+ relValue.config((0, import_drizzle_orm6.createTableRelationsHelpers)(tables[tableName]))
1950
+ ]);
1951
+ const namedRelations = Object.fromEntries(
1952
+ rawRelations.map(([relName, config]) => {
1953
+ const namedConfig = Object.fromEntries(
1954
+ Object.entries(config).map(([innerRelName, innerRelValue]) => [
1955
+ innerRelName,
1956
+ {
1957
+ relation: innerRelValue,
1958
+ targetTableName: tableEntries.find(
1959
+ ([tableName, tableValue]) => tableValue === innerRelValue.referencedTable
1960
+ )[0]
1961
+ }
1962
+ ])
1963
+ );
1964
+ return [relName, namedConfig];
1965
+ })
1966
+ );
1967
+ const queries = {};
1968
+ const mutations = {};
1969
+ const gqlSchemaTypes = Object.fromEntries(
1970
+ Object.entries(tables).map(([tableName, table]) => [
1971
+ tableName,
1972
+ generateTableTypes(
1973
+ tableName,
1974
+ tables,
1975
+ namedRelations,
1976
+ true,
1977
+ relationsDepthLimit
1978
+ )
1979
+ ])
1980
+ );
1981
+ const inputs = {};
1982
+ const interfaces = {};
1983
+ const outputs = {};
1984
+ for (const [tableName, tableTypes] of Object.entries(gqlSchemaTypes)) {
1985
+ const { insertInput, updateInput, tableFilters, tableOrder } = tableTypes.inputs;
1986
+ const {
1987
+ selectSingleOutput,
1988
+ selectArrOutput,
1989
+ singleTableItemOutput,
1990
+ arrTableItemOutput,
1991
+ tableFieldsInterface
1992
+ } = tableTypes.outputs;
1993
+ const selectArrGenerated = generateSelectArray3(
1994
+ db,
1995
+ tableName,
1996
+ tables,
1997
+ namedRelations,
1998
+ tableOrder,
1999
+ tableFilters
2000
+ );
2001
+ const selectSingleGenerated = generateSelectSingle3(
2002
+ db,
2003
+ tableName,
2004
+ tables,
2005
+ namedRelations,
2006
+ tableOrder,
2007
+ tableFilters
2008
+ );
2009
+ const insertArrGenerated = generateInsertArray3(
2010
+ db,
2011
+ tableName,
2012
+ schema[tableName],
2013
+ insertInput
2014
+ );
2015
+ const insertSingleGenerated = generateInsertSingle3(
2016
+ db,
2017
+ tableName,
2018
+ schema[tableName],
2019
+ insertInput
2020
+ );
2021
+ const updateGenerated = generateUpdate3(
2022
+ db,
2023
+ tableName,
2024
+ schema[tableName],
2025
+ updateInput,
2026
+ tableFilters
2027
+ );
2028
+ const deleteGenerated = generateDelete3(
2029
+ db,
2030
+ tableName,
2031
+ schema[tableName],
2032
+ tableFilters
2033
+ );
2034
+ queries[selectArrGenerated.name] = {
2035
+ type: selectArrOutput,
2036
+ args: selectArrGenerated.args,
2037
+ resolve: selectArrGenerated.resolver
2038
+ };
2039
+ queries[selectSingleGenerated.name] = {
2040
+ type: selectSingleOutput,
2041
+ args: selectSingleGenerated.args,
2042
+ resolve: selectSingleGenerated.resolver
2043
+ };
2044
+ mutations[insertArrGenerated.name] = {
2045
+ type: arrTableItemOutput,
2046
+ args: insertArrGenerated.args,
2047
+ resolve: insertArrGenerated.resolver
2048
+ };
2049
+ mutations[insertSingleGenerated.name] = {
2050
+ type: singleTableItemOutput,
2051
+ args: insertSingleGenerated.args,
2052
+ resolve: insertSingleGenerated.resolver
2053
+ };
2054
+ mutations[updateGenerated.name] = {
2055
+ type: arrTableItemOutput,
2056
+ args: updateGenerated.args,
2057
+ resolve: updateGenerated.resolver
2058
+ };
2059
+ mutations[deleteGenerated.name] = {
2060
+ type: arrTableItemOutput,
2061
+ args: deleteGenerated.args,
2062
+ resolve: deleteGenerated.resolver
2063
+ };
2064
+ [insertInput, updateInput, tableFilters, tableOrder].forEach(
2065
+ (e) => inputs[e.name] = e
2066
+ );
2067
+ outputs[selectSingleOutput.name] = selectSingleOutput;
2068
+ outputs[singleTableItemOutput.name] = singleTableItemOutput;
2069
+ interfaces[tableFieldsInterface.name] = tableFieldsInterface;
2070
+ }
2071
+ return { queries, mutations, inputs, interfaces, types: outputs };
2072
+ };
2073
+
2074
+ // src/index.ts
2075
+ var buildSchema = (db, config) => {
2076
+ const schema = db._.fullSchema;
2077
+ if (!schema) {
2078
+ throw new Error(
2079
+ "Drizzle-GraphQL Error: Schema not found in drizzle instance. Make sure you're using drizzle-orm v0.30.9 or above and schema is passed to drizzle constructor!"
2080
+ );
2081
+ }
2082
+ if (typeof config?.relationsDepthLimit === "number") {
2083
+ if (config.relationsDepthLimit < 0) {
2084
+ throw new Error(
2085
+ "Drizzle-GraphQL Error: config.relationsDepthLimit is supposed to be nonnegative integer or undefined!"
2086
+ );
2087
+ }
2088
+ if (config.relationsDepthLimit !== ~~config.relationsDepthLimit) {
2089
+ throw new Error(
2090
+ "Drizzle-GraphQL Error: config.relationsDepthLimit is supposed to be nonnegative integer or undefined!"
2091
+ );
2092
+ }
2093
+ }
2094
+ let generatorOutput;
2095
+ if ((0, import_drizzle_orm7.is)(db, import_mysql_core3.MySqlDatabase)) {
2096
+ generatorOutput = generateSchemaData(db, schema, config?.relationsDepthLimit);
2097
+ } else if ((0, import_drizzle_orm7.is)(db, import_pg_core3.PgDatabase)) {
2098
+ generatorOutput = generateSchemaData2(db, schema, config?.relationsDepthLimit);
2099
+ } else if ((0, import_drizzle_orm7.is)(db, import_sqlite_core3.BaseSQLiteDatabase)) {
2100
+ generatorOutput = generateSchemaData3(db, schema, config?.relationsDepthLimit);
2101
+ } else
2102
+ throw new Error("Drizzle-GraphQL Error: Unknown database instance type");
2103
+ const { queries, mutations, inputs, types } = generatorOutput;
2104
+ const graphQLSchemaConfig = {
2105
+ types: [...Object.values(inputs), ...Object.values(types)],
2106
+ query: new import_graphql7.GraphQLObjectType({
2107
+ name: "Query",
2108
+ fields: queries
2109
+ })
2110
+ };
2111
+ if (config?.mutations !== false) {
2112
+ const mutation = new import_graphql7.GraphQLObjectType({
2113
+ name: "Mutation",
2114
+ fields: mutations
2115
+ });
2116
+ graphQLSchemaConfig.mutation = mutation;
2117
+ }
2118
+ const outputSchema = new import_graphql7.GraphQLSchema(graphQLSchemaConfig);
2119
+ return { schema: outputSchema, entities: generatorOutput };
2120
+ };
2121
+ // Annotate the CommonJS export names for ESM import in node:
2122
+ 0 && (module.exports = {
2123
+ buildSchema
2124
+ });
2125
+ //# sourceMappingURL=index.cjs.map