@vantreeseba/drizzle-graphql 2.0.0 → 4.0.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.
- package/README.md +390 -0
- package/dist/README.md +390 -0
- package/dist/index.cjs +1760 -463
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +220 -7
- package/dist/index.d.ts +220 -7
- package/dist/index.js +1712 -401
- package/dist/index.js.map +1 -1
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -30,28 +30,29 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
+
GraphQLBigIntString: () => GraphQLBigIntString,
|
|
34
|
+
GraphQLDate: () => import_graphql_scalars.GraphQLDate,
|
|
35
|
+
GraphQLDateTime: () => import_graphql_scalars.GraphQLDateTime,
|
|
36
|
+
GraphQLJSON: () => import_graphql_scalars.GraphQLJSON,
|
|
37
|
+
GraphQLUUID: () => import_graphql_scalars.GraphQLUUID,
|
|
33
38
|
buildSchema: () => buildSchema,
|
|
34
39
|
createRelationResolverFactory: () => createRelationResolverFactory,
|
|
40
|
+
defaultErrorMapper: () => defaultErrorMapper,
|
|
41
|
+
drizzleExecutorKey: () => drizzleExecutorKey,
|
|
35
42
|
extractFilters: () => extractFilters,
|
|
36
43
|
extractOrderBy: () => extractOrderBy,
|
|
37
44
|
extractRelationJoinColumns: () => extractRelationJoinColumns
|
|
38
45
|
});
|
|
39
46
|
module.exports = __toCommonJS(index_exports);
|
|
40
|
-
var
|
|
47
|
+
var import_drizzle_orm8 = require("drizzle-orm");
|
|
41
48
|
var import_mysql_core3 = require("drizzle-orm/mysql-core");
|
|
42
49
|
var import_pg_core3 = require("drizzle-orm/pg-core");
|
|
43
50
|
var import_sqlite_core3 = require("drizzle-orm/sqlite-core");
|
|
44
|
-
var
|
|
45
|
-
|
|
46
|
-
// src/util/builders/mysql.ts
|
|
47
|
-
var import_drizzle_orm4 = require("drizzle-orm");
|
|
48
|
-
var import_mysql_core2 = require("drizzle-orm/mysql-core");
|
|
49
|
-
var import_graphql4 = require("graphql");
|
|
50
|
-
var import_graphql_parse_resolve_info = require("graphql-parse-resolve-info");
|
|
51
|
+
var import_graphql9 = require("graphql");
|
|
51
52
|
|
|
52
53
|
// src/util/builders/common.ts
|
|
53
54
|
var import_drizzle_orm3 = require("drizzle-orm");
|
|
54
|
-
var
|
|
55
|
+
var import_graphql4 = require("graphql");
|
|
55
56
|
|
|
56
57
|
// src/util/batch-loader/index.ts
|
|
57
58
|
var DRIZZLE_LOADERS_KEY = /* @__PURE__ */ Symbol("drizzle-graphql-loaders");
|
|
@@ -107,6 +108,7 @@ var capitalize = (input) => input?.length ? `${input[0].toLocaleUpperCase()}${in
|
|
|
107
108
|
// src/util/data-mappers/index.ts
|
|
108
109
|
var import_drizzle_orm = require("drizzle-orm");
|
|
109
110
|
var import_graphql = require("graphql");
|
|
111
|
+
var isJsonColumn = (column) => (column.dataType ?? "").includes("json") && column.columnType !== "PgGeometryObject";
|
|
110
112
|
var remapToGraphQLCore = (key, value, tableName, column, relationMap) => {
|
|
111
113
|
if (Array.isArray(value)) {
|
|
112
114
|
const relations = relationMap?.[tableName];
|
|
@@ -136,6 +138,9 @@ var remapToGraphQLCore = (key, value, tableName, column, relationMap) => {
|
|
|
136
138
|
if (!column) {
|
|
137
139
|
return value;
|
|
138
140
|
}
|
|
141
|
+
if (isJsonColumn(column)) {
|
|
142
|
+
return value;
|
|
143
|
+
}
|
|
139
144
|
if (value instanceof Date) {
|
|
140
145
|
return value.toISOString();
|
|
141
146
|
}
|
|
@@ -213,17 +218,7 @@ var remapFromGraphQLCore = (value, column, columnName) => {
|
|
|
213
218
|
}
|
|
214
219
|
}
|
|
215
220
|
if (dataType.includes("json") && column.columnType !== "PgGeometryObject") {
|
|
216
|
-
|
|
217
|
-
return value;
|
|
218
|
-
}
|
|
219
|
-
try {
|
|
220
|
-
return JSON.parse(value);
|
|
221
|
-
} catch (e) {
|
|
222
|
-
throw new import_graphql.GraphQLError(
|
|
223
|
-
`Invalid JSON in field '${columnName}':
|
|
224
|
-
${e instanceof Error ? e.message : "Unknown error"}`
|
|
225
|
-
);
|
|
226
|
-
}
|
|
221
|
+
return value;
|
|
227
222
|
}
|
|
228
223
|
switch (dataType) {
|
|
229
224
|
case "date": {
|
|
@@ -308,15 +303,55 @@ var import_drizzle_orm2 = require("drizzle-orm");
|
|
|
308
303
|
var import_mysql_core = require("drizzle-orm/mysql-core");
|
|
309
304
|
var import_pg_core = require("drizzle-orm/pg-core");
|
|
310
305
|
var import_sqlite_core = require("drizzle-orm/sqlite-core");
|
|
306
|
+
var import_graphql3 = require("graphql");
|
|
307
|
+
|
|
308
|
+
// src/util/scalars/index.ts
|
|
311
309
|
var import_graphql2 = require("graphql");
|
|
312
310
|
var import_graphql_scalars = require("graphql-scalars");
|
|
311
|
+
var asDecimalString = (value) => {
|
|
312
|
+
if (typeof value === "bigint") {
|
|
313
|
+
return value.toString();
|
|
314
|
+
}
|
|
315
|
+
if (typeof value === "number") {
|
|
316
|
+
if (!Number.isInteger(value)) {
|
|
317
|
+
throw new import_graphql2.GraphQLError(`BigInt cannot represent non-integer value: ${value}`);
|
|
318
|
+
}
|
|
319
|
+
if (!Number.isSafeInteger(value)) {
|
|
320
|
+
throw new import_graphql2.GraphQLError(
|
|
321
|
+
`BigInt cannot represent the number ${value} without precision loss \u2014 pass it as a string instead`
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
return String(value);
|
|
325
|
+
}
|
|
326
|
+
if (typeof value === "string") {
|
|
327
|
+
if (!/^-?\d+$/.test(value)) {
|
|
328
|
+
throw new import_graphql2.GraphQLError(`BigInt cannot represent non-integer value: "${value}"`);
|
|
329
|
+
}
|
|
330
|
+
return value;
|
|
331
|
+
}
|
|
332
|
+
throw new import_graphql2.GraphQLError(`BigInt cannot represent value: ${JSON.stringify(value)}`);
|
|
333
|
+
};
|
|
334
|
+
var GraphQLBigIntString = new import_graphql2.GraphQLScalarType({
|
|
335
|
+
name: "BigInt",
|
|
336
|
+
description: "A 64-bit integer, transported as a decimal string so that values beyond JavaScript's safe integer range survive the round-trip intact.",
|
|
337
|
+
serialize: asDecimalString,
|
|
338
|
+
parseValue: asDecimalString,
|
|
339
|
+
parseLiteral: (ast) => {
|
|
340
|
+
if (ast.kind !== import_graphql2.Kind.STRING && ast.kind !== import_graphql2.Kind.INT) {
|
|
341
|
+
throw new import_graphql2.GraphQLError(`BigInt cannot represent a ${ast.kind}`, { nodes: ast });
|
|
342
|
+
}
|
|
343
|
+
return asDecimalString(ast.value);
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
// src/util/type-converter/index.ts
|
|
313
348
|
var allowedNameChars = /^[a-zA-Z0-9_]+$/;
|
|
314
349
|
var enumMap = /* @__PURE__ */ new WeakMap();
|
|
315
350
|
var generateEnumCached = (column, columnName, tableName) => {
|
|
316
351
|
if (enumMap.has(column)) {
|
|
317
352
|
return enumMap.get(column);
|
|
318
353
|
}
|
|
319
|
-
const gqlEnum = new
|
|
354
|
+
const gqlEnum = new import_graphql3.GraphQLEnumType({
|
|
320
355
|
name: `${capitalize(tableName)}${capitalize(columnName)}Enum`,
|
|
321
356
|
values: Object.fromEntries(
|
|
322
357
|
column.enumValues.map((e, index) => [
|
|
@@ -331,25 +366,25 @@ var generateEnumCached = (column, columnName, tableName) => {
|
|
|
331
366
|
enumMap.set(column, gqlEnum);
|
|
332
367
|
return gqlEnum;
|
|
333
368
|
};
|
|
334
|
-
var geoXyType = new
|
|
369
|
+
var geoXyType = new import_graphql3.GraphQLObjectType({
|
|
335
370
|
name: "PgGeometryObject",
|
|
336
371
|
fields: {
|
|
337
|
-
x: { type:
|
|
338
|
-
y: { type:
|
|
372
|
+
x: { type: import_graphql3.GraphQLFloat },
|
|
373
|
+
y: { type: import_graphql3.GraphQLFloat }
|
|
339
374
|
}
|
|
340
375
|
});
|
|
341
|
-
var geoXyInputType = new
|
|
376
|
+
var geoXyInputType = new import_graphql3.GraphQLInputObjectType({
|
|
342
377
|
name: "PgGeometryObjectInput",
|
|
343
378
|
fields: {
|
|
344
|
-
x: { type:
|
|
345
|
-
y: { type:
|
|
379
|
+
x: { type: import_graphql3.GraphQLFloat },
|
|
380
|
+
y: { type: import_graphql3.GraphQLFloat }
|
|
346
381
|
}
|
|
347
382
|
});
|
|
348
383
|
var columnToGraphQLCore = (column, columnName, tableName, isInput) => {
|
|
349
384
|
const { type: baseType } = (0, import_drizzle_orm2.extractExtendedColumnType)(column);
|
|
350
385
|
switch (baseType) {
|
|
351
386
|
case "boolean":
|
|
352
|
-
return { type:
|
|
387
|
+
return { type: import_graphql3.GraphQLBoolean, description: "Boolean" };
|
|
353
388
|
case "object":
|
|
354
389
|
if (column instanceof import_pg_core.PgTimestamp || column instanceof import_pg_core.PgDate) {
|
|
355
390
|
return { type: import_graphql_scalars.GraphQLDateTime, description: "DateTime" };
|
|
@@ -358,9 +393,9 @@ var columnToGraphQLCore = (column, columnName, tableName, isInput) => {
|
|
|
358
393
|
type: isInput ? geoXyInputType : geoXyType,
|
|
359
394
|
description: "Geometry points XY"
|
|
360
395
|
} : column.columnType === "PgBytea" ? {
|
|
361
|
-
type: new
|
|
396
|
+
type: new import_graphql3.GraphQLList(new import_graphql3.GraphQLNonNull(import_graphql3.GraphQLInt)),
|
|
362
397
|
description: "Buffer"
|
|
363
|
-
} : { type:
|
|
398
|
+
} : { type: import_graphql_scalars.GraphQLJSON, description: "JSON" };
|
|
364
399
|
case "string":
|
|
365
400
|
if (column.enumValues?.length) {
|
|
366
401
|
return { type: generateEnumCached(column, columnName, tableName) };
|
|
@@ -368,34 +403,37 @@ var columnToGraphQLCore = (column, columnName, tableName, isInput) => {
|
|
|
368
403
|
if (column instanceof import_pg_core.PgTimestamp || column instanceof import_pg_core.PgTimestampString) {
|
|
369
404
|
return { type: import_graphql_scalars.GraphQLDateTime, description: "DateTime" };
|
|
370
405
|
}
|
|
406
|
+
if (column instanceof import_pg_core.PgUUID) {
|
|
407
|
+
return { type: import_graphql_scalars.GraphQLUUID, description: "UUID" };
|
|
408
|
+
}
|
|
371
409
|
if (column instanceof import_pg_core.PgDateString) {
|
|
372
|
-
return isInput ? { type:
|
|
410
|
+
return isInput ? { type: import_graphql3.GraphQLString, description: "Date" } : { type: import_graphql_scalars.GraphQLDate, description: "Date" };
|
|
373
411
|
}
|
|
374
|
-
return { type:
|
|
412
|
+
return { type: import_graphql3.GraphQLString, description: "String" };
|
|
375
413
|
case "bigint":
|
|
376
|
-
return { type:
|
|
414
|
+
return { type: GraphQLBigIntString, description: "BigInt" };
|
|
377
415
|
case "number": {
|
|
378
416
|
const dims = column.dimensions;
|
|
379
417
|
if (dims !== void 0 && dims > 0) {
|
|
380
418
|
const baseDesc = (0, import_drizzle_orm2.is)(column, import_pg_core.PgInteger) || (0, import_drizzle_orm2.is)(column, import_pg_core.PgSerial) ? "Integer" : "Float";
|
|
381
|
-
const baseType2 = baseDesc === "Integer" ?
|
|
419
|
+
const baseType2 = baseDesc === "Integer" ? import_graphql3.GraphQLInt : import_graphql3.GraphQLFloat;
|
|
382
420
|
return {
|
|
383
|
-
type: new
|
|
421
|
+
type: new import_graphql3.GraphQLList(new import_graphql3.GraphQLNonNull(baseType2)),
|
|
384
422
|
description: `Array<${baseDesc}>`
|
|
385
423
|
};
|
|
386
424
|
}
|
|
387
|
-
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:
|
|
425
|
+
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_graphql3.GraphQLInt, description: "Integer" } : { type: import_graphql3.GraphQLFloat, description: "Float" };
|
|
388
426
|
}
|
|
389
427
|
case "array": {
|
|
390
428
|
if (column.columnType === "PgVector") {
|
|
391
429
|
return {
|
|
392
|
-
type: new
|
|
430
|
+
type: new import_graphql3.GraphQLList(new import_graphql3.GraphQLNonNull(import_graphql3.GraphQLFloat)),
|
|
393
431
|
description: "Array<Float>"
|
|
394
432
|
};
|
|
395
433
|
}
|
|
396
434
|
if (column.columnType === "PgGeometry") {
|
|
397
435
|
return {
|
|
398
|
-
type: new
|
|
436
|
+
type: new import_graphql3.GraphQLList(new import_graphql3.GraphQLNonNull(import_graphql3.GraphQLFloat)),
|
|
399
437
|
description: "Tuple<[Float, Float]>"
|
|
400
438
|
};
|
|
401
439
|
}
|
|
@@ -406,7 +444,7 @@ var columnToGraphQLCore = (column, columnName, tableName, isInput) => {
|
|
|
406
444
|
isInput
|
|
407
445
|
);
|
|
408
446
|
return {
|
|
409
|
-
type: new
|
|
447
|
+
type: new import_graphql3.GraphQLList(new import_graphql3.GraphQLNonNull(innerType.type)),
|
|
410
448
|
description: `Array<${innerType.description}>`
|
|
411
449
|
};
|
|
412
450
|
}
|
|
@@ -426,7 +464,7 @@ var drizzleColumnToGraphQLType = (column, columnName, tableName, forceNullable =
|
|
|
426
464
|
}
|
|
427
465
|
if (column.notNull && !(defaultIsNullable && (column.hasDefault || column.defaultFn))) {
|
|
428
466
|
return {
|
|
429
|
-
type: new
|
|
467
|
+
type: new import_graphql3.GraphQLNonNull(typeDesc.type),
|
|
430
468
|
description: typeDesc.description
|
|
431
469
|
};
|
|
432
470
|
}
|
|
@@ -504,6 +542,23 @@ var extractRelationJoinColumns = (relEntry, parentTable, targetTable) => {
|
|
|
504
542
|
}
|
|
505
543
|
return { localColPropName, foreignCol: targetCol, foreignColPropName };
|
|
506
544
|
};
|
|
545
|
+
var drizzleExecutorKey = /* @__PURE__ */ Symbol.for("drizzle-graphql:executor");
|
|
546
|
+
var resolveExecutor = (db, context) => {
|
|
547
|
+
if (context && typeof context === "object") {
|
|
548
|
+
const executor = context[drizzleExecutorKey];
|
|
549
|
+
if (executor) {
|
|
550
|
+
return executor;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
return db;
|
|
554
|
+
};
|
|
555
|
+
var resolveQueryExecutor = (db, context, tableName, buildTimeQueryBase) => {
|
|
556
|
+
const executor = resolveExecutor(db, context);
|
|
557
|
+
return {
|
|
558
|
+
executor,
|
|
559
|
+
queryBase: buildTimeQueryBase ? executor?.query?.[tableName] ?? buildTimeQueryBase : buildTimeQueryBase
|
|
560
|
+
};
|
|
561
|
+
};
|
|
507
562
|
var batchedPaginatedRelationQuery = async (db, targetTable, foreignCol, whereCondition, orderByArg, limit, offset, pkNames) => {
|
|
508
563
|
const cols = (0, import_drizzle_orm3.getColumns)(targetTable);
|
|
509
564
|
const orderExprs = [
|
|
@@ -525,7 +580,7 @@ var batchedPaginatedRelationQuery = async (db, targetTable, foreignCol, whereCon
|
|
|
525
580
|
}
|
|
526
581
|
return rows;
|
|
527
582
|
};
|
|
528
|
-
var createRelationResolverFactory = (db, tables) => ({ tableName, relationName, relEntry, isOne }) => {
|
|
583
|
+
var createRelationResolverFactory = (db, tables, filterCtx) => ({ tableName, relationName, relEntry, isOne }) => {
|
|
529
584
|
const parentTable = tables[tableName];
|
|
530
585
|
const targetTableName = relEntry.targetTableName;
|
|
531
586
|
const targetTable = tables[targetTableName];
|
|
@@ -555,15 +610,16 @@ var createRelationResolverFactory = (db, tables) => ({ tableName, relationName,
|
|
|
555
610
|
});
|
|
556
611
|
const loaderKey = `${tableName}::${relationName}::${argsKey}`;
|
|
557
612
|
const loader = getOrCreateLoader(context, loaderKey, async (parentIds) => {
|
|
613
|
+
const executor = resolveExecutor(db, context);
|
|
558
614
|
const uniqueIds = [...new Set(parentIds)];
|
|
559
615
|
const whereCondition = (0, import_drizzle_orm3.and)(
|
|
560
616
|
(0, import_drizzle_orm3.inArray)(foreignCol, uniqueIds),
|
|
561
|
-
whereArg ? extractFilters(targetTable, targetTableName, whereArg) : void 0
|
|
617
|
+
whereArg ? extractFilters(targetTable, targetTableName, whereArg, relationFilterCtx(filterCtx, targetTableName)) : void 0
|
|
562
618
|
);
|
|
563
619
|
let rows;
|
|
564
620
|
if (limit != null || offset != null) {
|
|
565
621
|
rows = await batchedPaginatedRelationQuery(
|
|
566
|
-
|
|
622
|
+
executor,
|
|
567
623
|
targetTable,
|
|
568
624
|
foreignCol,
|
|
569
625
|
whereCondition,
|
|
@@ -573,7 +629,7 @@ var createRelationResolverFactory = (db, tables) => ({ tableName, relationName,
|
|
|
573
629
|
targetPkNames
|
|
574
630
|
);
|
|
575
631
|
} else {
|
|
576
|
-
let q =
|
|
632
|
+
let q = executor.select().from(targetTable).where(whereCondition);
|
|
577
633
|
if (orderByArg) {
|
|
578
634
|
q = q.orderBy(...extractOrderBy(targetTable, orderByArg));
|
|
579
635
|
}
|
|
@@ -594,7 +650,31 @@ var createRelationResolverFactory = (db, tables) => ({ tableName, relationName,
|
|
|
594
650
|
return loader.load(localValue);
|
|
595
651
|
};
|
|
596
652
|
};
|
|
597
|
-
var
|
|
653
|
+
var AGGREGATE_FIELD_SUFFIX = "Aggregate";
|
|
654
|
+
var relationAggregateJoinColumns = (tree, table, selectionCtx) => {
|
|
655
|
+
const relations = selectionCtx?.relationMap[selectionCtx.tableName];
|
|
656
|
+
if (!relations || !selectionCtx) {
|
|
657
|
+
return [];
|
|
658
|
+
}
|
|
659
|
+
const tableColumns = (0, import_drizzle_orm3.getColumns)(table);
|
|
660
|
+
const needed = [];
|
|
661
|
+
for (const fieldData of Object.values(tree)) {
|
|
662
|
+
if (tableColumns[fieldData.name] || !fieldData.name.endsWith(AGGREGATE_FIELD_SUFFIX)) {
|
|
663
|
+
continue;
|
|
664
|
+
}
|
|
665
|
+
const relEntry = relations[fieldData.name.slice(0, -AGGREGATE_FIELD_SUFFIX.length)];
|
|
666
|
+
const targetTable = relEntry ? selectionCtx.tables[relEntry.targetTableName] : void 0;
|
|
667
|
+
if (!relEntry || !targetTable) {
|
|
668
|
+
continue;
|
|
669
|
+
}
|
|
670
|
+
const joinCols = extractRelationJoinColumns(relEntry, table, targetTable);
|
|
671
|
+
if (joinCols) {
|
|
672
|
+
needed.push(joinCols.localColPropName);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
return needed;
|
|
676
|
+
};
|
|
677
|
+
var extractSelectedColumnsFromTree = (tree, table, selectionCtx) => {
|
|
598
678
|
const tableColumns = (0, import_drizzle_orm3.getColumns)(table);
|
|
599
679
|
const treeEntries = Object.entries(tree);
|
|
600
680
|
const selectedColumns = [];
|
|
@@ -604,6 +684,9 @@ var extractSelectedColumnsFromTree = (tree, table) => {
|
|
|
604
684
|
}
|
|
605
685
|
selectedColumns.push([fieldData.name, true]);
|
|
606
686
|
}
|
|
687
|
+
for (const columnName of relationAggregateJoinColumns(tree, table, selectionCtx)) {
|
|
688
|
+
selectedColumns.push([columnName, true]);
|
|
689
|
+
}
|
|
607
690
|
if (!selectedColumns.length) {
|
|
608
691
|
const columnKeys = Object.entries(tableColumns);
|
|
609
692
|
const columnName = columnKeys.find((e) => rqbCrashTypes.find((haram) => e[1].columnType !== haram))?.[0] ?? columnKeys[0][0];
|
|
@@ -611,7 +694,7 @@ var extractSelectedColumnsFromTree = (tree, table) => {
|
|
|
611
694
|
}
|
|
612
695
|
return Object.fromEntries(selectedColumns);
|
|
613
696
|
};
|
|
614
|
-
var extractSelectedColumnsFromTreeSQLFormat = (tree, table) => {
|
|
697
|
+
var extractSelectedColumnsFromTreeSQLFormat = (tree, table, selectionCtx) => {
|
|
615
698
|
const tableColumns = (0, import_drizzle_orm3.getColumns)(table);
|
|
616
699
|
const treeEntries = Object.entries(tree);
|
|
617
700
|
const selectedColumns = [];
|
|
@@ -621,6 +704,9 @@ var extractSelectedColumnsFromTreeSQLFormat = (tree, table) => {
|
|
|
621
704
|
}
|
|
622
705
|
selectedColumns.push([fieldData.name, tableColumns[fieldData.name]]);
|
|
623
706
|
}
|
|
707
|
+
for (const columnName of relationAggregateJoinColumns(tree, table, selectionCtx)) {
|
|
708
|
+
selectedColumns.push([columnName, tableColumns[columnName]]);
|
|
709
|
+
}
|
|
624
710
|
if (!selectedColumns.length) {
|
|
625
711
|
const columnKeys = Object.entries(tableColumns);
|
|
626
712
|
const columnName = columnKeys.find((e) => rqbCrashTypes.find((haram) => e[1].columnType !== haram))?.[0] ?? columnKeys[0][0];
|
|
@@ -628,12 +714,12 @@ var extractSelectedColumnsFromTreeSQLFormat = (tree, table) => {
|
|
|
628
714
|
}
|
|
629
715
|
return Object.fromEntries(selectedColumns);
|
|
630
716
|
};
|
|
631
|
-
var innerOrder = new
|
|
717
|
+
var innerOrder = new import_graphql4.GraphQLInputObjectType({
|
|
632
718
|
name: "InnerOrder",
|
|
633
719
|
fields: {
|
|
634
720
|
direction: {
|
|
635
|
-
type: new
|
|
636
|
-
new
|
|
721
|
+
type: new import_graphql4.GraphQLNonNull(
|
|
722
|
+
new import_graphql4.GraphQLEnumType({
|
|
637
723
|
name: "OrderDirection",
|
|
638
724
|
description: "Order by direction",
|
|
639
725
|
values: {
|
|
@@ -650,7 +736,7 @@ var innerOrder = new import_graphql3.GraphQLInputObjectType({
|
|
|
650
736
|
)
|
|
651
737
|
},
|
|
652
738
|
priority: {
|
|
653
|
-
type: new
|
|
739
|
+
type: new import_graphql4.GraphQLNonNull(import_graphql4.GraphQLInt),
|
|
654
740
|
description: "Priority of current field"
|
|
655
741
|
}
|
|
656
742
|
}
|
|
@@ -659,13 +745,13 @@ var resolveGenericFilterName = (column, columnName, columnGraphQLType) => {
|
|
|
659
745
|
if (columnName === "id" || columnName.endsWith("Id")) {
|
|
660
746
|
return "Id";
|
|
661
747
|
}
|
|
662
|
-
if (columnGraphQLType.type ===
|
|
748
|
+
if (columnGraphQLType.type === import_graphql4.GraphQLBoolean) {
|
|
663
749
|
return "Boolean";
|
|
664
750
|
}
|
|
665
|
-
if (columnGraphQLType.type instanceof
|
|
751
|
+
if (columnGraphQLType.type instanceof import_graphql4.GraphQLEnumType) {
|
|
666
752
|
return columnGraphQLType.type.name;
|
|
667
753
|
}
|
|
668
|
-
if (columnGraphQLType.type instanceof
|
|
754
|
+
if (columnGraphQLType.type instanceof import_graphql4.GraphQLList) {
|
|
669
755
|
const desc2 = columnGraphQLType.description ?? "";
|
|
670
756
|
return desc2.includes("Integer") ? "IntArray" : "FloatArray";
|
|
671
757
|
}
|
|
@@ -684,7 +770,7 @@ var generateColumnFilterValues = (column, tableName, columnName, cacheCtx) => {
|
|
|
684
770
|
}
|
|
685
771
|
const colType = columnGraphQLType.type;
|
|
686
772
|
const colDesc = columnGraphQLType.description;
|
|
687
|
-
const colArr = new
|
|
773
|
+
const colArr = new import_graphql4.GraphQLList(new import_graphql4.GraphQLNonNull(colType));
|
|
688
774
|
const isId = genericName === "Id";
|
|
689
775
|
const baseFields = {
|
|
690
776
|
eq: { type: colType, description: colDesc },
|
|
@@ -694,26 +780,26 @@ var generateColumnFilterValues = (column, tableName, columnName, cacheCtx) => {
|
|
|
694
780
|
gt: { type: colType, description: colDesc },
|
|
695
781
|
gte: { type: colType, description: colDesc },
|
|
696
782
|
...isId ? {} : {
|
|
697
|
-
like: { type:
|
|
698
|
-
notLike: { type:
|
|
699
|
-
ilike: { type:
|
|
700
|
-
notIlike: { type:
|
|
783
|
+
like: { type: import_graphql4.GraphQLString },
|
|
784
|
+
notLike: { type: import_graphql4.GraphQLString },
|
|
785
|
+
ilike: { type: import_graphql4.GraphQLString },
|
|
786
|
+
notIlike: { type: import_graphql4.GraphQLString }
|
|
701
787
|
},
|
|
702
788
|
inArray: { type: colArr, description: `Array<${colDesc}>` },
|
|
703
789
|
notInArray: { type: colArr, description: `Array<${colDesc}>` },
|
|
704
|
-
isNull: { type:
|
|
705
|
-
isNotNull: { type:
|
|
790
|
+
isNull: { type: import_graphql4.GraphQLBoolean },
|
|
791
|
+
isNotNull: { type: import_graphql4.GraphQLBoolean }
|
|
706
792
|
};
|
|
707
|
-
const orType = new
|
|
793
|
+
const orType = new import_graphql4.GraphQLInputObjectType({
|
|
708
794
|
name: `${genericName}FilterOr`,
|
|
709
795
|
fields: { ...baseFields }
|
|
710
796
|
});
|
|
711
|
-
const mainType = new
|
|
797
|
+
const mainType = new import_graphql4.GraphQLInputObjectType({
|
|
712
798
|
name: `${genericName}Filter`,
|
|
713
799
|
fields: {
|
|
714
800
|
...baseFields,
|
|
715
801
|
OR: {
|
|
716
|
-
type: new
|
|
802
|
+
type: new import_graphql4.GraphQLList(new import_graphql4.GraphQLNonNull(orType))
|
|
717
803
|
}
|
|
718
804
|
}
|
|
719
805
|
});
|
|
@@ -776,41 +862,109 @@ var generateTableOrderTypeCached = (table, tableName, typeNameMapper, cacheCtx)
|
|
|
776
862
|
return cacheCtx.orderTypeCache.get(table);
|
|
777
863
|
}
|
|
778
864
|
const orderColumns = generateTableOrderCached(table);
|
|
779
|
-
const order = new
|
|
865
|
+
const order = new import_graphql4.GraphQLInputObjectType({
|
|
780
866
|
name: `${resolveTypeName(tableName, typeNameMapper)}OrderBy`,
|
|
781
867
|
fields: orderColumns
|
|
782
868
|
});
|
|
783
869
|
cacheCtx.orderTypeCache.set(table, order);
|
|
784
870
|
return order;
|
|
785
871
|
};
|
|
786
|
-
var
|
|
872
|
+
var isFilterableRelation = (relation) => !relation.through;
|
|
873
|
+
var generateListRelationFilterCached = (targetTable, targetTableName, cacheCtx, typeNameMapper, relationMap, tables) => {
|
|
874
|
+
const cached = cacheCtx.listRelationFilterCache.get(targetTableName);
|
|
875
|
+
if (cached) {
|
|
876
|
+
return cached;
|
|
877
|
+
}
|
|
878
|
+
const listFilter = new import_graphql4.GraphQLInputObjectType({
|
|
879
|
+
name: `${resolveTypeName(targetTableName, typeNameMapper)}ListRelationFilter`,
|
|
880
|
+
fields: () => {
|
|
881
|
+
const targetFilters = generateTableFilterTypeCached(
|
|
882
|
+
targetTable,
|
|
883
|
+
targetTableName,
|
|
884
|
+
cacheCtx,
|
|
885
|
+
typeNameMapper,
|
|
886
|
+
relationMap,
|
|
887
|
+
tables
|
|
888
|
+
);
|
|
889
|
+
return {
|
|
890
|
+
some: { type: targetFilters, description: "At least one related row matches" },
|
|
891
|
+
none: { type: targetFilters, description: "No related row matches" },
|
|
892
|
+
every: { type: targetFilters, description: "Every related row matches" }
|
|
893
|
+
};
|
|
894
|
+
}
|
|
895
|
+
});
|
|
896
|
+
cacheCtx.listRelationFilterCache.set(targetTableName, listFilter);
|
|
897
|
+
return listFilter;
|
|
898
|
+
};
|
|
899
|
+
var generateRelationFilterFields = (tableName, cacheCtx, typeNameMapper, columnFields, relationMap, tables) => {
|
|
900
|
+
const relations = relationMap?.[tableName];
|
|
901
|
+
if (!relations || !tables) {
|
|
902
|
+
return {};
|
|
903
|
+
}
|
|
904
|
+
const fields = {};
|
|
905
|
+
for (const [relationName, relEntry] of Object.entries(relations)) {
|
|
906
|
+
if (relationName in columnFields) {
|
|
907
|
+
continue;
|
|
908
|
+
}
|
|
909
|
+
const targetTable = tables[relEntry.targetTableName];
|
|
910
|
+
const relation = relEntry.relation ?? relEntry;
|
|
911
|
+
if (!targetTable || !isFilterableRelation(relation)) {
|
|
912
|
+
continue;
|
|
913
|
+
}
|
|
914
|
+
fields[relationName] = (0, import_drizzle_orm3.is)(relation, import_drizzle_orm3.One) ? {
|
|
915
|
+
type: generateTableFilterTypeCached(
|
|
916
|
+
targetTable,
|
|
917
|
+
relEntry.targetTableName,
|
|
918
|
+
cacheCtx,
|
|
919
|
+
typeNameMapper,
|
|
920
|
+
relationMap,
|
|
921
|
+
tables
|
|
922
|
+
),
|
|
923
|
+
description: `Matches rows whose ${relationName} matches these filters`
|
|
924
|
+
} : {
|
|
925
|
+
type: generateListRelationFilterCached(
|
|
926
|
+
targetTable,
|
|
927
|
+
relEntry.targetTableName,
|
|
928
|
+
cacheCtx,
|
|
929
|
+
typeNameMapper,
|
|
930
|
+
relationMap,
|
|
931
|
+
tables
|
|
932
|
+
)
|
|
933
|
+
};
|
|
934
|
+
}
|
|
935
|
+
return fields;
|
|
936
|
+
};
|
|
937
|
+
var generateTableFilterTypeCached = (table, tableName, cacheCtx, typeNameMapper, relationMap, tables) => {
|
|
787
938
|
if (cacheCtx.filterTypeCache.has(table)) {
|
|
788
939
|
return cacheCtx.filterTypeCache.get(table);
|
|
789
940
|
}
|
|
790
|
-
const
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
fields: {
|
|
941
|
+
const buildFields = () => {
|
|
942
|
+
const filterColumns = generateTableFilterValuesCached(table, tableName, cacheCtx);
|
|
943
|
+
return {
|
|
794
944
|
...filterColumns,
|
|
945
|
+
...generateRelationFilterFields(tableName, cacheCtx, typeNameMapper, filterColumns, relationMap, tables)
|
|
946
|
+
};
|
|
947
|
+
};
|
|
948
|
+
const orFilters = new import_graphql4.GraphQLInputObjectType({
|
|
949
|
+
name: `${resolveTypeName(tableName, typeNameMapper)}FiltersOr`,
|
|
950
|
+
fields: buildFields
|
|
951
|
+
});
|
|
952
|
+
const filters = new import_graphql4.GraphQLInputObjectType({
|
|
953
|
+
name: `${resolveTypeName(tableName, typeNameMapper)}Filters`,
|
|
954
|
+
fields: () => ({
|
|
955
|
+
...buildFields(),
|
|
795
956
|
OR: {
|
|
796
|
-
type: new
|
|
797
|
-
new import_graphql3.GraphQLNonNull(
|
|
798
|
-
new import_graphql3.GraphQLInputObjectType({
|
|
799
|
-
name: `${resolveTypeName(tableName, typeNameMapper)}FiltersOr`,
|
|
800
|
-
fields: filterColumns
|
|
801
|
-
})
|
|
802
|
-
)
|
|
803
|
-
)
|
|
957
|
+
type: new import_graphql4.GraphQLList(new import_graphql4.GraphQLNonNull(orFilters))
|
|
804
958
|
}
|
|
805
|
-
}
|
|
959
|
+
})
|
|
806
960
|
});
|
|
807
961
|
cacheCtx.filterTypeCache.set(table, filters);
|
|
808
962
|
return filters;
|
|
809
963
|
};
|
|
810
|
-
var generateSelectFields = (tables, tableName, relationMap, fromTableName, fromRelationName, withOrder, relationsDepthLimit, cacheCtx, typeNameMapper, usedTables = /* @__PURE__ */ new Set(), resolverFactory, currentDepth = 0) => {
|
|
964
|
+
var generateSelectFields = (tables, tableName, relationMap, fromTableName, fromRelationName, withOrder, relationsDepthLimit, cacheCtx, typeNameMapper, usedTables = /* @__PURE__ */ new Set(), resolverFactory, currentDepth = 0, relationAggregateFactory) => {
|
|
811
965
|
const table = tables[tableName];
|
|
812
966
|
const order = withOrder ? generateTableOrderTypeCached(table, tableName, typeNameMapper, cacheCtx) : void 0;
|
|
813
|
-
const filters = generateTableFilterTypeCached(table, tableName, cacheCtx, typeNameMapper);
|
|
967
|
+
const filters = generateTableFilterTypeCached(table, tableName, cacheCtx, typeNameMapper, relationMap, tables);
|
|
814
968
|
const tableFields = generateTableSelectTypeFieldsCached(table, tableName);
|
|
815
969
|
const relationsForTable = relationMap[tableName];
|
|
816
970
|
const relationEntries = relationsForTable ? Object.entries(relationsForTable) : [];
|
|
@@ -846,7 +1000,7 @@ var generateSelectFields = (tables, tableName, relationMap, fromTableName, fromR
|
|
|
846
1000
|
}
|
|
847
1001
|
if (isRootCall && !cacheCtx.objectTypeCache.has(tableName)) {
|
|
848
1002
|
const typeName = resolveTypeName(tableName, typeNameMapper);
|
|
849
|
-
const shell = new
|
|
1003
|
+
const shell = new import_graphql4.GraphQLObjectType({
|
|
850
1004
|
name: typeName,
|
|
851
1005
|
fields: () => ({ ...tableFields, ...container.fields })
|
|
852
1006
|
});
|
|
@@ -874,7 +1028,8 @@ var generateSelectFields = (tables, tableName, relationMap, fromTableName, fromR
|
|
|
874
1028
|
typeNameMapper,
|
|
875
1029
|
nextUsedTables,
|
|
876
1030
|
resolverFactory,
|
|
877
|
-
currentDepth + 1
|
|
1031
|
+
currentDepth + 1,
|
|
1032
|
+
relationAggregateFactory
|
|
878
1033
|
);
|
|
879
1034
|
let relType = cacheCtx.objectTypeCache.get(targetTableName);
|
|
880
1035
|
if (!relType) {
|
|
@@ -886,7 +1041,7 @@ var generateSelectFields = (tables, tableName, relationMap, fromTableName, fromR
|
|
|
886
1041
|
cacheCtx.relationFieldContainers.set(targetTableName, targetContainer);
|
|
887
1042
|
}
|
|
888
1043
|
const capturedTargetContainer = targetContainer;
|
|
889
|
-
relType = new
|
|
1044
|
+
relType = new import_graphql4.GraphQLObjectType({
|
|
890
1045
|
name: resolveTypeName(targetTableName, typeNameMapper),
|
|
891
1046
|
fields: () => ({ ...targetTableFields, ...capturedTargetContainer.fields })
|
|
892
1047
|
});
|
|
@@ -909,16 +1064,36 @@ var generateSelectFields = (tables, tableName, relationMap, fromTableName, fromR
|
|
|
909
1064
|
rawRelationFields.push([
|
|
910
1065
|
relationName,
|
|
911
1066
|
{
|
|
912
|
-
type: new
|
|
1067
|
+
type: new import_graphql4.GraphQLNonNull(new import_graphql4.GraphQLList(new import_graphql4.GraphQLNonNull(relType))),
|
|
913
1068
|
args: {
|
|
914
1069
|
where: { type: relSelectData.filters },
|
|
915
1070
|
orderBy: { type: relSelectData.order },
|
|
916
|
-
offset: { type:
|
|
917
|
-
limit: { type:
|
|
1071
|
+
offset: { type: import_graphql4.GraphQLInt },
|
|
1072
|
+
limit: { type: import_graphql4.GraphQLInt }
|
|
918
1073
|
},
|
|
919
1074
|
resolve
|
|
920
1075
|
}
|
|
921
1076
|
]);
|
|
1077
|
+
const aggregateFieldName = `${relationName}Aggregate`;
|
|
1078
|
+
if (!tableFields[aggregateFieldName] && !relationsForTable?.[aggregateFieldName]) {
|
|
1079
|
+
const relationAggregate = relationAggregateFactory?.({
|
|
1080
|
+
tableName,
|
|
1081
|
+
relationName,
|
|
1082
|
+
relEntry
|
|
1083
|
+
});
|
|
1084
|
+
if (relationAggregate) {
|
|
1085
|
+
rawRelationFields.push([
|
|
1086
|
+
aggregateFieldName,
|
|
1087
|
+
{
|
|
1088
|
+
type: new import_graphql4.GraphQLNonNull(relationAggregate.type),
|
|
1089
|
+
args: {
|
|
1090
|
+
where: { type: relSelectData.filters }
|
|
1091
|
+
},
|
|
1092
|
+
resolve: relationAggregate.resolve
|
|
1093
|
+
}
|
|
1094
|
+
]);
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
922
1097
|
}
|
|
923
1098
|
const builtRelationFields = Object.fromEntries(rawRelationFields);
|
|
924
1099
|
if (isRootCall) {
|
|
@@ -942,7 +1117,7 @@ var generateSelectFields = (tables, tableName, relationMap, fromTableName, fromR
|
|
|
942
1117
|
relationFields: {}
|
|
943
1118
|
};
|
|
944
1119
|
};
|
|
945
|
-
var generateTableTypes = (tableName, tables, relationMap, withReturning, relationsDepthLimit, cacheCtx, typeNameMapper = void 0, insertPrefix = "create", updatePrefix = "update", resolverFactory) => {
|
|
1120
|
+
var generateTableTypes = (tableName, tables, relationMap, withReturning, relationsDepthLimit, cacheCtx, typeNameMapper = void 0, insertPrefix = "create", updatePrefix = "update", resolverFactory, relationAggregateFactory) => {
|
|
946
1121
|
const { tableFields, relationFields, filters, order } = generateSelectFields(
|
|
947
1122
|
tables,
|
|
948
1123
|
tableName,
|
|
@@ -957,7 +1132,8 @@ var generateTableTypes = (tableName, tables, relationMap, withReturning, relatio
|
|
|
957
1132
|
typeNameMapper,
|
|
958
1133
|
/* @__PURE__ */ new Set(),
|
|
959
1134
|
resolverFactory,
|
|
960
|
-
0
|
|
1135
|
+
0,
|
|
1136
|
+
relationAggregateFactory
|
|
961
1137
|
);
|
|
962
1138
|
const table = tables[tableName];
|
|
963
1139
|
const columns = (0, import_drizzle_orm3.getColumns)(table);
|
|
@@ -974,22 +1150,22 @@ var generateTableTypes = (tableName, tables, relationMap, withReturning, relatio
|
|
|
974
1150
|
drizzleColumnToGraphQLType(columnDescription, columnName, tableName, true, false, true)
|
|
975
1151
|
])
|
|
976
1152
|
);
|
|
977
|
-
const insertInput = new
|
|
1153
|
+
const insertInput = new import_graphql4.GraphQLInputObjectType({
|
|
978
1154
|
name: `${capitalize(insertPrefix)}${resolveTypeName(tableName, typeNameMapper)}Input`,
|
|
979
1155
|
fields: insertFields
|
|
980
1156
|
});
|
|
981
|
-
const updateInput = new
|
|
1157
|
+
const updateInput = new import_graphql4.GraphQLInputObjectType({
|
|
982
1158
|
name: `${capitalize(updatePrefix)}${resolveTypeName(tableName, typeNameMapper)}Input`,
|
|
983
1159
|
fields: updateFields
|
|
984
1160
|
});
|
|
985
|
-
const selectSingleOutput = cacheCtx.objectTypeCache.get(tableName) ?? new
|
|
1161
|
+
const selectSingleOutput = cacheCtx.objectTypeCache.get(tableName) ?? new import_graphql4.GraphQLObjectType({
|
|
986
1162
|
name: resolveTypeName(tableName, typeNameMapper),
|
|
987
1163
|
fields: { ...tableFields, ...relationFields }
|
|
988
1164
|
});
|
|
989
|
-
const selectArrOutput = new
|
|
1165
|
+
const selectArrOutput = new import_graphql4.GraphQLNonNull(new import_graphql4.GraphQLList(new import_graphql4.GraphQLNonNull(selectSingleOutput)));
|
|
990
1166
|
const arrTableItemOutput = withReturning ? (
|
|
991
1167
|
// ? new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(singleTableItemOutput!)))
|
|
992
|
-
new
|
|
1168
|
+
new import_graphql4.GraphQLNonNull(new import_graphql4.GraphQLList(new import_graphql4.GraphQLNonNull(selectSingleOutput)))
|
|
993
1169
|
) : void 0;
|
|
994
1170
|
const inputs = {
|
|
995
1171
|
insertInput,
|
|
@@ -1012,19 +1188,10 @@ var generateTableTypes = (tableName, tables, relationMap, withReturning, relatio
|
|
|
1012
1188
|
outputs
|
|
1013
1189
|
};
|
|
1014
1190
|
};
|
|
1015
|
-
var
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
)) {
|
|
1020
|
-
if (!config) {
|
|
1021
|
-
continue;
|
|
1022
|
-
}
|
|
1023
|
-
const { direction } = config;
|
|
1024
|
-
res.push(direction === "asc" ? (0, import_drizzle_orm3.asc)((0, import_drizzle_orm3.getColumns)(table)[column]) : (0, import_drizzle_orm3.desc)((0, import_drizzle_orm3.getColumns)(table)[column]));
|
|
1025
|
-
}
|
|
1026
|
-
return res;
|
|
1027
|
-
};
|
|
1191
|
+
var orderByEntries = (orderArgs) => Object.entries(orderArgs).sort((a, b) => (b[1]?.priority ?? 0) - (a[1]?.priority ?? 0)).filter(([, config]) => config).map(([column, config]) => [column, config.direction]);
|
|
1192
|
+
var extractOrderBy = (table, orderArgs) => orderByEntries(orderArgs).map(
|
|
1193
|
+
([column, direction]) => direction === "asc" ? (0, import_drizzle_orm3.asc)((0, import_drizzle_orm3.getColumns)(table)[column]) : (0, import_drizzle_orm3.desc)((0, import_drizzle_orm3.getColumns)(table)[column])
|
|
1194
|
+
);
|
|
1028
1195
|
var extractFiltersColumn = (column, columnName, operators) => {
|
|
1029
1196
|
if (!operators.OR?.length) {
|
|
1030
1197
|
delete operators.OR;
|
|
@@ -1032,7 +1199,7 @@ var extractFiltersColumn = (column, columnName, operators) => {
|
|
|
1032
1199
|
const entries = Object.entries(operators);
|
|
1033
1200
|
if (operators.OR) {
|
|
1034
1201
|
if (entries.length > 1) {
|
|
1035
|
-
throw new
|
|
1202
|
+
throw new import_graphql4.GraphQLError(`WHERE ${columnName}: Cannot specify both fields and 'OR' in column operators!`);
|
|
1036
1203
|
}
|
|
1037
1204
|
const variants2 = [];
|
|
1038
1205
|
for (const variant of operators.OR) {
|
|
@@ -1059,7 +1226,7 @@ var extractFiltersColumn = (column, columnName, operators) => {
|
|
|
1059
1226
|
variants.push(stringValueOps[operatorName](column, operatorValue));
|
|
1060
1227
|
} else if (operatorName in arrayValueOps) {
|
|
1061
1228
|
if (!operatorValue.length) {
|
|
1062
|
-
throw new
|
|
1229
|
+
throw new import_graphql4.GraphQLError(`WHERE ${columnName}: Unable to use operator ${operatorName} with an empty array!`);
|
|
1063
1230
|
}
|
|
1064
1231
|
const arrayValue = operatorValue.map((val) => remapFromGraphQLCore(val, column, columnName));
|
|
1065
1232
|
variants.push(arrayValueOps[operatorName](column, arrayValue));
|
|
@@ -1069,7 +1236,67 @@ var extractFiltersColumn = (column, columnName, operators) => {
|
|
|
1069
1236
|
}
|
|
1070
1237
|
return variants.length ? variants.length > 1 ? (0, import_drizzle_orm3.and)(...variants) : variants[0] : void 0;
|
|
1071
1238
|
};
|
|
1072
|
-
var
|
|
1239
|
+
var relationFilterCtx = (base, tableKey) => base ? { ...base, tableKey } : void 0;
|
|
1240
|
+
var buildRelationJoinCondition = (parentTable, relation, aliasedTarget, relationName) => {
|
|
1241
|
+
const sourceColumns = relation.sourceColumns;
|
|
1242
|
+
const targetColumns = relation.targetColumns;
|
|
1243
|
+
if (!sourceColumns?.length || sourceColumns.length !== targetColumns?.length) {
|
|
1244
|
+
throw new import_graphql4.GraphQLError(`WHERE ${relationName}: Relation cannot be used as a filter`);
|
|
1245
|
+
}
|
|
1246
|
+
const parentColumns = Object.values((0, import_drizzle_orm3.getColumns)(parentTable));
|
|
1247
|
+
const targetColumnsByName = Object.values((0, import_drizzle_orm3.getColumns)(aliasedTarget));
|
|
1248
|
+
const conditions = [];
|
|
1249
|
+
for (let i = 0; i < sourceColumns.length; i++) {
|
|
1250
|
+
const localColumn = parentColumns.find((c) => c.name === sourceColumns[i].name);
|
|
1251
|
+
const foreignColumn = targetColumnsByName.find((c) => c.name === targetColumns[i].name);
|
|
1252
|
+
if (!localColumn || !foreignColumn) {
|
|
1253
|
+
throw new import_graphql4.GraphQLError(`WHERE ${relationName}: Relation cannot be used as a filter`);
|
|
1254
|
+
}
|
|
1255
|
+
conditions.push((0, import_drizzle_orm3.eq)(localColumn, foreignColumn));
|
|
1256
|
+
}
|
|
1257
|
+
return conditions.length > 1 ? (0, import_drizzle_orm3.and)(...conditions) : conditions[0];
|
|
1258
|
+
};
|
|
1259
|
+
var buildRelationExists = (parentTable, relationName, relEntry, innerFilters, mode, ctx) => {
|
|
1260
|
+
const { targetTableName } = relEntry;
|
|
1261
|
+
const targetTable = ctx.tables[targetTableName];
|
|
1262
|
+
const relation = relEntry.relation ?? relEntry;
|
|
1263
|
+
if (!targetTable || !isFilterableRelation(relation)) {
|
|
1264
|
+
throw new import_graphql4.GraphQLError(`WHERE ${relationName}: Relation cannot be used as a filter`);
|
|
1265
|
+
}
|
|
1266
|
+
ctx.aliases ??= { n: 0 };
|
|
1267
|
+
const aliases = ctx.aliases;
|
|
1268
|
+
const aliasedTarget = (0, import_drizzle_orm3.aliasedTable)(targetTable, `dgql_rel_${aliases.n++}`);
|
|
1269
|
+
const joinCondition = buildRelationJoinCondition(parentTable, relation, aliasedTarget, relationName);
|
|
1270
|
+
const relationWhere = relation.where ? (0, import_drizzle_orm3.relationsFilterToSQL)(relation.isReversed ? parentTable : aliasedTarget, relation.where) : void 0;
|
|
1271
|
+
const inner = innerFilters ? extractFilters(aliasedTarget, targetTableName, innerFilters, { ...ctx, tableKey: targetTableName, aliases }) : void 0;
|
|
1272
|
+
if (mode === "every") {
|
|
1273
|
+
if (!inner) {
|
|
1274
|
+
return void 0;
|
|
1275
|
+
}
|
|
1276
|
+
return import_drizzle_orm3.sql`not exists (select 1 from ${(0, import_drizzle_orm3.getTableAsAliasSQL)(aliasedTarget)} where ${(0, import_drizzle_orm3.and)(joinCondition, relationWhere, (0, import_drizzle_orm3.not)(inner))})`;
|
|
1277
|
+
}
|
|
1278
|
+
const condition = (0, import_drizzle_orm3.and)(joinCondition, relationWhere, inner);
|
|
1279
|
+
return mode === "none" ? import_drizzle_orm3.sql`not exists (select 1 from ${(0, import_drizzle_orm3.getTableAsAliasSQL)(aliasedTarget)} where ${condition})` : import_drizzle_orm3.sql`exists (select 1 from ${(0, import_drizzle_orm3.getTableAsAliasSQL)(aliasedTarget)} where ${condition})`;
|
|
1280
|
+
};
|
|
1281
|
+
var extractRelationFilter = (parentTable, relationName, relEntry, value, ctx) => {
|
|
1282
|
+
const relation = relEntry.relation ?? relEntry;
|
|
1283
|
+
if ((0, import_drizzle_orm3.is)(relation, import_drizzle_orm3.One)) {
|
|
1284
|
+
return buildRelationExists(parentTable, relationName, relEntry, value, "some", ctx);
|
|
1285
|
+
}
|
|
1286
|
+
const variants = [];
|
|
1287
|
+
for (const mode of ["some", "none", "every"]) {
|
|
1288
|
+
const inner = value[mode];
|
|
1289
|
+
if (inner === void 0 || inner === null) {
|
|
1290
|
+
continue;
|
|
1291
|
+
}
|
|
1292
|
+
const extracted = buildRelationExists(parentTable, relationName, relEntry, inner, mode, ctx);
|
|
1293
|
+
if (extracted) {
|
|
1294
|
+
variants.push(extracted);
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
return variants.length ? variants.length > 1 ? (0, import_drizzle_orm3.and)(...variants) : variants[0] : void 0;
|
|
1298
|
+
};
|
|
1299
|
+
var extractFilters = (table, tableName, filters, relationCtx) => {
|
|
1073
1300
|
if (!filters.OR?.length) {
|
|
1074
1301
|
delete filters.OR;
|
|
1075
1302
|
}
|
|
@@ -1079,28 +1306,33 @@ var extractFilters = (table, tableName, filters) => {
|
|
|
1079
1306
|
}
|
|
1080
1307
|
if (filters.OR) {
|
|
1081
1308
|
if (entries.length > 1) {
|
|
1082
|
-
throw new
|
|
1309
|
+
throw new import_graphql4.GraphQLError(`WHERE ${tableName}: Cannot specify both fields and 'OR' in table filters!`);
|
|
1083
1310
|
}
|
|
1084
1311
|
const variants2 = [];
|
|
1085
1312
|
for (const variant of filters.OR) {
|
|
1086
|
-
const extracted = extractFilters(table, tableName, variant);
|
|
1313
|
+
const extracted = extractFilters(table, tableName, variant, relationCtx);
|
|
1087
1314
|
if (extracted) {
|
|
1088
1315
|
variants2.push(extracted);
|
|
1089
1316
|
}
|
|
1090
1317
|
}
|
|
1091
1318
|
return variants2.length ? variants2.length > 1 ? (0, import_drizzle_orm3.or)(...variants2) : variants2[0] : void 0;
|
|
1092
1319
|
}
|
|
1320
|
+
const columns = (0, import_drizzle_orm3.getColumns)(table);
|
|
1321
|
+
const relations = relationCtx?.relationMap[relationCtx.tableKey];
|
|
1093
1322
|
const variants = [];
|
|
1094
|
-
for (const [
|
|
1095
|
-
if (operators === null) {
|
|
1323
|
+
for (const [fieldName, operators] of entries) {
|
|
1324
|
+
if (operators === null || operators === void 0) {
|
|
1096
1325
|
continue;
|
|
1097
1326
|
}
|
|
1098
|
-
const column =
|
|
1099
|
-
|
|
1327
|
+
const column = columns[fieldName];
|
|
1328
|
+
const extracted = column ? extractFiltersColumn(column, fieldName, operators) : relations?.[fieldName] && relationCtx ? extractRelationFilter(table, fieldName, relations[fieldName], operators, relationCtx) : void 0;
|
|
1329
|
+
if (extracted) {
|
|
1330
|
+
variants.push(extracted);
|
|
1331
|
+
}
|
|
1100
1332
|
}
|
|
1101
1333
|
return variants.length ? variants.length > 1 ? (0, import_drizzle_orm3.and)(...variants) : variants[0] : void 0;
|
|
1102
1334
|
};
|
|
1103
|
-
var extractRelationsParamsInner = (relationMap, tables, tableName, typeName, originField, typeNameMapper, _isInitial = false) => {
|
|
1335
|
+
var extractRelationsParamsInner = (relationMap, tables, tableName, typeName, originField, typeNameMapper, _isInitial = false, filterCtx) => {
|
|
1104
1336
|
const relationsForTable = relationMap[tableName];
|
|
1105
1337
|
if (!relationsForTable) {
|
|
1106
1338
|
return void 0;
|
|
@@ -1122,7 +1354,11 @@ var extractRelationsParamsInner = (relationMap, tables, tableName, typeName, ori
|
|
|
1122
1354
|
if (!relFieldSelection) {
|
|
1123
1355
|
continue;
|
|
1124
1356
|
}
|
|
1125
|
-
const columns = extractSelectedColumnsFromTree(relFieldSelection, tables[targetTableName]
|
|
1357
|
+
const columns = extractSelectedColumnsFromTree(relFieldSelection, tables[targetTableName], {
|
|
1358
|
+
tableName: targetTableName,
|
|
1359
|
+
relationMap,
|
|
1360
|
+
tables
|
|
1361
|
+
});
|
|
1126
1362
|
const thisRecord = {};
|
|
1127
1363
|
thisRecord.columns = columns;
|
|
1128
1364
|
const relationField = Object.values(baseField).find((e) => e.name === relName);
|
|
@@ -1130,23 +1366,34 @@ var extractRelationsParamsInner = (relationMap, tables, tableName, typeName, ori
|
|
|
1130
1366
|
const offset = relationArgs?.offset ?? void 0;
|
|
1131
1367
|
const limit = relationArgs?.limit ?? void 0;
|
|
1132
1368
|
const relWhere = relationArgs?.where;
|
|
1133
|
-
thisRecord.where = relWhere ? {
|
|
1369
|
+
thisRecord.where = relWhere ? {
|
|
1370
|
+
RAW: (aliasedTable2) => extractFilters(aliasedTable2, relName, relWhere, relationFilterCtx(filterCtx, targetTableName))
|
|
1371
|
+
} : void 0;
|
|
1134
1372
|
const hasPagination = offset != null || limit != null;
|
|
1135
1373
|
const pkNames = targetPkNames ?? [];
|
|
1136
|
-
thisRecord.orderBy = relationArgs?.orderBy ? (
|
|
1374
|
+
thisRecord.orderBy = relationArgs?.orderBy ? (aliasedTable2) => extractOrderBy(aliasedTable2, relationArgs.orderBy) : hasPagination && pkNames.length ? (aliasedTable2) => primaryKeyOrderExprs(aliasedTable2, pkNames) : void 0;
|
|
1137
1375
|
thisRecord.offset = offset;
|
|
1138
1376
|
thisRecord.limit = limit;
|
|
1139
|
-
const relWith = relationField ? extractRelationsParamsInner(
|
|
1377
|
+
const relWith = relationField ? extractRelationsParamsInner(
|
|
1378
|
+
relationMap,
|
|
1379
|
+
tables,
|
|
1380
|
+
targetTableName,
|
|
1381
|
+
relTypeName,
|
|
1382
|
+
relationField,
|
|
1383
|
+
typeNameMapper,
|
|
1384
|
+
false,
|
|
1385
|
+
filterCtx
|
|
1386
|
+
) : void 0;
|
|
1140
1387
|
thisRecord.with = relWith;
|
|
1141
1388
|
args[relName] = thisRecord;
|
|
1142
1389
|
}
|
|
1143
1390
|
return args;
|
|
1144
1391
|
};
|
|
1145
|
-
var extractRelationsParams = (relationMap, tables, tableName, info, typeName, typeNameMapper) => {
|
|
1392
|
+
var extractRelationsParams = (relationMap, tables, tableName, info, typeName, typeNameMapper, filterCtx) => {
|
|
1146
1393
|
if (!info) {
|
|
1147
1394
|
return void 0;
|
|
1148
1395
|
}
|
|
1149
|
-
return extractRelationsParamsInner(relationMap, tables, tableName, typeName, info, typeNameMapper, true);
|
|
1396
|
+
return extractRelationsParamsInner(relationMap, tables, tableName, typeName, info, typeNameMapper, true, filterCtx);
|
|
1150
1397
|
};
|
|
1151
1398
|
var pruneNonEagerRelations = (relationMap, shouldEagerLoad) => {
|
|
1152
1399
|
const out = {};
|
|
@@ -1190,42 +1437,310 @@ var primaryKeyOrderExprs = (table, pkNames) => {
|
|
|
1190
1437
|
const cols = (0, import_drizzle_orm3.getColumns)(table);
|
|
1191
1438
|
return pkNames.map((n) => cols[n]).filter(Boolean).map((col) => (0, import_drizzle_orm3.asc)(col));
|
|
1192
1439
|
};
|
|
1440
|
+
var getUniqueColumnSets = (table, getTableConfig4) => {
|
|
1441
|
+
const cols = (0, import_drizzle_orm3.getColumns)(table);
|
|
1442
|
+
const propNameByColumnName = new Map(Object.entries(cols).map(([propName, col]) => [col.name, propName]));
|
|
1443
|
+
const toPropNames = (columnNames) => {
|
|
1444
|
+
const propNames = [];
|
|
1445
|
+
for (const columnName of columnNames) {
|
|
1446
|
+
const propName = columnName === void 0 ? void 0 : propNameByColumnName.get(columnName);
|
|
1447
|
+
if (!propName) {
|
|
1448
|
+
return void 0;
|
|
1449
|
+
}
|
|
1450
|
+
propNames.push(propName);
|
|
1451
|
+
}
|
|
1452
|
+
return propNames.length ? propNames : void 0;
|
|
1453
|
+
};
|
|
1454
|
+
const config = getTableConfig4(table);
|
|
1455
|
+
const candidates = [
|
|
1456
|
+
// Inline `.primaryKey()` columns, then table-level `primaryKey({ columns })`.
|
|
1457
|
+
Object.entries(cols).filter(([, col]) => col.primary).map(([propName]) => propName),
|
|
1458
|
+
...config.primaryKeys.map((pk) => toPropNames(pk.columns.map((c) => c.name))),
|
|
1459
|
+
...(config.uniqueConstraints ?? []).map((uc) => toPropNames(uc.columns.map((c) => c.name))),
|
|
1460
|
+
...(config.indexes ?? []).filter((index) => index.config.unique).map((index) => toPropNames(index.config.columns.map((c) => c?.name))),
|
|
1461
|
+
...Object.entries(cols).filter(([, col]) => col.isUnique).map(([propName]) => [propName])
|
|
1462
|
+
];
|
|
1463
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1464
|
+
const sets = [];
|
|
1465
|
+
for (const set of candidates) {
|
|
1466
|
+
if (!set?.length) {
|
|
1467
|
+
continue;
|
|
1468
|
+
}
|
|
1469
|
+
const key = [...set].sort().join(",");
|
|
1470
|
+
if (seen.has(key)) {
|
|
1471
|
+
continue;
|
|
1472
|
+
}
|
|
1473
|
+
seen.add(key);
|
|
1474
|
+
sets.push(set);
|
|
1475
|
+
}
|
|
1476
|
+
return sets;
|
|
1477
|
+
};
|
|
1478
|
+
var DISTINCT_RN = "__drizzle_graphql_distinct_rn";
|
|
1479
|
+
var columnEnumCache = /* @__PURE__ */ new WeakMap();
|
|
1480
|
+
var generateColumnEnum = (table, enumName, description, predicate = () => true) => {
|
|
1481
|
+
let tableCache = columnEnumCache.get(table);
|
|
1482
|
+
const cached = tableCache?.get(enumName);
|
|
1483
|
+
if (cached) {
|
|
1484
|
+
return cached;
|
|
1485
|
+
}
|
|
1486
|
+
const columnNames = Object.entries((0, import_drizzle_orm3.getColumns)(table)).filter(([columnName, column]) => predicate(column, columnName)).map(([columnName]) => columnName);
|
|
1487
|
+
if (!columnNames.length) {
|
|
1488
|
+
return void 0;
|
|
1489
|
+
}
|
|
1490
|
+
const enumType = new import_graphql4.GraphQLEnumType({
|
|
1491
|
+
name: enumName,
|
|
1492
|
+
description,
|
|
1493
|
+
values: Object.fromEntries(columnNames.map((columnName) => [columnName, { value: columnName }]))
|
|
1494
|
+
});
|
|
1495
|
+
if (!tableCache) {
|
|
1496
|
+
tableCache = /* @__PURE__ */ new Map();
|
|
1497
|
+
columnEnumCache.set(table, tableCache);
|
|
1498
|
+
}
|
|
1499
|
+
tableCache.set(enumName, enumType);
|
|
1500
|
+
return enumType;
|
|
1501
|
+
};
|
|
1502
|
+
var generateDistinctEnum = (table, typeName) => generateColumnEnum(table, `${typeName}DistinctColumn`, `Columns of ${typeName} that a query can be made distinct on`);
|
|
1503
|
+
var conflictActionEnum = new import_graphql4.GraphQLEnumType({
|
|
1504
|
+
name: "ConflictAction",
|
|
1505
|
+
description: "What an upsert does when a row with the same unique key already exists",
|
|
1506
|
+
values: {
|
|
1507
|
+
UPDATE: { value: "UPDATE", description: "Overwrite the conflicting row with the supplied values" },
|
|
1508
|
+
NOTHING: { value: "NOTHING", description: "Keep the existing row and insert nothing" }
|
|
1509
|
+
}
|
|
1510
|
+
});
|
|
1511
|
+
var generateOnConflictInput = (params) => {
|
|
1512
|
+
const { table, typeName, uniqueSets, tableFilters, withTarget } = params;
|
|
1513
|
+
const updateEnum = generateColumnEnum(
|
|
1514
|
+
table,
|
|
1515
|
+
`${typeName}UpdateColumn`,
|
|
1516
|
+
`Columns of ${typeName} that an upsert can overwrite`
|
|
1517
|
+
);
|
|
1518
|
+
if (!updateEnum) {
|
|
1519
|
+
return void 0;
|
|
1520
|
+
}
|
|
1521
|
+
const fields = {
|
|
1522
|
+
action: {
|
|
1523
|
+
type: conflictActionEnum,
|
|
1524
|
+
defaultValue: "UPDATE",
|
|
1525
|
+
description: "Whether a conflicting row is overwritten or left alone. Defaults to UPDATE."
|
|
1526
|
+
},
|
|
1527
|
+
update: {
|
|
1528
|
+
type: new import_graphql4.GraphQLList(new import_graphql4.GraphQLNonNull(updateEnum)),
|
|
1529
|
+
description: "Columns to overwrite on conflict. Defaults to every column the request supplied, minus the conflict target. Columns the request did not supply cannot be listed here \u2014 there would be no value to write."
|
|
1530
|
+
}
|
|
1531
|
+
};
|
|
1532
|
+
if (withTarget) {
|
|
1533
|
+
const uniqueColumns = new Set(uniqueSets.flat());
|
|
1534
|
+
const targetEnum = generateColumnEnum(
|
|
1535
|
+
table,
|
|
1536
|
+
`${typeName}ConflictTarget`,
|
|
1537
|
+
`Columns of ${typeName} that carry a unique constraint, and so can be conflicted on`,
|
|
1538
|
+
(_column, columnName) => uniqueColumns.has(columnName)
|
|
1539
|
+
);
|
|
1540
|
+
if (!targetEnum) {
|
|
1541
|
+
return void 0;
|
|
1542
|
+
}
|
|
1543
|
+
fields["target"] = {
|
|
1544
|
+
type: new import_graphql4.GraphQLList(new import_graphql4.GraphQLNonNull(targetEnum)),
|
|
1545
|
+
description: "The unique column set a conflict is detected on. Must match one of the table\u2019s unique constraints exactly. Defaults to the primary key."
|
|
1546
|
+
};
|
|
1547
|
+
fields["where"] = {
|
|
1548
|
+
type: tableFilters,
|
|
1549
|
+
description: "Only overwrite conflicting rows that match this filter. Others are left alone."
|
|
1550
|
+
};
|
|
1551
|
+
}
|
|
1552
|
+
return new import_graphql4.GraphQLInputObjectType({
|
|
1553
|
+
name: `${typeName}OnConflict`,
|
|
1554
|
+
description: `Conflict handling for an upsert of ${typeName}`,
|
|
1555
|
+
fields
|
|
1556
|
+
});
|
|
1557
|
+
};
|
|
1558
|
+
var resolveConflictPlan = (params) => {
|
|
1559
|
+
const { table, values, onConflict, pkNames, uniqueSets, excludedRef, withTarget, buildWhere } = params;
|
|
1560
|
+
const columns = (0, import_drizzle_orm3.getColumns)(table);
|
|
1561
|
+
let target;
|
|
1562
|
+
if (withTarget) {
|
|
1563
|
+
const targetNames2 = onConflict?.target?.length ? onConflict.target : [...pkNames];
|
|
1564
|
+
if (!targetNames2.length) {
|
|
1565
|
+
throw new import_graphql4.GraphQLError(
|
|
1566
|
+
"Unable to upsert: no conflict target was given and this table has no primary key. Pass onConflict.target."
|
|
1567
|
+
);
|
|
1568
|
+
}
|
|
1569
|
+
const requested = [...targetNames2].sort().join(",");
|
|
1570
|
+
if (!uniqueSets.some((set2) => [...set2].sort().join(",") === requested)) {
|
|
1571
|
+
throw new import_graphql4.GraphQLError(
|
|
1572
|
+
`Unable to upsert: [${targetNames2.join(", ")}] is not a unique constraint on this table. Valid conflict targets: ${uniqueSets.map((set2) => `[${set2.join(", ")}]`).join(", ")}.`
|
|
1573
|
+
);
|
|
1574
|
+
}
|
|
1575
|
+
target = targetNames2.map((name) => columns[name]);
|
|
1576
|
+
}
|
|
1577
|
+
if ((onConflict?.action ?? "UPDATE") === "NOTHING") {
|
|
1578
|
+
return { action: "NOTHING", target, set: {}, setWhere: void 0 };
|
|
1579
|
+
}
|
|
1580
|
+
const supplied = new Set(values.flatMap((row) => Object.keys(row)));
|
|
1581
|
+
const targetNames = new Set(withTarget ? onConflict?.target?.length ? onConflict.target : pkNames : []);
|
|
1582
|
+
let updateNames;
|
|
1583
|
+
if (onConflict?.update?.length) {
|
|
1584
|
+
const unsupplied = onConflict.update.filter((name) => !supplied.has(name));
|
|
1585
|
+
if (unsupplied.length) {
|
|
1586
|
+
throw new import_graphql4.GraphQLError(
|
|
1587
|
+
`Unable to upsert: onConflict.update lists ${unsupplied.join(", ")}, which the values do not supply.`
|
|
1588
|
+
);
|
|
1589
|
+
}
|
|
1590
|
+
updateNames = onConflict.update;
|
|
1591
|
+
} else {
|
|
1592
|
+
updateNames = [...supplied].filter((name) => !targetNames.has(name));
|
|
1593
|
+
}
|
|
1594
|
+
if (!updateNames.length) {
|
|
1595
|
+
return { action: "NOTHING", target, set: {}, setWhere: void 0 };
|
|
1596
|
+
}
|
|
1597
|
+
const set = Object.fromEntries(updateNames.map((name) => [name, excludedRef(columns[name].name)]));
|
|
1598
|
+
const setWhere = onConflict?.where && buildWhere ? buildWhere(onConflict.where) : void 0;
|
|
1599
|
+
return { action: "UPDATE", target, set, setWhere };
|
|
1600
|
+
};
|
|
1601
|
+
var excludedColumnRef = (columnName) => import_drizzle_orm3.sql`excluded.${import_drizzle_orm3.sql.identifier(columnName)}`;
|
|
1602
|
+
var mysqlValuesColumnRef = (columnName) => import_drizzle_orm3.sql`values(${import_drizzle_orm3.sql.identifier(columnName)})`;
|
|
1603
|
+
var selectDistinctKeys = async (params) => {
|
|
1604
|
+
const { db, table, tableName, distinct, pkNames, where, orderBy, limit, offset } = params;
|
|
1605
|
+
const cols = (0, import_drizzle_orm3.getColumns)(table);
|
|
1606
|
+
if (!pkNames.length) {
|
|
1607
|
+
throw new import_graphql4.GraphQLError(`Table ${tableName} has no primary key, so 'distinct' cannot be applied to it.`);
|
|
1608
|
+
}
|
|
1609
|
+
const partitionCols = distinct.map((name) => cols[name]).filter(Boolean);
|
|
1610
|
+
if (!partitionCols.length) {
|
|
1611
|
+
throw new import_graphql4.GraphQLError(`No known columns were given to 'distinct' on ${tableName}.`);
|
|
1612
|
+
}
|
|
1613
|
+
const orderEntries = orderBy ? orderByEntries(orderBy) : [];
|
|
1614
|
+
const windowOrder = [
|
|
1615
|
+
...orderEntries.map(([column, direction]) => direction === "asc" ? (0, import_drizzle_orm3.asc)(cols[column]) : (0, import_drizzle_orm3.desc)(cols[column])),
|
|
1616
|
+
...primaryKeyOrderExprs(table, pkNames)
|
|
1617
|
+
];
|
|
1618
|
+
const rowNumber = import_drizzle_orm3.sql`row_number() over (partition by ${import_drizzle_orm3.sql.join(partitionCols, import_drizzle_orm3.sql`, `)} order by ${import_drizzle_orm3.sql.join(
|
|
1619
|
+
windowOrder,
|
|
1620
|
+
import_drizzle_orm3.sql`, `
|
|
1621
|
+
)})`.as(DISTINCT_RN);
|
|
1622
|
+
const sub = db.select({ ...cols, [DISTINCT_RN]: rowNumber }).from(table).where(where).as("__dgql_distinct");
|
|
1623
|
+
const outerOrder = [
|
|
1624
|
+
...orderEntries.map(([column, direction]) => direction === "asc" ? (0, import_drizzle_orm3.asc)(sub[column]) : (0, import_drizzle_orm3.desc)(sub[column])),
|
|
1625
|
+
...pkNames.filter((name) => sub[name]).map((name) => (0, import_drizzle_orm3.asc)(sub[name]))
|
|
1626
|
+
];
|
|
1627
|
+
let query = db.select(Object.fromEntries(pkNames.map((name) => [name, sub[name]]))).from(sub).where((0, import_drizzle_orm3.eq)(sub[DISTINCT_RN], 1)).orderBy(...outerOrder);
|
|
1628
|
+
if (offset) {
|
|
1629
|
+
query = query.offset(offset);
|
|
1630
|
+
}
|
|
1631
|
+
if (limit != null) {
|
|
1632
|
+
query = query.limit(limit);
|
|
1633
|
+
}
|
|
1634
|
+
return await query;
|
|
1635
|
+
};
|
|
1636
|
+
var primaryKeyRestriction = (table, pkNames, keys) => {
|
|
1637
|
+
const cols = (0, import_drizzle_orm3.getColumns)(table);
|
|
1638
|
+
if (pkNames.length === 1) {
|
|
1639
|
+
const name = pkNames[0];
|
|
1640
|
+
return (0, import_drizzle_orm3.inArray)(
|
|
1641
|
+
cols[name],
|
|
1642
|
+
keys.map((key) => key[name])
|
|
1643
|
+
);
|
|
1644
|
+
}
|
|
1645
|
+
return (0, import_drizzle_orm3.or)(...keys.map((key) => (0, import_drizzle_orm3.and)(...pkNames.map((name) => (0, import_drizzle_orm3.eq)(cols[name], key[name])))));
|
|
1646
|
+
};
|
|
1193
1647
|
var prepareMutationRelationColumns = (params) => {
|
|
1194
1648
|
const { relationMap, tables, tableName, typeName, typeNameMapper, table, pkNames, parsedInfo } = params;
|
|
1195
1649
|
const withParams = relationMap[tableName] ? extractRelationsParams(relationMap, tables, tableName, parsedInfo, typeName, typeNameMapper) : void 0;
|
|
1196
1650
|
const hasRelations = !!(withParams && Object.keys(withParams).length);
|
|
1197
|
-
const baseColumns = extractSelectedColumnsFromTreeSQLFormat(parsedInfo.fieldsByTypeName[typeName], table
|
|
1651
|
+
const baseColumns = extractSelectedColumnsFromTreeSQLFormat(parsedInfo.fieldsByTypeName[typeName], table, {
|
|
1652
|
+
tableName,
|
|
1653
|
+
relationMap,
|
|
1654
|
+
tables
|
|
1655
|
+
});
|
|
1198
1656
|
const columns = hasRelations ? withPrimaryKeyColumns(baseColumns, table, pkNames) : baseColumns;
|
|
1199
1657
|
return { columns, hasRelations, withParams };
|
|
1200
1658
|
};
|
|
1201
|
-
var toGraphQLError = (e) =>
|
|
1659
|
+
var toGraphQLError = (e) => {
|
|
1660
|
+
if (e instanceof import_graphql4.GraphQLError) {
|
|
1661
|
+
return e;
|
|
1662
|
+
}
|
|
1663
|
+
return e instanceof Error ? new import_graphql4.GraphQLError(e.message, { originalError: e }) : e;
|
|
1664
|
+
};
|
|
1665
|
+
var defaultErrorMapper = (error) => {
|
|
1666
|
+
if (error instanceof import_graphql4.GraphQLError && !error.originalError) {
|
|
1667
|
+
return error;
|
|
1668
|
+
}
|
|
1669
|
+
return new import_graphql4.GraphQLError("Internal server error", {
|
|
1670
|
+
originalError: error instanceof import_graphql4.GraphQLError ? error.originalError ?? error : error instanceof Error ? error : null,
|
|
1671
|
+
extensions: { code: "INTERNAL_SERVER_ERROR" }
|
|
1672
|
+
});
|
|
1673
|
+
};
|
|
1674
|
+
var applyErrorMapper = (entities, mapError) => {
|
|
1675
|
+
const wrap = (resolve) => (...args) => {
|
|
1676
|
+
try {
|
|
1677
|
+
const result = resolve(...args);
|
|
1678
|
+
if (result && typeof result.then === "function") {
|
|
1679
|
+
return result.then(void 0, (e) => {
|
|
1680
|
+
throw mapError(e);
|
|
1681
|
+
});
|
|
1682
|
+
}
|
|
1683
|
+
return result;
|
|
1684
|
+
} catch (e) {
|
|
1685
|
+
throw mapError(e);
|
|
1686
|
+
}
|
|
1687
|
+
};
|
|
1688
|
+
for (const field of [...Object.values(entities.queries), ...Object.values(entities.mutations)]) {
|
|
1689
|
+
if (field?.resolve) {
|
|
1690
|
+
field.resolve = wrap(field.resolve);
|
|
1691
|
+
}
|
|
1692
|
+
}
|
|
1693
|
+
for (const type of Object.values(entities.types)) {
|
|
1694
|
+
if (typeof type?.getFields !== "function") {
|
|
1695
|
+
continue;
|
|
1696
|
+
}
|
|
1697
|
+
for (const field of Object.values(type.getFields())) {
|
|
1698
|
+
if (field?.resolve) {
|
|
1699
|
+
field.resolve = wrap(field.resolve);
|
|
1700
|
+
}
|
|
1701
|
+
}
|
|
1702
|
+
}
|
|
1703
|
+
for (const tableResolvers of Object.values(entities.fieldResolvers ?? {})) {
|
|
1704
|
+
for (const [relationName, resolve] of Object.entries(tableResolvers)) {
|
|
1705
|
+
tableResolvers[relationName] = wrap(resolve);
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
};
|
|
1202
1709
|
var computeResolverFieldNames = (tableName, typeNameMapper, prefixes, suffixes) => {
|
|
1203
1710
|
const mapped = typeNameMapper?.(tableName);
|
|
1204
1711
|
const typeName = mapped ? capitalize(mapped.singular) : capitalize(tableName);
|
|
1205
1712
|
const listFieldName = (mapped?.plural ?? uncapitalize(tableName)) + suffixes.list;
|
|
1206
1713
|
const singleFieldName = mapped?.singular ?? uncapitalize(tableName) + suffixes.single;
|
|
1714
|
+
const aggregateFieldName = `${mapped?.plural ?? uncapitalize(tableName)}Aggregate`;
|
|
1207
1715
|
const createArrayFieldName = `${prefixes.insert}${mapped ? capitalize(mapped.plural) : capitalize(tableName)}`;
|
|
1208
1716
|
const createSingleFieldName = mapped ? `${prefixes.insert}${capitalize(mapped.singular)}` : `${prefixes.insert}${capitalize(tableName)}${suffixes.single}`;
|
|
1717
|
+
const upsertPrefix = prefixes.upsert ?? "upsert";
|
|
1718
|
+
const upsertArrayFieldName = `${upsertPrefix}${mapped ? capitalize(mapped.plural) : capitalize(tableName)}`;
|
|
1719
|
+
const upsertSingleFieldName = mapped ? `${upsertPrefix}${capitalize(mapped.singular)}` : `${upsertPrefix}${capitalize(tableName)}${suffixes.single}`;
|
|
1209
1720
|
const updateFieldName = `${prefixes.update}${mapped ? capitalize(mapped.singular) : capitalize(tableName)}`;
|
|
1210
1721
|
const deleteFieldName = `${prefixes.delete}${mapped ? capitalize(mapped.singular) : capitalize(tableName)}`;
|
|
1211
1722
|
return {
|
|
1212
1723
|
typeName,
|
|
1213
1724
|
listFieldName,
|
|
1214
1725
|
singleFieldName,
|
|
1726
|
+
aggregateFieldName,
|
|
1215
1727
|
createArrayFieldName,
|
|
1216
1728
|
createSingleFieldName,
|
|
1729
|
+
upsertArrayFieldName,
|
|
1730
|
+
upsertSingleFieldName,
|
|
1217
1731
|
updateFieldName,
|
|
1218
1732
|
deleteFieldName
|
|
1219
1733
|
};
|
|
1220
1734
|
};
|
|
1221
|
-
var selectArrayArgs = (orderArgs, filterArgs) => ({
|
|
1222
|
-
offset: { type:
|
|
1223
|
-
limit: { type:
|
|
1735
|
+
var selectArrayArgs = (orderArgs, filterArgs, distinctEnum) => ({
|
|
1736
|
+
offset: { type: import_graphql4.GraphQLInt },
|
|
1737
|
+
limit: { type: import_graphql4.GraphQLInt },
|
|
1224
1738
|
orderBy: { type: orderArgs },
|
|
1225
|
-
where: { type: filterArgs }
|
|
1739
|
+
where: { type: filterArgs },
|
|
1740
|
+
...distinctEnum ? { distinct: { type: new import_graphql4.GraphQLList(new import_graphql4.GraphQLNonNull(distinctEnum)) } } : {}
|
|
1226
1741
|
});
|
|
1227
1742
|
var selectSingleArgs = (orderArgs, filterArgs) => ({
|
|
1228
|
-
offset: { type:
|
|
1743
|
+
offset: { type: import_graphql4.GraphQLInt },
|
|
1229
1744
|
orderBy: { type: orderArgs },
|
|
1230
1745
|
where: { type: filterArgs }
|
|
1231
1746
|
});
|
|
@@ -1242,22 +1757,52 @@ var runRelationalSelect = async (opts) => {
|
|
|
1242
1757
|
offset,
|
|
1243
1758
|
orderBy,
|
|
1244
1759
|
where,
|
|
1245
|
-
single
|
|
1760
|
+
single,
|
|
1761
|
+
filterCtx,
|
|
1762
|
+
pkNames
|
|
1246
1763
|
} = opts;
|
|
1764
|
+
const distinct = opts.distinct?.length ? opts.distinct : void 0;
|
|
1765
|
+
const needsDefaultOrder = single || offset != null || opts.limit != null;
|
|
1766
|
+
let distinctKeys;
|
|
1767
|
+
if (distinct) {
|
|
1768
|
+
distinctKeys = await selectDistinctKeys({
|
|
1769
|
+
db: opts.db,
|
|
1770
|
+
table,
|
|
1771
|
+
tableName,
|
|
1772
|
+
distinct,
|
|
1773
|
+
pkNames: pkNames ?? [],
|
|
1774
|
+
where: where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0,
|
|
1775
|
+
orderBy,
|
|
1776
|
+
limit: single ? 1 : opts.limit,
|
|
1777
|
+
offset
|
|
1778
|
+
});
|
|
1779
|
+
if (!distinctKeys.length) {
|
|
1780
|
+
return single ? void 0 : [];
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1247
1783
|
const params = {
|
|
1248
|
-
columns: extractSelectedColumnsFromTree(parsedInfo.fieldsByTypeName[typeName], table
|
|
1249
|
-
|
|
1784
|
+
columns: extractSelectedColumnsFromTree(parsedInfo.fieldsByTypeName[typeName], table, {
|
|
1785
|
+
tableName,
|
|
1786
|
+
relationMap,
|
|
1787
|
+
tables
|
|
1788
|
+
}),
|
|
1789
|
+
offset: distinctKeys ? void 0 : offset,
|
|
1250
1790
|
// drizzle-orm v1 RQB calls orderBy/where with the aliased table proxy — use it
|
|
1251
1791
|
// directly so column refs match the CTE alias.
|
|
1252
|
-
orderBy:
|
|
1253
|
-
|
|
1254
|
-
|
|
1792
|
+
orderBy: distinctKeys ? (aliasedTable2) => [
|
|
1793
|
+
...orderBy ? extractOrderBy(aliasedTable2, orderBy) : [],
|
|
1794
|
+
...primaryKeyOrderExprs(aliasedTable2, pkNames)
|
|
1795
|
+
] : orderBy ? (aliasedTable2) => extractOrderBy(aliasedTable2, orderBy) : needsDefaultOrder && pkNames?.length ? (aliasedTable2) => primaryKeyOrderExprs(aliasedTable2, pkNames) : void 0,
|
|
1796
|
+
where: distinctKeys ? { RAW: (aliasedTable2) => primaryKeyRestriction(aliasedTable2, pkNames, distinctKeys) } : where ? {
|
|
1797
|
+
RAW: (aliasedTable2) => extractFilters(aliasedTable2, tableName, where, relationFilterCtx(filterCtx, tableName))
|
|
1798
|
+
} : void 0,
|
|
1799
|
+
with: relationMap[tableName] ? extractRelationsParams(relationMap, tables, tableName, parsedInfo, typeName, typeNameMapper, filterCtx) : void 0
|
|
1255
1800
|
};
|
|
1256
1801
|
if (single) {
|
|
1257
1802
|
const result2 = await queryBase.findFirst(params);
|
|
1258
1803
|
return result2 ? remapToGraphQLSingleOutput(result2, tableName, table, relationMap) : void 0;
|
|
1259
1804
|
}
|
|
1260
|
-
params.limit = opts.limit;
|
|
1805
|
+
params.limit = distinctKeys ? void 0 : opts.limit;
|
|
1261
1806
|
const result = await queryBase.findMany(params);
|
|
1262
1807
|
return remapToGraphQLArrayOutput(result, tableName, table, relationMap);
|
|
1263
1808
|
};
|
|
@@ -1330,55 +1875,331 @@ var eagerLoadMutationRelations = async (db, tableName, rows, pkNames, withParams
|
|
|
1330
1875
|
};
|
|
1331
1876
|
|
|
1332
1877
|
// src/util/builders/mysql.ts
|
|
1333
|
-
var
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1878
|
+
var import_drizzle_orm5 = require("drizzle-orm");
|
|
1879
|
+
var import_mysql_core2 = require("drizzle-orm/mysql-core");
|
|
1880
|
+
var import_graphql6 = require("graphql");
|
|
1881
|
+
var import_graphql_parse_resolve_info2 = require("graphql-parse-resolve-info");
|
|
1882
|
+
|
|
1883
|
+
// src/util/builders/aggregates.ts
|
|
1884
|
+
var import_drizzle_orm4 = require("drizzle-orm");
|
|
1885
|
+
var import_graphql5 = require("graphql");
|
|
1886
|
+
var import_graphql_parse_resolve_info = require("graphql-parse-resolve-info");
|
|
1887
|
+
var AGGREGATE_OPS = ["avg", "sum", "min", "max", "countNonNull", "countDistinct"];
|
|
1888
|
+
var COUNT_OPS = /* @__PURE__ */ new Set(["countNonNull", "countDistinct"]);
|
|
1889
|
+
var OP_FNS = {
|
|
1890
|
+
avg: import_drizzle_orm4.avg,
|
|
1891
|
+
sum: import_drizzle_orm4.sum,
|
|
1892
|
+
min: import_drizzle_orm4.min,
|
|
1893
|
+
max: import_drizzle_orm4.max,
|
|
1894
|
+
countNonNull: import_drizzle_orm4.count,
|
|
1895
|
+
countDistinct: import_drizzle_orm4.countDistinct
|
|
1896
|
+
};
|
|
1897
|
+
var SEP = "__";
|
|
1898
|
+
var classifyAggregateColumns = (table, tableName) => {
|
|
1899
|
+
const numeric = {};
|
|
1900
|
+
const orderable = {};
|
|
1901
|
+
const all = {};
|
|
1902
|
+
for (const [columnName, column] of Object.entries((0, import_drizzle_orm4.getColumns)(table))) {
|
|
1903
|
+
all[columnName] = column;
|
|
1904
|
+
const converted = drizzleColumnToGraphQLType(column, columnName, tableName, true, false, false);
|
|
1905
|
+
const gqlType = converted.type;
|
|
1906
|
+
if (gqlType instanceof import_graphql5.GraphQLList || gqlType instanceof import_graphql5.GraphQLObjectType) {
|
|
1907
|
+
continue;
|
|
1908
|
+
}
|
|
1909
|
+
const { type: dataType, constraint } = (0, import_drizzle_orm4.extractExtendedColumnType)(column);
|
|
1910
|
+
if (dataType === "boolean" || dataType === "array" || dataType === "custom") {
|
|
1911
|
+
continue;
|
|
1912
|
+
}
|
|
1913
|
+
if (dataType === "object" && constraint !== "date") {
|
|
1914
|
+
continue;
|
|
1915
|
+
}
|
|
1916
|
+
orderable[columnName] = { column, converted };
|
|
1917
|
+
if (dataType === "number") {
|
|
1918
|
+
numeric[columnName] = column;
|
|
1919
|
+
}
|
|
1339
1920
|
}
|
|
1340
|
-
|
|
1341
|
-
const table = tables[tableName];
|
|
1342
|
-
return {
|
|
1343
|
-
name: fieldName,
|
|
1344
|
-
resolver: async (_source, args, _context, info) => {
|
|
1345
|
-
try {
|
|
1346
|
-
const parsedInfo = (0, import_graphql_parse_resolve_info.parseResolveInfo)(info, { deep: true });
|
|
1347
|
-
return await runRelationalSelect({
|
|
1348
|
-
queryBase,
|
|
1349
|
-
tables,
|
|
1350
|
-
tableName,
|
|
1351
|
-
table,
|
|
1352
|
-
relationMap,
|
|
1353
|
-
typeName,
|
|
1354
|
-
typeNameMapper,
|
|
1355
|
-
parsedInfo,
|
|
1356
|
-
...args,
|
|
1357
|
-
single: false
|
|
1358
|
-
});
|
|
1359
|
-
} catch (e) {
|
|
1360
|
-
throw toGraphQLError(e);
|
|
1361
|
-
}
|
|
1362
|
-
},
|
|
1363
|
-
args: queryArgs
|
|
1364
|
-
};
|
|
1921
|
+
return { numeric, orderable, all };
|
|
1365
1922
|
};
|
|
1366
|
-
var
|
|
1367
|
-
const
|
|
1368
|
-
if (
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1923
|
+
var generateAggregateTypes = (table, tableName, typeName, cacheCtx) => {
|
|
1924
|
+
const cached = cacheCtx?.aggregateTypeCache.get(tableName);
|
|
1925
|
+
if (cached) {
|
|
1926
|
+
return cached;
|
|
1927
|
+
}
|
|
1928
|
+
const { numeric, orderable, all } = classifyAggregateColumns(table, tableName);
|
|
1929
|
+
const fields = {
|
|
1930
|
+
count: { type: new import_graphql5.GraphQLNonNull(import_graphql5.GraphQLInt) }
|
|
1931
|
+
};
|
|
1932
|
+
if (Object.keys(numeric).length) {
|
|
1933
|
+
for (const op of ["avg", "sum"]) {
|
|
1934
|
+
fields[op] = {
|
|
1935
|
+
type: new import_graphql5.GraphQLObjectType({
|
|
1936
|
+
name: `${typeName}${capitalize(op)}Aggregate`,
|
|
1937
|
+
fields: Object.fromEntries(Object.keys(numeric).map((columnName) => [columnName, { type: import_graphql5.GraphQLFloat }]))
|
|
1938
|
+
})
|
|
1939
|
+
};
|
|
1940
|
+
}
|
|
1941
|
+
}
|
|
1942
|
+
if (Object.keys(orderable).length) {
|
|
1943
|
+
for (const op of ["min", "max"]) {
|
|
1944
|
+
fields[op] = {
|
|
1945
|
+
type: new import_graphql5.GraphQLObjectType({
|
|
1946
|
+
name: `${typeName}${capitalize(op)}Aggregate`,
|
|
1947
|
+
fields: Object.fromEntries(
|
|
1948
|
+
Object.entries(orderable).map(([columnName, { converted }]) => [
|
|
1949
|
+
columnName,
|
|
1950
|
+
{ type: converted.type, description: converted.description }
|
|
1951
|
+
])
|
|
1952
|
+
)
|
|
1953
|
+
})
|
|
1954
|
+
};
|
|
1955
|
+
}
|
|
1956
|
+
}
|
|
1957
|
+
const countSets = { countNonNull: all, countDistinct: orderable };
|
|
1958
|
+
for (const [op, columns] of Object.entries(countSets)) {
|
|
1959
|
+
const columnNames = Object.keys(columns);
|
|
1960
|
+
if (!columnNames.length) {
|
|
1961
|
+
continue;
|
|
1962
|
+
}
|
|
1963
|
+
fields[op] = {
|
|
1964
|
+
type: new import_graphql5.GraphQLObjectType({
|
|
1965
|
+
name: `${typeName}${capitalize(op)}Aggregate`,
|
|
1966
|
+
fields: Object.fromEntries(
|
|
1967
|
+
columnNames.map((columnName) => [columnName, { type: new import_graphql5.GraphQLNonNull(import_graphql5.GraphQLInt) }])
|
|
1968
|
+
)
|
|
1969
|
+
})
|
|
1970
|
+
};
|
|
1971
|
+
}
|
|
1972
|
+
const aggregateType = new import_graphql5.GraphQLObjectType({
|
|
1973
|
+
name: `${typeName}Aggregate`,
|
|
1974
|
+
fields
|
|
1975
|
+
});
|
|
1976
|
+
cacheCtx?.aggregateTypeCache.set(tableName, aggregateType);
|
|
1977
|
+
return aggregateType;
|
|
1978
|
+
};
|
|
1979
|
+
var parseDriverDateTime = (raw) => {
|
|
1980
|
+
let v = raw.includes(" ") ? raw.replace(" ", "T") : raw;
|
|
1981
|
+
if (!v.includes("T")) {
|
|
1982
|
+
v = `${v}T00:00:00`;
|
|
1983
|
+
}
|
|
1984
|
+
if (!/(?:[Zz]|[+-]\d{2}(?::?\d{2})?)$/.test(v)) {
|
|
1985
|
+
v = `${v}Z`;
|
|
1986
|
+
}
|
|
1987
|
+
const parsed = new Date(v);
|
|
1988
|
+
return Number.isNaN(parsed.getTime()) ? new Date(raw) : parsed;
|
|
1989
|
+
};
|
|
1990
|
+
var aggregateTarget = (table, tableName, typeName) => {
|
|
1991
|
+
const { orderable } = classifyAggregateColumns(table, tableName);
|
|
1992
|
+
return {
|
|
1993
|
+
tableName,
|
|
1994
|
+
typeName,
|
|
1995
|
+
columns: (0, import_drizzle_orm4.getColumns)(table),
|
|
1996
|
+
dateTimeColumns: new Set(
|
|
1997
|
+
Object.entries(orderable).filter(([, { converted }]) => converted.description === "DateTime").map(([columnName]) => columnName)
|
|
1998
|
+
)
|
|
1999
|
+
};
|
|
2000
|
+
};
|
|
2001
|
+
var parseAggregateRequest = (info, target) => {
|
|
2002
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info.parseResolveInfo)(info, { deep: true });
|
|
2003
|
+
const selectionTree = parsedInfo.fieldsByTypeName[`${target.typeName}Aggregate`] ?? {};
|
|
2004
|
+
const request = {
|
|
2005
|
+
count: false,
|
|
2006
|
+
ops: { avg: [], sum: [], min: [], max: [], countNonNull: [], countDistinct: [] },
|
|
2007
|
+
selection: {}
|
|
2008
|
+
};
|
|
2009
|
+
for (const field of Object.values(selectionTree)) {
|
|
2010
|
+
if (field.name === "count") {
|
|
2011
|
+
request.count = true;
|
|
2012
|
+
request.selection.count = (0, import_drizzle_orm4.count)();
|
|
2013
|
+
continue;
|
|
2014
|
+
}
|
|
2015
|
+
if (!AGGREGATE_OPS.includes(field.name)) {
|
|
2016
|
+
continue;
|
|
2017
|
+
}
|
|
2018
|
+
const op = field.name;
|
|
2019
|
+
const subTree = field.fieldsByTypeName[`${target.typeName}${capitalize(op)}Aggregate`];
|
|
2020
|
+
if (!subTree) {
|
|
2021
|
+
continue;
|
|
2022
|
+
}
|
|
2023
|
+
for (const subField of Object.values(subTree)) {
|
|
2024
|
+
const columnName = subField.name;
|
|
2025
|
+
const column = target.columns[columnName];
|
|
2026
|
+
if (!column || request.ops[op].includes(columnName)) {
|
|
2027
|
+
continue;
|
|
2028
|
+
}
|
|
2029
|
+
request.ops[op].push(columnName);
|
|
2030
|
+
request.selection[`${op}${SEP}${columnName}`] = OP_FNS[op](column);
|
|
2031
|
+
}
|
|
2032
|
+
}
|
|
2033
|
+
return request;
|
|
2034
|
+
};
|
|
2035
|
+
var assembleAggregateRow = (row, request, target) => {
|
|
2036
|
+
const result = {};
|
|
2037
|
+
if (request.count) {
|
|
2038
|
+
result.count = row.count == null ? 0 : Number(row.count);
|
|
2039
|
+
}
|
|
2040
|
+
for (const op of AGGREGATE_OPS) {
|
|
2041
|
+
if (!request.ops[op].length) {
|
|
2042
|
+
continue;
|
|
2043
|
+
}
|
|
2044
|
+
const opResult = {};
|
|
2045
|
+
for (const columnName of request.ops[op]) {
|
|
2046
|
+
const value = row[`${op}${SEP}${columnName}`];
|
|
2047
|
+
if (COUNT_OPS.has(op)) {
|
|
2048
|
+
opResult[columnName] = value == null ? 0 : Number(value);
|
|
2049
|
+
} else if (value == null) {
|
|
2050
|
+
opResult[columnName] = null;
|
|
2051
|
+
} else if (op === "avg" || op === "sum") {
|
|
2052
|
+
opResult[columnName] = Number(value);
|
|
2053
|
+
} else {
|
|
2054
|
+
const column = target.columns[columnName];
|
|
2055
|
+
const decoded = typeof value === "string" && target.dateTimeColumns.has(columnName) ? parseDriverDateTime(value) : value;
|
|
2056
|
+
opResult[columnName] = remapToGraphQLCore(columnName, decoded, target.tableName, column);
|
|
2057
|
+
}
|
|
2058
|
+
}
|
|
2059
|
+
result[op] = opResult;
|
|
2060
|
+
}
|
|
2061
|
+
return result;
|
|
2062
|
+
};
|
|
2063
|
+
var generateAggregate = (db, tableName, table, typeName, fieldName, filterArgs, filterCtx) => {
|
|
2064
|
+
const target = aggregateTarget(table, tableName, typeName);
|
|
2065
|
+
const queryArgs = {
|
|
2066
|
+
where: { type: filterArgs }
|
|
2067
|
+
};
|
|
2068
|
+
return {
|
|
2069
|
+
name: fieldName,
|
|
2070
|
+
resolver: async (_source, args, context, info) => {
|
|
2071
|
+
try {
|
|
2072
|
+
const request = parseAggregateRequest(info, target);
|
|
2073
|
+
if (!Object.keys(request.selection).length) {
|
|
2074
|
+
return {};
|
|
2075
|
+
}
|
|
2076
|
+
let query = resolveExecutor(db, context).select(request.selection).from(table);
|
|
2077
|
+
if (args.where) {
|
|
2078
|
+
query = query.where(extractFilters(table, tableName, args.where, relationFilterCtx(filterCtx, tableName)));
|
|
2079
|
+
}
|
|
2080
|
+
const rows = await query;
|
|
2081
|
+
return assembleAggregateRow(rows[0] ?? {}, request, target);
|
|
2082
|
+
} catch (e) {
|
|
2083
|
+
throw toGraphQLError(e);
|
|
2084
|
+
}
|
|
2085
|
+
},
|
|
2086
|
+
args: queryArgs
|
|
2087
|
+
};
|
|
2088
|
+
};
|
|
2089
|
+
var GROUP_KEY = "__dgql_group_key";
|
|
2090
|
+
var createRelationAggregateFactory = (db, tables, cacheCtx, typeNameMapper, filterCtx) => {
|
|
2091
|
+
return ({ tableName, relationName, relEntry }) => {
|
|
2092
|
+
const parentTable = tables[tableName];
|
|
2093
|
+
const targetTableName = relEntry.targetTableName;
|
|
2094
|
+
const targetTable = tables[targetTableName];
|
|
2095
|
+
if (!parentTable || !targetTable) {
|
|
2096
|
+
return void 0;
|
|
2097
|
+
}
|
|
2098
|
+
const joinCols = extractRelationJoinColumns(relEntry, parentTable, targetTable);
|
|
2099
|
+
if (!joinCols) {
|
|
2100
|
+
return void 0;
|
|
2101
|
+
}
|
|
2102
|
+
const { localColPropName, foreignCol } = joinCols;
|
|
2103
|
+
const targetTypeName = resolveTypeName(targetTableName, typeNameMapper);
|
|
2104
|
+
const type = generateAggregateTypes(targetTable, targetTableName, targetTypeName, cacheCtx);
|
|
2105
|
+
const target = aggregateTarget(targetTable, targetTableName, targetTypeName);
|
|
2106
|
+
const resolve = async (parent, args, context, info) => {
|
|
2107
|
+
try {
|
|
2108
|
+
const request = parseAggregateRequest(info, target);
|
|
2109
|
+
if (!Object.keys(request.selection).length) {
|
|
2110
|
+
return {};
|
|
2111
|
+
}
|
|
2112
|
+
const localValue = parent[localColPropName];
|
|
2113
|
+
if (localValue == null) {
|
|
2114
|
+
return assembleAggregateRow({}, request, target);
|
|
2115
|
+
}
|
|
2116
|
+
const whereArg = args?.where;
|
|
2117
|
+
const argsKey = JSON.stringify({
|
|
2118
|
+
where: whereArg ?? null,
|
|
2119
|
+
selection: Object.keys(request.selection).sort()
|
|
2120
|
+
});
|
|
2121
|
+
const loaderKey = `${tableName}::${relationName}::aggregate::${argsKey}`;
|
|
2122
|
+
const loader = getOrCreateLoader(context, loaderKey, async (parentIds) => {
|
|
2123
|
+
const executor = resolveExecutor(db, context);
|
|
2124
|
+
const uniqueIds = [...new Set(parentIds)];
|
|
2125
|
+
const whereCondition = (0, import_drizzle_orm4.and)(
|
|
2126
|
+
(0, import_drizzle_orm4.inArray)(foreignCol, uniqueIds),
|
|
2127
|
+
whereArg ? extractFilters(targetTable, targetTableName, whereArg, relationFilterCtx(filterCtx, targetTableName)) : void 0
|
|
2128
|
+
);
|
|
2129
|
+
const rows = await executor.select({ [GROUP_KEY]: foreignCol, ...request.selection }).from(targetTable).where(whereCondition).groupBy(foreignCol);
|
|
2130
|
+
const byKey = new Map(rows.map((row) => [row[GROUP_KEY], row]));
|
|
2131
|
+
return parentIds.map((id) => byKey.get(id) ?? {});
|
|
2132
|
+
});
|
|
2133
|
+
return assembleAggregateRow(await loader.load(localValue), request, target);
|
|
2134
|
+
} catch (e) {
|
|
2135
|
+
throw toGraphQLError(e);
|
|
2136
|
+
}
|
|
2137
|
+
};
|
|
2138
|
+
return { type, resolve };
|
|
2139
|
+
};
|
|
2140
|
+
};
|
|
2141
|
+
|
|
2142
|
+
// src/util/builders/mysql.ts
|
|
2143
|
+
var generateSelectArray = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper, filterCtx, distinctEnabled = true) => {
|
|
2144
|
+
const queryBase = db.query[tableName];
|
|
2145
|
+
if (!queryBase) {
|
|
2146
|
+
throw new Error(
|
|
2147
|
+
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
2148
|
+
);
|
|
2149
|
+
}
|
|
2150
|
+
const table = tables[tableName];
|
|
2151
|
+
const pkNames = mysqlPrimaryKeyPropNames(table);
|
|
2152
|
+
const queryArgs = selectArrayArgs(
|
|
2153
|
+
orderArgs,
|
|
2154
|
+
filterArgs,
|
|
2155
|
+
distinctEnabled ? generateDistinctEnum(table, typeName) : void 0
|
|
2156
|
+
);
|
|
2157
|
+
return {
|
|
2158
|
+
name: fieldName,
|
|
2159
|
+
resolver: async (_source, args, context, info) => {
|
|
2160
|
+
try {
|
|
2161
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, { deep: true });
|
|
2162
|
+
const { executor, queryBase: requestQueryBase } = resolveQueryExecutor(db, context, tableName, queryBase);
|
|
2163
|
+
return await runRelationalSelect({
|
|
2164
|
+
queryBase: requestQueryBase,
|
|
2165
|
+
tables,
|
|
2166
|
+
tableName,
|
|
2167
|
+
table,
|
|
2168
|
+
relationMap,
|
|
2169
|
+
typeName,
|
|
2170
|
+
typeNameMapper,
|
|
2171
|
+
parsedInfo,
|
|
2172
|
+
...args,
|
|
2173
|
+
single: false,
|
|
2174
|
+
filterCtx,
|
|
2175
|
+
pkNames,
|
|
2176
|
+
db: executor
|
|
2177
|
+
});
|
|
2178
|
+
} catch (e) {
|
|
2179
|
+
throw toGraphQLError(e);
|
|
2180
|
+
}
|
|
2181
|
+
},
|
|
2182
|
+
args: queryArgs
|
|
2183
|
+
};
|
|
2184
|
+
};
|
|
2185
|
+
var generateSelectSingle = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper, filterCtx) => {
|
|
2186
|
+
const queryBase = db.query[tableName];
|
|
2187
|
+
if (!queryBase) {
|
|
2188
|
+
throw new Error(
|
|
2189
|
+
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
2190
|
+
);
|
|
1372
2191
|
}
|
|
1373
2192
|
const queryArgs = selectSingleArgs(orderArgs, filterArgs);
|
|
1374
2193
|
const table = tables[tableName];
|
|
2194
|
+
const pkNames = mysqlPrimaryKeyPropNames(table);
|
|
1375
2195
|
return {
|
|
1376
2196
|
name: fieldName,
|
|
1377
|
-
resolver: async (_source, args,
|
|
2197
|
+
resolver: async (_source, args, context, info) => {
|
|
1378
2198
|
try {
|
|
1379
|
-
const parsedInfo = (0,
|
|
2199
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, { deep: true });
|
|
2200
|
+
const { executor, queryBase: requestQueryBase } = resolveQueryExecutor(db, context, tableName, queryBase);
|
|
1380
2201
|
return await runRelationalSelect({
|
|
1381
|
-
queryBase,
|
|
2202
|
+
queryBase: requestQueryBase,
|
|
1382
2203
|
tables,
|
|
1383
2204
|
tableName,
|
|
1384
2205
|
table,
|
|
@@ -1387,7 +2208,10 @@ var generateSelectSingle = (db, tableName, tables, relationMap, orderArgs, filte
|
|
|
1387
2208
|
typeNameMapper,
|
|
1388
2209
|
parsedInfo,
|
|
1389
2210
|
...args,
|
|
1390
|
-
single: true
|
|
2211
|
+
single: true,
|
|
2212
|
+
filterCtx,
|
|
2213
|
+
pkNames,
|
|
2214
|
+
db: executor
|
|
1391
2215
|
});
|
|
1392
2216
|
} catch (e) {
|
|
1393
2217
|
throw toGraphQLError(e);
|
|
@@ -1399,18 +2223,18 @@ var generateSelectSingle = (db, tableName, tables, relationMap, orderArgs, filte
|
|
|
1399
2223
|
var generateInsertArray = (db, _tableName, table, baseType, fieldName) => {
|
|
1400
2224
|
const queryArgs = {
|
|
1401
2225
|
values: {
|
|
1402
|
-
type: new
|
|
2226
|
+
type: new import_graphql6.GraphQLNonNull(new import_graphql6.GraphQLList(new import_graphql6.GraphQLNonNull(baseType)))
|
|
1403
2227
|
}
|
|
1404
2228
|
};
|
|
1405
2229
|
return {
|
|
1406
2230
|
name: fieldName,
|
|
1407
|
-
resolver: async (_source, args,
|
|
2231
|
+
resolver: async (_source, args, context, _info) => {
|
|
1408
2232
|
try {
|
|
1409
2233
|
const input = remapFromGraphQLArrayInput(args.values, table);
|
|
1410
2234
|
if (!input.length) {
|
|
1411
|
-
throw new
|
|
2235
|
+
throw new import_graphql6.GraphQLError("No values were provided!");
|
|
1412
2236
|
}
|
|
1413
|
-
await db.insert(table).values(input);
|
|
2237
|
+
await resolveExecutor(db, context).insert(table).values(input);
|
|
1414
2238
|
return { isSuccess: true };
|
|
1415
2239
|
} catch (e) {
|
|
1416
2240
|
throw toGraphQLError(e);
|
|
@@ -1422,15 +2246,57 @@ var generateInsertArray = (db, _tableName, table, baseType, fieldName) => {
|
|
|
1422
2246
|
var generateInsertSingle = (db, _tableName, table, baseType, fieldName) => {
|
|
1423
2247
|
const queryArgs = {
|
|
1424
2248
|
values: {
|
|
1425
|
-
type: new
|
|
2249
|
+
type: new import_graphql6.GraphQLNonNull(baseType)
|
|
1426
2250
|
}
|
|
1427
2251
|
};
|
|
1428
2252
|
return {
|
|
1429
2253
|
name: fieldName,
|
|
1430
|
-
resolver: async (_source, args,
|
|
2254
|
+
resolver: async (_source, args, context, _info) => {
|
|
1431
2255
|
try {
|
|
1432
2256
|
const input = remapFromGraphQLSingleInput(args.values, table);
|
|
1433
|
-
await db.insert(table).values(input);
|
|
2257
|
+
await resolveExecutor(db, context).insert(table).values(input);
|
|
2258
|
+
return { isSuccess: true };
|
|
2259
|
+
} catch (e) {
|
|
2260
|
+
throw toGraphQLError(e);
|
|
2261
|
+
}
|
|
2262
|
+
},
|
|
2263
|
+
args: queryArgs
|
|
2264
|
+
};
|
|
2265
|
+
};
|
|
2266
|
+
var generateUpsert = (db, table, baseType, onConflictType, fieldName, single) => {
|
|
2267
|
+
const queryArgs = {
|
|
2268
|
+
values: {
|
|
2269
|
+
type: single ? new import_graphql6.GraphQLNonNull(baseType) : new import_graphql6.GraphQLNonNull(new import_graphql6.GraphQLList(new import_graphql6.GraphQLNonNull(baseType)))
|
|
2270
|
+
},
|
|
2271
|
+
onConflict: {
|
|
2272
|
+
type: onConflictType,
|
|
2273
|
+
description: "How a conflicting row is resolved. Defaults to overwriting it."
|
|
2274
|
+
}
|
|
2275
|
+
};
|
|
2276
|
+
const pkNames = mysqlPrimaryKeyPropNames(table);
|
|
2277
|
+
return {
|
|
2278
|
+
name: fieldName,
|
|
2279
|
+
resolver: async (_source, args, context, _info) => {
|
|
2280
|
+
try {
|
|
2281
|
+
const input = single ? [remapFromGraphQLSingleInput(args.values, table)] : remapFromGraphQLArrayInput(args.values, table);
|
|
2282
|
+
if (!input.length) {
|
|
2283
|
+
throw new import_graphql6.GraphQLError("No values were provided!");
|
|
2284
|
+
}
|
|
2285
|
+
const plan = resolveConflictPlan({
|
|
2286
|
+
table,
|
|
2287
|
+
values: input,
|
|
2288
|
+
onConflict: args.onConflict,
|
|
2289
|
+
pkNames,
|
|
2290
|
+
uniqueSets: [],
|
|
2291
|
+
excludedRef: mysqlValuesColumnRef,
|
|
2292
|
+
withTarget: false
|
|
2293
|
+
});
|
|
2294
|
+
const executor = resolveExecutor(db, context);
|
|
2295
|
+
if (plan.action === "NOTHING") {
|
|
2296
|
+
await executor.insert(table).ignore().values(input);
|
|
2297
|
+
} else {
|
|
2298
|
+
await executor.insert(table).values(input).onDuplicateKeyUpdate({ set: plan.set });
|
|
2299
|
+
}
|
|
1434
2300
|
return { isSuccess: true };
|
|
1435
2301
|
} catch (e) {
|
|
1436
2302
|
throw toGraphQLError(e);
|
|
@@ -1439,10 +2305,10 @@ var generateInsertSingle = (db, _tableName, table, baseType, fieldName) => {
|
|
|
1439
2305
|
args: queryArgs
|
|
1440
2306
|
};
|
|
1441
2307
|
};
|
|
1442
|
-
var generateUpdate = (db, tableName, table, setArgs, filterArgs, fieldName) => {
|
|
2308
|
+
var generateUpdate = (db, tableName, table, setArgs, filterArgs, fieldName, filterCtx) => {
|
|
1443
2309
|
const queryArgs = {
|
|
1444
2310
|
set: {
|
|
1445
|
-
type: new
|
|
2311
|
+
type: new import_graphql6.GraphQLNonNull(setArgs)
|
|
1446
2312
|
},
|
|
1447
2313
|
where: {
|
|
1448
2314
|
type: filterArgs
|
|
@@ -1450,16 +2316,17 @@ var generateUpdate = (db, tableName, table, setArgs, filterArgs, fieldName) => {
|
|
|
1450
2316
|
};
|
|
1451
2317
|
return {
|
|
1452
2318
|
name: fieldName,
|
|
1453
|
-
resolver: async (_source, args,
|
|
2319
|
+
resolver: async (_source, args, context, _info) => {
|
|
1454
2320
|
try {
|
|
1455
2321
|
const { where, set } = args;
|
|
1456
2322
|
const input = remapFromGraphQLSingleInput(set, table);
|
|
1457
2323
|
if (!Object.keys(input).length) {
|
|
1458
|
-
throw new
|
|
2324
|
+
throw new import_graphql6.GraphQLError("Unable to update with no values specified!");
|
|
1459
2325
|
}
|
|
1460
|
-
|
|
2326
|
+
const executor = resolveExecutor(db, context);
|
|
2327
|
+
let query = executor.update(table).set(input);
|
|
1461
2328
|
if (where) {
|
|
1462
|
-
const filters = extractFilters(table, tableName, where);
|
|
2329
|
+
const filters = extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName));
|
|
1463
2330
|
query = query.where(filters);
|
|
1464
2331
|
}
|
|
1465
2332
|
await query;
|
|
@@ -1471,7 +2338,7 @@ var generateUpdate = (db, tableName, table, setArgs, filterArgs, fieldName) => {
|
|
|
1471
2338
|
args: queryArgs
|
|
1472
2339
|
};
|
|
1473
2340
|
};
|
|
1474
|
-
var generateDelete = (db, tableName, table, filterArgs, fieldName) => {
|
|
2341
|
+
var generateDelete = (db, tableName, table, filterArgs, fieldName, filterCtx) => {
|
|
1475
2342
|
const queryArgs = {
|
|
1476
2343
|
where: {
|
|
1477
2344
|
type: filterArgs
|
|
@@ -1479,12 +2346,13 @@ var generateDelete = (db, tableName, table, filterArgs, fieldName) => {
|
|
|
1479
2346
|
};
|
|
1480
2347
|
return {
|
|
1481
2348
|
name: fieldName,
|
|
1482
|
-
resolver: async (_source, args,
|
|
2349
|
+
resolver: async (_source, args, context, _info) => {
|
|
1483
2350
|
try {
|
|
1484
2351
|
const { where } = args;
|
|
1485
|
-
|
|
2352
|
+
const executor = resolveExecutor(db, context);
|
|
2353
|
+
let query = executor.delete(table);
|
|
1486
2354
|
if (where) {
|
|
1487
|
-
const filters = extractFilters(table, tableName, where);
|
|
2355
|
+
const filters = extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName));
|
|
1488
2356
|
query = query.where(filters);
|
|
1489
2357
|
}
|
|
1490
2358
|
await query;
|
|
@@ -1497,10 +2365,11 @@ var generateDelete = (db, tableName, table, filterArgs, fieldName) => {
|
|
|
1497
2365
|
};
|
|
1498
2366
|
};
|
|
1499
2367
|
var mysqlPrimaryKeyPropNames = (table) => getPrimaryKeyPropNamesFromConfig(table, import_mysql_core2.getTableConfig);
|
|
1500
|
-
var generateSchemaData = (db, schema, relations,
|
|
2368
|
+
var generateSchemaData = (db, schema, relations, options) => {
|
|
2369
|
+
const { relationsDepthLimit, prefixes, suffixes, typeNameMapper, shouldEagerLoad, features } = options;
|
|
1501
2370
|
const rawSchema = schema;
|
|
1502
2371
|
const schemaEntries = Object.entries(rawSchema);
|
|
1503
|
-
const tableEntries = schemaEntries.filter(([_key, value]) => (0,
|
|
2372
|
+
const tableEntries = schemaEntries.filter(([_key, value]) => (0, import_drizzle_orm5.is)(value, import_mysql_core2.MySqlTable));
|
|
1504
2373
|
const tables = Object.fromEntries(tableEntries);
|
|
1505
2374
|
if (!tableEntries.length) {
|
|
1506
2375
|
throw new Error(
|
|
@@ -1510,7 +2379,8 @@ var generateSchemaData = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
1510
2379
|
const namedRelations = buildNamedRelations(relations ?? {}, tableEntries);
|
|
1511
2380
|
attachTargetPrimaryKeys(namedRelations, tables, mysqlPrimaryKeyPropNames);
|
|
1512
2381
|
const eagerRelations = pruneNonEagerRelations(namedRelations, shouldEagerLoad);
|
|
1513
|
-
const
|
|
2382
|
+
const filterCtx = { tables, relationMap: namedRelations };
|
|
2383
|
+
const resolverFactory = createRelationResolverFactory(db, tables, filterCtx);
|
|
1514
2384
|
const cacheCtx = {
|
|
1515
2385
|
genericFilterCache: /* @__PURE__ */ new Map(),
|
|
1516
2386
|
objectTypeCache: /* @__PURE__ */ new Map(),
|
|
@@ -1518,8 +2388,11 @@ var generateSchemaData = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
1518
2388
|
fullyBuiltTables: /* @__PURE__ */ new Set(),
|
|
1519
2389
|
relationTypeCache: /* @__PURE__ */ new Map(),
|
|
1520
2390
|
orderTypeCache: /* @__PURE__ */ new WeakMap(),
|
|
1521
|
-
filterTypeCache: /* @__PURE__ */ new WeakMap()
|
|
2391
|
+
filterTypeCache: /* @__PURE__ */ new WeakMap(),
|
|
2392
|
+
listRelationFilterCache: /* @__PURE__ */ new Map(),
|
|
2393
|
+
aggregateTypeCache: /* @__PURE__ */ new Map()
|
|
1522
2394
|
};
|
|
2395
|
+
const relationAggregateFactory = features.relationAggregates ? createRelationAggregateFactory(db, tables, cacheCtx, typeNameMapper, filterCtx) : void 0;
|
|
1523
2396
|
const queries = {};
|
|
1524
2397
|
const mutations = {};
|
|
1525
2398
|
const gqlSchemaTypes = Object.fromEntries(
|
|
@@ -1535,22 +2408,24 @@ var generateSchemaData = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
1535
2408
|
typeNameMapper,
|
|
1536
2409
|
prefixes.insert,
|
|
1537
2410
|
prefixes.update,
|
|
1538
|
-
resolverFactory
|
|
2411
|
+
resolverFactory,
|
|
2412
|
+
relationAggregateFactory
|
|
1539
2413
|
)
|
|
1540
2414
|
])
|
|
1541
2415
|
);
|
|
1542
|
-
const mutationReturnType = new
|
|
2416
|
+
const mutationReturnType = new import_graphql6.GraphQLObjectType({
|
|
1543
2417
|
name: "MutationReturn",
|
|
1544
2418
|
fields: {
|
|
1545
2419
|
isSuccess: {
|
|
1546
|
-
type: new
|
|
2420
|
+
type: new import_graphql6.GraphQLNonNull(import_graphql6.GraphQLBoolean)
|
|
1547
2421
|
}
|
|
1548
2422
|
}
|
|
1549
2423
|
});
|
|
1550
2424
|
const inputs = {};
|
|
1551
|
-
const outputs = {
|
|
1552
|
-
|
|
1553
|
-
|
|
2425
|
+
const outputs = {};
|
|
2426
|
+
if (features.insert || features.upsert || features.update || features.delete) {
|
|
2427
|
+
outputs.MutationReturn = mutationReturnType;
|
|
2428
|
+
}
|
|
1554
2429
|
for (const [tableName, tableTypes] of Object.entries(gqlSchemaTypes)) {
|
|
1555
2430
|
const { insertInput, updateInput, tableFilters, tableOrder } = tableTypes.inputs;
|
|
1556
2431
|
const { selectSingleOutput, selectArrOutput } = tableTypes.outputs;
|
|
@@ -1558,8 +2433,11 @@ var generateSchemaData = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
1558
2433
|
typeName,
|
|
1559
2434
|
listFieldName,
|
|
1560
2435
|
singleFieldName,
|
|
2436
|
+
aggregateFieldName,
|
|
1561
2437
|
createArrayFieldName,
|
|
1562
2438
|
createSingleFieldName,
|
|
2439
|
+
upsertArrayFieldName,
|
|
2440
|
+
upsertSingleFieldName,
|
|
1563
2441
|
updateFieldName,
|
|
1564
2442
|
deleteFieldName
|
|
1565
2443
|
} = computeResolverFieldNames(tableName, typeNameMapper, prefixes, suffixes);
|
|
@@ -1572,7 +2450,9 @@ var generateSchemaData = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
1572
2450
|
tableFilters,
|
|
1573
2451
|
listFieldName,
|
|
1574
2452
|
typeName,
|
|
1575
|
-
typeNameMapper
|
|
2453
|
+
typeNameMapper,
|
|
2454
|
+
filterCtx,
|
|
2455
|
+
features.distinct
|
|
1576
2456
|
);
|
|
1577
2457
|
const selectSingleGenerated = generateSelectSingle(
|
|
1578
2458
|
db,
|
|
@@ -1583,37 +2463,40 @@ var generateSchemaData = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
1583
2463
|
tableFilters,
|
|
1584
2464
|
singleFieldName,
|
|
1585
2465
|
typeName,
|
|
1586
|
-
typeNameMapper
|
|
1587
|
-
|
|
1588
|
-
const insertArrGenerated = generateInsertArray(
|
|
1589
|
-
db,
|
|
1590
|
-
tableName,
|
|
1591
|
-
schema[tableName],
|
|
1592
|
-
insertInput,
|
|
1593
|
-
createArrayFieldName
|
|
1594
|
-
);
|
|
1595
|
-
const insertSingleGenerated = generateInsertSingle(
|
|
1596
|
-
db,
|
|
1597
|
-
tableName,
|
|
1598
|
-
schema[tableName],
|
|
1599
|
-
insertInput,
|
|
1600
|
-
createSingleFieldName
|
|
2466
|
+
typeNameMapper,
|
|
2467
|
+
filterCtx
|
|
1601
2468
|
);
|
|
1602
|
-
const
|
|
2469
|
+
const insertArrGenerated = features.insert ? generateInsertArray(db, tableName, schema[tableName], insertInput, createArrayFieldName) : void 0;
|
|
2470
|
+
const insertSingleGenerated = features.insert ? generateInsertSingle(db, tableName, schema[tableName], insertInput, createSingleFieldName) : void 0;
|
|
2471
|
+
const onConflictInput = features.upsert ? generateOnConflictInput({
|
|
2472
|
+
table: schema[tableName],
|
|
2473
|
+
typeName,
|
|
2474
|
+
uniqueSets: [],
|
|
2475
|
+
tableFilters,
|
|
2476
|
+
withTarget: false
|
|
2477
|
+
}) : void 0;
|
|
2478
|
+
const upsertArrGenerated = onConflictInput ? generateUpsert(db, schema[tableName], insertInput, onConflictInput, upsertArrayFieldName, false) : void 0;
|
|
2479
|
+
const upsertSingleGenerated = onConflictInput ? generateUpsert(db, schema[tableName], insertInput, onConflictInput, upsertSingleFieldName, true) : void 0;
|
|
2480
|
+
const updateGenerated = features.update ? generateUpdate(
|
|
1603
2481
|
db,
|
|
1604
2482
|
tableName,
|
|
1605
2483
|
schema[tableName],
|
|
1606
2484
|
updateInput,
|
|
1607
2485
|
tableFilters,
|
|
1608
|
-
updateFieldName
|
|
1609
|
-
|
|
1610
|
-
|
|
2486
|
+
updateFieldName,
|
|
2487
|
+
filterCtx
|
|
2488
|
+
) : void 0;
|
|
2489
|
+
const deleteGenerated = features.delete ? generateDelete(db, tableName, schema[tableName], tableFilters, deleteFieldName, filterCtx) : void 0;
|
|
2490
|
+
const aggregateType = features.aggregates ? generateAggregateTypes(schema[tableName], tableName, typeName, cacheCtx) : void 0;
|
|
2491
|
+
const aggregateGenerated = features.aggregates ? generateAggregate(
|
|
1611
2492
|
db,
|
|
1612
2493
|
tableName,
|
|
1613
2494
|
schema[tableName],
|
|
2495
|
+
typeName,
|
|
2496
|
+
aggregateFieldName,
|
|
1614
2497
|
tableFilters,
|
|
1615
|
-
|
|
1616
|
-
);
|
|
2498
|
+
filterCtx
|
|
2499
|
+
) : void 0;
|
|
1617
2500
|
queries[selectArrGenerated.name] = {
|
|
1618
2501
|
type: selectArrOutput,
|
|
1619
2502
|
args: selectArrGenerated.args,
|
|
@@ -1624,36 +2507,50 @@ var generateSchemaData = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
1624
2507
|
args: selectSingleGenerated.args,
|
|
1625
2508
|
resolve: selectSingleGenerated.resolver
|
|
1626
2509
|
};
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
2510
|
+
if (aggregateGenerated && aggregateType) {
|
|
2511
|
+
queries[aggregateGenerated.name] = {
|
|
2512
|
+
type: new import_graphql6.GraphQLNonNull(aggregateType),
|
|
2513
|
+
args: aggregateGenerated.args,
|
|
2514
|
+
resolve: aggregateGenerated.resolver
|
|
2515
|
+
};
|
|
2516
|
+
}
|
|
2517
|
+
for (const generated of [
|
|
2518
|
+
insertArrGenerated,
|
|
2519
|
+
insertSingleGenerated,
|
|
2520
|
+
upsertArrGenerated,
|
|
2521
|
+
upsertSingleGenerated,
|
|
2522
|
+
updateGenerated,
|
|
2523
|
+
deleteGenerated
|
|
2524
|
+
]) {
|
|
2525
|
+
if (generated) {
|
|
2526
|
+
mutations[generated.name] = {
|
|
2527
|
+
type: mutationReturnType,
|
|
2528
|
+
args: generated.args,
|
|
2529
|
+
resolve: generated.resolver
|
|
2530
|
+
};
|
|
2531
|
+
}
|
|
2532
|
+
}
|
|
2533
|
+
const activeInputs = [
|
|
2534
|
+
// The insert input types the upsert mutations too, so either feature keeps it.
|
|
2535
|
+
...features.insert || onConflictInput ? [insertInput] : [],
|
|
2536
|
+
...onConflictInput ? [onConflictInput] : [],
|
|
2537
|
+
...features.update ? [updateInput] : [],
|
|
2538
|
+
tableFilters,
|
|
2539
|
+
tableOrder
|
|
2540
|
+
];
|
|
2541
|
+
activeInputs.forEach((e) => {
|
|
1648
2542
|
inputs[e.name] = e;
|
|
1649
2543
|
});
|
|
1650
2544
|
outputs[selectSingleOutput.name] = selectSingleOutput;
|
|
2545
|
+
if (aggregateType) {
|
|
2546
|
+
outputs[aggregateType.name] = aggregateType;
|
|
2547
|
+
}
|
|
1651
2548
|
}
|
|
1652
2549
|
const fieldResolvers = {};
|
|
1653
2550
|
for (const [tableName, tableRelations] of Object.entries(namedRelations)) {
|
|
1654
2551
|
const relResolvers = {};
|
|
1655
2552
|
for (const [relName, relEntry] of Object.entries(tableRelations)) {
|
|
1656
|
-
const isOne = (0,
|
|
2553
|
+
const isOne = (0, import_drizzle_orm5.is)(relEntry.relation ?? relEntry, import_drizzle_orm5.One);
|
|
1657
2554
|
const resolver = resolverFactory({ tableName, relationName: relName, relEntry, isOne });
|
|
1658
2555
|
if (resolver) {
|
|
1659
2556
|
relResolvers[relName] = resolver;
|
|
@@ -1667,22 +2564,28 @@ var generateSchemaData = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
1667
2564
|
};
|
|
1668
2565
|
|
|
1669
2566
|
// src/util/builders/pg.ts
|
|
1670
|
-
var
|
|
2567
|
+
var import_drizzle_orm6 = require("drizzle-orm");
|
|
1671
2568
|
var import_pg_core2 = require("drizzle-orm/pg-core");
|
|
1672
|
-
var
|
|
1673
|
-
var
|
|
1674
|
-
var generateSelectArray2 = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper) => {
|
|
2569
|
+
var import_graphql7 = require("graphql");
|
|
2570
|
+
var import_graphql_parse_resolve_info3 = require("graphql-parse-resolve-info");
|
|
2571
|
+
var generateSelectArray2 = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper, filterCtx, distinctEnabled = true) => {
|
|
1675
2572
|
const queryBase = db.query[tableName];
|
|
1676
|
-
const queryArgs = selectArrayArgs(orderArgs, filterArgs);
|
|
1677
2573
|
const table = tables[tableName];
|
|
2574
|
+
const pkNames = pgPrimaryKeyPropNames(table);
|
|
2575
|
+
const queryArgs = selectArrayArgs(
|
|
2576
|
+
orderArgs,
|
|
2577
|
+
filterArgs,
|
|
2578
|
+
distinctEnabled ? generateDistinctEnum(table, typeName) : void 0
|
|
2579
|
+
);
|
|
1678
2580
|
return {
|
|
1679
2581
|
name: fieldName,
|
|
1680
|
-
resolver: async (_source, args,
|
|
2582
|
+
resolver: async (_source, args, context, info) => {
|
|
1681
2583
|
try {
|
|
1682
|
-
const parsedInfo = (0,
|
|
1683
|
-
|
|
2584
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, { deep: true });
|
|
2585
|
+
const { executor, queryBase: requestQueryBase } = resolveQueryExecutor(db, context, tableName, queryBase);
|
|
2586
|
+
if (requestQueryBase) {
|
|
1684
2587
|
return await runRelationalSelect({
|
|
1685
|
-
queryBase,
|
|
2588
|
+
queryBase: requestQueryBase,
|
|
1686
2589
|
tables,
|
|
1687
2590
|
tableName,
|
|
1688
2591
|
table,
|
|
@@ -1691,26 +2594,57 @@ var generateSelectArray2 = (db, tableName, tables, relationMap, orderArgs, filte
|
|
|
1691
2594
|
typeNameMapper,
|
|
1692
2595
|
parsedInfo,
|
|
1693
2596
|
...args,
|
|
1694
|
-
single: false
|
|
2597
|
+
single: false,
|
|
2598
|
+
filterCtx,
|
|
2599
|
+
pkNames,
|
|
2600
|
+
db: executor
|
|
1695
2601
|
});
|
|
1696
2602
|
}
|
|
1697
|
-
const { offset, limit, orderBy, where } = args;
|
|
2603
|
+
const { offset, limit, orderBy, where, distinct } = args;
|
|
1698
2604
|
const selectedColumnsSql = extractSelectedColumnsFromTreeSQLFormat(
|
|
1699
2605
|
parsedInfo.fieldsByTypeName[typeName],
|
|
1700
|
-
table
|
|
2606
|
+
table,
|
|
2607
|
+
{ tableName, relationMap, tables }
|
|
1701
2608
|
);
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
2609
|
+
const whereSql = where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0;
|
|
2610
|
+
let distinctKeys;
|
|
2611
|
+
if (distinct?.length) {
|
|
2612
|
+
distinctKeys = await selectDistinctKeys({
|
|
2613
|
+
db: executor,
|
|
2614
|
+
table,
|
|
2615
|
+
tableName,
|
|
2616
|
+
distinct,
|
|
2617
|
+
pkNames,
|
|
2618
|
+
where: whereSql,
|
|
2619
|
+
orderBy,
|
|
2620
|
+
limit,
|
|
2621
|
+
offset
|
|
2622
|
+
});
|
|
2623
|
+
if (!distinctKeys.length) {
|
|
2624
|
+
return [];
|
|
2625
|
+
}
|
|
1705
2626
|
}
|
|
1706
|
-
|
|
1707
|
-
|
|
2627
|
+
let q = executor.select(selectedColumnsSql).from(table);
|
|
2628
|
+
if (distinctKeys) {
|
|
2629
|
+
q = q.where(primaryKeyRestriction(table, pkNames, distinctKeys));
|
|
2630
|
+
} else if (whereSql) {
|
|
2631
|
+
q = q.where(whereSql);
|
|
1708
2632
|
}
|
|
1709
|
-
if (
|
|
1710
|
-
q = q.
|
|
2633
|
+
if (orderBy) {
|
|
2634
|
+
q = q.orderBy(
|
|
2635
|
+
...extractOrderBy(table, orderBy),
|
|
2636
|
+
...distinctKeys ? primaryKeyOrderExprs(table, pkNames) : []
|
|
2637
|
+
);
|
|
2638
|
+
} else if ((distinctKeys || offset != null || limit != null) && pkNames.length) {
|
|
2639
|
+
q = q.orderBy(...primaryKeyOrderExprs(table, pkNames));
|
|
1711
2640
|
}
|
|
1712
|
-
if (
|
|
1713
|
-
|
|
2641
|
+
if (!distinctKeys) {
|
|
2642
|
+
if (offset) {
|
|
2643
|
+
q = q.offset(offset);
|
|
2644
|
+
}
|
|
2645
|
+
if (limit) {
|
|
2646
|
+
q = q.limit(limit);
|
|
2647
|
+
}
|
|
1714
2648
|
}
|
|
1715
2649
|
return remapToGraphQLArrayOutput(await q, tableName, table, relationMap);
|
|
1716
2650
|
} catch (e) {
|
|
@@ -1720,18 +2654,20 @@ var generateSelectArray2 = (db, tableName, tables, relationMap, orderArgs, filte
|
|
|
1720
2654
|
args: queryArgs
|
|
1721
2655
|
};
|
|
1722
2656
|
};
|
|
1723
|
-
var generateSelectSingle2 = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper) => {
|
|
2657
|
+
var generateSelectSingle2 = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper, filterCtx) => {
|
|
1724
2658
|
const queryBase = db.query[tableName];
|
|
1725
2659
|
const queryArgs = selectSingleArgs(orderArgs, filterArgs);
|
|
1726
2660
|
const table = tables[tableName];
|
|
2661
|
+
const pkNames = pgPrimaryKeyPropNames(table);
|
|
1727
2662
|
return {
|
|
1728
2663
|
name: fieldName,
|
|
1729
|
-
resolver: async (_source, args,
|
|
2664
|
+
resolver: async (_source, args, context, info) => {
|
|
1730
2665
|
try {
|
|
1731
|
-
const parsedInfo = (0,
|
|
1732
|
-
|
|
2666
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, { deep: true });
|
|
2667
|
+
const { executor, queryBase: requestQueryBase } = resolveQueryExecutor(db, context, tableName, queryBase);
|
|
2668
|
+
if (requestQueryBase) {
|
|
1733
2669
|
return await runRelationalSelect({
|
|
1734
|
-
queryBase,
|
|
2670
|
+
queryBase: requestQueryBase,
|
|
1735
2671
|
tables,
|
|
1736
2672
|
tableName,
|
|
1737
2673
|
table,
|
|
@@ -1740,20 +2676,26 @@ var generateSelectSingle2 = (db, tableName, tables, relationMap, orderArgs, filt
|
|
|
1740
2676
|
typeNameMapper,
|
|
1741
2677
|
parsedInfo,
|
|
1742
2678
|
...args,
|
|
1743
|
-
single: true
|
|
2679
|
+
single: true,
|
|
2680
|
+
filterCtx,
|
|
2681
|
+
pkNames,
|
|
2682
|
+
db: executor
|
|
1744
2683
|
});
|
|
1745
2684
|
}
|
|
1746
2685
|
const { offset, orderBy, where } = args;
|
|
1747
2686
|
const selectedColumnsSql = extractSelectedColumnsFromTreeSQLFormat(
|
|
1748
2687
|
parsedInfo.fieldsByTypeName[typeName],
|
|
1749
|
-
table
|
|
2688
|
+
table,
|
|
2689
|
+
{ tableName, relationMap, tables }
|
|
1750
2690
|
);
|
|
1751
|
-
let q =
|
|
2691
|
+
let q = executor.select(selectedColumnsSql).from(table);
|
|
1752
2692
|
if (where) {
|
|
1753
|
-
q = q.where(extractFilters(table, tableName, where));
|
|
2693
|
+
q = q.where(extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)));
|
|
1754
2694
|
}
|
|
1755
2695
|
if (orderBy) {
|
|
1756
2696
|
q = q.orderBy(...extractOrderBy(table, orderBy));
|
|
2697
|
+
} else if (pkNames.length) {
|
|
2698
|
+
q = q.orderBy(...primaryKeyOrderExprs(table, pkNames));
|
|
1757
2699
|
}
|
|
1758
2700
|
if (offset) {
|
|
1759
2701
|
q = q.offset(offset);
|
|
@@ -1772,19 +2714,19 @@ var pgPrimaryKeyPropNames = (table) => getPrimaryKeyPropNamesFromConfig(table, i
|
|
|
1772
2714
|
var generateInsertArray2 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false) => {
|
|
1773
2715
|
const queryArgs = {
|
|
1774
2716
|
values: {
|
|
1775
|
-
type: new
|
|
2717
|
+
type: new import_graphql7.GraphQLNonNull(new import_graphql7.GraphQLList(new import_graphql7.GraphQLNonNull(baseType)))
|
|
1776
2718
|
}
|
|
1777
2719
|
};
|
|
1778
2720
|
const pkNames = pgPrimaryKeyPropNames(table);
|
|
1779
2721
|
return {
|
|
1780
2722
|
name: fieldName,
|
|
1781
|
-
resolver: async (_source, args,
|
|
2723
|
+
resolver: async (_source, args, context, info) => {
|
|
1782
2724
|
try {
|
|
1783
2725
|
const input = remapFromGraphQLArrayInput(args.values, table);
|
|
1784
2726
|
if (!input.length) {
|
|
1785
|
-
throw new
|
|
2727
|
+
throw new import_graphql7.GraphQLError("No values were provided!");
|
|
1786
2728
|
}
|
|
1787
|
-
const parsedInfo = (0,
|
|
2729
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
|
|
1788
2730
|
deep: true
|
|
1789
2731
|
});
|
|
1790
2732
|
const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
|
|
@@ -1797,12 +2739,13 @@ var generateInsertArray2 = (db, tableName, table, tables, relationMap, baseType,
|
|
|
1797
2739
|
pkNames,
|
|
1798
2740
|
parsedInfo
|
|
1799
2741
|
});
|
|
1800
|
-
|
|
2742
|
+
const executor = resolveExecutor(db, context);
|
|
2743
|
+
let query = executor.insert(table).values(input).returning(columns);
|
|
1801
2744
|
if (conflictDoNothing) {
|
|
1802
2745
|
query = query.onConflictDoNothing();
|
|
1803
2746
|
}
|
|
1804
2747
|
const result = await query;
|
|
1805
|
-
const enriched = hasRelations ? await eagerLoadMutationRelations(
|
|
2748
|
+
const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
|
|
1806
2749
|
return remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
|
|
1807
2750
|
} catch (e) {
|
|
1808
2751
|
throw toGraphQLError(e);
|
|
@@ -1814,16 +2757,16 @@ var generateInsertArray2 = (db, tableName, table, tables, relationMap, baseType,
|
|
|
1814
2757
|
var generateInsertSingle2 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false) => {
|
|
1815
2758
|
const queryArgs = {
|
|
1816
2759
|
values: {
|
|
1817
|
-
type: new
|
|
2760
|
+
type: new import_graphql7.GraphQLNonNull(baseType)
|
|
1818
2761
|
}
|
|
1819
2762
|
};
|
|
1820
2763
|
const pkNames = pgPrimaryKeyPropNames(table);
|
|
1821
2764
|
return {
|
|
1822
2765
|
name: fieldName,
|
|
1823
|
-
resolver: async (_source, args,
|
|
2766
|
+
resolver: async (_source, args, context, info) => {
|
|
1824
2767
|
try {
|
|
1825
2768
|
const input = remapFromGraphQLSingleInput(args.values, table);
|
|
1826
|
-
const parsedInfo = (0,
|
|
2769
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
|
|
1827
2770
|
deep: true
|
|
1828
2771
|
});
|
|
1829
2772
|
const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
|
|
@@ -1836,7 +2779,8 @@ var generateInsertSingle2 = (db, tableName, table, tables, relationMap, baseType
|
|
|
1836
2779
|
pkNames,
|
|
1837
2780
|
parsedInfo
|
|
1838
2781
|
});
|
|
1839
|
-
|
|
2782
|
+
const executor = resolveExecutor(db, context);
|
|
2783
|
+
let query = executor.insert(table).values(input).returning(columns);
|
|
1840
2784
|
if (conflictDoNothing) {
|
|
1841
2785
|
query = query.onConflictDoNothing();
|
|
1842
2786
|
}
|
|
@@ -1844,7 +2788,7 @@ var generateInsertSingle2 = (db, tableName, table, tables, relationMap, baseType
|
|
|
1844
2788
|
if (!result[0]) {
|
|
1845
2789
|
return void 0;
|
|
1846
2790
|
}
|
|
1847
|
-
const enriched = hasRelations ? await eagerLoadMutationRelations(
|
|
2791
|
+
const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
|
|
1848
2792
|
return remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap);
|
|
1849
2793
|
} catch (e) {
|
|
1850
2794
|
throw toGraphQLError(e);
|
|
@@ -1853,10 +2797,66 @@ var generateInsertSingle2 = (db, tableName, table, tables, relationMap, baseType
|
|
|
1853
2797
|
args: queryArgs
|
|
1854
2798
|
};
|
|
1855
2799
|
};
|
|
1856
|
-
var
|
|
2800
|
+
var generateUpsert2 = (db, tableName, table, tables, relationMap, baseType, onConflictType, uniqueSets, fieldName, typeName, single, typeNameMapper, filterCtx) => {
|
|
2801
|
+
const queryArgs = {
|
|
2802
|
+
values: {
|
|
2803
|
+
type: single ? new import_graphql7.GraphQLNonNull(baseType) : new import_graphql7.GraphQLNonNull(new import_graphql7.GraphQLList(new import_graphql7.GraphQLNonNull(baseType)))
|
|
2804
|
+
},
|
|
2805
|
+
onConflict: {
|
|
2806
|
+
type: onConflictType,
|
|
2807
|
+
description: "How a conflicting row is resolved. Defaults to overwriting it on the primary key."
|
|
2808
|
+
}
|
|
2809
|
+
};
|
|
2810
|
+
const pkNames = pgPrimaryKeyPropNames(table);
|
|
2811
|
+
return {
|
|
2812
|
+
name: fieldName,
|
|
2813
|
+
resolver: async (_source, args, context, info) => {
|
|
2814
|
+
try {
|
|
2815
|
+
const input = single ? [remapFromGraphQLSingleInput(args.values, table)] : remapFromGraphQLArrayInput(args.values, table);
|
|
2816
|
+
if (!input.length) {
|
|
2817
|
+
throw new import_graphql7.GraphQLError("No values were provided!");
|
|
2818
|
+
}
|
|
2819
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, { deep: true });
|
|
2820
|
+
const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
|
|
2821
|
+
relationMap,
|
|
2822
|
+
tables,
|
|
2823
|
+
tableName,
|
|
2824
|
+
typeName,
|
|
2825
|
+
typeNameMapper,
|
|
2826
|
+
table,
|
|
2827
|
+
pkNames,
|
|
2828
|
+
parsedInfo
|
|
2829
|
+
});
|
|
2830
|
+
const plan = resolveConflictPlan({
|
|
2831
|
+
table,
|
|
2832
|
+
values: input,
|
|
2833
|
+
onConflict: args.onConflict,
|
|
2834
|
+
pkNames,
|
|
2835
|
+
uniqueSets,
|
|
2836
|
+
excludedRef: excludedColumnRef,
|
|
2837
|
+
withTarget: true,
|
|
2838
|
+
buildWhere: (where) => extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName))
|
|
2839
|
+
});
|
|
2840
|
+
const executor = resolveExecutor(db, context);
|
|
2841
|
+
let query = executor.insert(table).values(input).returning(columns);
|
|
2842
|
+
query = plan.action === "NOTHING" ? query.onConflictDoNothing(plan.target ? { target: plan.target } : void 0) : query.onConflictDoUpdate({ target: plan.target, set: plan.set, setWhere: plan.setWhere });
|
|
2843
|
+
const result = await query;
|
|
2844
|
+
if (single && !result[0]) {
|
|
2845
|
+
return void 0;
|
|
2846
|
+
}
|
|
2847
|
+
const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
|
|
2848
|
+
return single ? remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap) : remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
|
|
2849
|
+
} catch (e) {
|
|
2850
|
+
throw toGraphQLError(e);
|
|
2851
|
+
}
|
|
2852
|
+
},
|
|
2853
|
+
args: queryArgs
|
|
2854
|
+
};
|
|
2855
|
+
};
|
|
2856
|
+
var generateUpdate2 = (db, tableName, table, tables, relationMap, setArgs, filterArgs, fieldName, typeName, typeNameMapper, filterCtx) => {
|
|
1857
2857
|
const queryArgs = {
|
|
1858
2858
|
set: {
|
|
1859
|
-
type: new
|
|
2859
|
+
type: new import_graphql7.GraphQLNonNull(setArgs)
|
|
1860
2860
|
},
|
|
1861
2861
|
where: {
|
|
1862
2862
|
type: filterArgs
|
|
@@ -1865,10 +2865,10 @@ var generateUpdate2 = (db, tableName, table, tables, relationMap, setArgs, filte
|
|
|
1865
2865
|
const pkNames = pgPrimaryKeyPropNames(table);
|
|
1866
2866
|
return {
|
|
1867
2867
|
name: fieldName,
|
|
1868
|
-
resolver: async (_source, args,
|
|
2868
|
+
resolver: async (_source, args, context, info) => {
|
|
1869
2869
|
try {
|
|
1870
2870
|
const { where, set } = args;
|
|
1871
|
-
const parsedInfo = (0,
|
|
2871
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
|
|
1872
2872
|
deep: true
|
|
1873
2873
|
});
|
|
1874
2874
|
const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
|
|
@@ -1883,16 +2883,17 @@ var generateUpdate2 = (db, tableName, table, tables, relationMap, setArgs, filte
|
|
|
1883
2883
|
});
|
|
1884
2884
|
const input = remapFromGraphQLSingleInput(set, table);
|
|
1885
2885
|
if (!Object.keys(input).length) {
|
|
1886
|
-
throw new
|
|
2886
|
+
throw new import_graphql7.GraphQLError("Unable to update with no values specified!");
|
|
1887
2887
|
}
|
|
1888
|
-
|
|
2888
|
+
const executor = resolveExecutor(db, context);
|
|
2889
|
+
let query = executor.update(table).set(input);
|
|
1889
2890
|
if (where) {
|
|
1890
|
-
const filters = extractFilters(table, tableName, where);
|
|
2891
|
+
const filters = extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName));
|
|
1891
2892
|
query = query.where(filters);
|
|
1892
2893
|
}
|
|
1893
2894
|
query = query.returning(columns);
|
|
1894
2895
|
const result = await query;
|
|
1895
|
-
const enriched = hasRelations ? await eagerLoadMutationRelations(
|
|
2896
|
+
const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
|
|
1896
2897
|
return remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
|
|
1897
2898
|
} catch (e) {
|
|
1898
2899
|
throw toGraphQLError(e);
|
|
@@ -1901,7 +2902,7 @@ var generateUpdate2 = (db, tableName, table, tables, relationMap, setArgs, filte
|
|
|
1901
2902
|
args: queryArgs
|
|
1902
2903
|
};
|
|
1903
2904
|
};
|
|
1904
|
-
var generateDelete2 = (db, tableName, table, filterArgs, fieldName, typeName) => {
|
|
2905
|
+
var generateDelete2 = (db, tableName, table, filterArgs, fieldName, typeName, filterCtx, selectionCtx) => {
|
|
1905
2906
|
const queryArgs = {
|
|
1906
2907
|
where: {
|
|
1907
2908
|
type: filterArgs
|
|
@@ -1909,19 +2910,21 @@ var generateDelete2 = (db, tableName, table, filterArgs, fieldName, typeName) =>
|
|
|
1909
2910
|
};
|
|
1910
2911
|
return {
|
|
1911
2912
|
name: fieldName,
|
|
1912
|
-
resolver: async (_source, args,
|
|
2913
|
+
resolver: async (_source, args, context, info) => {
|
|
1913
2914
|
try {
|
|
1914
2915
|
const { where } = args;
|
|
1915
|
-
const parsedInfo = (0,
|
|
2916
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
|
|
1916
2917
|
deep: true
|
|
1917
2918
|
});
|
|
1918
2919
|
const columns = extractSelectedColumnsFromTreeSQLFormat(
|
|
1919
2920
|
parsedInfo.fieldsByTypeName[typeName],
|
|
1920
|
-
table
|
|
2921
|
+
table,
|
|
2922
|
+
selectionCtx
|
|
1921
2923
|
);
|
|
1922
|
-
|
|
2924
|
+
const executor = resolveExecutor(db, context);
|
|
2925
|
+
let query = executor.delete(table);
|
|
1923
2926
|
if (where) {
|
|
1924
|
-
const filters = extractFilters(table, tableName, where);
|
|
2927
|
+
const filters = extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName));
|
|
1925
2928
|
query = query.where(filters);
|
|
1926
2929
|
}
|
|
1927
2930
|
query = query.returning(columns);
|
|
@@ -1934,9 +2937,10 @@ var generateDelete2 = (db, tableName, table, filterArgs, fieldName, typeName) =>
|
|
|
1934
2937
|
args: queryArgs
|
|
1935
2938
|
};
|
|
1936
2939
|
};
|
|
1937
|
-
function generateSchemaData2(db, schema, relations,
|
|
2940
|
+
function generateSchemaData2(db, schema, relations, options) {
|
|
2941
|
+
const { relationsDepthLimit, prefixes, suffixes, conflictDoNothing, typeNameMapper, shouldEagerLoad, features } = options;
|
|
1938
2942
|
const schemaEntries = Object.entries(schema);
|
|
1939
|
-
const tableEntries = schemaEntries.filter(([_key, value]) => (0,
|
|
2943
|
+
const tableEntries = schemaEntries.filter(([_key, value]) => (0, import_drizzle_orm6.is)(value, import_pg_core2.PgTable));
|
|
1940
2944
|
const tables = Object.fromEntries(tableEntries);
|
|
1941
2945
|
if (!tableEntries.length) {
|
|
1942
2946
|
throw new Error(
|
|
@@ -1946,7 +2950,8 @@ function generateSchemaData2(db, schema, relations, relationsDepthLimit, prefixe
|
|
|
1946
2950
|
const namedRelations = buildNamedRelations(relations ?? {}, tableEntries);
|
|
1947
2951
|
attachTargetPrimaryKeys(namedRelations, tables, pgPrimaryKeyPropNames);
|
|
1948
2952
|
const eagerRelations = pruneNonEagerRelations(namedRelations, shouldEagerLoad);
|
|
1949
|
-
const
|
|
2953
|
+
const filterCtx = { tables, relationMap: namedRelations };
|
|
2954
|
+
const resolverFactory = createRelationResolverFactory(db, tables, filterCtx);
|
|
1950
2955
|
const cacheCtx = {
|
|
1951
2956
|
genericFilterCache: /* @__PURE__ */ new Map(),
|
|
1952
2957
|
objectTypeCache: /* @__PURE__ */ new Map(),
|
|
@@ -1954,8 +2959,11 @@ function generateSchemaData2(db, schema, relations, relationsDepthLimit, prefixe
|
|
|
1954
2959
|
fullyBuiltTables: /* @__PURE__ */ new Set(),
|
|
1955
2960
|
relationTypeCache: /* @__PURE__ */ new Map(),
|
|
1956
2961
|
orderTypeCache: /* @__PURE__ */ new WeakMap(),
|
|
1957
|
-
filterTypeCache: /* @__PURE__ */ new WeakMap()
|
|
2962
|
+
filterTypeCache: /* @__PURE__ */ new WeakMap(),
|
|
2963
|
+
listRelationFilterCache: /* @__PURE__ */ new Map(),
|
|
2964
|
+
aggregateTypeCache: /* @__PURE__ */ new Map()
|
|
1958
2965
|
};
|
|
2966
|
+
const relationAggregateFactory = features.relationAggregates ? createRelationAggregateFactory(db, tables, cacheCtx, typeNameMapper, filterCtx) : void 0;
|
|
1959
2967
|
const queries = {};
|
|
1960
2968
|
const mutations = {};
|
|
1961
2969
|
const gqlSchemaTypes = Object.fromEntries(
|
|
@@ -1971,7 +2979,8 @@ function generateSchemaData2(db, schema, relations, relationsDepthLimit, prefixe
|
|
|
1971
2979
|
typeNameMapper,
|
|
1972
2980
|
prefixes.insert,
|
|
1973
2981
|
prefixes.update,
|
|
1974
|
-
resolverFactory
|
|
2982
|
+
resolverFactory,
|
|
2983
|
+
relationAggregateFactory
|
|
1975
2984
|
)
|
|
1976
2985
|
])
|
|
1977
2986
|
);
|
|
@@ -1984,8 +2993,11 @@ function generateSchemaData2(db, schema, relations, relationsDepthLimit, prefixe
|
|
|
1984
2993
|
typeName,
|
|
1985
2994
|
listFieldName,
|
|
1986
2995
|
singleFieldName,
|
|
2996
|
+
aggregateFieldName,
|
|
1987
2997
|
createArrayFieldName,
|
|
1988
2998
|
createSingleFieldName,
|
|
2999
|
+
upsertArrayFieldName,
|
|
3000
|
+
upsertSingleFieldName,
|
|
1989
3001
|
updateFieldName,
|
|
1990
3002
|
deleteFieldName
|
|
1991
3003
|
} = computeResolverFieldNames(tableName, typeNameMapper, prefixes, suffixes);
|
|
@@ -1998,7 +3010,9 @@ function generateSchemaData2(db, schema, relations, relationsDepthLimit, prefixe
|
|
|
1998
3010
|
tableFilters,
|
|
1999
3011
|
listFieldName,
|
|
2000
3012
|
typeName,
|
|
2001
|
-
typeNameMapper
|
|
3013
|
+
typeNameMapper,
|
|
3014
|
+
filterCtx,
|
|
3015
|
+
features.distinct
|
|
2002
3016
|
);
|
|
2003
3017
|
const selectSingleGenerated = generateSelectSingle2(
|
|
2004
3018
|
db,
|
|
@@ -2009,9 +3023,10 @@ function generateSchemaData2(db, schema, relations, relationsDepthLimit, prefixe
|
|
|
2009
3023
|
tableFilters,
|
|
2010
3024
|
singleFieldName,
|
|
2011
3025
|
typeName,
|
|
2012
|
-
typeNameMapper
|
|
3026
|
+
typeNameMapper,
|
|
3027
|
+
filterCtx
|
|
2013
3028
|
);
|
|
2014
|
-
const insertArrGenerated = generateInsertArray2(
|
|
3029
|
+
const insertArrGenerated = features.insert ? generateInsertArray2(
|
|
2015
3030
|
db,
|
|
2016
3031
|
tableName,
|
|
2017
3032
|
schema[tableName],
|
|
@@ -2022,8 +3037,8 @@ function generateSchemaData2(db, schema, relations, relationsDepthLimit, prefixe
|
|
|
2022
3037
|
typeName,
|
|
2023
3038
|
typeNameMapper,
|
|
2024
3039
|
conflictDoNothing
|
|
2025
|
-
);
|
|
2026
|
-
const insertSingleGenerated = generateInsertSingle2(
|
|
3040
|
+
) : void 0;
|
|
3041
|
+
const insertSingleGenerated = features.insert ? generateInsertSingle2(
|
|
2027
3042
|
db,
|
|
2028
3043
|
tableName,
|
|
2029
3044
|
schema[tableName],
|
|
@@ -2034,8 +3049,46 @@ function generateSchemaData2(db, schema, relations, relationsDepthLimit, prefixe
|
|
|
2034
3049
|
typeName,
|
|
2035
3050
|
typeNameMapper,
|
|
2036
3051
|
conflictDoNothing
|
|
2037
|
-
);
|
|
2038
|
-
const
|
|
3052
|
+
) : void 0;
|
|
3053
|
+
const uniqueSets = features.upsert ? getUniqueColumnSets(schema[tableName], import_pg_core2.getTableConfig) : [];
|
|
3054
|
+
const onConflictInput = features.upsert ? generateOnConflictInput({
|
|
3055
|
+
table: schema[tableName],
|
|
3056
|
+
typeName,
|
|
3057
|
+
uniqueSets,
|
|
3058
|
+
tableFilters,
|
|
3059
|
+
withTarget: true
|
|
3060
|
+
}) : void 0;
|
|
3061
|
+
const upsertArrGenerated = onConflictInput ? generateUpsert2(
|
|
3062
|
+
db,
|
|
3063
|
+
tableName,
|
|
3064
|
+
schema[tableName],
|
|
3065
|
+
tables,
|
|
3066
|
+
eagerRelations,
|
|
3067
|
+
insertInput,
|
|
3068
|
+
onConflictInput,
|
|
3069
|
+
uniqueSets,
|
|
3070
|
+
upsertArrayFieldName,
|
|
3071
|
+
typeName,
|
|
3072
|
+
false,
|
|
3073
|
+
typeNameMapper,
|
|
3074
|
+
filterCtx
|
|
3075
|
+
) : void 0;
|
|
3076
|
+
const upsertSingleGenerated = onConflictInput ? generateUpsert2(
|
|
3077
|
+
db,
|
|
3078
|
+
tableName,
|
|
3079
|
+
schema[tableName],
|
|
3080
|
+
tables,
|
|
3081
|
+
eagerRelations,
|
|
3082
|
+
insertInput,
|
|
3083
|
+
onConflictInput,
|
|
3084
|
+
uniqueSets,
|
|
3085
|
+
upsertSingleFieldName,
|
|
3086
|
+
typeName,
|
|
3087
|
+
true,
|
|
3088
|
+
typeNameMapper,
|
|
3089
|
+
filterCtx
|
|
3090
|
+
) : void 0;
|
|
3091
|
+
const updateGenerated = features.update ? generateUpdate2(
|
|
2039
3092
|
db,
|
|
2040
3093
|
tableName,
|
|
2041
3094
|
schema[tableName],
|
|
@@ -2045,16 +3098,29 @@ function generateSchemaData2(db, schema, relations, relationsDepthLimit, prefixe
|
|
|
2045
3098
|
tableFilters,
|
|
2046
3099
|
updateFieldName,
|
|
2047
3100
|
typeName,
|
|
2048
|
-
typeNameMapper
|
|
2049
|
-
|
|
2050
|
-
|
|
3101
|
+
typeNameMapper,
|
|
3102
|
+
filterCtx
|
|
3103
|
+
) : void 0;
|
|
3104
|
+
const deleteGenerated = features.delete ? generateDelete2(
|
|
2051
3105
|
db,
|
|
2052
3106
|
tableName,
|
|
2053
3107
|
schema[tableName],
|
|
2054
3108
|
tableFilters,
|
|
2055
3109
|
deleteFieldName,
|
|
2056
|
-
typeName
|
|
2057
|
-
|
|
3110
|
+
typeName,
|
|
3111
|
+
filterCtx,
|
|
3112
|
+
{ tableName, relationMap: namedRelations, tables }
|
|
3113
|
+
) : void 0;
|
|
3114
|
+
const aggregateType = features.aggregates ? generateAggregateTypes(schema[tableName], tableName, typeName, cacheCtx) : void 0;
|
|
3115
|
+
const aggregateGenerated = features.aggregates ? generateAggregate(
|
|
3116
|
+
db,
|
|
3117
|
+
tableName,
|
|
3118
|
+
schema[tableName],
|
|
3119
|
+
typeName,
|
|
3120
|
+
aggregateFieldName,
|
|
3121
|
+
tableFilters,
|
|
3122
|
+
filterCtx
|
|
3123
|
+
) : void 0;
|
|
2058
3124
|
queries[selectArrGenerated.name] = {
|
|
2059
3125
|
type: selectArrOutput,
|
|
2060
3126
|
args: selectArrGenerated.args,
|
|
@@ -2065,37 +3131,77 @@ function generateSchemaData2(db, schema, relations, relationsDepthLimit, prefixe
|
|
|
2065
3131
|
args: selectSingleGenerated.args,
|
|
2066
3132
|
resolve: selectSingleGenerated.resolver
|
|
2067
3133
|
};
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
3134
|
+
if (aggregateGenerated && aggregateType) {
|
|
3135
|
+
queries[aggregateGenerated.name] = {
|
|
3136
|
+
type: new import_graphql7.GraphQLNonNull(aggregateType),
|
|
3137
|
+
args: aggregateGenerated.args,
|
|
3138
|
+
resolve: aggregateGenerated.resolver
|
|
3139
|
+
};
|
|
3140
|
+
}
|
|
3141
|
+
if (insertArrGenerated) {
|
|
3142
|
+
mutations[insertArrGenerated.name] = {
|
|
3143
|
+
type: arrTableItemOutput,
|
|
3144
|
+
args: insertArrGenerated.args,
|
|
3145
|
+
resolve: insertArrGenerated.resolver
|
|
3146
|
+
};
|
|
3147
|
+
}
|
|
3148
|
+
if (insertSingleGenerated) {
|
|
3149
|
+
mutations[insertSingleGenerated.name] = {
|
|
3150
|
+
type: singleTableItemOutput,
|
|
3151
|
+
args: insertSingleGenerated.args,
|
|
3152
|
+
resolve: insertSingleGenerated.resolver
|
|
3153
|
+
};
|
|
3154
|
+
}
|
|
3155
|
+
if (upsertArrGenerated) {
|
|
3156
|
+
mutations[upsertArrGenerated.name] = {
|
|
3157
|
+
type: arrTableItemOutput,
|
|
3158
|
+
args: upsertArrGenerated.args,
|
|
3159
|
+
resolve: upsertArrGenerated.resolver
|
|
3160
|
+
};
|
|
3161
|
+
}
|
|
3162
|
+
if (upsertSingleGenerated) {
|
|
3163
|
+
mutations[upsertSingleGenerated.name] = {
|
|
3164
|
+
type: singleTableItemOutput,
|
|
3165
|
+
args: upsertSingleGenerated.args,
|
|
3166
|
+
resolve: upsertSingleGenerated.resolver
|
|
3167
|
+
};
|
|
3168
|
+
}
|
|
3169
|
+
if (updateGenerated) {
|
|
3170
|
+
mutations[updateGenerated.name] = {
|
|
3171
|
+
type: arrTableItemOutput,
|
|
3172
|
+
args: updateGenerated.args,
|
|
3173
|
+
resolve: updateGenerated.resolver
|
|
3174
|
+
};
|
|
3175
|
+
}
|
|
3176
|
+
if (deleteGenerated) {
|
|
3177
|
+
mutations[deleteGenerated.name] = {
|
|
3178
|
+
type: arrTableItemOutput,
|
|
3179
|
+
args: deleteGenerated.args,
|
|
3180
|
+
resolve: deleteGenerated.resolver
|
|
3181
|
+
};
|
|
3182
|
+
}
|
|
3183
|
+
const activeInputs = [
|
|
3184
|
+
// The insert input types the upsert mutations too, so either feature keeps it.
|
|
3185
|
+
...features.insert || onConflictInput ? [insertInput] : [],
|
|
3186
|
+
...onConflictInput ? [onConflictInput] : [],
|
|
3187
|
+
...features.update ? [updateInput] : [],
|
|
3188
|
+
tableFilters,
|
|
3189
|
+
tableOrder
|
|
3190
|
+
];
|
|
3191
|
+
activeInputs.forEach((e) => {
|
|
2089
3192
|
inputs[e.name] = e;
|
|
2090
3193
|
});
|
|
2091
3194
|
outputs[selectSingleOutput.name] = selectSingleOutput;
|
|
2092
3195
|
outputs[singleTableItemOutput.name] = singleTableItemOutput;
|
|
3196
|
+
if (aggregateType) {
|
|
3197
|
+
outputs[aggregateType.name] = aggregateType;
|
|
3198
|
+
}
|
|
2093
3199
|
}
|
|
2094
3200
|
const fieldResolvers = {};
|
|
2095
3201
|
for (const [tableName, tableRelations] of Object.entries(namedRelations)) {
|
|
2096
3202
|
const relResolvers = {};
|
|
2097
3203
|
for (const [relName, relEntry] of Object.entries(tableRelations)) {
|
|
2098
|
-
const isOne = (0,
|
|
3204
|
+
const isOne = (0, import_drizzle_orm6.is)(relEntry.relation ?? relEntry, import_drizzle_orm6.One);
|
|
2099
3205
|
const resolver = resolverFactory({ tableName, relationName: relName, relEntry, isOne });
|
|
2100
3206
|
if (resolver) {
|
|
2101
3207
|
relResolvers[relName] = resolver;
|
|
@@ -2109,26 +3215,32 @@ function generateSchemaData2(db, schema, relations, relationsDepthLimit, prefixe
|
|
|
2109
3215
|
}
|
|
2110
3216
|
|
|
2111
3217
|
// src/util/builders/sqlite.ts
|
|
2112
|
-
var
|
|
3218
|
+
var import_drizzle_orm7 = require("drizzle-orm");
|
|
2113
3219
|
var import_sqlite_core2 = require("drizzle-orm/sqlite-core");
|
|
2114
|
-
var
|
|
2115
|
-
var
|
|
2116
|
-
var generateSelectArray3 = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper) => {
|
|
3220
|
+
var import_graphql8 = require("graphql");
|
|
3221
|
+
var import_graphql_parse_resolve_info4 = require("graphql-parse-resolve-info");
|
|
3222
|
+
var generateSelectArray3 = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper, filterCtx, distinctEnabled = true) => {
|
|
2117
3223
|
const queryBase = db.query[tableName];
|
|
2118
3224
|
if (!queryBase) {
|
|
2119
3225
|
throw new Error(
|
|
2120
3226
|
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
2121
3227
|
);
|
|
2122
3228
|
}
|
|
2123
|
-
const queryArgs = selectArrayArgs(orderArgs, filterArgs);
|
|
2124
3229
|
const table = tables[tableName];
|
|
3230
|
+
const pkNames = sqlitePrimaryKeyPropNames(table);
|
|
3231
|
+
const queryArgs = selectArrayArgs(
|
|
3232
|
+
orderArgs,
|
|
3233
|
+
filterArgs,
|
|
3234
|
+
distinctEnabled ? generateDistinctEnum(table, typeName) : void 0
|
|
3235
|
+
);
|
|
2125
3236
|
return {
|
|
2126
3237
|
name: fieldName,
|
|
2127
|
-
resolver: async (_source, args,
|
|
3238
|
+
resolver: async (_source, args, context, info) => {
|
|
2128
3239
|
try {
|
|
2129
|
-
const parsedInfo = (0,
|
|
3240
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, { deep: true });
|
|
3241
|
+
const { executor, queryBase: requestQueryBase } = resolveQueryExecutor(db, context, tableName, queryBase);
|
|
2130
3242
|
return await runRelationalSelect({
|
|
2131
|
-
queryBase,
|
|
3243
|
+
queryBase: requestQueryBase,
|
|
2132
3244
|
tables,
|
|
2133
3245
|
tableName,
|
|
2134
3246
|
table,
|
|
@@ -2137,7 +3249,10 @@ var generateSelectArray3 = (db, tableName, tables, relationMap, orderArgs, filte
|
|
|
2137
3249
|
typeNameMapper,
|
|
2138
3250
|
parsedInfo,
|
|
2139
3251
|
...args,
|
|
2140
|
-
single: false
|
|
3252
|
+
single: false,
|
|
3253
|
+
filterCtx,
|
|
3254
|
+
pkNames,
|
|
3255
|
+
db: executor
|
|
2141
3256
|
});
|
|
2142
3257
|
} catch (e) {
|
|
2143
3258
|
throw toGraphQLError(e);
|
|
@@ -2146,7 +3261,7 @@ var generateSelectArray3 = (db, tableName, tables, relationMap, orderArgs, filte
|
|
|
2146
3261
|
args: queryArgs
|
|
2147
3262
|
};
|
|
2148
3263
|
};
|
|
2149
|
-
var generateSelectSingle3 = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper) => {
|
|
3264
|
+
var generateSelectSingle3 = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper, filterCtx) => {
|
|
2150
3265
|
const queryBase = db.query[tableName];
|
|
2151
3266
|
if (!queryBase) {
|
|
2152
3267
|
throw new Error(
|
|
@@ -2155,13 +3270,15 @@ var generateSelectSingle3 = (db, tableName, tables, relationMap, orderArgs, filt
|
|
|
2155
3270
|
}
|
|
2156
3271
|
const queryArgs = selectSingleArgs(orderArgs, filterArgs);
|
|
2157
3272
|
const table = tables[tableName];
|
|
3273
|
+
const pkNames = sqlitePrimaryKeyPropNames(table);
|
|
2158
3274
|
return {
|
|
2159
3275
|
name: fieldName,
|
|
2160
|
-
resolver: async (_source, args,
|
|
3276
|
+
resolver: async (_source, args, context, info) => {
|
|
2161
3277
|
try {
|
|
2162
|
-
const parsedInfo = (0,
|
|
3278
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, { deep: true });
|
|
3279
|
+
const { executor, queryBase: requestQueryBase } = resolveQueryExecutor(db, context, tableName, queryBase);
|
|
2163
3280
|
return await runRelationalSelect({
|
|
2164
|
-
queryBase,
|
|
3281
|
+
queryBase: requestQueryBase,
|
|
2165
3282
|
tables,
|
|
2166
3283
|
tableName,
|
|
2167
3284
|
table,
|
|
@@ -2170,7 +3287,10 @@ var generateSelectSingle3 = (db, tableName, tables, relationMap, orderArgs, filt
|
|
|
2170
3287
|
typeNameMapper,
|
|
2171
3288
|
parsedInfo,
|
|
2172
3289
|
...args,
|
|
2173
|
-
single: true
|
|
3290
|
+
single: true,
|
|
3291
|
+
filterCtx,
|
|
3292
|
+
pkNames,
|
|
3293
|
+
db: executor
|
|
2174
3294
|
});
|
|
2175
3295
|
} catch (e) {
|
|
2176
3296
|
throw toGraphQLError(e);
|
|
@@ -2180,22 +3300,22 @@ var generateSelectSingle3 = (db, tableName, tables, relationMap, orderArgs, filt
|
|
|
2180
3300
|
};
|
|
2181
3301
|
};
|
|
2182
3302
|
var sqlitePrimaryKeyPropNames = (table) => getPrimaryKeyPropNamesFromConfig(table, import_sqlite_core2.getTableConfig);
|
|
2183
|
-
var generateInsertArray3 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper) => {
|
|
3303
|
+
var generateInsertArray3 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false) => {
|
|
2184
3304
|
const queryArgs = {
|
|
2185
3305
|
values: {
|
|
2186
|
-
type: new
|
|
3306
|
+
type: new import_graphql8.GraphQLNonNull(new import_graphql8.GraphQLList(new import_graphql8.GraphQLNonNull(baseType)))
|
|
2187
3307
|
}
|
|
2188
3308
|
};
|
|
2189
3309
|
const pkNames = sqlitePrimaryKeyPropNames(table);
|
|
2190
3310
|
return {
|
|
2191
3311
|
name: fieldName,
|
|
2192
|
-
resolver: async (_source, args,
|
|
3312
|
+
resolver: async (_source, args, context, info) => {
|
|
2193
3313
|
try {
|
|
2194
3314
|
const input = remapFromGraphQLArrayInput(args.values, table);
|
|
2195
3315
|
if (!input.length) {
|
|
2196
|
-
throw new
|
|
3316
|
+
throw new import_graphql8.GraphQLError("No values were provided!");
|
|
2197
3317
|
}
|
|
2198
|
-
const parsedInfo = (0,
|
|
3318
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
|
|
2199
3319
|
deep: true
|
|
2200
3320
|
});
|
|
2201
3321
|
const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
|
|
@@ -2208,8 +3328,13 @@ var generateInsertArray3 = (db, tableName, table, tables, relationMap, baseType,
|
|
|
2208
3328
|
pkNames,
|
|
2209
3329
|
parsedInfo
|
|
2210
3330
|
});
|
|
2211
|
-
const
|
|
2212
|
-
|
|
3331
|
+
const executor = resolveExecutor(db, context);
|
|
3332
|
+
let query = executor.insert(table).values(input).returning(columns);
|
|
3333
|
+
if (conflictDoNothing) {
|
|
3334
|
+
query = query.onConflictDoNothing();
|
|
3335
|
+
}
|
|
3336
|
+
const result = await query;
|
|
3337
|
+
const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
|
|
2213
3338
|
return remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
|
|
2214
3339
|
} catch (e) {
|
|
2215
3340
|
throw toGraphQLError(e);
|
|
@@ -2218,19 +3343,19 @@ var generateInsertArray3 = (db, tableName, table, tables, relationMap, baseType,
|
|
|
2218
3343
|
args: queryArgs
|
|
2219
3344
|
};
|
|
2220
3345
|
};
|
|
2221
|
-
var generateInsertSingle3 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper) => {
|
|
3346
|
+
var generateInsertSingle3 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false) => {
|
|
2222
3347
|
const queryArgs = {
|
|
2223
3348
|
values: {
|
|
2224
|
-
type: new
|
|
3349
|
+
type: new import_graphql8.GraphQLNonNull(baseType)
|
|
2225
3350
|
}
|
|
2226
3351
|
};
|
|
2227
3352
|
const pkNames = sqlitePrimaryKeyPropNames(table);
|
|
2228
3353
|
return {
|
|
2229
3354
|
name: fieldName,
|
|
2230
|
-
resolver: async (_source, args,
|
|
3355
|
+
resolver: async (_source, args, context, info) => {
|
|
2231
3356
|
try {
|
|
2232
3357
|
const input = remapFromGraphQLSingleInput(args.values, table);
|
|
2233
|
-
const parsedInfo = (0,
|
|
3358
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
|
|
2234
3359
|
deep: true
|
|
2235
3360
|
});
|
|
2236
3361
|
const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
|
|
@@ -2243,11 +3368,16 @@ var generateInsertSingle3 = (db, tableName, table, tables, relationMap, baseType
|
|
|
2243
3368
|
pkNames,
|
|
2244
3369
|
parsedInfo
|
|
2245
3370
|
});
|
|
2246
|
-
const
|
|
3371
|
+
const executor = resolveExecutor(db, context);
|
|
3372
|
+
let query = executor.insert(table).values(input).returning(columns);
|
|
3373
|
+
if (conflictDoNothing) {
|
|
3374
|
+
query = query.onConflictDoNothing();
|
|
3375
|
+
}
|
|
3376
|
+
const result = await query;
|
|
2247
3377
|
if (!result[0]) {
|
|
2248
3378
|
return void 0;
|
|
2249
3379
|
}
|
|
2250
|
-
const enriched = hasRelations ? await eagerLoadMutationRelations(
|
|
3380
|
+
const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
|
|
2251
3381
|
return remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap);
|
|
2252
3382
|
} catch (e) {
|
|
2253
3383
|
throw toGraphQLError(e);
|
|
@@ -2256,10 +3386,66 @@ var generateInsertSingle3 = (db, tableName, table, tables, relationMap, baseType
|
|
|
2256
3386
|
args: queryArgs
|
|
2257
3387
|
};
|
|
2258
3388
|
};
|
|
2259
|
-
var
|
|
3389
|
+
var generateUpsert3 = (db, tableName, table, tables, relationMap, baseType, onConflictType, uniqueSets, fieldName, typeName, single, typeNameMapper, filterCtx) => {
|
|
3390
|
+
const queryArgs = {
|
|
3391
|
+
values: {
|
|
3392
|
+
type: single ? new import_graphql8.GraphQLNonNull(baseType) : new import_graphql8.GraphQLNonNull(new import_graphql8.GraphQLList(new import_graphql8.GraphQLNonNull(baseType)))
|
|
3393
|
+
},
|
|
3394
|
+
onConflict: {
|
|
3395
|
+
type: onConflictType,
|
|
3396
|
+
description: "How a conflicting row is resolved. Defaults to overwriting it on the primary key."
|
|
3397
|
+
}
|
|
3398
|
+
};
|
|
3399
|
+
const pkNames = sqlitePrimaryKeyPropNames(table);
|
|
3400
|
+
return {
|
|
3401
|
+
name: fieldName,
|
|
3402
|
+
resolver: async (_source, args, context, info) => {
|
|
3403
|
+
try {
|
|
3404
|
+
const input = single ? [remapFromGraphQLSingleInput(args.values, table)] : remapFromGraphQLArrayInput(args.values, table);
|
|
3405
|
+
if (!input.length) {
|
|
3406
|
+
throw new import_graphql8.GraphQLError("No values were provided!");
|
|
3407
|
+
}
|
|
3408
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, { deep: true });
|
|
3409
|
+
const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
|
|
3410
|
+
relationMap,
|
|
3411
|
+
tables,
|
|
3412
|
+
tableName,
|
|
3413
|
+
typeName,
|
|
3414
|
+
typeNameMapper,
|
|
3415
|
+
table,
|
|
3416
|
+
pkNames,
|
|
3417
|
+
parsedInfo
|
|
3418
|
+
});
|
|
3419
|
+
const plan = resolveConflictPlan({
|
|
3420
|
+
table,
|
|
3421
|
+
values: input,
|
|
3422
|
+
onConflict: args.onConflict,
|
|
3423
|
+
pkNames,
|
|
3424
|
+
uniqueSets,
|
|
3425
|
+
excludedRef: excludedColumnRef,
|
|
3426
|
+
withTarget: true,
|
|
3427
|
+
buildWhere: (where) => extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName))
|
|
3428
|
+
});
|
|
3429
|
+
const executor = resolveExecutor(db, context);
|
|
3430
|
+
let query = executor.insert(table).values(input).returning(columns);
|
|
3431
|
+
query = plan.action === "NOTHING" ? query.onConflictDoNothing(plan.target ? { target: plan.target } : void 0) : query.onConflictDoUpdate({ target: plan.target, set: plan.set, setWhere: plan.setWhere });
|
|
3432
|
+
const result = await query;
|
|
3433
|
+
if (single && !result[0]) {
|
|
3434
|
+
return void 0;
|
|
3435
|
+
}
|
|
3436
|
+
const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
|
|
3437
|
+
return single ? remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap) : remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
|
|
3438
|
+
} catch (e) {
|
|
3439
|
+
throw toGraphQLError(e);
|
|
3440
|
+
}
|
|
3441
|
+
},
|
|
3442
|
+
args: queryArgs
|
|
3443
|
+
};
|
|
3444
|
+
};
|
|
3445
|
+
var generateUpdate3 = (db, tableName, table, tables, relationMap, setArgs, filterArgs, fieldName, typeName, typeNameMapper, filterCtx) => {
|
|
2260
3446
|
const queryArgs = {
|
|
2261
3447
|
set: {
|
|
2262
|
-
type: new
|
|
3448
|
+
type: new import_graphql8.GraphQLNonNull(setArgs)
|
|
2263
3449
|
},
|
|
2264
3450
|
where: {
|
|
2265
3451
|
type: filterArgs
|
|
@@ -2268,10 +3454,10 @@ var generateUpdate3 = (db, tableName, table, tables, relationMap, setArgs, filte
|
|
|
2268
3454
|
const pkNames = sqlitePrimaryKeyPropNames(table);
|
|
2269
3455
|
return {
|
|
2270
3456
|
name: fieldName,
|
|
2271
|
-
resolver: async (_source, args,
|
|
3457
|
+
resolver: async (_source, args, context, info) => {
|
|
2272
3458
|
try {
|
|
2273
3459
|
const { where, set } = args;
|
|
2274
|
-
const parsedInfo = (0,
|
|
3460
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
|
|
2275
3461
|
deep: true
|
|
2276
3462
|
});
|
|
2277
3463
|
const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
|
|
@@ -2286,16 +3472,17 @@ var generateUpdate3 = (db, tableName, table, tables, relationMap, setArgs, filte
|
|
|
2286
3472
|
});
|
|
2287
3473
|
const input = remapFromGraphQLSingleInput(set, table);
|
|
2288
3474
|
if (!Object.keys(input).length) {
|
|
2289
|
-
throw new
|
|
3475
|
+
throw new import_graphql8.GraphQLError("Unable to update with no values specified!");
|
|
2290
3476
|
}
|
|
2291
|
-
|
|
3477
|
+
const executor = resolveExecutor(db, context);
|
|
3478
|
+
let query = executor.update(table).set(input);
|
|
2292
3479
|
if (where) {
|
|
2293
|
-
const filters = extractFilters(table, tableName, where);
|
|
3480
|
+
const filters = extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName));
|
|
2294
3481
|
query = query.where(filters);
|
|
2295
3482
|
}
|
|
2296
3483
|
query = query.returning(columns);
|
|
2297
3484
|
const result = await query;
|
|
2298
|
-
const enriched = hasRelations ? await eagerLoadMutationRelations(
|
|
3485
|
+
const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
|
|
2299
3486
|
return remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
|
|
2300
3487
|
} catch (e) {
|
|
2301
3488
|
throw toGraphQLError(e);
|
|
@@ -2304,7 +3491,7 @@ var generateUpdate3 = (db, tableName, table, tables, relationMap, setArgs, filte
|
|
|
2304
3491
|
args: queryArgs
|
|
2305
3492
|
};
|
|
2306
3493
|
};
|
|
2307
|
-
var generateDelete3 = (db, tableName, table, filterArgs, fieldName, typeName) => {
|
|
3494
|
+
var generateDelete3 = (db, tableName, table, filterArgs, fieldName, typeName, filterCtx, selectionCtx) => {
|
|
2308
3495
|
const queryArgs = {
|
|
2309
3496
|
where: {
|
|
2310
3497
|
type: filterArgs
|
|
@@ -2312,19 +3499,21 @@ var generateDelete3 = (db, tableName, table, filterArgs, fieldName, typeName) =>
|
|
|
2312
3499
|
};
|
|
2313
3500
|
return {
|
|
2314
3501
|
name: fieldName,
|
|
2315
|
-
resolver: async (_source, args,
|
|
3502
|
+
resolver: async (_source, args, context, info) => {
|
|
2316
3503
|
try {
|
|
2317
3504
|
const { where } = args;
|
|
2318
|
-
const parsedInfo = (0,
|
|
3505
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
|
|
2319
3506
|
deep: true
|
|
2320
3507
|
});
|
|
2321
3508
|
const columns = extractSelectedColumnsFromTreeSQLFormat(
|
|
2322
3509
|
parsedInfo.fieldsByTypeName[typeName],
|
|
2323
|
-
table
|
|
3510
|
+
table,
|
|
3511
|
+
selectionCtx
|
|
2324
3512
|
);
|
|
2325
|
-
|
|
3513
|
+
const executor = resolveExecutor(db, context);
|
|
3514
|
+
let query = executor.delete(table);
|
|
2326
3515
|
if (where) {
|
|
2327
|
-
const filters = extractFilters(table, tableName, where);
|
|
3516
|
+
const filters = extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName));
|
|
2328
3517
|
query = query.where(filters);
|
|
2329
3518
|
}
|
|
2330
3519
|
query = query.returning(columns);
|
|
@@ -2337,10 +3526,11 @@ var generateDelete3 = (db, tableName, table, filterArgs, fieldName, typeName) =>
|
|
|
2337
3526
|
args: queryArgs
|
|
2338
3527
|
};
|
|
2339
3528
|
};
|
|
2340
|
-
var generateSchemaData3 = (db, schema, relations,
|
|
3529
|
+
var generateSchemaData3 = (db, schema, relations, options) => {
|
|
3530
|
+
const { relationsDepthLimit, prefixes, suffixes, conflictDoNothing, typeNameMapper, shouldEagerLoad, features } = options;
|
|
2341
3531
|
const rawSchema = schema;
|
|
2342
3532
|
const schemaEntries = Object.entries(rawSchema);
|
|
2343
|
-
const tableEntries = schemaEntries.filter(([_key, value]) => (0,
|
|
3533
|
+
const tableEntries = schemaEntries.filter(([_key, value]) => (0, import_drizzle_orm7.is)(value, import_sqlite_core2.SQLiteTable));
|
|
2344
3534
|
const tables = Object.fromEntries(tableEntries);
|
|
2345
3535
|
if (!tableEntries.length) {
|
|
2346
3536
|
throw new Error(
|
|
@@ -2350,7 +3540,8 @@ var generateSchemaData3 = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
2350
3540
|
const namedRelations = buildNamedRelations(relations ?? {}, tableEntries);
|
|
2351
3541
|
attachTargetPrimaryKeys(namedRelations, tables, sqlitePrimaryKeyPropNames);
|
|
2352
3542
|
const eagerRelations = pruneNonEagerRelations(namedRelations, shouldEagerLoad);
|
|
2353
|
-
const
|
|
3543
|
+
const filterCtx = { tables, relationMap: namedRelations };
|
|
3544
|
+
const resolverFactory = createRelationResolverFactory(db, tables, filterCtx);
|
|
2354
3545
|
const cacheCtx = {
|
|
2355
3546
|
genericFilterCache: /* @__PURE__ */ new Map(),
|
|
2356
3547
|
objectTypeCache: /* @__PURE__ */ new Map(),
|
|
@@ -2358,8 +3549,11 @@ var generateSchemaData3 = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
2358
3549
|
fullyBuiltTables: /* @__PURE__ */ new Set(),
|
|
2359
3550
|
relationTypeCache: /* @__PURE__ */ new Map(),
|
|
2360
3551
|
orderTypeCache: /* @__PURE__ */ new WeakMap(),
|
|
2361
|
-
filterTypeCache: /* @__PURE__ */ new WeakMap()
|
|
3552
|
+
filterTypeCache: /* @__PURE__ */ new WeakMap(),
|
|
3553
|
+
listRelationFilterCache: /* @__PURE__ */ new Map(),
|
|
3554
|
+
aggregateTypeCache: /* @__PURE__ */ new Map()
|
|
2362
3555
|
};
|
|
3556
|
+
const relationAggregateFactory = features.relationAggregates ? createRelationAggregateFactory(db, tables, cacheCtx, typeNameMapper, filterCtx) : void 0;
|
|
2363
3557
|
const queries = {};
|
|
2364
3558
|
const mutations = {};
|
|
2365
3559
|
const gqlSchemaTypes = Object.fromEntries(
|
|
@@ -2375,7 +3569,8 @@ var generateSchemaData3 = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
2375
3569
|
typeNameMapper,
|
|
2376
3570
|
prefixes.insert,
|
|
2377
3571
|
prefixes.update,
|
|
2378
|
-
resolverFactory
|
|
3572
|
+
resolverFactory,
|
|
3573
|
+
relationAggregateFactory
|
|
2379
3574
|
)
|
|
2380
3575
|
])
|
|
2381
3576
|
);
|
|
@@ -2388,8 +3583,11 @@ var generateSchemaData3 = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
2388
3583
|
typeName,
|
|
2389
3584
|
listFieldName,
|
|
2390
3585
|
singleFieldName,
|
|
3586
|
+
aggregateFieldName,
|
|
2391
3587
|
createArrayFieldName,
|
|
2392
3588
|
createSingleFieldName,
|
|
3589
|
+
upsertArrayFieldName,
|
|
3590
|
+
upsertSingleFieldName,
|
|
2393
3591
|
updateFieldName,
|
|
2394
3592
|
deleteFieldName
|
|
2395
3593
|
} = computeResolverFieldNames(tableName, typeNameMapper, prefixes, suffixes);
|
|
@@ -2402,7 +3600,9 @@ var generateSchemaData3 = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
2402
3600
|
tableFilters,
|
|
2403
3601
|
listFieldName,
|
|
2404
3602
|
typeName,
|
|
2405
|
-
typeNameMapper
|
|
3603
|
+
typeNameMapper,
|
|
3604
|
+
filterCtx,
|
|
3605
|
+
features.distinct
|
|
2406
3606
|
);
|
|
2407
3607
|
const selectSingleGenerated = generateSelectSingle3(
|
|
2408
3608
|
db,
|
|
@@ -2413,9 +3613,10 @@ var generateSchemaData3 = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
2413
3613
|
tableFilters,
|
|
2414
3614
|
singleFieldName,
|
|
2415
3615
|
typeName,
|
|
2416
|
-
typeNameMapper
|
|
3616
|
+
typeNameMapper,
|
|
3617
|
+
filterCtx
|
|
2417
3618
|
);
|
|
2418
|
-
const insertArrGenerated = generateInsertArray3(
|
|
3619
|
+
const insertArrGenerated = features.insert ? generateInsertArray3(
|
|
2419
3620
|
db,
|
|
2420
3621
|
tableName,
|
|
2421
3622
|
schema[tableName],
|
|
@@ -2424,9 +3625,10 @@ var generateSchemaData3 = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
2424
3625
|
insertInput,
|
|
2425
3626
|
createArrayFieldName,
|
|
2426
3627
|
typeName,
|
|
2427
|
-
typeNameMapper
|
|
2428
|
-
|
|
2429
|
-
|
|
3628
|
+
typeNameMapper,
|
|
3629
|
+
conflictDoNothing
|
|
3630
|
+
) : void 0;
|
|
3631
|
+
const insertSingleGenerated = features.insert ? generateInsertSingle3(
|
|
2430
3632
|
db,
|
|
2431
3633
|
tableName,
|
|
2432
3634
|
schema[tableName],
|
|
@@ -2435,9 +3637,48 @@ var generateSchemaData3 = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
2435
3637
|
insertInput,
|
|
2436
3638
|
createSingleFieldName,
|
|
2437
3639
|
typeName,
|
|
2438
|
-
typeNameMapper
|
|
2439
|
-
|
|
2440
|
-
|
|
3640
|
+
typeNameMapper,
|
|
3641
|
+
conflictDoNothing
|
|
3642
|
+
) : void 0;
|
|
3643
|
+
const uniqueSets = features.upsert ? getUniqueColumnSets(schema[tableName], import_sqlite_core2.getTableConfig) : [];
|
|
3644
|
+
const onConflictInput = features.upsert ? generateOnConflictInput({
|
|
3645
|
+
table: schema[tableName],
|
|
3646
|
+
typeName,
|
|
3647
|
+
uniqueSets,
|
|
3648
|
+
tableFilters,
|
|
3649
|
+
withTarget: true
|
|
3650
|
+
}) : void 0;
|
|
3651
|
+
const upsertArrGenerated = onConflictInput ? generateUpsert3(
|
|
3652
|
+
db,
|
|
3653
|
+
tableName,
|
|
3654
|
+
schema[tableName],
|
|
3655
|
+
tables,
|
|
3656
|
+
eagerRelations,
|
|
3657
|
+
insertInput,
|
|
3658
|
+
onConflictInput,
|
|
3659
|
+
uniqueSets,
|
|
3660
|
+
upsertArrayFieldName,
|
|
3661
|
+
typeName,
|
|
3662
|
+
false,
|
|
3663
|
+
typeNameMapper,
|
|
3664
|
+
filterCtx
|
|
3665
|
+
) : void 0;
|
|
3666
|
+
const upsertSingleGenerated = onConflictInput ? generateUpsert3(
|
|
3667
|
+
db,
|
|
3668
|
+
tableName,
|
|
3669
|
+
schema[tableName],
|
|
3670
|
+
tables,
|
|
3671
|
+
eagerRelations,
|
|
3672
|
+
insertInput,
|
|
3673
|
+
onConflictInput,
|
|
3674
|
+
uniqueSets,
|
|
3675
|
+
upsertSingleFieldName,
|
|
3676
|
+
typeName,
|
|
3677
|
+
true,
|
|
3678
|
+
typeNameMapper,
|
|
3679
|
+
filterCtx
|
|
3680
|
+
) : void 0;
|
|
3681
|
+
const updateGenerated = features.update ? generateUpdate3(
|
|
2441
3682
|
db,
|
|
2442
3683
|
tableName,
|
|
2443
3684
|
schema[tableName],
|
|
@@ -2447,16 +3688,29 @@ var generateSchemaData3 = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
2447
3688
|
tableFilters,
|
|
2448
3689
|
updateFieldName,
|
|
2449
3690
|
typeName,
|
|
2450
|
-
typeNameMapper
|
|
2451
|
-
|
|
2452
|
-
|
|
3691
|
+
typeNameMapper,
|
|
3692
|
+
filterCtx
|
|
3693
|
+
) : void 0;
|
|
3694
|
+
const deleteGenerated = features.delete ? generateDelete3(
|
|
2453
3695
|
db,
|
|
2454
3696
|
tableName,
|
|
2455
3697
|
schema[tableName],
|
|
2456
3698
|
tableFilters,
|
|
2457
3699
|
deleteFieldName,
|
|
2458
|
-
typeName
|
|
2459
|
-
|
|
3700
|
+
typeName,
|
|
3701
|
+
filterCtx,
|
|
3702
|
+
{ tableName, relationMap: namedRelations, tables }
|
|
3703
|
+
) : void 0;
|
|
3704
|
+
const aggregateType = features.aggregates ? generateAggregateTypes(schema[tableName], tableName, typeName, cacheCtx) : void 0;
|
|
3705
|
+
const aggregateGenerated = features.aggregates ? generateAggregate(
|
|
3706
|
+
db,
|
|
3707
|
+
tableName,
|
|
3708
|
+
schema[tableName],
|
|
3709
|
+
typeName,
|
|
3710
|
+
aggregateFieldName,
|
|
3711
|
+
tableFilters,
|
|
3712
|
+
filterCtx
|
|
3713
|
+
) : void 0;
|
|
2460
3714
|
queries[selectArrGenerated.name] = {
|
|
2461
3715
|
type: selectArrOutput,
|
|
2462
3716
|
args: selectArrGenerated.args,
|
|
@@ -2467,37 +3721,77 @@ var generateSchemaData3 = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
2467
3721
|
args: selectSingleGenerated.args,
|
|
2468
3722
|
resolve: selectSingleGenerated.resolver
|
|
2469
3723
|
};
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
3724
|
+
if (aggregateGenerated && aggregateType) {
|
|
3725
|
+
queries[aggregateGenerated.name] = {
|
|
3726
|
+
type: new import_graphql8.GraphQLNonNull(aggregateType),
|
|
3727
|
+
args: aggregateGenerated.args,
|
|
3728
|
+
resolve: aggregateGenerated.resolver
|
|
3729
|
+
};
|
|
3730
|
+
}
|
|
3731
|
+
if (insertArrGenerated) {
|
|
3732
|
+
mutations[insertArrGenerated.name] = {
|
|
3733
|
+
type: arrTableItemOutput,
|
|
3734
|
+
args: insertArrGenerated.args,
|
|
3735
|
+
resolve: insertArrGenerated.resolver
|
|
3736
|
+
};
|
|
3737
|
+
}
|
|
3738
|
+
if (insertSingleGenerated) {
|
|
3739
|
+
mutations[insertSingleGenerated.name] = {
|
|
3740
|
+
type: singleTableItemOutput,
|
|
3741
|
+
args: insertSingleGenerated.args,
|
|
3742
|
+
resolve: insertSingleGenerated.resolver
|
|
3743
|
+
};
|
|
3744
|
+
}
|
|
3745
|
+
if (upsertArrGenerated) {
|
|
3746
|
+
mutations[upsertArrGenerated.name] = {
|
|
3747
|
+
type: arrTableItemOutput,
|
|
3748
|
+
args: upsertArrGenerated.args,
|
|
3749
|
+
resolve: upsertArrGenerated.resolver
|
|
3750
|
+
};
|
|
3751
|
+
}
|
|
3752
|
+
if (upsertSingleGenerated) {
|
|
3753
|
+
mutations[upsertSingleGenerated.name] = {
|
|
3754
|
+
type: singleTableItemOutput,
|
|
3755
|
+
args: upsertSingleGenerated.args,
|
|
3756
|
+
resolve: upsertSingleGenerated.resolver
|
|
3757
|
+
};
|
|
3758
|
+
}
|
|
3759
|
+
if (updateGenerated) {
|
|
3760
|
+
mutations[updateGenerated.name] = {
|
|
3761
|
+
type: arrTableItemOutput,
|
|
3762
|
+
args: updateGenerated.args,
|
|
3763
|
+
resolve: updateGenerated.resolver
|
|
3764
|
+
};
|
|
3765
|
+
}
|
|
3766
|
+
if (deleteGenerated) {
|
|
3767
|
+
mutations[deleteGenerated.name] = {
|
|
3768
|
+
type: arrTableItemOutput,
|
|
3769
|
+
args: deleteGenerated.args,
|
|
3770
|
+
resolve: deleteGenerated.resolver
|
|
3771
|
+
};
|
|
3772
|
+
}
|
|
3773
|
+
const activeInputs = [
|
|
3774
|
+
// The insert input types the upsert mutations too, so either feature keeps it.
|
|
3775
|
+
...features.insert || onConflictInput ? [insertInput] : [],
|
|
3776
|
+
...onConflictInput ? [onConflictInput] : [],
|
|
3777
|
+
...features.update ? [updateInput] : [],
|
|
3778
|
+
tableFilters,
|
|
3779
|
+
tableOrder
|
|
3780
|
+
];
|
|
3781
|
+
activeInputs.forEach((e) => {
|
|
2491
3782
|
inputs[e.name] = e;
|
|
2492
3783
|
});
|
|
2493
3784
|
outputs[selectSingleOutput.name] = selectSingleOutput;
|
|
2494
3785
|
outputs[singleTableItemOutput.name] = singleTableItemOutput;
|
|
3786
|
+
if (aggregateType) {
|
|
3787
|
+
outputs[aggregateType.name] = aggregateType;
|
|
3788
|
+
}
|
|
2495
3789
|
}
|
|
2496
3790
|
const fieldResolvers = {};
|
|
2497
3791
|
for (const [tableName, tableRelations] of Object.entries(namedRelations)) {
|
|
2498
3792
|
const relResolvers = {};
|
|
2499
3793
|
for (const [relName, relEntry] of Object.entries(tableRelations)) {
|
|
2500
|
-
const isOne = (0,
|
|
3794
|
+
const isOne = (0, import_drizzle_orm7.is)(relEntry.relation ?? relEntry, import_drizzle_orm7.One);
|
|
2501
3795
|
const resolver = resolverFactory({ tableName, relationName: relName, relEntry, isOne });
|
|
2502
3796
|
if (resolver) {
|
|
2503
3797
|
relResolvers[relName] = resolver;
|
|
@@ -2524,13 +3818,23 @@ var buildSchema = (db, config) => {
|
|
|
2524
3818
|
const prefixes = {
|
|
2525
3819
|
insert: config?.prefixes?.insert ?? "create",
|
|
2526
3820
|
delete: config?.prefixes?.delete ?? "delete",
|
|
2527
|
-
update: config?.prefixes?.update ?? "update"
|
|
3821
|
+
update: config?.prefixes?.update ?? "update",
|
|
3822
|
+
upsert: config?.prefixes?.upsert ?? "upsert"
|
|
2528
3823
|
};
|
|
2529
3824
|
const suffixes = {
|
|
2530
3825
|
list: config?.suffixes?.list ?? "",
|
|
2531
3826
|
single: config?.suffixes?.single ?? "Single"
|
|
2532
3827
|
};
|
|
2533
3828
|
const typeNameMapper = config?.typeNameMapper;
|
|
3829
|
+
const features = {
|
|
3830
|
+
aggregates: config?.features?.aggregates ?? true,
|
|
3831
|
+
relationAggregates: config?.features?.relationAggregates ?? true,
|
|
3832
|
+
distinct: config?.features?.distinct ?? true,
|
|
3833
|
+
insert: config?.features?.insert ?? true,
|
|
3834
|
+
update: config?.features?.update ?? true,
|
|
3835
|
+
delete: config?.features?.delete ?? true,
|
|
3836
|
+
upsert: config?.features?.upsert ?? false
|
|
3837
|
+
};
|
|
2534
3838
|
const eagerOpt = config?.eagerLoadRelations;
|
|
2535
3839
|
const shouldEagerLoad = eagerOpt === void 0 || eagerOpt === true ? () => true : eagerOpt === false ? () => false : eagerOpt;
|
|
2536
3840
|
if (!typeNameMapper && suffixes.list === suffixes.single) {
|
|
@@ -2550,66 +3854,59 @@ var buildSchema = (db, config) => {
|
|
|
2550
3854
|
);
|
|
2551
3855
|
}
|
|
2552
3856
|
}
|
|
3857
|
+
const generatorOptions = {
|
|
3858
|
+
relationsDepthLimit: config?.relationsDepthLimit,
|
|
3859
|
+
prefixes,
|
|
3860
|
+
suffixes,
|
|
3861
|
+
conflictDoNothing: config?.conflictDoNothing ?? false,
|
|
3862
|
+
typeNameMapper,
|
|
3863
|
+
shouldEagerLoad,
|
|
3864
|
+
features
|
|
3865
|
+
};
|
|
2553
3866
|
let generatorOutput;
|
|
2554
|
-
if ((0,
|
|
2555
|
-
generatorOutput = generateSchemaData(
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
prefixes,
|
|
2561
|
-
suffixes,
|
|
2562
|
-
typeNameMapper,
|
|
2563
|
-
shouldEagerLoad
|
|
2564
|
-
);
|
|
2565
|
-
} else if ((0, import_drizzle_orm7.is)(db, import_pg_core3.PgAsyncDatabase)) {
|
|
2566
|
-
generatorOutput = generateSchemaData2(
|
|
2567
|
-
db,
|
|
2568
|
-
schema,
|
|
2569
|
-
relations,
|
|
2570
|
-
config?.relationsDepthLimit,
|
|
2571
|
-
prefixes,
|
|
2572
|
-
suffixes,
|
|
2573
|
-
config?.conflictDoNothing ?? false,
|
|
2574
|
-
typeNameMapper,
|
|
2575
|
-
shouldEagerLoad
|
|
2576
|
-
);
|
|
2577
|
-
} else if ((0, import_drizzle_orm7.is)(db, import_sqlite_core3.BaseSQLiteDatabase)) {
|
|
2578
|
-
generatorOutput = generateSchemaData3(
|
|
2579
|
-
db,
|
|
2580
|
-
schema,
|
|
2581
|
-
relations,
|
|
2582
|
-
config?.relationsDepthLimit,
|
|
2583
|
-
prefixes,
|
|
2584
|
-
suffixes,
|
|
2585
|
-
typeNameMapper,
|
|
2586
|
-
shouldEagerLoad
|
|
2587
|
-
);
|
|
3867
|
+
if ((0, import_drizzle_orm8.is)(db, import_mysql_core3.MySqlDatabase)) {
|
|
3868
|
+
generatorOutput = generateSchemaData(db, schema, relations, generatorOptions);
|
|
3869
|
+
} else if ((0, import_drizzle_orm8.is)(db, import_pg_core3.PgAsyncDatabase)) {
|
|
3870
|
+
generatorOutput = generateSchemaData2(db, schema, relations, generatorOptions);
|
|
3871
|
+
} else if ((0, import_drizzle_orm8.is)(db, import_sqlite_core3.BaseSQLiteDatabase)) {
|
|
3872
|
+
generatorOutput = generateSchemaData3(db, schema, relations, generatorOptions);
|
|
2588
3873
|
} else {
|
|
2589
3874
|
throw new Error("Drizzle-GraphQL Error: Unknown database instance type");
|
|
2590
3875
|
}
|
|
3876
|
+
const onError = config?.onError;
|
|
3877
|
+
applyErrorMapper(
|
|
3878
|
+
generatorOutput,
|
|
3879
|
+
onError ? (error) => onError(error) ?? defaultErrorMapper(error) : defaultErrorMapper
|
|
3880
|
+
);
|
|
2591
3881
|
const { queries, mutations, inputs, types } = generatorOutput;
|
|
2592
3882
|
const graphQLSchemaConfig = {
|
|
2593
3883
|
types: [...Object.values(inputs), ...Object.values(types)],
|
|
2594
|
-
query: new
|
|
3884
|
+
query: new import_graphql9.GraphQLObjectType({
|
|
2595
3885
|
name: "Query",
|
|
2596
3886
|
fields: queries
|
|
2597
3887
|
})
|
|
2598
3888
|
};
|
|
2599
|
-
if (config?.mutations !== false) {
|
|
2600
|
-
const mutation = new
|
|
3889
|
+
if (config?.mutations !== false && Object.keys(mutations).length) {
|
|
3890
|
+
const mutation = new import_graphql9.GraphQLObjectType({
|
|
2601
3891
|
name: "Mutation",
|
|
2602
3892
|
fields: mutations
|
|
2603
3893
|
});
|
|
2604
3894
|
graphQLSchemaConfig.mutation = mutation;
|
|
2605
3895
|
}
|
|
2606
|
-
const outputSchema = new
|
|
3896
|
+
const outputSchema = new import_graphql9.GraphQLSchema(graphQLSchemaConfig);
|
|
2607
3897
|
return { schema: outputSchema, entities: generatorOutput };
|
|
2608
3898
|
};
|
|
2609
3899
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2610
3900
|
0 && (module.exports = {
|
|
3901
|
+
GraphQLBigIntString,
|
|
3902
|
+
GraphQLDate,
|
|
3903
|
+
GraphQLDateTime,
|
|
3904
|
+
GraphQLJSON,
|
|
3905
|
+
GraphQLUUID,
|
|
2611
3906
|
buildSchema,
|
|
2612
3907
|
createRelationResolverFactory,
|
|
3908
|
+
defaultErrorMapper,
|
|
3909
|
+
drizzleExecutorKey,
|
|
2613
3910
|
extractFilters,
|
|
2614
3911
|
extractOrderBy,
|
|
2615
3912
|
extractRelationJoinColumns
|