@vantreeseba/drizzle-graphql 1.0.2 → 2.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 +75 -0
- package/dist/README.md +75 -0
- package/dist/index.cjs +894 -581
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +111 -18
- package/dist/index.d.ts +111 -18
- package/dist/index.js +922 -613
- package/dist/index.js.map +1 -1
- package/dist/package.json +7 -7
- package/package.json +7 -7
package/dist/index.cjs
CHANGED
|
@@ -31,8 +31,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
buildSchema: () => buildSchema,
|
|
34
|
+
createRelationResolverFactory: () => createRelationResolverFactory,
|
|
34
35
|
extractFilters: () => extractFilters,
|
|
35
|
-
extractOrderBy: () => extractOrderBy
|
|
36
|
+
extractOrderBy: () => extractOrderBy,
|
|
37
|
+
extractRelationJoinColumns: () => extractRelationJoinColumns
|
|
36
38
|
});
|
|
37
39
|
module.exports = __toCommonJS(index_exports);
|
|
38
40
|
var import_drizzle_orm7 = require("drizzle-orm");
|
|
@@ -51,11 +53,56 @@ var import_graphql_parse_resolve_info = require("graphql-parse-resolve-info");
|
|
|
51
53
|
var import_drizzle_orm3 = require("drizzle-orm");
|
|
52
54
|
var import_graphql3 = require("graphql");
|
|
53
55
|
|
|
56
|
+
// src/util/batch-loader/index.ts
|
|
57
|
+
var DRIZZLE_LOADERS_KEY = /* @__PURE__ */ Symbol("drizzle-graphql-loaders");
|
|
58
|
+
var BatchLoader = class {
|
|
59
|
+
constructor(batchFn) {
|
|
60
|
+
this.batchFn = batchFn;
|
|
61
|
+
}
|
|
62
|
+
batch = [];
|
|
63
|
+
scheduled = false;
|
|
64
|
+
load(key) {
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
|
+
this.batch.push({ key, resolve, reject });
|
|
67
|
+
if (!this.scheduled) {
|
|
68
|
+
this.scheduled = true;
|
|
69
|
+
Promise.resolve().then(() => this.dispatch());
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
async dispatch() {
|
|
74
|
+
const current = this.batch.splice(0);
|
|
75
|
+
this.scheduled = false;
|
|
76
|
+
try {
|
|
77
|
+
const results = await this.batchFn(current.map(({ key }) => key));
|
|
78
|
+
for (let i = 0; i < current.length; i++) {
|
|
79
|
+
current[i].resolve(results[i]);
|
|
80
|
+
}
|
|
81
|
+
} catch (err) {
|
|
82
|
+
for (const { reject } of current) {
|
|
83
|
+
reject(err);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
var getOrCreateLoader = (context, key, batchFn) => {
|
|
89
|
+
if (!context || typeof context !== "object") {
|
|
90
|
+
return new BatchLoader(batchFn);
|
|
91
|
+
}
|
|
92
|
+
if (!context[DRIZZLE_LOADERS_KEY]) {
|
|
93
|
+
context[DRIZZLE_LOADERS_KEY] = /* @__PURE__ */ new Map();
|
|
94
|
+
}
|
|
95
|
+
const loaders = context[DRIZZLE_LOADERS_KEY];
|
|
96
|
+
if (!loaders.has(key)) {
|
|
97
|
+
loaders.set(key, new BatchLoader(batchFn));
|
|
98
|
+
}
|
|
99
|
+
return loaders.get(key);
|
|
100
|
+
};
|
|
101
|
+
|
|
54
102
|
// src/util/case-ops/index.ts
|
|
55
103
|
var import_pluralize = __toESM(require("pluralize"), 1);
|
|
56
104
|
var uncapitalize = (input) => input?.length ? `${input[0].toLocaleLowerCase()}${input.length > 1 ? input.slice(1, input.length) : ""}` : input;
|
|
57
105
|
var capitalize = (input) => input?.length ? `${input[0].toLocaleUpperCase()}${input.length > 1 ? input.slice(1, input.length) : ""}` : input;
|
|
58
|
-
var singularize = (input) => import_pluralize.default.singular(input);
|
|
59
106
|
|
|
60
107
|
// src/util/data-mappers/index.ts
|
|
61
108
|
var import_drizzle_orm = require("drizzle-orm");
|
|
@@ -115,6 +162,11 @@ var remapToGraphQLCore = (key, value, tableName, column, relationMap) => {
|
|
|
115
162
|
var remapToGraphQLSingleOutput = (queryOutput, tableName, table, relationMap) => {
|
|
116
163
|
for (const [key, value] of Object.entries(queryOutput)) {
|
|
117
164
|
if (value === void 0 || value === null) {
|
|
165
|
+
const relEntry = value === null ? relationMap?.[tableName]?.[key] : void 0;
|
|
166
|
+
if (relEntry && (0, import_drizzle_orm.is)(relEntry.relation, import_drizzle_orm.One)) {
|
|
167
|
+
queryOutput[key] = null;
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
118
170
|
delete queryOutput[key];
|
|
119
171
|
continue;
|
|
120
172
|
}
|
|
@@ -362,26 +414,6 @@ var columnToGraphQLCore = (column, columnName, tableName, isInput) => {
|
|
|
362
414
|
throw new Error(`Drizzle-GraphQL Error: Type ${column.dataType} is not implemented!`);
|
|
363
415
|
}
|
|
364
416
|
};
|
|
365
|
-
var drizzleRelationToGraphQLInsertType = (tables, relationMap) => {
|
|
366
|
-
if (!relationMap) {
|
|
367
|
-
return null;
|
|
368
|
-
}
|
|
369
|
-
for (const [tableName, _val] of Object.entries(relationMap)) {
|
|
370
|
-
const table = tables[tableName];
|
|
371
|
-
if (!table) {
|
|
372
|
-
continue;
|
|
373
|
-
}
|
|
374
|
-
const columns = (0, import_drizzle_orm2.getColumns)(table);
|
|
375
|
-
const columnEntries = Object.entries(columns).filter(([_key, value]) => value.primary);
|
|
376
|
-
const _connectFields = Object.fromEntries(
|
|
377
|
-
columnEntries.map(([columnName, columnDescription]) => [
|
|
378
|
-
columnName,
|
|
379
|
-
drizzleColumnToGraphQLType(columnDescription, columnName, tableName, false, true, true)
|
|
380
|
-
])
|
|
381
|
-
);
|
|
382
|
-
}
|
|
383
|
-
return null;
|
|
384
|
-
};
|
|
385
417
|
var drizzleColumnToGraphQLType = (column, columnName, tableName, forceNullable = false, defaultIsNullable = false, isInput = false) => {
|
|
386
418
|
const typeDesc = columnToGraphQLCore(column, columnName, tableName, isInput);
|
|
387
419
|
const noDesc = ["string", "boolean", "number"];
|
|
@@ -403,7 +435,10 @@ var drizzleColumnToGraphQLType = (column, columnName, tableName, forceNullable =
|
|
|
403
435
|
|
|
404
436
|
// src/util/builders/common.ts
|
|
405
437
|
var rqbCrashTypes = ["SQLiteBigInt", "SQLiteBlobJson", "SQLiteBlobBuffer"];
|
|
406
|
-
var
|
|
438
|
+
var resolveTypeName = (name, typeNameMapper) => {
|
|
439
|
+
const mapped = typeNameMapper?.(name);
|
|
440
|
+
return mapped ? capitalize(mapped.singular) : capitalize(name);
|
|
441
|
+
};
|
|
407
442
|
var buildNamedRelations = (relations, tableEntries) => {
|
|
408
443
|
const namedRelations = {};
|
|
409
444
|
for (const [relTableName, relConfig] of Object.entries(relations)) {
|
|
@@ -436,6 +471,129 @@ var buildNamedRelations = (relations, tableEntries) => {
|
|
|
436
471
|
}
|
|
437
472
|
return namedRelations;
|
|
438
473
|
};
|
|
474
|
+
var attachTargetPrimaryKeys = (namedRelations, tables, resolvePkNames) => {
|
|
475
|
+
const cache = /* @__PURE__ */ new Map();
|
|
476
|
+
for (const rels of Object.values(namedRelations)) {
|
|
477
|
+
for (const relEntry of Object.values(rels)) {
|
|
478
|
+
const { targetTableName } = relEntry;
|
|
479
|
+
let pk = cache.get(targetTableName);
|
|
480
|
+
if (!pk) {
|
|
481
|
+
const targetTable = tables[targetTableName];
|
|
482
|
+
pk = targetTable ? resolvePkNames(targetTable) : [];
|
|
483
|
+
cache.set(targetTableName, pk);
|
|
484
|
+
}
|
|
485
|
+
relEntry.targetPkNames = pk;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
};
|
|
489
|
+
var extractRelationJoinColumns = (relEntry, parentTable, targetTable) => {
|
|
490
|
+
const rel = relEntry.relation ?? relEntry;
|
|
491
|
+
const sourceColumns = rel.sourceColumns;
|
|
492
|
+
const targetColumns = rel.targetColumns;
|
|
493
|
+
if (!sourceColumns?.length || !targetColumns?.length) {
|
|
494
|
+
return void 0;
|
|
495
|
+
}
|
|
496
|
+
const sourceCol = sourceColumns[0];
|
|
497
|
+
const targetCol = targetColumns[0];
|
|
498
|
+
const parentCols = (0, import_drizzle_orm3.getColumns)(parentTable);
|
|
499
|
+
const localColPropName = Object.entries(parentCols).find(([, c]) => c === sourceCol)?.[0];
|
|
500
|
+
const targetCols = (0, import_drizzle_orm3.getColumns)(targetTable);
|
|
501
|
+
const foreignColPropName = Object.entries(targetCols).find(([, c]) => c === targetCol)?.[0];
|
|
502
|
+
if (!localColPropName || !foreignColPropName) {
|
|
503
|
+
return void 0;
|
|
504
|
+
}
|
|
505
|
+
return { localColPropName, foreignCol: targetCol, foreignColPropName };
|
|
506
|
+
};
|
|
507
|
+
var batchedPaginatedRelationQuery = async (db, targetTable, foreignCol, whereCondition, orderByArg, limit, offset, pkNames) => {
|
|
508
|
+
const cols = (0, import_drizzle_orm3.getColumns)(targetTable);
|
|
509
|
+
const orderExprs = [
|
|
510
|
+
...orderByArg ? extractOrderBy(targetTable, orderByArg) : [],
|
|
511
|
+
...primaryKeyOrderExprs(targetTable, pkNames)
|
|
512
|
+
];
|
|
513
|
+
const orderClause = orderExprs.length ? import_drizzle_orm3.sql` order by ${import_drizzle_orm3.sql.join(orderExprs, import_drizzle_orm3.sql`, `)}` : import_drizzle_orm3.sql``;
|
|
514
|
+
const RN = "__drizzle_graphql_rn";
|
|
515
|
+
const rowNumber = import_drizzle_orm3.sql`row_number() over (partition by ${foreignCol}${orderClause})`.as(RN);
|
|
516
|
+
const sub = db.select({ ...cols, [RN]: rowNumber }).from(targetTable).where(whereCondition).as("__paginated");
|
|
517
|
+
const lower = offset ?? 0;
|
|
518
|
+
const windowConds = [(0, import_drizzle_orm3.gt)(sub[RN], lower)];
|
|
519
|
+
if (limit != null) {
|
|
520
|
+
windowConds.push((0, import_drizzle_orm3.lte)(sub[RN], lower + limit));
|
|
521
|
+
}
|
|
522
|
+
const rows = await db.select().from(sub).where((0, import_drizzle_orm3.and)(...windowConds)).orderBy(sub[RN]);
|
|
523
|
+
for (const row of rows) {
|
|
524
|
+
delete row[RN];
|
|
525
|
+
}
|
|
526
|
+
return rows;
|
|
527
|
+
};
|
|
528
|
+
var createRelationResolverFactory = (db, tables) => ({ tableName, relationName, relEntry, isOne }) => {
|
|
529
|
+
const parentTable = tables[tableName];
|
|
530
|
+
const targetTableName = relEntry.targetTableName;
|
|
531
|
+
const targetTable = tables[targetTableName];
|
|
532
|
+
if (!parentTable || !targetTable) {
|
|
533
|
+
return void 0;
|
|
534
|
+
}
|
|
535
|
+
const joinCols = extractRelationJoinColumns(relEntry, parentTable, targetTable);
|
|
536
|
+
if (!joinCols) {
|
|
537
|
+
return void 0;
|
|
538
|
+
}
|
|
539
|
+
const { localColPropName, foreignCol, foreignColPropName } = joinCols;
|
|
540
|
+
const targetPkNames = relEntry.targetPkNames ?? [];
|
|
541
|
+
return async (parent, args, context) => {
|
|
542
|
+
if (parent[relationName] !== void 0) {
|
|
543
|
+
return parent[relationName];
|
|
544
|
+
}
|
|
545
|
+
const localValue = parent[localColPropName];
|
|
546
|
+
if (localValue == null) {
|
|
547
|
+
return isOne ? null : [];
|
|
548
|
+
}
|
|
549
|
+
const { where: whereArg, orderBy: orderByArg, limit, offset } = args ?? {};
|
|
550
|
+
const argsKey = JSON.stringify({
|
|
551
|
+
where: whereArg ?? null,
|
|
552
|
+
orderBy: orderByArg ?? null,
|
|
553
|
+
limit: limit ?? null,
|
|
554
|
+
offset: offset ?? null
|
|
555
|
+
});
|
|
556
|
+
const loaderKey = `${tableName}::${relationName}::${argsKey}`;
|
|
557
|
+
const loader = getOrCreateLoader(context, loaderKey, async (parentIds) => {
|
|
558
|
+
const uniqueIds = [...new Set(parentIds)];
|
|
559
|
+
const whereCondition = (0, import_drizzle_orm3.and)(
|
|
560
|
+
(0, import_drizzle_orm3.inArray)(foreignCol, uniqueIds),
|
|
561
|
+
whereArg ? extractFilters(targetTable, targetTableName, whereArg) : void 0
|
|
562
|
+
);
|
|
563
|
+
let rows;
|
|
564
|
+
if (limit != null || offset != null) {
|
|
565
|
+
rows = await batchedPaginatedRelationQuery(
|
|
566
|
+
db,
|
|
567
|
+
targetTable,
|
|
568
|
+
foreignCol,
|
|
569
|
+
whereCondition,
|
|
570
|
+
orderByArg,
|
|
571
|
+
limit ?? null,
|
|
572
|
+
offset ?? null,
|
|
573
|
+
targetPkNames
|
|
574
|
+
);
|
|
575
|
+
} else {
|
|
576
|
+
let q = db.select().from(targetTable).where(whereCondition);
|
|
577
|
+
if (orderByArg) {
|
|
578
|
+
q = q.orderBy(...extractOrderBy(targetTable, orderByArg));
|
|
579
|
+
}
|
|
580
|
+
rows = await q;
|
|
581
|
+
}
|
|
582
|
+
if (isOne) {
|
|
583
|
+
const byKey = new Map(rows.map((row) => [String(row[foreignColPropName]), row]));
|
|
584
|
+
remapToGraphQLArrayOutput(rows, targetTableName, targetTable);
|
|
585
|
+
return parentIds.map((id) => byKey.get(String(id)) ?? null);
|
|
586
|
+
}
|
|
587
|
+
const grouped = new Map(uniqueIds.map((id) => [String(id), []]));
|
|
588
|
+
for (const row of rows) {
|
|
589
|
+
grouped.get(String(row[foreignColPropName]))?.push(row);
|
|
590
|
+
}
|
|
591
|
+
remapToGraphQLArrayOutput(rows, targetTableName, targetTable);
|
|
592
|
+
return parentIds.map((id) => grouped.get(String(id)) ?? []);
|
|
593
|
+
});
|
|
594
|
+
return loader.load(localValue);
|
|
595
|
+
};
|
|
596
|
+
};
|
|
439
597
|
var extractSelectedColumnsFromTree = (tree, table) => {
|
|
440
598
|
const tableColumns = (0, import_drizzle_orm3.getColumns)(table);
|
|
441
599
|
const treeEntries = Object.entries(tree);
|
|
@@ -613,34 +771,32 @@ var generateTableSelectTypeFieldsCached = (table, tableName) => {
|
|
|
613
771
|
fieldMap.set(table, remapped);
|
|
614
772
|
return remapped;
|
|
615
773
|
};
|
|
616
|
-
var
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
return orderTypeMap.get(table);
|
|
774
|
+
var generateTableOrderTypeCached = (table, tableName, typeNameMapper, cacheCtx) => {
|
|
775
|
+
if (cacheCtx.orderTypeCache.has(table)) {
|
|
776
|
+
return cacheCtx.orderTypeCache.get(table);
|
|
620
777
|
}
|
|
621
778
|
const orderColumns = generateTableOrderCached(table);
|
|
622
779
|
const order = new import_graphql3.GraphQLInputObjectType({
|
|
623
|
-
name: `${
|
|
780
|
+
name: `${resolveTypeName(tableName, typeNameMapper)}OrderBy`,
|
|
624
781
|
fields: orderColumns
|
|
625
782
|
});
|
|
626
|
-
|
|
783
|
+
cacheCtx.orderTypeCache.set(table, order);
|
|
627
784
|
return order;
|
|
628
785
|
};
|
|
629
|
-
var
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
return filterTypeMap.get(table);
|
|
786
|
+
var generateTableFilterTypeCached = (table, tableName, cacheCtx, typeNameMapper) => {
|
|
787
|
+
if (cacheCtx.filterTypeCache.has(table)) {
|
|
788
|
+
return cacheCtx.filterTypeCache.get(table);
|
|
633
789
|
}
|
|
634
790
|
const filterColumns = generateTableFilterValuesCached(table, tableName, cacheCtx);
|
|
635
791
|
const filters = new import_graphql3.GraphQLInputObjectType({
|
|
636
|
-
name: `${
|
|
792
|
+
name: `${resolveTypeName(tableName, typeNameMapper)}Filters`,
|
|
637
793
|
fields: {
|
|
638
794
|
...filterColumns,
|
|
639
795
|
OR: {
|
|
640
796
|
type: new import_graphql3.GraphQLList(
|
|
641
797
|
new import_graphql3.GraphQLNonNull(
|
|
642
798
|
new import_graphql3.GraphQLInputObjectType({
|
|
643
|
-
name: `${
|
|
799
|
+
name: `${resolveTypeName(tableName, typeNameMapper)}FiltersOr`,
|
|
644
800
|
fields: filterColumns
|
|
645
801
|
})
|
|
646
802
|
)
|
|
@@ -648,16 +804,24 @@ var generateTableFilterTypeCached = (table, tableName, cacheCtx, singularTypes)
|
|
|
648
804
|
}
|
|
649
805
|
}
|
|
650
806
|
});
|
|
651
|
-
|
|
807
|
+
cacheCtx.filterTypeCache.set(table, filters);
|
|
652
808
|
return filters;
|
|
653
809
|
};
|
|
654
|
-
var generateSelectFields = (tables, tableName, relationMap, fromTableName, fromRelationName, withOrder,
|
|
810
|
+
var generateSelectFields = (tables, tableName, relationMap, fromTableName, fromRelationName, withOrder, relationsDepthLimit, cacheCtx, typeNameMapper, usedTables = /* @__PURE__ */ new Set(), resolverFactory, currentDepth = 0) => {
|
|
655
811
|
const table = tables[tableName];
|
|
656
|
-
const order = withOrder ? generateTableOrderTypeCached(table, tableName,
|
|
657
|
-
const filters = generateTableFilterTypeCached(table, tableName, cacheCtx,
|
|
812
|
+
const order = withOrder ? generateTableOrderTypeCached(table, tableName, typeNameMapper, cacheCtx) : void 0;
|
|
813
|
+
const filters = generateTableFilterTypeCached(table, tableName, cacheCtx, typeNameMapper);
|
|
658
814
|
const tableFields = generateTableSelectTypeFieldsCached(table, tableName);
|
|
659
815
|
const relationsForTable = relationMap[tableName];
|
|
660
816
|
const relationEntries = relationsForTable ? Object.entries(relationsForTable) : [];
|
|
817
|
+
if (relationsDepthLimit !== void 0 && currentDepth >= relationsDepthLimit) {
|
|
818
|
+
return {
|
|
819
|
+
order,
|
|
820
|
+
filters,
|
|
821
|
+
tableFields,
|
|
822
|
+
relationFields: {}
|
|
823
|
+
};
|
|
824
|
+
}
|
|
661
825
|
if (usedTables.has(tableName)) {
|
|
662
826
|
return {
|
|
663
827
|
order,
|
|
@@ -681,7 +845,7 @@ var generateSelectFields = (tables, tableName, relationMap, fromTableName, fromR
|
|
|
681
845
|
cacheCtx.relationFieldContainers.set(tableName, container);
|
|
682
846
|
}
|
|
683
847
|
if (isRootCall && !cacheCtx.objectTypeCache.has(tableName)) {
|
|
684
|
-
const typeName =
|
|
848
|
+
const typeName = resolveTypeName(tableName, typeNameMapper);
|
|
685
849
|
const shell = new import_graphql3.GraphQLObjectType({
|
|
686
850
|
name: typeName,
|
|
687
851
|
fields: () => ({ ...tableFields, ...container.fields })
|
|
@@ -705,10 +869,12 @@ var generateSelectFields = (tables, tableName, relationMap, fromTableName, fromR
|
|
|
705
869
|
relationName,
|
|
706
870
|
// fromRelationName for the relation type
|
|
707
871
|
!isOne,
|
|
708
|
-
|
|
872
|
+
relationsDepthLimit,
|
|
709
873
|
cacheCtx,
|
|
710
|
-
|
|
711
|
-
nextUsedTables
|
|
874
|
+
typeNameMapper,
|
|
875
|
+
nextUsedTables,
|
|
876
|
+
resolverFactory,
|
|
877
|
+
currentDepth + 1
|
|
712
878
|
);
|
|
713
879
|
let relType = cacheCtx.objectTypeCache.get(targetTableName);
|
|
714
880
|
if (!relType) {
|
|
@@ -721,11 +887,12 @@ var generateSelectFields = (tables, tableName, relationMap, fromTableName, fromR
|
|
|
721
887
|
}
|
|
722
888
|
const capturedTargetContainer = targetContainer;
|
|
723
889
|
relType = new import_graphql3.GraphQLObjectType({
|
|
724
|
-
name:
|
|
890
|
+
name: resolveTypeName(targetTableName, typeNameMapper),
|
|
725
891
|
fields: () => ({ ...targetTableFields, ...capturedTargetContainer.fields })
|
|
726
892
|
});
|
|
727
893
|
cacheCtx.objectTypeCache.set(targetTableName, relType);
|
|
728
894
|
}
|
|
895
|
+
const resolve = resolverFactory?.({ tableName, relationName, relEntry, isOne });
|
|
729
896
|
if (isOne) {
|
|
730
897
|
rawRelationFields.push([
|
|
731
898
|
relationName,
|
|
@@ -733,7 +900,8 @@ var generateSelectFields = (tables, tableName, relationMap, fromTableName, fromR
|
|
|
733
900
|
type: relType,
|
|
734
901
|
args: {
|
|
735
902
|
where: { type: relSelectData.filters }
|
|
736
|
-
}
|
|
903
|
+
},
|
|
904
|
+
resolve
|
|
737
905
|
}
|
|
738
906
|
]);
|
|
739
907
|
continue;
|
|
@@ -747,7 +915,8 @@ var generateSelectFields = (tables, tableName, relationMap, fromTableName, fromR
|
|
|
747
915
|
orderBy: { type: relSelectData.order },
|
|
748
916
|
offset: { type: import_graphql3.GraphQLInt },
|
|
749
917
|
limit: { type: import_graphql3.GraphQLInt }
|
|
750
|
-
}
|
|
918
|
+
},
|
|
919
|
+
resolve
|
|
751
920
|
}
|
|
752
921
|
]);
|
|
753
922
|
}
|
|
@@ -773,7 +942,7 @@ var generateSelectFields = (tables, tableName, relationMap, fromTableName, fromR
|
|
|
773
942
|
relationFields: {}
|
|
774
943
|
};
|
|
775
944
|
};
|
|
776
|
-
var generateTableTypes = (tableName, tables, relationMap, withReturning, relationsDepthLimit, cacheCtx,
|
|
945
|
+
var generateTableTypes = (tableName, tables, relationMap, withReturning, relationsDepthLimit, cacheCtx, typeNameMapper = void 0, insertPrefix = "create", updatePrefix = "update", resolverFactory) => {
|
|
777
946
|
const { tableFields, relationFields, filters, order } = generateSelectFields(
|
|
778
947
|
tables,
|
|
779
948
|
tableName,
|
|
@@ -785,12 +954,14 @@ var generateTableTypes = (tableName, tables, relationMap, withReturning, relatio
|
|
|
785
954
|
true,
|
|
786
955
|
relationsDepthLimit,
|
|
787
956
|
cacheCtx,
|
|
788
|
-
|
|
957
|
+
typeNameMapper,
|
|
958
|
+
/* @__PURE__ */ new Set(),
|
|
959
|
+
resolverFactory,
|
|
960
|
+
0
|
|
789
961
|
);
|
|
790
962
|
const table = tables[tableName];
|
|
791
963
|
const columns = (0, import_drizzle_orm3.getColumns)(table);
|
|
792
964
|
const columnEntries = Object.entries(columns);
|
|
793
|
-
const _insertNested = drizzleRelationToGraphQLInsertType(tables, relationMap[tableName] ?? {});
|
|
794
965
|
const insertFields = Object.fromEntries(
|
|
795
966
|
columnEntries.map(([columnName, columnDescription]) => [
|
|
796
967
|
columnName,
|
|
@@ -804,15 +975,15 @@ var generateTableTypes = (tableName, tables, relationMap, withReturning, relatio
|
|
|
804
975
|
])
|
|
805
976
|
);
|
|
806
977
|
const insertInput = new import_graphql3.GraphQLInputObjectType({
|
|
807
|
-
name: `${capitalize(insertPrefix)}${
|
|
978
|
+
name: `${capitalize(insertPrefix)}${resolveTypeName(tableName, typeNameMapper)}Input`,
|
|
808
979
|
fields: insertFields
|
|
809
980
|
});
|
|
810
981
|
const updateInput = new import_graphql3.GraphQLInputObjectType({
|
|
811
|
-
name: `${capitalize(updatePrefix)}${
|
|
982
|
+
name: `${capitalize(updatePrefix)}${resolveTypeName(tableName, typeNameMapper)}Input`,
|
|
812
983
|
fields: updateFields
|
|
813
984
|
});
|
|
814
985
|
const selectSingleOutput = cacheCtx.objectTypeCache.get(tableName) ?? new import_graphql3.GraphQLObjectType({
|
|
815
|
-
name:
|
|
986
|
+
name: resolveTypeName(tableName, typeNameMapper),
|
|
816
987
|
fields: { ...tableFields, ...relationFields }
|
|
817
988
|
});
|
|
818
989
|
const selectArrOutput = new import_graphql3.GraphQLNonNull(new import_graphql3.GraphQLList(new import_graphql3.GraphQLNonNull(selectSingleOutput)));
|
|
@@ -872,55 +1043,28 @@ var extractFiltersColumn = (column, columnName, operators) => {
|
|
|
872
1043
|
}
|
|
873
1044
|
return variants2.length ? variants2.length > 1 ? (0, import_drizzle_orm3.or)(...variants2) : variants2[0] : void 0;
|
|
874
1045
|
}
|
|
1046
|
+
const singleValueOps = { eq: import_drizzle_orm3.eq, ne: import_drizzle_orm3.ne, gt: import_drizzle_orm3.gt, gte: import_drizzle_orm3.gte, lt: import_drizzle_orm3.lt, lte: import_drizzle_orm3.lte };
|
|
1047
|
+
const stringValueOps = { like: import_drizzle_orm3.like, notLike: import_drizzle_orm3.notLike, ilike: import_drizzle_orm3.ilike, notIlike: import_drizzle_orm3.notIlike };
|
|
1048
|
+
const arrayValueOps = { inArray: import_drizzle_orm3.inArray, notInArray: import_drizzle_orm3.notInArray };
|
|
1049
|
+
const nullableOps = { isNull: import_drizzle_orm3.isNull, isNotNull: import_drizzle_orm3.isNotNull };
|
|
875
1050
|
const variants = [];
|
|
876
1051
|
for (const [operatorName, operatorValue] of entries) {
|
|
877
1052
|
if (operatorValue === null || operatorValue === false) {
|
|
878
1053
|
continue;
|
|
879
1054
|
}
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
case "gte":
|
|
889
|
-
operator = operator ?? import_drizzle_orm3.gte;
|
|
890
|
-
case "lt":
|
|
891
|
-
operator = operator ?? import_drizzle_orm3.lt;
|
|
892
|
-
case "lte": {
|
|
893
|
-
operator = operator ?? import_drizzle_orm3.lte;
|
|
894
|
-
const singleValue = remapFromGraphQLCore(operatorValue, column, columnName);
|
|
895
|
-
variants.push(operator(column, singleValue));
|
|
896
|
-
break;
|
|
1055
|
+
if (operatorName in singleValueOps) {
|
|
1056
|
+
const singleValue = remapFromGraphQLCore(operatorValue, column, columnName);
|
|
1057
|
+
variants.push(singleValueOps[operatorName](column, singleValue));
|
|
1058
|
+
} else if (operatorName in stringValueOps) {
|
|
1059
|
+
variants.push(stringValueOps[operatorName](column, operatorValue));
|
|
1060
|
+
} else if (operatorName in arrayValueOps) {
|
|
1061
|
+
if (!operatorValue.length) {
|
|
1062
|
+
throw new import_graphql3.GraphQLError(`WHERE ${columnName}: Unable to use operator ${operatorName} with an empty array!`);
|
|
897
1063
|
}
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
case "ilike":
|
|
903
|
-
operator = operator ?? import_drizzle_orm3.ilike;
|
|
904
|
-
case "notIlike":
|
|
905
|
-
operator = operator ?? import_drizzle_orm3.notIlike;
|
|
906
|
-
variants.push(operator(column, operatorValue));
|
|
907
|
-
break;
|
|
908
|
-
case "inArray":
|
|
909
|
-
operator = operator ?? import_drizzle_orm3.inArray;
|
|
910
|
-
case "notInArray": {
|
|
911
|
-
operator = operator ?? import_drizzle_orm3.notInArray;
|
|
912
|
-
if (!operatorValue.length) {
|
|
913
|
-
throw new import_graphql3.GraphQLError(`WHERE ${columnName}: Unable to use operator ${operatorName} with an empty array!`);
|
|
914
|
-
}
|
|
915
|
-
const arrayValue = operatorValue.map((val) => remapFromGraphQLCore(val, column, columnName));
|
|
916
|
-
variants.push(operator(column, arrayValue));
|
|
917
|
-
break;
|
|
918
|
-
}
|
|
919
|
-
case "isNull":
|
|
920
|
-
operator = operator ?? import_drizzle_orm3.isNull;
|
|
921
|
-
case "isNotNull":
|
|
922
|
-
operator = operator ?? import_drizzle_orm3.isNotNull;
|
|
923
|
-
variants.push(operator(column));
|
|
1064
|
+
const arrayValue = operatorValue.map((val) => remapFromGraphQLCore(val, column, columnName));
|
|
1065
|
+
variants.push(arrayValueOps[operatorName](column, arrayValue));
|
|
1066
|
+
} else if (operatorName in nullableOps) {
|
|
1067
|
+
variants.push(nullableOps[operatorName](column));
|
|
924
1068
|
}
|
|
925
1069
|
}
|
|
926
1070
|
return variants.length ? variants.length > 1 ? (0, import_drizzle_orm3.and)(...variants) : variants[0] : void 0;
|
|
@@ -956,7 +1100,7 @@ var extractFilters = (table, tableName, filters) => {
|
|
|
956
1100
|
}
|
|
957
1101
|
return variants.length ? variants.length > 1 ? (0, import_drizzle_orm3.and)(...variants) : variants[0] : void 0;
|
|
958
1102
|
};
|
|
959
|
-
var extractRelationsParamsInner = (relationMap, tables, tableName, typeName, originField,
|
|
1103
|
+
var extractRelationsParamsInner = (relationMap, tables, tableName, typeName, originField, typeNameMapper, _isInitial = false) => {
|
|
960
1104
|
const relationsForTable = relationMap[tableName];
|
|
961
1105
|
if (!relationsForTable) {
|
|
962
1106
|
return void 0;
|
|
@@ -966,8 +1110,9 @@ var extractRelationsParamsInner = (relationMap, tables, tableName, typeName, ori
|
|
|
966
1110
|
return void 0;
|
|
967
1111
|
}
|
|
968
1112
|
const args = {};
|
|
969
|
-
for (const [relName,
|
|
970
|
-
const
|
|
1113
|
+
for (const [relName, relEntry] of Object.entries(relationsForTable)) {
|
|
1114
|
+
const { targetTableName, targetPkNames } = relEntry;
|
|
1115
|
+
const relTypeName = resolveTypeName(targetTableName, typeNameMapper);
|
|
971
1116
|
const field = baseField[relName] ?? Object.values(baseField).find((f) => f.name === relName);
|
|
972
1117
|
if (!field) {
|
|
973
1118
|
continue;
|
|
@@ -986,140 +1131,279 @@ var extractRelationsParamsInner = (relationMap, tables, tableName, typeName, ori
|
|
|
986
1131
|
const limit = relationArgs?.limit ?? void 0;
|
|
987
1132
|
const relWhere = relationArgs?.where;
|
|
988
1133
|
thisRecord.where = relWhere ? { RAW: (aliasedTable) => extractFilters(aliasedTable, relName, relWhere) } : void 0;
|
|
989
|
-
|
|
1134
|
+
const hasPagination = offset != null || limit != null;
|
|
1135
|
+
const pkNames = targetPkNames ?? [];
|
|
1136
|
+
thisRecord.orderBy = relationArgs?.orderBy ? (aliasedTable) => extractOrderBy(aliasedTable, relationArgs.orderBy) : hasPagination && pkNames.length ? (aliasedTable) => primaryKeyOrderExprs(aliasedTable, pkNames) : void 0;
|
|
990
1137
|
thisRecord.offset = offset;
|
|
991
1138
|
thisRecord.limit = limit;
|
|
992
|
-
const relWith = relationField ? extractRelationsParamsInner(relationMap, tables, targetTableName, relTypeName, relationField,
|
|
1139
|
+
const relWith = relationField ? extractRelationsParamsInner(relationMap, tables, targetTableName, relTypeName, relationField, typeNameMapper) : void 0;
|
|
993
1140
|
thisRecord.with = relWith;
|
|
994
1141
|
args[relName] = thisRecord;
|
|
995
1142
|
}
|
|
996
1143
|
return args;
|
|
997
1144
|
};
|
|
998
|
-
var extractRelationsParams = (relationMap, tables, tableName, info, typeName,
|
|
1145
|
+
var extractRelationsParams = (relationMap, tables, tableName, info, typeName, typeNameMapper) => {
|
|
999
1146
|
if (!info) {
|
|
1000
1147
|
return void 0;
|
|
1001
1148
|
}
|
|
1002
|
-
return extractRelationsParamsInner(relationMap, tables, tableName, typeName, info,
|
|
1149
|
+
return extractRelationsParamsInner(relationMap, tables, tableName, typeName, info, typeNameMapper, true);
|
|
1150
|
+
};
|
|
1151
|
+
var pruneNonEagerRelations = (relationMap, shouldEagerLoad) => {
|
|
1152
|
+
const out = {};
|
|
1153
|
+
for (const [tableName, rels] of Object.entries(relationMap)) {
|
|
1154
|
+
out[tableName] = Object.fromEntries(
|
|
1155
|
+
Object.entries(rels).filter(([relationName]) => shouldEagerLoad(tableName, relationName))
|
|
1156
|
+
);
|
|
1157
|
+
}
|
|
1158
|
+
return out;
|
|
1159
|
+
};
|
|
1160
|
+
var getPrimaryKeyPropNames = (table, compositePkColumnNames) => {
|
|
1161
|
+
const cols = (0, import_drizzle_orm3.getColumns)(table);
|
|
1162
|
+
const entries = Object.entries(cols);
|
|
1163
|
+
const inlinePks = entries.filter(([, c]) => c.primary).map(([k]) => k);
|
|
1164
|
+
if (inlinePks.length) {
|
|
1165
|
+
return inlinePks;
|
|
1166
|
+
}
|
|
1167
|
+
if (compositePkColumnNames?.length) {
|
|
1168
|
+
const wanted = new Set(compositePkColumnNames);
|
|
1169
|
+
const fromComposite = entries.filter(([, c]) => wanted.has(c.name)).map(([k]) => k);
|
|
1170
|
+
if (fromComposite.length) {
|
|
1171
|
+
return fromComposite;
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
return [];
|
|
1175
|
+
};
|
|
1176
|
+
var withPrimaryKeyColumns = (columns, table, pkNames) => {
|
|
1177
|
+
const allCols = (0, import_drizzle_orm3.getColumns)(table);
|
|
1178
|
+
for (const pk of pkNames) {
|
|
1179
|
+
if (!(pk in columns) && allCols[pk]) {
|
|
1180
|
+
columns[pk] = allCols[pk];
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
return columns;
|
|
1184
|
+
};
|
|
1185
|
+
var getPrimaryKeyPropNamesFromConfig = (table, getTableConfig4) => {
|
|
1186
|
+
const compositePkColumnNames = getTableConfig4(table).primaryKeys.flatMap((pk) => pk.columns.map((c) => c.name));
|
|
1187
|
+
return getPrimaryKeyPropNames(table, compositePkColumnNames);
|
|
1188
|
+
};
|
|
1189
|
+
var primaryKeyOrderExprs = (table, pkNames) => {
|
|
1190
|
+
const cols = (0, import_drizzle_orm3.getColumns)(table);
|
|
1191
|
+
return pkNames.map((n) => cols[n]).filter(Boolean).map((col) => (0, import_drizzle_orm3.asc)(col));
|
|
1192
|
+
};
|
|
1193
|
+
var prepareMutationRelationColumns = (params) => {
|
|
1194
|
+
const { relationMap, tables, tableName, typeName, typeNameMapper, table, pkNames, parsedInfo } = params;
|
|
1195
|
+
const withParams = relationMap[tableName] ? extractRelationsParams(relationMap, tables, tableName, parsedInfo, typeName, typeNameMapper) : void 0;
|
|
1196
|
+
const hasRelations = !!(withParams && Object.keys(withParams).length);
|
|
1197
|
+
const baseColumns = extractSelectedColumnsFromTreeSQLFormat(parsedInfo.fieldsByTypeName[typeName], table);
|
|
1198
|
+
const columns = hasRelations ? withPrimaryKeyColumns(baseColumns, table, pkNames) : baseColumns;
|
|
1199
|
+
return { columns, hasRelations, withParams };
|
|
1200
|
+
};
|
|
1201
|
+
var toGraphQLError = (e) => e instanceof Error ? new import_graphql3.GraphQLError(e.message) : e;
|
|
1202
|
+
var computeResolverFieldNames = (tableName, typeNameMapper, prefixes, suffixes) => {
|
|
1203
|
+
const mapped = typeNameMapper?.(tableName);
|
|
1204
|
+
const typeName = mapped ? capitalize(mapped.singular) : capitalize(tableName);
|
|
1205
|
+
const listFieldName = (mapped?.plural ?? uncapitalize(tableName)) + suffixes.list;
|
|
1206
|
+
const singleFieldName = mapped?.singular ?? uncapitalize(tableName) + suffixes.single;
|
|
1207
|
+
const createArrayFieldName = `${prefixes.insert}${mapped ? capitalize(mapped.plural) : capitalize(tableName)}`;
|
|
1208
|
+
const createSingleFieldName = mapped ? `${prefixes.insert}${capitalize(mapped.singular)}` : `${prefixes.insert}${capitalize(tableName)}${suffixes.single}`;
|
|
1209
|
+
const updateFieldName = `${prefixes.update}${mapped ? capitalize(mapped.singular) : capitalize(tableName)}`;
|
|
1210
|
+
const deleteFieldName = `${prefixes.delete}${mapped ? capitalize(mapped.singular) : capitalize(tableName)}`;
|
|
1211
|
+
return {
|
|
1212
|
+
typeName,
|
|
1213
|
+
listFieldName,
|
|
1214
|
+
singleFieldName,
|
|
1215
|
+
createArrayFieldName,
|
|
1216
|
+
createSingleFieldName,
|
|
1217
|
+
updateFieldName,
|
|
1218
|
+
deleteFieldName
|
|
1219
|
+
};
|
|
1220
|
+
};
|
|
1221
|
+
var selectArrayArgs = (orderArgs, filterArgs) => ({
|
|
1222
|
+
offset: { type: import_graphql3.GraphQLInt },
|
|
1223
|
+
limit: { type: import_graphql3.GraphQLInt },
|
|
1224
|
+
orderBy: { type: orderArgs },
|
|
1225
|
+
where: { type: filterArgs }
|
|
1226
|
+
});
|
|
1227
|
+
var selectSingleArgs = (orderArgs, filterArgs) => ({
|
|
1228
|
+
offset: { type: import_graphql3.GraphQLInt },
|
|
1229
|
+
orderBy: { type: orderArgs },
|
|
1230
|
+
where: { type: filterArgs }
|
|
1231
|
+
});
|
|
1232
|
+
var runRelationalSelect = async (opts) => {
|
|
1233
|
+
const {
|
|
1234
|
+
queryBase,
|
|
1235
|
+
tables,
|
|
1236
|
+
tableName,
|
|
1237
|
+
table,
|
|
1238
|
+
relationMap,
|
|
1239
|
+
typeName,
|
|
1240
|
+
typeNameMapper,
|
|
1241
|
+
parsedInfo,
|
|
1242
|
+
offset,
|
|
1243
|
+
orderBy,
|
|
1244
|
+
where,
|
|
1245
|
+
single
|
|
1246
|
+
} = opts;
|
|
1247
|
+
const params = {
|
|
1248
|
+
columns: extractSelectedColumnsFromTree(parsedInfo.fieldsByTypeName[typeName], table),
|
|
1249
|
+
offset,
|
|
1250
|
+
// drizzle-orm v1 RQB calls orderBy/where with the aliased table proxy — use it
|
|
1251
|
+
// directly so column refs match the CTE alias.
|
|
1252
|
+
orderBy: orderBy ? (aliasedTable) => extractOrderBy(aliasedTable, orderBy) : void 0,
|
|
1253
|
+
where: where ? { RAW: (aliasedTable) => extractFilters(aliasedTable, tableName, where) } : void 0,
|
|
1254
|
+
with: relationMap[tableName] ? extractRelationsParams(relationMap, tables, tableName, parsedInfo, typeName, typeNameMapper) : void 0
|
|
1255
|
+
};
|
|
1256
|
+
if (single) {
|
|
1257
|
+
const result2 = await queryBase.findFirst(params);
|
|
1258
|
+
return result2 ? remapToGraphQLSingleOutput(result2, tableName, table, relationMap) : void 0;
|
|
1259
|
+
}
|
|
1260
|
+
params.limit = opts.limit;
|
|
1261
|
+
const result = await queryBase.findMany(params);
|
|
1262
|
+
return remapToGraphQLArrayOutput(result, tableName, table, relationMap);
|
|
1263
|
+
};
|
|
1264
|
+
var eagerLoadMutationRelations = async (db, tableName, rows, pkNames, withParams) => {
|
|
1265
|
+
if (!rows.length || !pkNames.length || !withParams || !Object.keys(withParams).length) {
|
|
1266
|
+
return rows;
|
|
1267
|
+
}
|
|
1268
|
+
const queryBase = db.query?.[tableName];
|
|
1269
|
+
if (!queryBase) {
|
|
1270
|
+
return rows;
|
|
1271
|
+
}
|
|
1272
|
+
const keyableRows = rows.filter((row) => pkNames.every((n) => row[n] != null));
|
|
1273
|
+
if (!keyableRows.length) {
|
|
1274
|
+
return rows;
|
|
1275
|
+
}
|
|
1276
|
+
const pkColumns = {};
|
|
1277
|
+
for (const pk of pkNames) {
|
|
1278
|
+
pkColumns[pk] = true;
|
|
1279
|
+
}
|
|
1280
|
+
const relationNames = Object.keys(withParams);
|
|
1281
|
+
const keyOf = (row) => JSON.stringify(pkNames.map((n) => typeof row[n] === "bigint" ? row[n].toString() : row[n]));
|
|
1282
|
+
let whereRaw;
|
|
1283
|
+
if (pkNames.length === 1) {
|
|
1284
|
+
const pkName = pkNames[0];
|
|
1285
|
+
const ids = keyableRows.map((r) => r[pkName]);
|
|
1286
|
+
whereRaw = (aliased) => (0, import_drizzle_orm3.inArray)(aliased[pkName], ids);
|
|
1287
|
+
} else {
|
|
1288
|
+
whereRaw = (aliased) => {
|
|
1289
|
+
const lhs = import_drizzle_orm3.sql.join(
|
|
1290
|
+
pkNames.map((n) => import_drizzle_orm3.sql`${aliased[n]}`),
|
|
1291
|
+
import_drizzle_orm3.sql`, `
|
|
1292
|
+
);
|
|
1293
|
+
const tuples = import_drizzle_orm3.sql.join(
|
|
1294
|
+
keyableRows.map(
|
|
1295
|
+
(row) => import_drizzle_orm3.sql`(${import_drizzle_orm3.sql.join(
|
|
1296
|
+
pkNames.map((n) => import_drizzle_orm3.sql`${row[n]}`),
|
|
1297
|
+
import_drizzle_orm3.sql`, `
|
|
1298
|
+
)})`
|
|
1299
|
+
),
|
|
1300
|
+
import_drizzle_orm3.sql`, `
|
|
1301
|
+
);
|
|
1302
|
+
return import_drizzle_orm3.sql`(${lhs}) in (${tuples})`;
|
|
1303
|
+
};
|
|
1304
|
+
}
|
|
1305
|
+
let enriched;
|
|
1306
|
+
try {
|
|
1307
|
+
enriched = await queryBase.findMany({
|
|
1308
|
+
columns: pkColumns,
|
|
1309
|
+
where: { RAW: whereRaw },
|
|
1310
|
+
with: withParams
|
|
1311
|
+
});
|
|
1312
|
+
} catch (err) {
|
|
1313
|
+
console.warn(
|
|
1314
|
+
`[drizzle-graphql] eager-loading relations for a "${tableName}" mutation failed; falling back to lazy resolution.`,
|
|
1315
|
+
err
|
|
1316
|
+
);
|
|
1317
|
+
return rows;
|
|
1318
|
+
}
|
|
1319
|
+
const byKey = new Map(enriched.map((e) => [keyOf(e), e]));
|
|
1320
|
+
for (const row of rows) {
|
|
1321
|
+
const match = byKey.get(keyOf(row));
|
|
1322
|
+
if (!match) {
|
|
1323
|
+
continue;
|
|
1324
|
+
}
|
|
1325
|
+
for (const rel of relationNames) {
|
|
1326
|
+
row[rel] = match[rel];
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
return rows;
|
|
1003
1330
|
};
|
|
1004
1331
|
|
|
1005
1332
|
// src/util/builders/mysql.ts
|
|
1006
|
-
var
|
|
1007
|
-
var generateSelectArray = (db, tableName, tables, relationMap, orderArgs, filterArgs, listSuffix, singularTypes) => {
|
|
1008
|
-
const queryName = `${uncapitalize(tableName)}${listSuffix}`;
|
|
1333
|
+
var generateSelectArray = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper) => {
|
|
1009
1334
|
const queryBase = db.query[tableName];
|
|
1010
1335
|
if (!queryBase) {
|
|
1011
1336
|
throw new Error(
|
|
1012
1337
|
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
1013
1338
|
);
|
|
1014
1339
|
}
|
|
1015
|
-
const queryArgs =
|
|
1016
|
-
offset: {
|
|
1017
|
-
type: import_graphql4.GraphQLInt
|
|
1018
|
-
},
|
|
1019
|
-
limit: {
|
|
1020
|
-
type: import_graphql4.GraphQLInt
|
|
1021
|
-
},
|
|
1022
|
-
orderBy: {
|
|
1023
|
-
type: orderArgs
|
|
1024
|
-
},
|
|
1025
|
-
where: {
|
|
1026
|
-
type: filterArgs
|
|
1027
|
-
}
|
|
1028
|
-
};
|
|
1029
|
-
const typeName = toTypeName2(tableName, singularTypes);
|
|
1340
|
+
const queryArgs = selectArrayArgs(orderArgs, filterArgs);
|
|
1030
1341
|
const table = tables[tableName];
|
|
1031
1342
|
return {
|
|
1032
|
-
name:
|
|
1343
|
+
name: fieldName,
|
|
1033
1344
|
resolver: async (_source, args, _context, info) => {
|
|
1034
1345
|
try {
|
|
1035
|
-
const
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
with: relationMap[tableName] ? extractRelationsParams(relationMap, tables, tableName, parsedInfo, typeName, singularTypes) : void 0
|
|
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
|
|
1048
1358
|
});
|
|
1049
|
-
const result = await query;
|
|
1050
|
-
return remapToGraphQLArrayOutput(result, tableName, table, relationMap);
|
|
1051
1359
|
} catch (e) {
|
|
1052
|
-
|
|
1053
|
-
throw new import_graphql4.GraphQLError(e.message);
|
|
1054
|
-
}
|
|
1055
|
-
throw e;
|
|
1360
|
+
throw toGraphQLError(e);
|
|
1056
1361
|
}
|
|
1057
1362
|
},
|
|
1058
1363
|
args: queryArgs
|
|
1059
1364
|
};
|
|
1060
1365
|
};
|
|
1061
|
-
var generateSelectSingle = (db, tableName, tables, relationMap, orderArgs, filterArgs,
|
|
1062
|
-
const queryName = `${uncapitalize(tableName)}${singleSuffix}`;
|
|
1366
|
+
var generateSelectSingle = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper) => {
|
|
1063
1367
|
const queryBase = db.query[tableName];
|
|
1064
1368
|
if (!queryBase) {
|
|
1065
1369
|
throw new Error(
|
|
1066
1370
|
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
1067
1371
|
);
|
|
1068
1372
|
}
|
|
1069
|
-
const queryArgs =
|
|
1070
|
-
offset: {
|
|
1071
|
-
type: import_graphql4.GraphQLInt
|
|
1072
|
-
},
|
|
1073
|
-
orderBy: {
|
|
1074
|
-
type: orderArgs
|
|
1075
|
-
},
|
|
1076
|
-
where: {
|
|
1077
|
-
type: filterArgs
|
|
1078
|
-
}
|
|
1079
|
-
};
|
|
1080
|
-
const typeName = toTypeName2(tableName, singularTypes);
|
|
1373
|
+
const queryArgs = selectSingleArgs(orderArgs, filterArgs);
|
|
1081
1374
|
const table = tables[tableName];
|
|
1082
1375
|
return {
|
|
1083
|
-
name:
|
|
1376
|
+
name: fieldName,
|
|
1084
1377
|
resolver: async (_source, args, _context, info) => {
|
|
1085
1378
|
try {
|
|
1086
|
-
const
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1379
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info.parseResolveInfo)(info, { deep: true });
|
|
1380
|
+
return await runRelationalSelect({
|
|
1381
|
+
queryBase,
|
|
1382
|
+
tables,
|
|
1383
|
+
tableName,
|
|
1384
|
+
table,
|
|
1385
|
+
relationMap,
|
|
1386
|
+
typeName,
|
|
1387
|
+
typeNameMapper,
|
|
1388
|
+
parsedInfo,
|
|
1389
|
+
...args,
|
|
1390
|
+
single: true
|
|
1098
1391
|
});
|
|
1099
|
-
const result = await query;
|
|
1100
|
-
if (!result) {
|
|
1101
|
-
return void 0;
|
|
1102
|
-
}
|
|
1103
|
-
return remapToGraphQLSingleOutput(result, tableName, table, relationMap);
|
|
1104
1392
|
} catch (e) {
|
|
1105
|
-
|
|
1106
|
-
throw new import_graphql4.GraphQLError(e.message);
|
|
1107
|
-
}
|
|
1108
|
-
throw e;
|
|
1393
|
+
throw toGraphQLError(e);
|
|
1109
1394
|
}
|
|
1110
1395
|
},
|
|
1111
1396
|
args: queryArgs
|
|
1112
1397
|
};
|
|
1113
1398
|
};
|
|
1114
|
-
var generateInsertArray = (db,
|
|
1115
|
-
const queryName = `insertInto${capitalize(tableName)}`;
|
|
1399
|
+
var generateInsertArray = (db, _tableName, table, baseType, fieldName) => {
|
|
1116
1400
|
const queryArgs = {
|
|
1117
1401
|
values: {
|
|
1118
1402
|
type: new import_graphql4.GraphQLNonNull(new import_graphql4.GraphQLList(new import_graphql4.GraphQLNonNull(baseType)))
|
|
1119
1403
|
}
|
|
1120
1404
|
};
|
|
1121
1405
|
return {
|
|
1122
|
-
name:
|
|
1406
|
+
name: fieldName,
|
|
1123
1407
|
resolver: async (_source, args, _context, _info) => {
|
|
1124
1408
|
try {
|
|
1125
1409
|
const input = remapFromGraphQLArrayInput(args.values, table);
|
|
@@ -1129,41 +1413,33 @@ var generateInsertArray = (db, tableName, table, baseType) => {
|
|
|
1129
1413
|
await db.insert(table).values(input);
|
|
1130
1414
|
return { isSuccess: true };
|
|
1131
1415
|
} catch (e) {
|
|
1132
|
-
|
|
1133
|
-
throw new import_graphql4.GraphQLError(e.message);
|
|
1134
|
-
}
|
|
1135
|
-
throw e;
|
|
1416
|
+
throw toGraphQLError(e);
|
|
1136
1417
|
}
|
|
1137
1418
|
},
|
|
1138
1419
|
args: queryArgs
|
|
1139
1420
|
};
|
|
1140
1421
|
};
|
|
1141
|
-
var generateInsertSingle = (db,
|
|
1142
|
-
const queryName = `insertInto${capitalize(tableName)}Single`;
|
|
1422
|
+
var generateInsertSingle = (db, _tableName, table, baseType, fieldName) => {
|
|
1143
1423
|
const queryArgs = {
|
|
1144
1424
|
values: {
|
|
1145
1425
|
type: new import_graphql4.GraphQLNonNull(baseType)
|
|
1146
1426
|
}
|
|
1147
1427
|
};
|
|
1148
1428
|
return {
|
|
1149
|
-
name:
|
|
1429
|
+
name: fieldName,
|
|
1150
1430
|
resolver: async (_source, args, _context, _info) => {
|
|
1151
1431
|
try {
|
|
1152
1432
|
const input = remapFromGraphQLSingleInput(args.values, table);
|
|
1153
1433
|
await db.insert(table).values(input);
|
|
1154
1434
|
return { isSuccess: true };
|
|
1155
1435
|
} catch (e) {
|
|
1156
|
-
|
|
1157
|
-
throw new import_graphql4.GraphQLError(e.message);
|
|
1158
|
-
}
|
|
1159
|
-
throw e;
|
|
1436
|
+
throw toGraphQLError(e);
|
|
1160
1437
|
}
|
|
1161
1438
|
},
|
|
1162
1439
|
args: queryArgs
|
|
1163
1440
|
};
|
|
1164
1441
|
};
|
|
1165
|
-
var generateUpdate = (db, tableName, table, setArgs, filterArgs) => {
|
|
1166
|
-
const queryName = `update${capitalize(tableName)}`;
|
|
1442
|
+
var generateUpdate = (db, tableName, table, setArgs, filterArgs, fieldName) => {
|
|
1167
1443
|
const queryArgs = {
|
|
1168
1444
|
set: {
|
|
1169
1445
|
type: new import_graphql4.GraphQLNonNull(setArgs)
|
|
@@ -1173,7 +1449,7 @@ var generateUpdate = (db, tableName, table, setArgs, filterArgs) => {
|
|
|
1173
1449
|
}
|
|
1174
1450
|
};
|
|
1175
1451
|
return {
|
|
1176
|
-
name:
|
|
1452
|
+
name: fieldName,
|
|
1177
1453
|
resolver: async (_source, args, _context, _info) => {
|
|
1178
1454
|
try {
|
|
1179
1455
|
const { where, set } = args;
|
|
@@ -1189,24 +1465,20 @@ var generateUpdate = (db, tableName, table, setArgs, filterArgs) => {
|
|
|
1189
1465
|
await query;
|
|
1190
1466
|
return { isSuccess: true };
|
|
1191
1467
|
} catch (e) {
|
|
1192
|
-
|
|
1193
|
-
throw new import_graphql4.GraphQLError(e.message);
|
|
1194
|
-
}
|
|
1195
|
-
throw e;
|
|
1468
|
+
throw toGraphQLError(e);
|
|
1196
1469
|
}
|
|
1197
1470
|
},
|
|
1198
1471
|
args: queryArgs
|
|
1199
1472
|
};
|
|
1200
1473
|
};
|
|
1201
|
-
var generateDelete = (db, tableName, table, filterArgs) => {
|
|
1202
|
-
const queryName = `deleteFrom${capitalize(tableName)}`;
|
|
1474
|
+
var generateDelete = (db, tableName, table, filterArgs, fieldName) => {
|
|
1203
1475
|
const queryArgs = {
|
|
1204
1476
|
where: {
|
|
1205
1477
|
type: filterArgs
|
|
1206
1478
|
}
|
|
1207
1479
|
};
|
|
1208
1480
|
return {
|
|
1209
|
-
name:
|
|
1481
|
+
name: fieldName,
|
|
1210
1482
|
resolver: async (_source, args, _context, _info) => {
|
|
1211
1483
|
try {
|
|
1212
1484
|
const { where } = args;
|
|
@@ -1218,48 +1490,14 @@ var generateDelete = (db, tableName, table, filterArgs) => {
|
|
|
1218
1490
|
await query;
|
|
1219
1491
|
return { isSuccess: true };
|
|
1220
1492
|
} catch (e) {
|
|
1221
|
-
|
|
1222
|
-
throw new import_graphql4.GraphQLError(e.message);
|
|
1223
|
-
}
|
|
1224
|
-
throw e;
|
|
1493
|
+
throw toGraphQLError(e);
|
|
1225
1494
|
}
|
|
1226
1495
|
},
|
|
1227
1496
|
args: queryArgs
|
|
1228
1497
|
};
|
|
1229
1498
|
};
|
|
1230
|
-
var
|
|
1231
|
-
|
|
1232
|
-
for (const [relTableName, relConfig] of Object.entries(relations)) {
|
|
1233
|
-
if (!relConfig?.relations) {
|
|
1234
|
-
continue;
|
|
1235
|
-
}
|
|
1236
|
-
const namedConfig = {};
|
|
1237
|
-
for (const [innerRelName, innerRelValue] of Object.entries(relConfig.relations)) {
|
|
1238
|
-
const targetTable = innerRelValue.targetTable ?? innerRelValue.referencedTable;
|
|
1239
|
-
const directTargetName = innerRelValue.targetTableName;
|
|
1240
|
-
let targetTableName;
|
|
1241
|
-
if (directTargetName) {
|
|
1242
|
-
const targetEntry = tableEntries.find(([key]) => key === directTargetName);
|
|
1243
|
-
targetTableName = targetEntry?.[0];
|
|
1244
|
-
} else if (targetTable) {
|
|
1245
|
-
const targetEntry = tableEntries.find(([, tableValue]) => tableValue === targetTable);
|
|
1246
|
-
targetTableName = targetEntry?.[0];
|
|
1247
|
-
}
|
|
1248
|
-
if (!targetTableName) {
|
|
1249
|
-
continue;
|
|
1250
|
-
}
|
|
1251
|
-
namedConfig[innerRelName] = {
|
|
1252
|
-
relation: innerRelValue,
|
|
1253
|
-
targetTableName
|
|
1254
|
-
};
|
|
1255
|
-
}
|
|
1256
|
-
if (Object.keys(namedConfig).length > 0) {
|
|
1257
|
-
namedRelations[relTableName] = namedConfig;
|
|
1258
|
-
}
|
|
1259
|
-
}
|
|
1260
|
-
return namedRelations;
|
|
1261
|
-
};
|
|
1262
|
-
var generateSchemaData = (db, schema, relations, relationsDepthLimit, prefixes, suffixes, singularTypes = false) => {
|
|
1499
|
+
var mysqlPrimaryKeyPropNames = (table) => getPrimaryKeyPropNamesFromConfig(table, import_mysql_core2.getTableConfig);
|
|
1500
|
+
var generateSchemaData = (db, schema, relations, relationsDepthLimit, prefixes, suffixes, typeNameMapper, shouldEagerLoad = () => true) => {
|
|
1263
1501
|
const rawSchema = schema;
|
|
1264
1502
|
const schemaEntries = Object.entries(rawSchema);
|
|
1265
1503
|
const tableEntries = schemaEntries.filter(([_key, value]) => (0, import_drizzle_orm4.is)(value, import_mysql_core2.MySqlTable));
|
|
@@ -1269,13 +1507,18 @@ var generateSchemaData = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
1269
1507
|
"Drizzle-GraphQL Error: No tables detected in Drizzle-ORM's database instance. Did you forget to pass schema to drizzle constructor?"
|
|
1270
1508
|
);
|
|
1271
1509
|
}
|
|
1272
|
-
const namedRelations =
|
|
1510
|
+
const namedRelations = buildNamedRelations(relations ?? {}, tableEntries);
|
|
1511
|
+
attachTargetPrimaryKeys(namedRelations, tables, mysqlPrimaryKeyPropNames);
|
|
1512
|
+
const eagerRelations = pruneNonEagerRelations(namedRelations, shouldEagerLoad);
|
|
1513
|
+
const resolverFactory = createRelationResolverFactory(db, tables);
|
|
1273
1514
|
const cacheCtx = {
|
|
1274
1515
|
genericFilterCache: /* @__PURE__ */ new Map(),
|
|
1275
1516
|
objectTypeCache: /* @__PURE__ */ new Map(),
|
|
1276
1517
|
relationFieldContainers: /* @__PURE__ */ new Map(),
|
|
1277
1518
|
fullyBuiltTables: /* @__PURE__ */ new Set(),
|
|
1278
|
-
relationTypeCache: /* @__PURE__ */ new Map()
|
|
1519
|
+
relationTypeCache: /* @__PURE__ */ new Map(),
|
|
1520
|
+
orderTypeCache: /* @__PURE__ */ new WeakMap(),
|
|
1521
|
+
filterTypeCache: /* @__PURE__ */ new WeakMap()
|
|
1279
1522
|
};
|
|
1280
1523
|
const queries = {};
|
|
1281
1524
|
const mutations = {};
|
|
@@ -1289,9 +1532,10 @@ var generateSchemaData = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
1289
1532
|
false,
|
|
1290
1533
|
relationsDepthLimit,
|
|
1291
1534
|
cacheCtx,
|
|
1292
|
-
|
|
1535
|
+
typeNameMapper,
|
|
1293
1536
|
prefixes.insert,
|
|
1294
|
-
prefixes.update
|
|
1537
|
+
prefixes.update,
|
|
1538
|
+
resolverFactory
|
|
1295
1539
|
)
|
|
1296
1540
|
])
|
|
1297
1541
|
);
|
|
@@ -1310,30 +1554,66 @@ var generateSchemaData = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
1310
1554
|
for (const [tableName, tableTypes] of Object.entries(gqlSchemaTypes)) {
|
|
1311
1555
|
const { insertInput, updateInput, tableFilters, tableOrder } = tableTypes.inputs;
|
|
1312
1556
|
const { selectSingleOutput, selectArrOutput } = tableTypes.outputs;
|
|
1557
|
+
const {
|
|
1558
|
+
typeName,
|
|
1559
|
+
listFieldName,
|
|
1560
|
+
singleFieldName,
|
|
1561
|
+
createArrayFieldName,
|
|
1562
|
+
createSingleFieldName,
|
|
1563
|
+
updateFieldName,
|
|
1564
|
+
deleteFieldName
|
|
1565
|
+
} = computeResolverFieldNames(tableName, typeNameMapper, prefixes, suffixes);
|
|
1313
1566
|
const selectArrGenerated = generateSelectArray(
|
|
1314
1567
|
db,
|
|
1315
1568
|
tableName,
|
|
1316
1569
|
tables,
|
|
1317
|
-
|
|
1570
|
+
eagerRelations,
|
|
1318
1571
|
tableOrder,
|
|
1319
1572
|
tableFilters,
|
|
1320
|
-
|
|
1321
|
-
|
|
1573
|
+
listFieldName,
|
|
1574
|
+
typeName,
|
|
1575
|
+
typeNameMapper
|
|
1322
1576
|
);
|
|
1323
1577
|
const selectSingleGenerated = generateSelectSingle(
|
|
1324
1578
|
db,
|
|
1325
1579
|
tableName,
|
|
1326
1580
|
tables,
|
|
1327
|
-
|
|
1581
|
+
eagerRelations,
|
|
1328
1582
|
tableOrder,
|
|
1329
1583
|
tableFilters,
|
|
1330
|
-
|
|
1331
|
-
|
|
1584
|
+
singleFieldName,
|
|
1585
|
+
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
|
|
1601
|
+
);
|
|
1602
|
+
const updateGenerated = generateUpdate(
|
|
1603
|
+
db,
|
|
1604
|
+
tableName,
|
|
1605
|
+
schema[tableName],
|
|
1606
|
+
updateInput,
|
|
1607
|
+
tableFilters,
|
|
1608
|
+
updateFieldName
|
|
1609
|
+
);
|
|
1610
|
+
const deleteGenerated = generateDelete(
|
|
1611
|
+
db,
|
|
1612
|
+
tableName,
|
|
1613
|
+
schema[tableName],
|
|
1614
|
+
tableFilters,
|
|
1615
|
+
deleteFieldName
|
|
1332
1616
|
);
|
|
1333
|
-
const insertArrGenerated = generateInsertArray(db, tableName, schema[tableName], insertInput);
|
|
1334
|
-
const insertSingleGenerated = generateInsertSingle(db, tableName, schema[tableName], insertInput);
|
|
1335
|
-
const updateGenerated = generateUpdate(db, tableName, schema[tableName], updateInput, tableFilters);
|
|
1336
|
-
const deleteGenerated = generateDelete(db, tableName, schema[tableName], tableFilters);
|
|
1337
1617
|
queries[selectArrGenerated.name] = {
|
|
1338
1618
|
type: selectArrOutput,
|
|
1339
1619
|
args: selectArrGenerated.args,
|
|
@@ -1369,7 +1649,21 @@ var generateSchemaData = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
1369
1649
|
});
|
|
1370
1650
|
outputs[selectSingleOutput.name] = selectSingleOutput;
|
|
1371
1651
|
}
|
|
1372
|
-
|
|
1652
|
+
const fieldResolvers = {};
|
|
1653
|
+
for (const [tableName, tableRelations] of Object.entries(namedRelations)) {
|
|
1654
|
+
const relResolvers = {};
|
|
1655
|
+
for (const [relName, relEntry] of Object.entries(tableRelations)) {
|
|
1656
|
+
const isOne = (0, import_drizzle_orm4.is)(relEntry.relation ?? relEntry, import_drizzle_orm4.One);
|
|
1657
|
+
const resolver = resolverFactory({ tableName, relationName: relName, relEntry, isOne });
|
|
1658
|
+
if (resolver) {
|
|
1659
|
+
relResolvers[relName] = resolver;
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
if (Object.keys(relResolvers).length > 0) {
|
|
1663
|
+
fieldResolvers[tableName] = relResolvers;
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
return { queries, mutations, inputs, types: outputs, fieldResolvers };
|
|
1373
1667
|
};
|
|
1374
1668
|
|
|
1375
1669
|
// src/util/builders/pg.ts
|
|
@@ -1377,151 +1671,113 @@ var import_drizzle_orm5 = require("drizzle-orm");
|
|
|
1377
1671
|
var import_pg_core2 = require("drizzle-orm/pg-core");
|
|
1378
1672
|
var import_graphql5 = require("graphql");
|
|
1379
1673
|
var import_graphql_parse_resolve_info2 = require("graphql-parse-resolve-info");
|
|
1380
|
-
var
|
|
1381
|
-
var generateSelectArray2 = (db, tableName, tables, relationMap, orderArgs, filterArgs, listSuffix, singularTypes) => {
|
|
1382
|
-
const queryEntityBase = singularTypes ? singularize(uncapitalize(tableName)) : uncapitalize(tableName);
|
|
1383
|
-
const queryName = `${queryEntityBase}${listSuffix}`;
|
|
1674
|
+
var generateSelectArray2 = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper) => {
|
|
1384
1675
|
const queryBase = db.query[tableName];
|
|
1385
|
-
const queryArgs =
|
|
1386
|
-
offset: {
|
|
1387
|
-
type: import_graphql5.GraphQLInt
|
|
1388
|
-
},
|
|
1389
|
-
limit: {
|
|
1390
|
-
type: import_graphql5.GraphQLInt
|
|
1391
|
-
},
|
|
1392
|
-
orderBy: {
|
|
1393
|
-
type: orderArgs
|
|
1394
|
-
},
|
|
1395
|
-
where: {
|
|
1396
|
-
type: filterArgs
|
|
1397
|
-
}
|
|
1398
|
-
};
|
|
1399
|
-
const typeName = toTypeName3(tableName, singularTypes);
|
|
1676
|
+
const queryArgs = selectArrayArgs(orderArgs, filterArgs);
|
|
1400
1677
|
const table = tables[tableName];
|
|
1401
1678
|
return {
|
|
1402
|
-
name:
|
|
1679
|
+
name: fieldName,
|
|
1403
1680
|
resolver: async (_source, args, _context, info) => {
|
|
1404
1681
|
try {
|
|
1405
|
-
const
|
|
1406
|
-
const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, {
|
|
1407
|
-
deep: true
|
|
1408
|
-
});
|
|
1409
|
-
const selectedColumns = extractSelectedColumnsFromTree(parsedInfo.fieldsByTypeName[typeName], table);
|
|
1410
|
-
let result;
|
|
1682
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, { deep: true });
|
|
1411
1683
|
if (queryBase) {
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1684
|
+
return await runRelationalSelect({
|
|
1685
|
+
queryBase,
|
|
1686
|
+
tables,
|
|
1687
|
+
tableName,
|
|
1688
|
+
table,
|
|
1689
|
+
relationMap,
|
|
1690
|
+
typeName,
|
|
1691
|
+
typeNameMapper,
|
|
1692
|
+
parsedInfo,
|
|
1693
|
+
...args,
|
|
1694
|
+
single: false
|
|
1422
1695
|
});
|
|
1423
|
-
} else {
|
|
1424
|
-
let q = db.select(selectedColumns).from(table);
|
|
1425
|
-
if (where) {
|
|
1426
|
-
q = q.where(extractFilters(table, tableName, where));
|
|
1427
|
-
}
|
|
1428
|
-
if (orderBy) {
|
|
1429
|
-
q = q.orderBy(...extractOrderBy(table, orderBy));
|
|
1430
|
-
}
|
|
1431
|
-
if (offset) {
|
|
1432
|
-
q = q.offset(offset);
|
|
1433
|
-
}
|
|
1434
|
-
if (limit) {
|
|
1435
|
-
q = q.limit(limit);
|
|
1436
|
-
}
|
|
1437
|
-
result = await q;
|
|
1438
1696
|
}
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1697
|
+
const { offset, limit, orderBy, where } = args;
|
|
1698
|
+
const selectedColumnsSql = extractSelectedColumnsFromTreeSQLFormat(
|
|
1699
|
+
parsedInfo.fieldsByTypeName[typeName],
|
|
1700
|
+
table
|
|
1701
|
+
);
|
|
1702
|
+
let q = db.select(selectedColumnsSql).from(table);
|
|
1703
|
+
if (where) {
|
|
1704
|
+
q = q.where(extractFilters(table, tableName, where));
|
|
1443
1705
|
}
|
|
1444
|
-
|
|
1706
|
+
if (orderBy) {
|
|
1707
|
+
q = q.orderBy(...extractOrderBy(table, orderBy));
|
|
1708
|
+
}
|
|
1709
|
+
if (offset) {
|
|
1710
|
+
q = q.offset(offset);
|
|
1711
|
+
}
|
|
1712
|
+
if (limit) {
|
|
1713
|
+
q = q.limit(limit);
|
|
1714
|
+
}
|
|
1715
|
+
return remapToGraphQLArrayOutput(await q, tableName, table, relationMap);
|
|
1716
|
+
} catch (e) {
|
|
1717
|
+
throw toGraphQLError(e);
|
|
1445
1718
|
}
|
|
1446
1719
|
},
|
|
1447
1720
|
args: queryArgs
|
|
1448
1721
|
};
|
|
1449
1722
|
};
|
|
1450
|
-
var generateSelectSingle2 = (db, tableName, tables, relationMap, orderArgs, filterArgs,
|
|
1451
|
-
const queryEntityBase = singularTypes ? singularize(uncapitalize(tableName)) : uncapitalize(tableName);
|
|
1452
|
-
const queryName = `${queryEntityBase}${singleSuffix}`;
|
|
1723
|
+
var generateSelectSingle2 = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper) => {
|
|
1453
1724
|
const queryBase = db.query[tableName];
|
|
1454
|
-
const queryArgs =
|
|
1455
|
-
offset: {
|
|
1456
|
-
type: import_graphql5.GraphQLInt
|
|
1457
|
-
},
|
|
1458
|
-
orderBy: {
|
|
1459
|
-
type: orderArgs
|
|
1460
|
-
},
|
|
1461
|
-
where: {
|
|
1462
|
-
type: filterArgs
|
|
1463
|
-
}
|
|
1464
|
-
};
|
|
1465
|
-
const typeName = toTypeName3(tableName, singularTypes);
|
|
1725
|
+
const queryArgs = selectSingleArgs(orderArgs, filterArgs);
|
|
1466
1726
|
const table = tables[tableName];
|
|
1467
1727
|
return {
|
|
1468
|
-
name:
|
|
1728
|
+
name: fieldName,
|
|
1469
1729
|
resolver: async (_source, args, _context, info) => {
|
|
1470
1730
|
try {
|
|
1471
|
-
const
|
|
1472
|
-
const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, {
|
|
1473
|
-
deep: true
|
|
1474
|
-
});
|
|
1475
|
-
const selectedColumns = extractSelectedColumnsFromTree(parsedInfo.fieldsByTypeName[typeName], table);
|
|
1476
|
-
let result;
|
|
1731
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, { deep: true });
|
|
1477
1732
|
if (queryBase) {
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1733
|
+
return await runRelationalSelect({
|
|
1734
|
+
queryBase,
|
|
1735
|
+
tables,
|
|
1736
|
+
tableName,
|
|
1737
|
+
table,
|
|
1738
|
+
relationMap,
|
|
1739
|
+
typeName,
|
|
1740
|
+
typeNameMapper,
|
|
1741
|
+
parsedInfo,
|
|
1742
|
+
...args,
|
|
1743
|
+
single: true
|
|
1486
1744
|
});
|
|
1487
|
-
} else {
|
|
1488
|
-
let q = db.select(selectedColumns).from(table);
|
|
1489
|
-
if (where) {
|
|
1490
|
-
q = q.where(extractFilters(table, tableName, where));
|
|
1491
|
-
}
|
|
1492
|
-
if (orderBy) {
|
|
1493
|
-
q = q.orderBy(...extractOrderBy(table, orderBy));
|
|
1494
|
-
}
|
|
1495
|
-
if (offset) {
|
|
1496
|
-
q = q.offset(offset);
|
|
1497
|
-
}
|
|
1498
|
-
const rows = await q.limit(1);
|
|
1499
|
-
result = rows[0];
|
|
1500
1745
|
}
|
|
1501
|
-
|
|
1502
|
-
|
|
1746
|
+
const { offset, orderBy, where } = args;
|
|
1747
|
+
const selectedColumnsSql = extractSelectedColumnsFromTreeSQLFormat(
|
|
1748
|
+
parsedInfo.fieldsByTypeName[typeName],
|
|
1749
|
+
table
|
|
1750
|
+
);
|
|
1751
|
+
let q = db.select(selectedColumnsSql).from(table);
|
|
1752
|
+
if (where) {
|
|
1753
|
+
q = q.where(extractFilters(table, tableName, where));
|
|
1503
1754
|
}
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1755
|
+
if (orderBy) {
|
|
1756
|
+
q = q.orderBy(...extractOrderBy(table, orderBy));
|
|
1757
|
+
}
|
|
1758
|
+
if (offset) {
|
|
1759
|
+
q = q.offset(offset);
|
|
1508
1760
|
}
|
|
1509
|
-
|
|
1761
|
+
const rows = await q.limit(1);
|
|
1762
|
+
const result = rows[0];
|
|
1763
|
+
return result ? remapToGraphQLSingleOutput(result, tableName, table, relationMap) : void 0;
|
|
1764
|
+
} catch (e) {
|
|
1765
|
+
throw toGraphQLError(e);
|
|
1510
1766
|
}
|
|
1511
1767
|
},
|
|
1512
1768
|
args: queryArgs
|
|
1513
1769
|
};
|
|
1514
1770
|
};
|
|
1515
|
-
var
|
|
1516
|
-
|
|
1517
|
-
const typeName = toTypeName3(tableName, singularTypes);
|
|
1771
|
+
var pgPrimaryKeyPropNames = (table) => getPrimaryKeyPropNamesFromConfig(table, import_pg_core2.getTableConfig);
|
|
1772
|
+
var generateInsertArray2 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false) => {
|
|
1518
1773
|
const queryArgs = {
|
|
1519
1774
|
values: {
|
|
1520
1775
|
type: new import_graphql5.GraphQLNonNull(new import_graphql5.GraphQLList(new import_graphql5.GraphQLNonNull(baseType)))
|
|
1521
1776
|
}
|
|
1522
1777
|
};
|
|
1778
|
+
const pkNames = pgPrimaryKeyPropNames(table);
|
|
1523
1779
|
return {
|
|
1524
|
-
name:
|
|
1780
|
+
name: fieldName,
|
|
1525
1781
|
resolver: async (_source, args, _context, info) => {
|
|
1526
1782
|
try {
|
|
1527
1783
|
const input = remapFromGraphQLArrayInput(args.values, table);
|
|
@@ -1531,47 +1787,55 @@ var generateInsertArray2 = (db, tableName, table, baseType, prefix, conflictDoNo
|
|
|
1531
1787
|
const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, {
|
|
1532
1788
|
deep: true
|
|
1533
1789
|
});
|
|
1534
|
-
const columns =
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1790
|
+
const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
|
|
1791
|
+
relationMap,
|
|
1792
|
+
tables,
|
|
1793
|
+
tableName,
|
|
1794
|
+
typeName,
|
|
1795
|
+
typeNameMapper,
|
|
1796
|
+
table,
|
|
1797
|
+
pkNames,
|
|
1798
|
+
parsedInfo
|
|
1799
|
+
});
|
|
1538
1800
|
let query = db.insert(table).values(input).returning(columns);
|
|
1539
1801
|
if (conflictDoNothing) {
|
|
1540
1802
|
query = query.onConflictDoNothing();
|
|
1541
1803
|
}
|
|
1542
1804
|
const result = await query;
|
|
1543
|
-
|
|
1805
|
+
const enriched = hasRelations ? await eagerLoadMutationRelations(db, tableName, result, pkNames, withParams) : result;
|
|
1806
|
+
return remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
|
|
1544
1807
|
} catch (e) {
|
|
1545
|
-
|
|
1546
|
-
throw new import_graphql5.GraphQLError(e.message);
|
|
1547
|
-
}
|
|
1548
|
-
throw e;
|
|
1808
|
+
throw toGraphQLError(e);
|
|
1549
1809
|
}
|
|
1550
1810
|
},
|
|
1551
1811
|
args: queryArgs
|
|
1552
1812
|
};
|
|
1553
1813
|
};
|
|
1554
|
-
var generateInsertSingle2 = (db, tableName, table,
|
|
1555
|
-
const queryEntityBase = singularTypes ? singularize(capitalize(tableName)) : capitalize(tableName);
|
|
1556
|
-
const queryName = singularTypes ? `${prefix}${queryEntityBase}` : `${prefix}${queryEntityBase}Single`;
|
|
1557
|
-
const typeName = toTypeName3(tableName, singularTypes);
|
|
1814
|
+
var generateInsertSingle2 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false) => {
|
|
1558
1815
|
const queryArgs = {
|
|
1559
1816
|
values: {
|
|
1560
1817
|
type: new import_graphql5.GraphQLNonNull(baseType)
|
|
1561
1818
|
}
|
|
1562
1819
|
};
|
|
1820
|
+
const pkNames = pgPrimaryKeyPropNames(table);
|
|
1563
1821
|
return {
|
|
1564
|
-
name:
|
|
1822
|
+
name: fieldName,
|
|
1565
1823
|
resolver: async (_source, args, _context, info) => {
|
|
1566
1824
|
try {
|
|
1567
1825
|
const input = remapFromGraphQLSingleInput(args.values, table);
|
|
1568
1826
|
const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, {
|
|
1569
1827
|
deep: true
|
|
1570
1828
|
});
|
|
1571
|
-
const columns =
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1829
|
+
const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
|
|
1830
|
+
relationMap,
|
|
1831
|
+
tables,
|
|
1832
|
+
tableName,
|
|
1833
|
+
typeName,
|
|
1834
|
+
typeNameMapper,
|
|
1835
|
+
table,
|
|
1836
|
+
pkNames,
|
|
1837
|
+
parsedInfo
|
|
1838
|
+
});
|
|
1575
1839
|
let query = db.insert(table).values(input).returning(columns);
|
|
1576
1840
|
if (conflictDoNothing) {
|
|
1577
1841
|
query = query.onConflictDoNothing();
|
|
@@ -1580,20 +1844,16 @@ var generateInsertSingle2 = (db, tableName, table, baseType, prefix, conflictDoN
|
|
|
1580
1844
|
if (!result[0]) {
|
|
1581
1845
|
return void 0;
|
|
1582
1846
|
}
|
|
1583
|
-
|
|
1847
|
+
const enriched = hasRelations ? await eagerLoadMutationRelations(db, tableName, result, pkNames, withParams) : result;
|
|
1848
|
+
return remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap);
|
|
1584
1849
|
} catch (e) {
|
|
1585
|
-
|
|
1586
|
-
throw new import_graphql5.GraphQLError(e.message);
|
|
1587
|
-
}
|
|
1588
|
-
throw e;
|
|
1850
|
+
throw toGraphQLError(e);
|
|
1589
1851
|
}
|
|
1590
1852
|
},
|
|
1591
1853
|
args: queryArgs
|
|
1592
1854
|
};
|
|
1593
1855
|
};
|
|
1594
|
-
var generateUpdate2 = (db, tableName, table, setArgs, filterArgs,
|
|
1595
|
-
const queryName = `${prefix}${capitalize(tableName)}`;
|
|
1596
|
-
const typeName = toTypeName3(tableName, singularTypes);
|
|
1856
|
+
var generateUpdate2 = (db, tableName, table, tables, relationMap, setArgs, filterArgs, fieldName, typeName, typeNameMapper) => {
|
|
1597
1857
|
const queryArgs = {
|
|
1598
1858
|
set: {
|
|
1599
1859
|
type: new import_graphql5.GraphQLNonNull(setArgs)
|
|
@@ -1602,18 +1862,25 @@ var generateUpdate2 = (db, tableName, table, setArgs, filterArgs, prefix, singul
|
|
|
1602
1862
|
type: filterArgs
|
|
1603
1863
|
}
|
|
1604
1864
|
};
|
|
1865
|
+
const pkNames = pgPrimaryKeyPropNames(table);
|
|
1605
1866
|
return {
|
|
1606
|
-
name:
|
|
1867
|
+
name: fieldName,
|
|
1607
1868
|
resolver: async (_source, args, _context, info) => {
|
|
1608
1869
|
try {
|
|
1609
1870
|
const { where, set } = args;
|
|
1610
1871
|
const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, {
|
|
1611
1872
|
deep: true
|
|
1612
1873
|
});
|
|
1613
|
-
const columns =
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1874
|
+
const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
|
|
1875
|
+
relationMap,
|
|
1876
|
+
tables,
|
|
1877
|
+
tableName,
|
|
1878
|
+
typeName,
|
|
1879
|
+
typeNameMapper,
|
|
1880
|
+
table,
|
|
1881
|
+
pkNames,
|
|
1882
|
+
parsedInfo
|
|
1883
|
+
});
|
|
1617
1884
|
const input = remapFromGraphQLSingleInput(set, table);
|
|
1618
1885
|
if (!Object.keys(input).length) {
|
|
1619
1886
|
throw new import_graphql5.GraphQLError("Unable to update with no values specified!");
|
|
@@ -1625,27 +1892,23 @@ var generateUpdate2 = (db, tableName, table, setArgs, filterArgs, prefix, singul
|
|
|
1625
1892
|
}
|
|
1626
1893
|
query = query.returning(columns);
|
|
1627
1894
|
const result = await query;
|
|
1628
|
-
|
|
1895
|
+
const enriched = hasRelations ? await eagerLoadMutationRelations(db, tableName, result, pkNames, withParams) : result;
|
|
1896
|
+
return remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
|
|
1629
1897
|
} catch (e) {
|
|
1630
|
-
|
|
1631
|
-
throw new import_graphql5.GraphQLError(e.message);
|
|
1632
|
-
}
|
|
1633
|
-
throw e;
|
|
1898
|
+
throw toGraphQLError(e);
|
|
1634
1899
|
}
|
|
1635
1900
|
},
|
|
1636
1901
|
args: queryArgs
|
|
1637
1902
|
};
|
|
1638
1903
|
};
|
|
1639
|
-
var generateDelete2 = (db, tableName, table, filterArgs,
|
|
1640
|
-
const queryName = `${prefix}${capitalize(tableName)}`;
|
|
1641
|
-
const typeName = toTypeName3(tableName, singularTypes);
|
|
1904
|
+
var generateDelete2 = (db, tableName, table, filterArgs, fieldName, typeName) => {
|
|
1642
1905
|
const queryArgs = {
|
|
1643
1906
|
where: {
|
|
1644
1907
|
type: filterArgs
|
|
1645
1908
|
}
|
|
1646
1909
|
};
|
|
1647
1910
|
return {
|
|
1648
|
-
name:
|
|
1911
|
+
name: fieldName,
|
|
1649
1912
|
resolver: async (_source, args, _context, info) => {
|
|
1650
1913
|
try {
|
|
1651
1914
|
const { where } = args;
|
|
@@ -1665,16 +1928,13 @@ var generateDelete2 = (db, tableName, table, filterArgs, prefix, singularTypes =
|
|
|
1665
1928
|
const result = await query;
|
|
1666
1929
|
return remapToGraphQLArrayOutput(result, tableName, table);
|
|
1667
1930
|
} catch (e) {
|
|
1668
|
-
|
|
1669
|
-
throw new import_graphql5.GraphQLError(e.message);
|
|
1670
|
-
}
|
|
1671
|
-
throw e;
|
|
1931
|
+
throw toGraphQLError(e);
|
|
1672
1932
|
}
|
|
1673
1933
|
},
|
|
1674
1934
|
args: queryArgs
|
|
1675
1935
|
};
|
|
1676
1936
|
};
|
|
1677
|
-
function generateSchemaData2(db, schema, relations, relationsDepthLimit, prefixes, suffixes, conflictDoNothing = false,
|
|
1937
|
+
function generateSchemaData2(db, schema, relations, relationsDepthLimit, prefixes, suffixes, conflictDoNothing = false, typeNameMapper, shouldEagerLoad = () => true) {
|
|
1678
1938
|
const schemaEntries = Object.entries(schema);
|
|
1679
1939
|
const tableEntries = schemaEntries.filter(([_key, value]) => (0, import_drizzle_orm5.is)(value, import_pg_core2.PgTable));
|
|
1680
1940
|
const tables = Object.fromEntries(tableEntries);
|
|
@@ -1684,12 +1944,17 @@ function generateSchemaData2(db, schema, relations, relationsDepthLimit, prefixe
|
|
|
1684
1944
|
);
|
|
1685
1945
|
}
|
|
1686
1946
|
const namedRelations = buildNamedRelations(relations ?? {}, tableEntries);
|
|
1947
|
+
attachTargetPrimaryKeys(namedRelations, tables, pgPrimaryKeyPropNames);
|
|
1948
|
+
const eagerRelations = pruneNonEagerRelations(namedRelations, shouldEagerLoad);
|
|
1949
|
+
const resolverFactory = createRelationResolverFactory(db, tables);
|
|
1687
1950
|
const cacheCtx = {
|
|
1688
1951
|
genericFilterCache: /* @__PURE__ */ new Map(),
|
|
1689
1952
|
objectTypeCache: /* @__PURE__ */ new Map(),
|
|
1690
1953
|
relationFieldContainers: /* @__PURE__ */ new Map(),
|
|
1691
1954
|
fullyBuiltTables: /* @__PURE__ */ new Set(),
|
|
1692
|
-
relationTypeCache: /* @__PURE__ */ new Map()
|
|
1955
|
+
relationTypeCache: /* @__PURE__ */ new Map(),
|
|
1956
|
+
orderTypeCache: /* @__PURE__ */ new WeakMap(),
|
|
1957
|
+
filterTypeCache: /* @__PURE__ */ new WeakMap()
|
|
1693
1958
|
};
|
|
1694
1959
|
const queries = {};
|
|
1695
1960
|
const mutations = {};
|
|
@@ -1703,9 +1968,10 @@ function generateSchemaData2(db, schema, relations, relationsDepthLimit, prefixe
|
|
|
1703
1968
|
true,
|
|
1704
1969
|
relationsDepthLimit,
|
|
1705
1970
|
cacheCtx,
|
|
1706
|
-
|
|
1971
|
+
typeNameMapper,
|
|
1707
1972
|
prefixes.insert,
|
|
1708
|
-
prefixes.update
|
|
1973
|
+
prefixes.update,
|
|
1974
|
+
resolverFactory
|
|
1709
1975
|
)
|
|
1710
1976
|
])
|
|
1711
1977
|
);
|
|
@@ -1714,60 +1980,80 @@ function generateSchemaData2(db, schema, relations, relationsDepthLimit, prefixe
|
|
|
1714
1980
|
for (const [tableName, tableTypes] of Object.entries(gqlSchemaTypes)) {
|
|
1715
1981
|
const { insertInput, updateInput, tableFilters, tableOrder } = tableTypes.inputs;
|
|
1716
1982
|
const { selectSingleOutput, selectArrOutput, singleTableItemOutput, arrTableItemOutput } = tableTypes.outputs;
|
|
1983
|
+
const {
|
|
1984
|
+
typeName,
|
|
1985
|
+
listFieldName,
|
|
1986
|
+
singleFieldName,
|
|
1987
|
+
createArrayFieldName,
|
|
1988
|
+
createSingleFieldName,
|
|
1989
|
+
updateFieldName,
|
|
1990
|
+
deleteFieldName
|
|
1991
|
+
} = computeResolverFieldNames(tableName, typeNameMapper, prefixes, suffixes);
|
|
1717
1992
|
const selectArrGenerated = generateSelectArray2(
|
|
1718
1993
|
db,
|
|
1719
1994
|
tableName,
|
|
1720
1995
|
tables,
|
|
1721
|
-
|
|
1996
|
+
eagerRelations,
|
|
1722
1997
|
tableOrder,
|
|
1723
1998
|
tableFilters,
|
|
1724
|
-
|
|
1725
|
-
|
|
1999
|
+
listFieldName,
|
|
2000
|
+
typeName,
|
|
2001
|
+
typeNameMapper
|
|
1726
2002
|
);
|
|
1727
2003
|
const selectSingleGenerated = generateSelectSingle2(
|
|
1728
2004
|
db,
|
|
1729
2005
|
tableName,
|
|
1730
2006
|
tables,
|
|
1731
|
-
|
|
2007
|
+
eagerRelations,
|
|
1732
2008
|
tableOrder,
|
|
1733
2009
|
tableFilters,
|
|
1734
|
-
|
|
1735
|
-
|
|
2010
|
+
singleFieldName,
|
|
2011
|
+
typeName,
|
|
2012
|
+
typeNameMapper
|
|
1736
2013
|
);
|
|
1737
2014
|
const insertArrGenerated = generateInsertArray2(
|
|
1738
2015
|
db,
|
|
1739
2016
|
tableName,
|
|
1740
2017
|
schema[tableName],
|
|
2018
|
+
tables,
|
|
2019
|
+
eagerRelations,
|
|
1741
2020
|
insertInput,
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
2021
|
+
createArrayFieldName,
|
|
2022
|
+
typeName,
|
|
2023
|
+
typeNameMapper,
|
|
2024
|
+
conflictDoNothing
|
|
1745
2025
|
);
|
|
1746
2026
|
const insertSingleGenerated = generateInsertSingle2(
|
|
1747
2027
|
db,
|
|
1748
2028
|
tableName,
|
|
1749
2029
|
schema[tableName],
|
|
2030
|
+
tables,
|
|
2031
|
+
eagerRelations,
|
|
1750
2032
|
insertInput,
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
2033
|
+
createSingleFieldName,
|
|
2034
|
+
typeName,
|
|
2035
|
+
typeNameMapper,
|
|
2036
|
+
conflictDoNothing
|
|
1754
2037
|
);
|
|
1755
2038
|
const updateGenerated = generateUpdate2(
|
|
1756
2039
|
db,
|
|
1757
2040
|
tableName,
|
|
1758
2041
|
schema[tableName],
|
|
2042
|
+
tables,
|
|
2043
|
+
eagerRelations,
|
|
1759
2044
|
updateInput,
|
|
1760
2045
|
tableFilters,
|
|
1761
|
-
|
|
1762
|
-
|
|
2046
|
+
updateFieldName,
|
|
2047
|
+
typeName,
|
|
2048
|
+
typeNameMapper
|
|
1763
2049
|
);
|
|
1764
2050
|
const deleteGenerated = generateDelete2(
|
|
1765
2051
|
db,
|
|
1766
2052
|
tableName,
|
|
1767
2053
|
schema[tableName],
|
|
1768
2054
|
tableFilters,
|
|
1769
|
-
|
|
1770
|
-
|
|
2055
|
+
deleteFieldName,
|
|
2056
|
+
typeName
|
|
1771
2057
|
);
|
|
1772
2058
|
queries[selectArrGenerated.name] = {
|
|
1773
2059
|
type: selectArrOutput,
|
|
@@ -1805,7 +2091,21 @@ function generateSchemaData2(db, schema, relations, relationsDepthLimit, prefixe
|
|
|
1805
2091
|
outputs[selectSingleOutput.name] = selectSingleOutput;
|
|
1806
2092
|
outputs[singleTableItemOutput.name] = singleTableItemOutput;
|
|
1807
2093
|
}
|
|
1808
|
-
|
|
2094
|
+
const fieldResolvers = {};
|
|
2095
|
+
for (const [tableName, tableRelations] of Object.entries(namedRelations)) {
|
|
2096
|
+
const relResolvers = {};
|
|
2097
|
+
for (const [relName, relEntry] of Object.entries(tableRelations)) {
|
|
2098
|
+
const isOne = (0, import_drizzle_orm5.is)(relEntry.relation ?? relEntry, import_drizzle_orm5.One);
|
|
2099
|
+
const resolver = resolverFactory({ tableName, relationName: relName, relEntry, isOne });
|
|
2100
|
+
if (resolver) {
|
|
2101
|
+
relResolvers[relName] = resolver;
|
|
2102
|
+
}
|
|
2103
|
+
}
|
|
2104
|
+
if (Object.keys(relResolvers).length > 0) {
|
|
2105
|
+
fieldResolvers[tableName] = relResolvers;
|
|
2106
|
+
}
|
|
2107
|
+
}
|
|
2108
|
+
return { queries, mutations, inputs, types: outputs, fieldResolvers };
|
|
1809
2109
|
}
|
|
1810
2110
|
|
|
1811
2111
|
// src/util/builders/sqlite.ts
|
|
@@ -1813,126 +2113,82 @@ var import_drizzle_orm6 = require("drizzle-orm");
|
|
|
1813
2113
|
var import_sqlite_core2 = require("drizzle-orm/sqlite-core");
|
|
1814
2114
|
var import_graphql6 = require("graphql");
|
|
1815
2115
|
var import_graphql_parse_resolve_info3 = require("graphql-parse-resolve-info");
|
|
1816
|
-
var
|
|
1817
|
-
var generateSelectArray3 = (db, tableName, tables, relationMap, orderArgs, filterArgs, listSuffix, singularTypes) => {
|
|
1818
|
-
const queryEntityBase = singularTypes ? singularize(uncapitalize(tableName)) : uncapitalize(tableName);
|
|
1819
|
-
const queryName = `${queryEntityBase}${listSuffix}`;
|
|
2116
|
+
var generateSelectArray3 = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper) => {
|
|
1820
2117
|
const queryBase = db.query[tableName];
|
|
1821
2118
|
if (!queryBase) {
|
|
1822
2119
|
throw new Error(
|
|
1823
2120
|
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
1824
2121
|
);
|
|
1825
2122
|
}
|
|
1826
|
-
const queryArgs =
|
|
1827
|
-
offset: {
|
|
1828
|
-
type: import_graphql6.GraphQLInt
|
|
1829
|
-
},
|
|
1830
|
-
limit: {
|
|
1831
|
-
type: import_graphql6.GraphQLInt
|
|
1832
|
-
},
|
|
1833
|
-
orderBy: {
|
|
1834
|
-
type: orderArgs
|
|
1835
|
-
},
|
|
1836
|
-
where: {
|
|
1837
|
-
type: filterArgs
|
|
1838
|
-
}
|
|
1839
|
-
};
|
|
1840
|
-
const typeName = toTypeName4(tableName, singularTypes);
|
|
2123
|
+
const queryArgs = selectArrayArgs(orderArgs, filterArgs);
|
|
1841
2124
|
const table = tables[tableName];
|
|
1842
2125
|
return {
|
|
1843
|
-
name:
|
|
2126
|
+
name: fieldName,
|
|
1844
2127
|
resolver: async (_source, args, _context, info) => {
|
|
1845
2128
|
try {
|
|
1846
|
-
const
|
|
1847
|
-
|
|
1848
|
-
|
|
2129
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, { deep: true });
|
|
2130
|
+
return await runRelationalSelect({
|
|
2131
|
+
queryBase,
|
|
2132
|
+
tables,
|
|
2133
|
+
tableName,
|
|
2134
|
+
table,
|
|
2135
|
+
relationMap,
|
|
2136
|
+
typeName,
|
|
2137
|
+
typeNameMapper,
|
|
2138
|
+
parsedInfo,
|
|
2139
|
+
...args,
|
|
2140
|
+
single: false
|
|
1849
2141
|
});
|
|
1850
|
-
const query = queryBase.findMany({
|
|
1851
|
-
columns: extractSelectedColumnsFromTree(parsedInfo.fieldsByTypeName[typeName], table),
|
|
1852
|
-
offset,
|
|
1853
|
-
limit,
|
|
1854
|
-
// drizzle-orm v1 RQB calls orderBy with the aliased table proxy —
|
|
1855
|
-
// use it directly so column refs match the CTE alias.
|
|
1856
|
-
orderBy: orderBy ? (aliasedTable) => extractOrderBy(aliasedTable, orderBy) : void 0,
|
|
1857
|
-
where: where ? { RAW: (aliased) => extractFilters(aliased, tableName, where) } : void 0,
|
|
1858
|
-
with: relationMap[tableName] ? extractRelationsParams(relationMap, tables, tableName, parsedInfo, typeName, singularTypes) : void 0
|
|
1859
|
-
});
|
|
1860
|
-
const result = await query;
|
|
1861
|
-
return remapToGraphQLArrayOutput(result, tableName, table, relationMap);
|
|
1862
2142
|
} catch (e) {
|
|
1863
|
-
|
|
1864
|
-
throw new import_graphql6.GraphQLError(e.message);
|
|
1865
|
-
}
|
|
1866
|
-
throw e;
|
|
2143
|
+
throw toGraphQLError(e);
|
|
1867
2144
|
}
|
|
1868
2145
|
},
|
|
1869
2146
|
args: queryArgs
|
|
1870
2147
|
};
|
|
1871
2148
|
};
|
|
1872
|
-
var generateSelectSingle3 = (db, tableName, tables, relationMap, orderArgs, filterArgs,
|
|
1873
|
-
const queryEntityBase = singularTypes ? singularize(uncapitalize(tableName)) : uncapitalize(tableName);
|
|
1874
|
-
const queryName = `${queryEntityBase}${singleSuffix}`;
|
|
2149
|
+
var generateSelectSingle3 = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper) => {
|
|
1875
2150
|
const queryBase = db.query[tableName];
|
|
1876
2151
|
if (!queryBase) {
|
|
1877
2152
|
throw new Error(
|
|
1878
2153
|
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
1879
2154
|
);
|
|
1880
2155
|
}
|
|
1881
|
-
const queryArgs =
|
|
1882
|
-
offset: {
|
|
1883
|
-
type: import_graphql6.GraphQLInt
|
|
1884
|
-
},
|
|
1885
|
-
orderBy: {
|
|
1886
|
-
type: orderArgs
|
|
1887
|
-
},
|
|
1888
|
-
where: {
|
|
1889
|
-
type: filterArgs
|
|
1890
|
-
}
|
|
1891
|
-
};
|
|
1892
|
-
const typeName = toTypeName4(tableName, singularTypes);
|
|
2156
|
+
const queryArgs = selectSingleArgs(orderArgs, filterArgs);
|
|
1893
2157
|
const table = tables[tableName];
|
|
1894
2158
|
return {
|
|
1895
|
-
name:
|
|
2159
|
+
name: fieldName,
|
|
1896
2160
|
resolver: async (_source, args, _context, info) => {
|
|
1897
2161
|
try {
|
|
1898
|
-
const
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
2162
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, { deep: true });
|
|
2163
|
+
return await runRelationalSelect({
|
|
2164
|
+
queryBase,
|
|
2165
|
+
tables,
|
|
2166
|
+
tableName,
|
|
2167
|
+
table,
|
|
2168
|
+
relationMap,
|
|
2169
|
+
typeName,
|
|
2170
|
+
typeNameMapper,
|
|
2171
|
+
parsedInfo,
|
|
2172
|
+
...args,
|
|
2173
|
+
single: true
|
|
1910
2174
|
});
|
|
1911
|
-
const result = await query;
|
|
1912
|
-
if (!result) {
|
|
1913
|
-
return void 0;
|
|
1914
|
-
}
|
|
1915
|
-
return remapToGraphQLSingleOutput(result, tableName, table, relationMap);
|
|
1916
2175
|
} catch (e) {
|
|
1917
|
-
|
|
1918
|
-
throw new import_graphql6.GraphQLError(e.message);
|
|
1919
|
-
}
|
|
1920
|
-
throw e;
|
|
2176
|
+
throw toGraphQLError(e);
|
|
1921
2177
|
}
|
|
1922
2178
|
},
|
|
1923
2179
|
args: queryArgs
|
|
1924
2180
|
};
|
|
1925
2181
|
};
|
|
1926
|
-
var
|
|
1927
|
-
|
|
1928
|
-
const typeName = toTypeName4(tableName, singularTypes);
|
|
2182
|
+
var sqlitePrimaryKeyPropNames = (table) => getPrimaryKeyPropNamesFromConfig(table, import_sqlite_core2.getTableConfig);
|
|
2183
|
+
var generateInsertArray3 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper) => {
|
|
1929
2184
|
const queryArgs = {
|
|
1930
2185
|
values: {
|
|
1931
2186
|
type: new import_graphql6.GraphQLNonNull(new import_graphql6.GraphQLList(new import_graphql6.GraphQLNonNull(baseType)))
|
|
1932
2187
|
}
|
|
1933
2188
|
};
|
|
2189
|
+
const pkNames = sqlitePrimaryKeyPropNames(table);
|
|
1934
2190
|
return {
|
|
1935
|
-
name:
|
|
2191
|
+
name: fieldName,
|
|
1936
2192
|
resolver: async (_source, args, _context, info) => {
|
|
1937
2193
|
try {
|
|
1938
2194
|
const input = remapFromGraphQLArrayInput(args.values, table);
|
|
@@ -1942,61 +2198,65 @@ var generateInsertArray3 = (db, tableName, table, baseType, singularTypes = fals
|
|
|
1942
2198
|
const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
|
|
1943
2199
|
deep: true
|
|
1944
2200
|
});
|
|
1945
|
-
const columns =
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
2201
|
+
const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
|
|
2202
|
+
relationMap,
|
|
2203
|
+
tables,
|
|
2204
|
+
tableName,
|
|
2205
|
+
typeName,
|
|
2206
|
+
typeNameMapper,
|
|
2207
|
+
table,
|
|
2208
|
+
pkNames,
|
|
2209
|
+
parsedInfo
|
|
2210
|
+
});
|
|
1949
2211
|
const result = await db.insert(table).values(input).returning(columns).onConflictDoNothing();
|
|
1950
|
-
|
|
2212
|
+
const enriched = hasRelations ? await eagerLoadMutationRelations(db, tableName, result, pkNames, withParams) : result;
|
|
2213
|
+
return remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
|
|
1951
2214
|
} catch (e) {
|
|
1952
|
-
|
|
1953
|
-
throw new import_graphql6.GraphQLError(e.message);
|
|
1954
|
-
}
|
|
1955
|
-
throw e;
|
|
2215
|
+
throw toGraphQLError(e);
|
|
1956
2216
|
}
|
|
1957
2217
|
},
|
|
1958
2218
|
args: queryArgs
|
|
1959
2219
|
};
|
|
1960
2220
|
};
|
|
1961
|
-
var generateInsertSingle3 = (db, tableName, table, baseType,
|
|
1962
|
-
const queryEntityBase = singularTypes ? singularize(capitalize(tableName)) : capitalize(tableName);
|
|
1963
|
-
const queryName = singularTypes ? `insertInto${queryEntityBase}` : `insertInto${queryEntityBase}Single`;
|
|
1964
|
-
const typeName = toTypeName4(tableName, singularTypes);
|
|
2221
|
+
var generateInsertSingle3 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper) => {
|
|
1965
2222
|
const queryArgs = {
|
|
1966
2223
|
values: {
|
|
1967
2224
|
type: new import_graphql6.GraphQLNonNull(baseType)
|
|
1968
2225
|
}
|
|
1969
2226
|
};
|
|
2227
|
+
const pkNames = sqlitePrimaryKeyPropNames(table);
|
|
1970
2228
|
return {
|
|
1971
|
-
name:
|
|
2229
|
+
name: fieldName,
|
|
1972
2230
|
resolver: async (_source, args, _context, info) => {
|
|
1973
2231
|
try {
|
|
1974
2232
|
const input = remapFromGraphQLSingleInput(args.values, table);
|
|
1975
2233
|
const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
|
|
1976
2234
|
deep: true
|
|
1977
2235
|
});
|
|
1978
|
-
const columns =
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
2236
|
+
const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
|
|
2237
|
+
relationMap,
|
|
2238
|
+
tables,
|
|
2239
|
+
tableName,
|
|
2240
|
+
typeName,
|
|
2241
|
+
typeNameMapper,
|
|
2242
|
+
table,
|
|
2243
|
+
pkNames,
|
|
2244
|
+
parsedInfo
|
|
2245
|
+
});
|
|
1982
2246
|
const result = await db.insert(table).values(input).returning(columns).onConflictDoNothing();
|
|
1983
2247
|
if (!result[0]) {
|
|
1984
2248
|
return void 0;
|
|
1985
2249
|
}
|
|
1986
|
-
|
|
2250
|
+
const enriched = hasRelations ? await eagerLoadMutationRelations(db, tableName, result, pkNames, withParams) : result;
|
|
2251
|
+
return remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap);
|
|
1987
2252
|
} catch (e) {
|
|
1988
|
-
|
|
1989
|
-
throw new import_graphql6.GraphQLError(e.message);
|
|
1990
|
-
}
|
|
1991
|
-
throw e;
|
|
2253
|
+
throw toGraphQLError(e);
|
|
1992
2254
|
}
|
|
1993
2255
|
},
|
|
1994
2256
|
args: queryArgs
|
|
1995
2257
|
};
|
|
1996
2258
|
};
|
|
1997
|
-
var generateUpdate3 = (db, tableName, table, setArgs, filterArgs,
|
|
1998
|
-
const queryName = `update${capitalize(tableName)}`;
|
|
1999
|
-
const typeName = toTypeName4(tableName, singularTypes);
|
|
2259
|
+
var generateUpdate3 = (db, tableName, table, tables, relationMap, setArgs, filterArgs, fieldName, typeName, typeNameMapper) => {
|
|
2000
2260
|
const queryArgs = {
|
|
2001
2261
|
set: {
|
|
2002
2262
|
type: new import_graphql6.GraphQLNonNull(setArgs)
|
|
@@ -2005,18 +2265,25 @@ var generateUpdate3 = (db, tableName, table, setArgs, filterArgs, singularTypes
|
|
|
2005
2265
|
type: filterArgs
|
|
2006
2266
|
}
|
|
2007
2267
|
};
|
|
2268
|
+
const pkNames = sqlitePrimaryKeyPropNames(table);
|
|
2008
2269
|
return {
|
|
2009
|
-
name:
|
|
2270
|
+
name: fieldName,
|
|
2010
2271
|
resolver: async (_source, args, _context, info) => {
|
|
2011
2272
|
try {
|
|
2012
2273
|
const { where, set } = args;
|
|
2013
2274
|
const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
|
|
2014
2275
|
deep: true
|
|
2015
2276
|
});
|
|
2016
|
-
const columns =
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2277
|
+
const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
|
|
2278
|
+
relationMap,
|
|
2279
|
+
tables,
|
|
2280
|
+
tableName,
|
|
2281
|
+
typeName,
|
|
2282
|
+
typeNameMapper,
|
|
2283
|
+
table,
|
|
2284
|
+
pkNames,
|
|
2285
|
+
parsedInfo
|
|
2286
|
+
});
|
|
2020
2287
|
const input = remapFromGraphQLSingleInput(set, table);
|
|
2021
2288
|
if (!Object.keys(input).length) {
|
|
2022
2289
|
throw new import_graphql6.GraphQLError("Unable to update with no values specified!");
|
|
@@ -2028,27 +2295,23 @@ var generateUpdate3 = (db, tableName, table, setArgs, filterArgs, singularTypes
|
|
|
2028
2295
|
}
|
|
2029
2296
|
query = query.returning(columns);
|
|
2030
2297
|
const result = await query;
|
|
2031
|
-
|
|
2298
|
+
const enriched = hasRelations ? await eagerLoadMutationRelations(db, tableName, result, pkNames, withParams) : result;
|
|
2299
|
+
return remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
|
|
2032
2300
|
} catch (e) {
|
|
2033
|
-
|
|
2034
|
-
throw new import_graphql6.GraphQLError(e.message);
|
|
2035
|
-
}
|
|
2036
|
-
throw e;
|
|
2301
|
+
throw toGraphQLError(e);
|
|
2037
2302
|
}
|
|
2038
2303
|
},
|
|
2039
2304
|
args: queryArgs
|
|
2040
2305
|
};
|
|
2041
2306
|
};
|
|
2042
|
-
var generateDelete3 = (db, tableName, table, filterArgs,
|
|
2043
|
-
const queryName = `deleteFrom${capitalize(tableName)}`;
|
|
2044
|
-
const typeName = toTypeName4(tableName, singularTypes);
|
|
2307
|
+
var generateDelete3 = (db, tableName, table, filterArgs, fieldName, typeName) => {
|
|
2045
2308
|
const queryArgs = {
|
|
2046
2309
|
where: {
|
|
2047
2310
|
type: filterArgs
|
|
2048
2311
|
}
|
|
2049
2312
|
};
|
|
2050
2313
|
return {
|
|
2051
|
-
name:
|
|
2314
|
+
name: fieldName,
|
|
2052
2315
|
resolver: async (_source, args, _context, info) => {
|
|
2053
2316
|
try {
|
|
2054
2317
|
const { where } = args;
|
|
@@ -2068,16 +2331,13 @@ var generateDelete3 = (db, tableName, table, filterArgs, singularTypes = false)
|
|
|
2068
2331
|
const result = await query;
|
|
2069
2332
|
return remapToGraphQLArrayOutput(result, tableName, table);
|
|
2070
2333
|
} catch (e) {
|
|
2071
|
-
|
|
2072
|
-
throw new import_graphql6.GraphQLError(e.message);
|
|
2073
|
-
}
|
|
2074
|
-
throw e;
|
|
2334
|
+
throw toGraphQLError(e);
|
|
2075
2335
|
}
|
|
2076
2336
|
},
|
|
2077
2337
|
args: queryArgs
|
|
2078
2338
|
};
|
|
2079
2339
|
};
|
|
2080
|
-
var generateSchemaData3 = (db, schema, relations, relationsDepthLimit, prefixes, suffixes,
|
|
2340
|
+
var generateSchemaData3 = (db, schema, relations, relationsDepthLimit, prefixes, suffixes, typeNameMapper, shouldEagerLoad = () => true) => {
|
|
2081
2341
|
const rawSchema = schema;
|
|
2082
2342
|
const schemaEntries = Object.entries(rawSchema);
|
|
2083
2343
|
const tableEntries = schemaEntries.filter(([_key, value]) => (0, import_drizzle_orm6.is)(value, import_sqlite_core2.SQLiteTable));
|
|
@@ -2088,12 +2348,17 @@ var generateSchemaData3 = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
2088
2348
|
);
|
|
2089
2349
|
}
|
|
2090
2350
|
const namedRelations = buildNamedRelations(relations ?? {}, tableEntries);
|
|
2351
|
+
attachTargetPrimaryKeys(namedRelations, tables, sqlitePrimaryKeyPropNames);
|
|
2352
|
+
const eagerRelations = pruneNonEagerRelations(namedRelations, shouldEagerLoad);
|
|
2353
|
+
const resolverFactory = createRelationResolverFactory(db, tables);
|
|
2091
2354
|
const cacheCtx = {
|
|
2092
2355
|
genericFilterCache: /* @__PURE__ */ new Map(),
|
|
2093
2356
|
objectTypeCache: /* @__PURE__ */ new Map(),
|
|
2094
2357
|
relationFieldContainers: /* @__PURE__ */ new Map(),
|
|
2095
2358
|
fullyBuiltTables: /* @__PURE__ */ new Set(),
|
|
2096
|
-
relationTypeCache: /* @__PURE__ */ new Map()
|
|
2359
|
+
relationTypeCache: /* @__PURE__ */ new Map(),
|
|
2360
|
+
orderTypeCache: /* @__PURE__ */ new WeakMap(),
|
|
2361
|
+
filterTypeCache: /* @__PURE__ */ new WeakMap()
|
|
2097
2362
|
};
|
|
2098
2363
|
const queries = {};
|
|
2099
2364
|
const mutations = {};
|
|
@@ -2107,9 +2372,10 @@ var generateSchemaData3 = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
2107
2372
|
true,
|
|
2108
2373
|
relationsDepthLimit,
|
|
2109
2374
|
cacheCtx,
|
|
2110
|
-
|
|
2375
|
+
typeNameMapper,
|
|
2111
2376
|
prefixes.insert,
|
|
2112
|
-
prefixes.update
|
|
2377
|
+
prefixes.update,
|
|
2378
|
+
resolverFactory
|
|
2113
2379
|
)
|
|
2114
2380
|
])
|
|
2115
2381
|
);
|
|
@@ -2118,54 +2384,78 @@ var generateSchemaData3 = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
2118
2384
|
for (const [tableName, tableTypes] of Object.entries(gqlSchemaTypes)) {
|
|
2119
2385
|
const { insertInput, updateInput, tableFilters, tableOrder } = tableTypes.inputs;
|
|
2120
2386
|
const { selectSingleOutput, selectArrOutput, singleTableItemOutput, arrTableItemOutput } = tableTypes.outputs;
|
|
2387
|
+
const {
|
|
2388
|
+
typeName,
|
|
2389
|
+
listFieldName,
|
|
2390
|
+
singleFieldName,
|
|
2391
|
+
createArrayFieldName,
|
|
2392
|
+
createSingleFieldName,
|
|
2393
|
+
updateFieldName,
|
|
2394
|
+
deleteFieldName
|
|
2395
|
+
} = computeResolverFieldNames(tableName, typeNameMapper, prefixes, suffixes);
|
|
2121
2396
|
const selectArrGenerated = generateSelectArray3(
|
|
2122
2397
|
db,
|
|
2123
2398
|
tableName,
|
|
2124
2399
|
tables,
|
|
2125
|
-
|
|
2400
|
+
eagerRelations,
|
|
2126
2401
|
tableOrder,
|
|
2127
2402
|
tableFilters,
|
|
2128
|
-
|
|
2129
|
-
|
|
2403
|
+
listFieldName,
|
|
2404
|
+
typeName,
|
|
2405
|
+
typeNameMapper
|
|
2130
2406
|
);
|
|
2131
2407
|
const selectSingleGenerated = generateSelectSingle3(
|
|
2132
2408
|
db,
|
|
2133
2409
|
tableName,
|
|
2134
2410
|
tables,
|
|
2135
|
-
|
|
2411
|
+
eagerRelations,
|
|
2136
2412
|
tableOrder,
|
|
2137
2413
|
tableFilters,
|
|
2138
|
-
|
|
2139
|
-
|
|
2414
|
+
singleFieldName,
|
|
2415
|
+
typeName,
|
|
2416
|
+
typeNameMapper
|
|
2140
2417
|
);
|
|
2141
2418
|
const insertArrGenerated = generateInsertArray3(
|
|
2142
2419
|
db,
|
|
2143
2420
|
tableName,
|
|
2144
2421
|
schema[tableName],
|
|
2422
|
+
tables,
|
|
2423
|
+
eagerRelations,
|
|
2145
2424
|
insertInput,
|
|
2146
|
-
|
|
2425
|
+
createArrayFieldName,
|
|
2426
|
+
typeName,
|
|
2427
|
+
typeNameMapper
|
|
2147
2428
|
);
|
|
2148
2429
|
const insertSingleGenerated = generateInsertSingle3(
|
|
2149
2430
|
db,
|
|
2150
2431
|
tableName,
|
|
2151
2432
|
schema[tableName],
|
|
2433
|
+
tables,
|
|
2434
|
+
eagerRelations,
|
|
2152
2435
|
insertInput,
|
|
2153
|
-
|
|
2436
|
+
createSingleFieldName,
|
|
2437
|
+
typeName,
|
|
2438
|
+
typeNameMapper
|
|
2154
2439
|
);
|
|
2155
2440
|
const updateGenerated = generateUpdate3(
|
|
2156
2441
|
db,
|
|
2157
2442
|
tableName,
|
|
2158
2443
|
schema[tableName],
|
|
2444
|
+
tables,
|
|
2445
|
+
eagerRelations,
|
|
2159
2446
|
updateInput,
|
|
2160
2447
|
tableFilters,
|
|
2161
|
-
|
|
2448
|
+
updateFieldName,
|
|
2449
|
+
typeName,
|
|
2450
|
+
typeNameMapper
|
|
2162
2451
|
);
|
|
2163
2452
|
const deleteGenerated = generateDelete3(
|
|
2164
2453
|
db,
|
|
2165
2454
|
tableName,
|
|
2166
2455
|
schema[tableName],
|
|
2167
2456
|
tableFilters,
|
|
2168
|
-
|
|
2457
|
+
deleteFieldName,
|
|
2458
|
+
typeName
|
|
2169
2459
|
);
|
|
2170
2460
|
queries[selectArrGenerated.name] = {
|
|
2171
2461
|
type: selectArrOutput,
|
|
@@ -2203,28 +2493,51 @@ var generateSchemaData3 = (db, schema, relations, relationsDepthLimit, prefixes,
|
|
|
2203
2493
|
outputs[selectSingleOutput.name] = selectSingleOutput;
|
|
2204
2494
|
outputs[singleTableItemOutput.name] = singleTableItemOutput;
|
|
2205
2495
|
}
|
|
2206
|
-
|
|
2496
|
+
const fieldResolvers = {};
|
|
2497
|
+
for (const [tableName, tableRelations] of Object.entries(namedRelations)) {
|
|
2498
|
+
const relResolvers = {};
|
|
2499
|
+
for (const [relName, relEntry] of Object.entries(tableRelations)) {
|
|
2500
|
+
const isOne = (0, import_drizzle_orm6.is)(relEntry.relation ?? relEntry, import_drizzle_orm6.One);
|
|
2501
|
+
const resolver = resolverFactory({ tableName, relationName: relName, relEntry, isOne });
|
|
2502
|
+
if (resolver) {
|
|
2503
|
+
relResolvers[relName] = resolver;
|
|
2504
|
+
}
|
|
2505
|
+
}
|
|
2506
|
+
if (Object.keys(relResolvers).length > 0) {
|
|
2507
|
+
fieldResolvers[tableName] = relResolvers;
|
|
2508
|
+
}
|
|
2509
|
+
}
|
|
2510
|
+
return { queries, mutations, inputs, types: outputs, fieldResolvers };
|
|
2207
2511
|
};
|
|
2208
2512
|
|
|
2209
2513
|
// src/index.ts
|
|
2210
2514
|
var buildSchema = (db, config) => {
|
|
2211
|
-
const schema = db._.fullSchema;
|
|
2212
2515
|
const relations = db._.relations;
|
|
2213
|
-
|
|
2516
|
+
const schema = db._.fullSchema ?? Object.fromEntries(
|
|
2517
|
+
Object.entries(relations).filter(([, config2]) => config2?.table != null).map(([key, config2]) => [key, config2.table])
|
|
2518
|
+
);
|
|
2519
|
+
if (!schema || !Object.keys(schema).length) {
|
|
2214
2520
|
throw new Error(
|
|
2215
|
-
"Drizzle-GraphQL Error: Schema not found in drizzle instance.
|
|
2521
|
+
"Drizzle-GraphQL Error: Schema not found in drizzle instance. Pass relations (from buildRelations/defineRelations) to the drizzle constructor so drizzle-graphql can detect your tables."
|
|
2216
2522
|
);
|
|
2217
2523
|
}
|
|
2218
2524
|
const prefixes = {
|
|
2219
|
-
insert: config?.prefixes?.insert ?? "
|
|
2220
|
-
delete: config?.prefixes?.delete ?? "
|
|
2525
|
+
insert: config?.prefixes?.insert ?? "create",
|
|
2526
|
+
delete: config?.prefixes?.delete ?? "delete",
|
|
2221
2527
|
update: config?.prefixes?.update ?? "update"
|
|
2222
2528
|
};
|
|
2223
2529
|
const suffixes = {
|
|
2224
2530
|
list: config?.suffixes?.list ?? "",
|
|
2225
2531
|
single: config?.suffixes?.single ?? "Single"
|
|
2226
2532
|
};
|
|
2227
|
-
const
|
|
2533
|
+
const typeNameMapper = config?.typeNameMapper;
|
|
2534
|
+
const eagerOpt = config?.eagerLoadRelations;
|
|
2535
|
+
const shouldEagerLoad = eagerOpt === void 0 || eagerOpt === true ? () => true : eagerOpt === false ? () => false : eagerOpt;
|
|
2536
|
+
if (!typeNameMapper && suffixes.list === suffixes.single) {
|
|
2537
|
+
throw new Error(
|
|
2538
|
+
"Drizzle-GraphQL Error: List and single query suffixes cannot be the same. This would create conflicting GraphQL field names."
|
|
2539
|
+
);
|
|
2540
|
+
}
|
|
2228
2541
|
if (typeof config?.relationsDepthLimit === "number") {
|
|
2229
2542
|
if (config.relationsDepthLimit < 0) {
|
|
2230
2543
|
throw new Error(
|
|
@@ -2236,11 +2549,6 @@ var buildSchema = (db, config) => {
|
|
|
2236
2549
|
"Drizzle-GraphQL Error: config.relationsDepthLimit is supposed to be nonnegative integer or undefined!"
|
|
2237
2550
|
);
|
|
2238
2551
|
}
|
|
2239
|
-
if (suffixes.list === suffixes.single) {
|
|
2240
|
-
throw new Error(
|
|
2241
|
-
"Drizzle-GraphQL Error: List and single query suffixes cannot be the same. This would create conflicting GraphQL field names."
|
|
2242
|
-
);
|
|
2243
|
-
}
|
|
2244
2552
|
}
|
|
2245
2553
|
let generatorOutput;
|
|
2246
2554
|
if ((0, import_drizzle_orm7.is)(db, import_mysql_core3.MySqlDatabase)) {
|
|
@@ -2251,7 +2559,8 @@ var buildSchema = (db, config) => {
|
|
|
2251
2559
|
config?.relationsDepthLimit,
|
|
2252
2560
|
prefixes,
|
|
2253
2561
|
suffixes,
|
|
2254
|
-
|
|
2562
|
+
typeNameMapper,
|
|
2563
|
+
shouldEagerLoad
|
|
2255
2564
|
);
|
|
2256
2565
|
} else if ((0, import_drizzle_orm7.is)(db, import_pg_core3.PgAsyncDatabase)) {
|
|
2257
2566
|
generatorOutput = generateSchemaData2(
|
|
@@ -2262,7 +2571,8 @@ var buildSchema = (db, config) => {
|
|
|
2262
2571
|
prefixes,
|
|
2263
2572
|
suffixes,
|
|
2264
2573
|
config?.conflictDoNothing ?? false,
|
|
2265
|
-
|
|
2574
|
+
typeNameMapper,
|
|
2575
|
+
shouldEagerLoad
|
|
2266
2576
|
);
|
|
2267
2577
|
} else if ((0, import_drizzle_orm7.is)(db, import_sqlite_core3.BaseSQLiteDatabase)) {
|
|
2268
2578
|
generatorOutput = generateSchemaData3(
|
|
@@ -2272,7 +2582,8 @@ var buildSchema = (db, config) => {
|
|
|
2272
2582
|
config?.relationsDepthLimit,
|
|
2273
2583
|
prefixes,
|
|
2274
2584
|
suffixes,
|
|
2275
|
-
|
|
2585
|
+
typeNameMapper,
|
|
2586
|
+
shouldEagerLoad
|
|
2276
2587
|
);
|
|
2277
2588
|
} else {
|
|
2278
2589
|
throw new Error("Drizzle-GraphQL Error: Unknown database instance type");
|
|
@@ -2298,7 +2609,9 @@ var buildSchema = (db, config) => {
|
|
|
2298
2609
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2299
2610
|
0 && (module.exports = {
|
|
2300
2611
|
buildSchema,
|
|
2612
|
+
createRelationResolverFactory,
|
|
2301
2613
|
extractFilters,
|
|
2302
|
-
extractOrderBy
|
|
2614
|
+
extractOrderBy,
|
|
2615
|
+
extractRelationJoinColumns
|
|
2303
2616
|
});
|
|
2304
2617
|
//# sourceMappingURL=index.cjs.map
|