drizzle-graphql-plus 0.8.35 → 0.8.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs +495 -3398
- package/index.cjs.map +1 -1
- package/index.d.cts +18 -90
- package/index.d.ts +18 -90
- package/index.js +497 -3493
- package/index.js.map +1 -1
- package/package.json +3 -12
package/index.cjs
CHANGED
|
@@ -5,15 +5,18 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
5
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __esm = (fn, res) => function __init() {
|
|
9
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
10
|
+
};
|
|
8
11
|
var __export = (target, all) => {
|
|
9
12
|
for (var name in all)
|
|
10
13
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
14
|
};
|
|
12
|
-
var __copyProps = (to, from, except,
|
|
15
|
+
var __copyProps = (to, from, except, desc2) => {
|
|
13
16
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
17
|
for (let key of __getOwnPropNames(from))
|
|
15
18
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(
|
|
19
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc2 = __getOwnPropDesc(from, key)) || desc2.enumerable });
|
|
17
20
|
}
|
|
18
21
|
return to;
|
|
19
22
|
};
|
|
@@ -27,3269 +30,191 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
27
30
|
));
|
|
28
31
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
32
|
|
|
30
|
-
// src/
|
|
31
|
-
var
|
|
32
|
-
__export(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
makeScalarAcceptExports: () => makeScalarAcceptExports,
|
|
41
|
-
setCustomGraphQL: () => setCustomGraphQL,
|
|
42
|
-
setCustomGraphQLTypes: () => setCustomGraphQLTypes,
|
|
43
|
-
useDataLoaderCleanup: () => useDataLoaderCleanup,
|
|
44
|
-
useDataLoaderCleanupOnly: () => useDataLoaderCleanupOnly,
|
|
45
|
-
useDataLoaderContext: () => useDataLoaderContext
|
|
33
|
+
// src/export-tool/utils.ts
|
|
34
|
+
var utils_exports = {};
|
|
35
|
+
__export(utils_exports, {
|
|
36
|
+
extractExportDirectives: () => extractExportDirectives,
|
|
37
|
+
getExportDirective: () => getExportDirective,
|
|
38
|
+
getVariableName: () => getVariableName,
|
|
39
|
+
hasExportVariables: () => hasExportVariables,
|
|
40
|
+
isExportVariable: () => isExportVariable,
|
|
41
|
+
processExports: () => processExports,
|
|
42
|
+
resolveExportVariables: () => resolveExportVariables
|
|
46
43
|
});
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
var import_sqlite_core3 = require("drizzle-orm/sqlite-core");
|
|
54
|
-
var import_graphql7 = require("graphql");
|
|
55
|
-
|
|
56
|
-
// src/util/builders/mysql.ts
|
|
57
|
-
var import_drizzle_orm4 = require("drizzle-orm");
|
|
58
|
-
var import_mysql_core2 = require("drizzle-orm/mysql-core");
|
|
59
|
-
var import_graphql4 = require("graphql");
|
|
60
|
-
|
|
61
|
-
// src/util/builders/common.ts
|
|
62
|
-
var import_drizzle_orm3 = require("drizzle-orm");
|
|
63
|
-
var import_graphql3 = require("graphql");
|
|
64
|
-
|
|
65
|
-
// src/util/case-ops/index.ts
|
|
66
|
-
var uncapitalize = (input) => input.length ? `${input[0].toLocaleLowerCase()}${input.length > 1 ? input.slice(1, input.length) : ""}` : input;
|
|
67
|
-
var capitalize = (input) => input.length ? `${input[0].toLocaleUpperCase()}${input.length > 1 ? input.slice(1, input.length) : ""}` : input;
|
|
68
|
-
|
|
69
|
-
// src/util/data-mappers/index.ts
|
|
70
|
-
var import_drizzle_orm = require("drizzle-orm");
|
|
71
|
-
var import_graphql = require("graphql");
|
|
72
|
-
var remapToGraphQLCore = (key, value, tableName, column, relationMap) => {
|
|
73
|
-
if (value instanceof Date)
|
|
74
|
-
return value.toISOString();
|
|
75
|
-
if (value instanceof Buffer)
|
|
76
|
-
return Array.from(value);
|
|
77
|
-
if (typeof value === "bigint")
|
|
78
|
-
return value.toString();
|
|
79
|
-
if (Array.isArray(value)) {
|
|
80
|
-
const relations = relationMap?.[tableName];
|
|
81
|
-
if (relations?.[key]) {
|
|
82
|
-
return remapToGraphQLArrayOutput(
|
|
83
|
-
value,
|
|
84
|
-
relations[key].targetTableName,
|
|
85
|
-
relations[key].relation.referencedTable,
|
|
86
|
-
relationMap
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
if (column.columnType === "PgGeometry" || column.columnType === "PgVector")
|
|
90
|
-
return value;
|
|
91
|
-
return value.map((arrVal) => remapToGraphQLCore(key, arrVal, tableName, column, relationMap));
|
|
92
|
-
}
|
|
93
|
-
if (typeof value === "object") {
|
|
94
|
-
const relations = relationMap?.[tableName];
|
|
95
|
-
if (relations?.[key]) {
|
|
96
|
-
return remapToGraphQLSingleOutput(
|
|
97
|
-
value,
|
|
98
|
-
relations[key].targetTableName,
|
|
99
|
-
relations[key].relation.referencedTable,
|
|
100
|
-
relationMap
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
if (column.columnType === "PgGeometryObject")
|
|
104
|
-
return value;
|
|
105
|
-
return JSON.stringify(value);
|
|
44
|
+
function isExportVariable(value) {
|
|
45
|
+
return typeof value === "string" && value.startsWith("$_") && value.length > 2;
|
|
46
|
+
}
|
|
47
|
+
function getVariableName(value) {
|
|
48
|
+
if (!isExportVariable(value)) {
|
|
49
|
+
return null;
|
|
106
50
|
}
|
|
107
|
-
return value;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
} else {
|
|
114
|
-
queryOutput[key] = remapToGraphQLCore(key, value, tableName, table[key], relationMap);
|
|
115
|
-
}
|
|
51
|
+
return value.slice(2);
|
|
52
|
+
}
|
|
53
|
+
async function resolveExportVariables(args, exportStore, timeout, allowNull = true) {
|
|
54
|
+
if (isExportVariable(args)) {
|
|
55
|
+
const varName = getVariableName(args);
|
|
56
|
+
return await exportStore.waitFor(varName, timeout, allowNull);
|
|
116
57
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
58
|
+
if (Array.isArray(args)) {
|
|
59
|
+
const resolved = await Promise.all(
|
|
60
|
+
args.map(async (item) => {
|
|
61
|
+
if (isExportVariable(item)) {
|
|
62
|
+
const varName = getVariableName(item);
|
|
63
|
+
return await exportStore.waitFor(varName, timeout, allowNull);
|
|
64
|
+
} else if (typeof item === "object" && item !== null) {
|
|
65
|
+
return await resolveExportVariables(
|
|
66
|
+
item,
|
|
67
|
+
exportStore,
|
|
68
|
+
timeout,
|
|
69
|
+
allowNull
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
return item;
|
|
73
|
+
})
|
|
74
|
+
);
|
|
75
|
+
return resolved;
|
|
122
76
|
}
|
|
123
|
-
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
if (
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
throw new import_graphql.GraphQLError(`Field '${columnName}' is not an array!`);
|
|
136
|
-
}
|
|
137
|
-
return Buffer.from(value);
|
|
138
|
-
}
|
|
139
|
-
case "json": {
|
|
140
|
-
if (column.columnType === "PgGeometryObject")
|
|
141
|
-
return value;
|
|
142
|
-
try {
|
|
143
|
-
return JSON.parse(value);
|
|
144
|
-
} catch (e) {
|
|
145
|
-
throw new import_graphql.GraphQLError(
|
|
146
|
-
`Invalid JSON in field '${columnName}':
|
|
147
|
-
${e instanceof Error ? e.message : "Unknown error"}`
|
|
77
|
+
if (typeof args === "object" && args !== null) {
|
|
78
|
+
const resolved = {};
|
|
79
|
+
for (const [key, value] of Object.entries(args)) {
|
|
80
|
+
if (isExportVariable(value)) {
|
|
81
|
+
const varName = getVariableName(value);
|
|
82
|
+
resolved[key] = await exportStore.waitFor(varName, timeout, allowNull);
|
|
83
|
+
} else if (Array.isArray(value)) {
|
|
84
|
+
resolved[key] = await resolveExportVariables(
|
|
85
|
+
value,
|
|
86
|
+
exportStore,
|
|
87
|
+
timeout,
|
|
88
|
+
allowNull
|
|
148
89
|
);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
if (column.columnType === "PgGeometry" && value.length !== 2) {
|
|
156
|
-
throw new import_graphql.GraphQLError(
|
|
157
|
-
`Invalid float tuple in field '${columnName}': expected array with length of 2, received ${value.length}`
|
|
90
|
+
} else if (typeof value === "object" && value !== null) {
|
|
91
|
+
resolved[key] = await resolveExportVariables(
|
|
92
|
+
value,
|
|
93
|
+
exportStore,
|
|
94
|
+
timeout,
|
|
95
|
+
allowNull
|
|
158
96
|
);
|
|
97
|
+
} else {
|
|
98
|
+
resolved[key] = value;
|
|
159
99
|
}
|
|
160
|
-
return value;
|
|
161
|
-
}
|
|
162
|
-
case "bigint": {
|
|
163
|
-
try {
|
|
164
|
-
return BigInt(value);
|
|
165
|
-
} catch (error) {
|
|
166
|
-
throw new import_graphql.GraphQLError(`Field '${columnName}' is not a BigInt!`);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
default: {
|
|
170
|
-
return value;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
var remapFromGraphQLSingleInput = (queryInput, table) => {
|
|
175
|
-
for (const [key, value] of Object.entries(queryInput)) {
|
|
176
|
-
if (value === void 0) {
|
|
177
|
-
delete queryInput[key];
|
|
178
|
-
} else {
|
|
179
|
-
const column = (0, import_drizzle_orm.getTableColumns)(table)[key];
|
|
180
|
-
if (!column)
|
|
181
|
-
throw new import_graphql.GraphQLError(`Unknown column: ${key}`);
|
|
182
|
-
if (value === null && column.notNull) {
|
|
183
|
-
delete queryInput[key];
|
|
184
|
-
continue;
|
|
185
|
-
}
|
|
186
|
-
queryInput[key] = remapFromGraphQLCore(value, column, key);
|
|
187
100
|
}
|
|
101
|
+
return resolved;
|
|
188
102
|
}
|
|
189
|
-
return
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
// src/util/type-converter/index.ts
|
|
198
|
-
var import_drizzle_orm2 = require("drizzle-orm");
|
|
199
|
-
var import_mysql_core = require("drizzle-orm/mysql-core");
|
|
200
|
-
var import_pg_core = require("drizzle-orm/pg-core");
|
|
201
|
-
var import_sqlite_core = require("drizzle-orm/sqlite-core");
|
|
202
|
-
var import_graphql2 = require("graphql");
|
|
203
|
-
var allowedNameChars = /^[a-zA-Z0-9_]+$/;
|
|
204
|
-
var enumMap = /* @__PURE__ */ new WeakMap();
|
|
205
|
-
var generateEnumCached = (column, columnName, tableName) => {
|
|
206
|
-
if (enumMap.has(column))
|
|
207
|
-
return enumMap.get(column);
|
|
208
|
-
const gqlEnum = new import_graphql2.GraphQLEnumType({
|
|
209
|
-
name: `${capitalize(tableName)}${capitalize(columnName)}Enum`,
|
|
210
|
-
values: Object.fromEntries(column.enumValues.map((e, index) => [allowedNameChars.test(e) ? e : `Option${index}`, {
|
|
211
|
-
value: e,
|
|
212
|
-
description: `Value: ${e}`
|
|
213
|
-
}]))
|
|
214
|
-
});
|
|
215
|
-
enumMap.set(column, gqlEnum);
|
|
216
|
-
return gqlEnum;
|
|
217
|
-
};
|
|
218
|
-
var geoXyType = new import_graphql2.GraphQLObjectType({
|
|
219
|
-
name: "PgGeometryObject",
|
|
220
|
-
fields: {
|
|
221
|
-
x: { type: import_graphql2.GraphQLFloat },
|
|
222
|
-
y: { type: import_graphql2.GraphQLFloat }
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
var geoXyInputType = new import_graphql2.GraphQLInputObjectType({
|
|
226
|
-
name: "PgGeometryObjectInput",
|
|
227
|
-
fields: {
|
|
228
|
-
x: { type: import_graphql2.GraphQLFloat },
|
|
229
|
-
y: { type: import_graphql2.GraphQLFloat }
|
|
230
|
-
}
|
|
231
|
-
});
|
|
232
|
-
var columnToGraphQLCore = (column, columnName, tableName, isInput) => {
|
|
233
|
-
switch (column.dataType) {
|
|
234
|
-
case "boolean":
|
|
235
|
-
return { type: import_graphql2.GraphQLBoolean, description: "Boolean" };
|
|
236
|
-
case "json":
|
|
237
|
-
return column.columnType === "PgGeometryObject" ? {
|
|
238
|
-
type: isInput ? geoXyInputType : geoXyType,
|
|
239
|
-
description: "Geometry points XY"
|
|
240
|
-
} : { type: import_graphql2.GraphQLString, description: "JSON" };
|
|
241
|
-
case "date":
|
|
242
|
-
return { type: import_graphql2.GraphQLString, description: "Date" };
|
|
243
|
-
case "string":
|
|
244
|
-
if (column.enumValues?.length)
|
|
245
|
-
return { type: generateEnumCached(column, columnName, tableName) };
|
|
246
|
-
return { type: import_graphql2.GraphQLString, description: "String" };
|
|
247
|
-
case "bigint":
|
|
248
|
-
return { type: import_graphql2.GraphQLString, description: "BigInt" };
|
|
249
|
-
case "number":
|
|
250
|
-
return (0, import_drizzle_orm2.is)(column, import_pg_core.PgInteger) || (0, import_drizzle_orm2.is)(column, import_pg_core.PgSerial) || (0, import_drizzle_orm2.is)(column, import_mysql_core.MySqlInt) || (0, import_drizzle_orm2.is)(column, import_mysql_core.MySqlSerial) || (0, import_drizzle_orm2.is)(column, import_sqlite_core.SQLiteInteger) ? { type: import_graphql2.GraphQLInt, description: "Integer" } : { type: import_graphql2.GraphQLFloat, description: "Float" };
|
|
251
|
-
case "buffer":
|
|
252
|
-
return { type: new import_graphql2.GraphQLList(new import_graphql2.GraphQLNonNull(import_graphql2.GraphQLInt)), description: "Buffer" };
|
|
253
|
-
case "array": {
|
|
254
|
-
if (column.columnType === "PgVector") {
|
|
255
|
-
return {
|
|
256
|
-
type: new import_graphql2.GraphQLList(new import_graphql2.GraphQLNonNull(import_graphql2.GraphQLFloat)),
|
|
257
|
-
description: "Array<Float>"
|
|
258
|
-
};
|
|
259
|
-
}
|
|
260
|
-
if (column.columnType === "PgGeometry") {
|
|
261
|
-
return {
|
|
262
|
-
type: new import_graphql2.GraphQLList(new import_graphql2.GraphQLNonNull(import_graphql2.GraphQLFloat)),
|
|
263
|
-
description: "Tuple<[Float, Float]>"
|
|
264
|
-
};
|
|
265
|
-
}
|
|
266
|
-
const innerType = columnToGraphQLCore(
|
|
267
|
-
column.baseColumn,
|
|
268
|
-
columnName,
|
|
269
|
-
tableName,
|
|
270
|
-
isInput
|
|
271
|
-
);
|
|
272
|
-
return {
|
|
273
|
-
type: new import_graphql2.GraphQLList(new import_graphql2.GraphQLNonNull(innerType.type)),
|
|
274
|
-
description: `Array<${innerType.description}>`
|
|
275
|
-
};
|
|
276
|
-
}
|
|
277
|
-
case "custom":
|
|
278
|
-
default:
|
|
279
|
-
throw new Error(`Drizzle-GraphQL Error: Type ${column.dataType} is not implemented!`);
|
|
103
|
+
return args;
|
|
104
|
+
}
|
|
105
|
+
function getExportDirective(fieldNode) {
|
|
106
|
+
const node = fieldNode;
|
|
107
|
+
if (!node || !node.directives) {
|
|
108
|
+
return null;
|
|
280
109
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
delete typeDesc.description;
|
|
287
|
-
if (forceNullable)
|
|
288
|
-
return typeDesc;
|
|
289
|
-
if (column.notNull && !(defaultIsNullable && (column.hasDefault || column.defaultFn))) {
|
|
290
|
-
return {
|
|
291
|
-
type: new import_graphql2.GraphQLNonNull(typeDesc.type),
|
|
292
|
-
description: typeDesc.description
|
|
293
|
-
};
|
|
110
|
+
const exportDirective = node.directives.find(
|
|
111
|
+
(directive) => directive.name.value === "export"
|
|
112
|
+
);
|
|
113
|
+
if (!exportDirective) {
|
|
114
|
+
return null;
|
|
294
115
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
var extractSelectedColumnsFromTree = (tree, table) => {
|
|
301
|
-
const tableColumns = (0, import_drizzle_orm3.getTableColumns)(table);
|
|
302
|
-
const treeEntries = Object.entries(tree);
|
|
303
|
-
const selectedColumns = [];
|
|
304
|
-
for (const [fieldName, fieldData] of treeEntries) {
|
|
305
|
-
if (tableColumns[fieldData.name]) {
|
|
306
|
-
selectedColumns.push([fieldData.name, true]);
|
|
307
|
-
continue;
|
|
308
|
-
}
|
|
309
|
-
if (fieldData.fieldsByTypeName) {
|
|
310
|
-
for (const [typeName, typeFields] of Object.entries(
|
|
311
|
-
fieldData.fieldsByTypeName
|
|
312
|
-
)) {
|
|
313
|
-
for (const [subFieldName, subFieldData] of Object.entries(typeFields)) {
|
|
314
|
-
if (tableColumns[subFieldData.name]) {
|
|
315
|
-
selectedColumns.push([subFieldData.name, true]);
|
|
316
|
-
} else {
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
}
|
|
116
|
+
const asArg = exportDirective.arguments?.find(
|
|
117
|
+
(arg) => arg.name.value === "as"
|
|
118
|
+
);
|
|
119
|
+
if (!asArg || asArg.value.kind !== "StringValue") {
|
|
120
|
+
return null;
|
|
321
121
|
}
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
122
|
+
return asArg.value.value;
|
|
123
|
+
}
|
|
124
|
+
function extractExportDirectives(info) {
|
|
125
|
+
const exports2 = /* @__PURE__ */ new Map();
|
|
126
|
+
const fieldNode = info.fieldNodes[0];
|
|
127
|
+
if (!fieldNode || !fieldNode.selectionSet) {
|
|
128
|
+
return exports2;
|
|
328
129
|
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
for (const [fieldName, fieldData] of treeEntries) {
|
|
336
|
-
if (tableColumns[fieldData.name]) {
|
|
337
|
-
selectedColumns.push([fieldData.name, tableColumns[fieldData.name]]);
|
|
338
|
-
continue;
|
|
339
|
-
}
|
|
340
|
-
if (fieldData.fieldsByTypeName) {
|
|
341
|
-
for (const [typeName, typeFields] of Object.entries(
|
|
342
|
-
fieldData.fieldsByTypeName
|
|
343
|
-
)) {
|
|
344
|
-
for (const [subFieldName, subFieldData] of Object.entries(typeFields)) {
|
|
345
|
-
if (tableColumns[subFieldData.name]) {
|
|
346
|
-
selectedColumns.push([
|
|
347
|
-
subFieldData.name,
|
|
348
|
-
tableColumns[subFieldData.name]
|
|
349
|
-
]);
|
|
350
|
-
}
|
|
351
|
-
}
|
|
130
|
+
for (const selection of fieldNode.selectionSet.selections) {
|
|
131
|
+
if (selection.kind === "Field") {
|
|
132
|
+
const exportName = getExportDirective(selection);
|
|
133
|
+
if (exportName) {
|
|
134
|
+
const fieldName = selection.name.value;
|
|
135
|
+
exports2.set(fieldName, exportName);
|
|
352
136
|
}
|
|
353
137
|
}
|
|
354
138
|
}
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
selectedColumns.push([columnName, tableColumns[columnName]]);
|
|
361
|
-
}
|
|
362
|
-
return Object.fromEntries(selectedColumns);
|
|
363
|
-
};
|
|
364
|
-
var innerOrder = new import_graphql3.GraphQLInputObjectType({
|
|
365
|
-
name: "InnerOrder",
|
|
366
|
-
fields: {
|
|
367
|
-
direction: {
|
|
368
|
-
type: new import_graphql3.GraphQLNonNull(
|
|
369
|
-
new import_graphql3.GraphQLEnumType({
|
|
370
|
-
name: "OrderDirection",
|
|
371
|
-
description: "Order by direction",
|
|
372
|
-
values: {
|
|
373
|
-
asc: {
|
|
374
|
-
value: "asc",
|
|
375
|
-
description: "Ascending order"
|
|
376
|
-
},
|
|
377
|
-
desc: {
|
|
378
|
-
value: "desc",
|
|
379
|
-
description: "Descending order"
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
})
|
|
383
|
-
)
|
|
384
|
-
},
|
|
385
|
-
priority: {
|
|
386
|
-
type: new import_graphql3.GraphQLNonNull(import_graphql3.GraphQLInt),
|
|
387
|
-
description: "Priority of current field"
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
});
|
|
391
|
-
var generateColumnFilterValues = (column, tableName, columnName) => {
|
|
392
|
-
const columnGraphQLType = drizzleColumnToGraphQLType(
|
|
393
|
-
column,
|
|
394
|
-
columnName,
|
|
395
|
-
tableName,
|
|
396
|
-
true,
|
|
397
|
-
false,
|
|
398
|
-
true
|
|
399
|
-
);
|
|
400
|
-
const columnArr = new import_graphql3.GraphQLList(new import_graphql3.GraphQLNonNull(columnGraphQLType.type));
|
|
401
|
-
const baseFields = {
|
|
402
|
-
eq: {
|
|
403
|
-
type: columnGraphQLType.type,
|
|
404
|
-
description: columnGraphQLType.description
|
|
405
|
-
},
|
|
406
|
-
ne: {
|
|
407
|
-
type: columnGraphQLType.type,
|
|
408
|
-
description: columnGraphQLType.description
|
|
409
|
-
},
|
|
410
|
-
lt: {
|
|
411
|
-
type: columnGraphQLType.type,
|
|
412
|
-
description: columnGraphQLType.description
|
|
413
|
-
},
|
|
414
|
-
lte: {
|
|
415
|
-
type: columnGraphQLType.type,
|
|
416
|
-
description: columnGraphQLType.description
|
|
417
|
-
},
|
|
418
|
-
gt: {
|
|
419
|
-
type: columnGraphQLType.type,
|
|
420
|
-
description: columnGraphQLType.description
|
|
421
|
-
},
|
|
422
|
-
gte: {
|
|
423
|
-
type: columnGraphQLType.type,
|
|
424
|
-
description: columnGraphQLType.description
|
|
425
|
-
},
|
|
426
|
-
like: { type: import_graphql3.GraphQLString },
|
|
427
|
-
notLike: { type: import_graphql3.GraphQLString },
|
|
428
|
-
ilike: { type: import_graphql3.GraphQLString },
|
|
429
|
-
notIlike: { type: import_graphql3.GraphQLString },
|
|
430
|
-
inArray: {
|
|
431
|
-
type: columnArr,
|
|
432
|
-
description: `Array<${columnGraphQLType.description}>`
|
|
433
|
-
},
|
|
434
|
-
notInArray: {
|
|
435
|
-
type: columnArr,
|
|
436
|
-
description: `Array<${columnGraphQLType.description}>`
|
|
437
|
-
},
|
|
438
|
-
isNull: { type: import_graphql3.GraphQLBoolean },
|
|
439
|
-
isNotNull: { type: import_graphql3.GraphQLBoolean }
|
|
440
|
-
};
|
|
441
|
-
const type = new import_graphql3.GraphQLInputObjectType({
|
|
442
|
-
name: `${capitalize(tableName)}${capitalize(columnName)}Filters`,
|
|
443
|
-
fields: {
|
|
444
|
-
...baseFields,
|
|
445
|
-
OR: {
|
|
446
|
-
type: new import_graphql3.GraphQLList(
|
|
447
|
-
new import_graphql3.GraphQLNonNull(
|
|
448
|
-
new import_graphql3.GraphQLInputObjectType({
|
|
449
|
-
name: `${capitalize(tableName)}${capitalize(
|
|
450
|
-
columnName
|
|
451
|
-
)}filtersOr`,
|
|
452
|
-
fields: {
|
|
453
|
-
...baseFields
|
|
454
|
-
}
|
|
455
|
-
})
|
|
456
|
-
)
|
|
457
|
-
)
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
});
|
|
461
|
-
return type;
|
|
462
|
-
};
|
|
463
|
-
var orderMap = /* @__PURE__ */ new WeakMap();
|
|
464
|
-
var generateTableOrderCached = (table) => {
|
|
465
|
-
if (orderMap.has(table))
|
|
466
|
-
return orderMap.get(table);
|
|
467
|
-
const columns = (0, import_drizzle_orm3.getTableColumns)(table);
|
|
468
|
-
const columnEntries = Object.entries(columns);
|
|
469
|
-
const remapped = Object.fromEntries(
|
|
470
|
-
columnEntries.map(([columnName, columnDescription]) => [
|
|
471
|
-
columnName,
|
|
472
|
-
{ type: innerOrder }
|
|
473
|
-
])
|
|
474
|
-
);
|
|
475
|
-
orderMap.set(table, remapped);
|
|
476
|
-
return remapped;
|
|
477
|
-
};
|
|
478
|
-
var filterMap = /* @__PURE__ */ new WeakMap();
|
|
479
|
-
var generateTableFilterValuesCached = (table, tableName) => {
|
|
480
|
-
if (filterMap.has(table))
|
|
481
|
-
return filterMap.get(table);
|
|
482
|
-
const columns = (0, import_drizzle_orm3.getTableColumns)(table);
|
|
483
|
-
const columnEntries = Object.entries(columns);
|
|
484
|
-
const remapped = Object.fromEntries(
|
|
485
|
-
columnEntries.map(([columnName, columnDescription]) => [
|
|
486
|
-
columnName,
|
|
487
|
-
{
|
|
488
|
-
type: generateColumnFilterValues(
|
|
489
|
-
columnDescription,
|
|
490
|
-
tableName,
|
|
491
|
-
columnName
|
|
492
|
-
)
|
|
493
|
-
}
|
|
494
|
-
])
|
|
495
|
-
);
|
|
496
|
-
filterMap.set(table, remapped);
|
|
497
|
-
return remapped;
|
|
498
|
-
};
|
|
499
|
-
var fieldMap = /* @__PURE__ */ new WeakMap();
|
|
500
|
-
var generateTableSelectTypeFieldsCached = (table, tableName) => {
|
|
501
|
-
if (fieldMap.has(table))
|
|
502
|
-
return fieldMap.get(table);
|
|
503
|
-
const columns = (0, import_drizzle_orm3.getTableColumns)(table);
|
|
504
|
-
const columnEntries = Object.entries(columns);
|
|
505
|
-
const remapped = Object.fromEntries(
|
|
506
|
-
columnEntries.map(([columnName, columnDescription]) => [
|
|
507
|
-
columnName,
|
|
508
|
-
drizzleColumnToGraphQLType(columnDescription, columnName, tableName)
|
|
509
|
-
])
|
|
510
|
-
);
|
|
511
|
-
fieldMap.set(table, remapped);
|
|
512
|
-
return remapped;
|
|
513
|
-
};
|
|
514
|
-
var orderTypeMap = /* @__PURE__ */ new WeakMap();
|
|
515
|
-
var generateTableOrderTypeCached = (table, tableName) => {
|
|
516
|
-
if (orderTypeMap.has(table))
|
|
517
|
-
return orderTypeMap.get(table);
|
|
518
|
-
const orderColumns = generateTableOrderCached(table);
|
|
519
|
-
const order = new import_graphql3.GraphQLInputObjectType({
|
|
520
|
-
name: `${capitalize(tableName)}OrderBy`,
|
|
521
|
-
fields: orderColumns
|
|
522
|
-
});
|
|
523
|
-
orderTypeMap.set(table, order);
|
|
524
|
-
return order;
|
|
525
|
-
};
|
|
526
|
-
var filterTypeMap = /* @__PURE__ */ new WeakMap();
|
|
527
|
-
var generateTableFilterTypeCached = (table, tableName) => {
|
|
528
|
-
if (filterTypeMap.has(table))
|
|
529
|
-
return filterTypeMap.get(table);
|
|
530
|
-
const filterColumns = generateTableFilterValuesCached(table, tableName);
|
|
531
|
-
const filters = new import_graphql3.GraphQLInputObjectType({
|
|
532
|
-
name: `${capitalize(tableName)}Filters`,
|
|
533
|
-
fields: {
|
|
534
|
-
...filterColumns,
|
|
535
|
-
OR: {
|
|
536
|
-
type: new import_graphql3.GraphQLList(
|
|
537
|
-
new import_graphql3.GraphQLNonNull(
|
|
538
|
-
new import_graphql3.GraphQLInputObjectType({
|
|
539
|
-
name: `${capitalize(tableName)}FiltersOr`,
|
|
540
|
-
fields: filterColumns
|
|
541
|
-
})
|
|
542
|
-
)
|
|
543
|
-
)
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
});
|
|
547
|
-
filterTypeMap.set(table, filters);
|
|
548
|
-
return filters;
|
|
549
|
-
};
|
|
550
|
-
var interfaceTypeMap = /* @__PURE__ */ new WeakMap();
|
|
551
|
-
var generateTableInterfaceTypeCached = (table, tableName) => {
|
|
552
|
-
if (interfaceTypeMap.has(table))
|
|
553
|
-
return interfaceTypeMap.get(table);
|
|
554
|
-
const tableFields = generateTableSelectTypeFieldsCached(table, tableName);
|
|
555
|
-
const interfaceType = new import_graphql3.GraphQLInterfaceType({
|
|
556
|
-
name: `${capitalize(tableName)}Fields`,
|
|
557
|
-
fields: tableFields,
|
|
558
|
-
resolveType(obj) {
|
|
559
|
-
if (obj.__typename) {
|
|
560
|
-
return obj.__typename;
|
|
561
|
-
}
|
|
562
|
-
return null;
|
|
563
|
-
}
|
|
564
|
-
});
|
|
565
|
-
interfaceTypeMap.set(table, interfaceType);
|
|
566
|
-
return interfaceType;
|
|
567
|
-
};
|
|
568
|
-
var generateSelectFields = (tables, tableName, relationMap, typeName, withOrder, relationsDepthLimit, currentDepth = 0, usedTables = /* @__PURE__ */ new Set()) => {
|
|
569
|
-
const relations = relationMap[tableName];
|
|
570
|
-
const relationEntries = relations ? Object.entries(relations) : [];
|
|
571
|
-
const table = tables[tableName];
|
|
572
|
-
const order = withOrder ? generateTableOrderTypeCached(table, tableName) : void 0;
|
|
573
|
-
const filters = generateTableFilterTypeCached(table, tableName);
|
|
574
|
-
const tableFields = generateTableSelectTypeFieldsCached(table, tableName);
|
|
575
|
-
if (usedTables.has(tableName) || typeof relationsDepthLimit === "number" && currentDepth >= relationsDepthLimit || !relationEntries.length) {
|
|
576
|
-
return {
|
|
577
|
-
order,
|
|
578
|
-
filters,
|
|
579
|
-
tableFields,
|
|
580
|
-
relationFields: {}
|
|
581
|
-
};
|
|
582
|
-
}
|
|
583
|
-
const rawRelationFields = [];
|
|
584
|
-
const updatedUsedTables = new Set(usedTables).add(tableName);
|
|
585
|
-
const newDepth = currentDepth + 1;
|
|
586
|
-
for (const [relationName, { targetTableName, relation }] of relationEntries) {
|
|
587
|
-
const relTypeName = `${typeName}${capitalize(relationName)}Relation`;
|
|
588
|
-
const isOne = (0, import_drizzle_orm3.is)(relation, import_drizzle_orm3.One);
|
|
589
|
-
const relData = generateSelectFields(
|
|
590
|
-
tables,
|
|
591
|
-
targetTableName,
|
|
592
|
-
relationMap,
|
|
593
|
-
relTypeName,
|
|
594
|
-
!isOne,
|
|
595
|
-
relationsDepthLimit,
|
|
596
|
-
newDepth,
|
|
597
|
-
updatedUsedTables
|
|
598
|
-
);
|
|
599
|
-
const targetTable = tables[targetTableName];
|
|
600
|
-
const targetInterface = generateTableInterfaceTypeCached(
|
|
601
|
-
targetTable,
|
|
602
|
-
targetTableName
|
|
603
|
-
);
|
|
604
|
-
const relType = new import_graphql3.GraphQLObjectType({
|
|
605
|
-
name: relTypeName,
|
|
606
|
-
fields: { ...relData.tableFields, ...relData.relationFields },
|
|
607
|
-
interfaces: [targetInterface]
|
|
608
|
-
});
|
|
609
|
-
if (isOne) {
|
|
610
|
-
rawRelationFields.push([
|
|
611
|
-
relationName,
|
|
612
|
-
{
|
|
613
|
-
type: relType,
|
|
614
|
-
args: {
|
|
615
|
-
where: { type: relData.filters }
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
]);
|
|
619
|
-
continue;
|
|
620
|
-
}
|
|
621
|
-
rawRelationFields.push([
|
|
622
|
-
relationName,
|
|
623
|
-
{
|
|
624
|
-
type: new import_graphql3.GraphQLNonNull(new import_graphql3.GraphQLList(new import_graphql3.GraphQLNonNull(relType))),
|
|
625
|
-
args: {
|
|
626
|
-
where: { type: relData.filters },
|
|
627
|
-
orderBy: { type: relData.order },
|
|
628
|
-
offset: { type: import_graphql3.GraphQLInt },
|
|
629
|
-
limit: { type: import_graphql3.GraphQLInt }
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
]);
|
|
633
|
-
}
|
|
634
|
-
const relationFields = Object.fromEntries(rawRelationFields);
|
|
635
|
-
return {
|
|
636
|
-
order,
|
|
637
|
-
filters,
|
|
638
|
-
tableFields,
|
|
639
|
-
relationFields
|
|
640
|
-
};
|
|
641
|
-
};
|
|
642
|
-
var generateTableTypes = (tableName, tables, relationMap, withReturning, relationsDepthLimit) => {
|
|
643
|
-
const stylizedName = capitalize(tableName);
|
|
644
|
-
const { tableFields, relationFields, filters, order } = generateSelectFields(
|
|
645
|
-
tables,
|
|
646
|
-
tableName,
|
|
647
|
-
relationMap,
|
|
648
|
-
stylizedName,
|
|
649
|
-
true,
|
|
650
|
-
relationsDepthLimit
|
|
651
|
-
);
|
|
652
|
-
const table = tables[tableName];
|
|
653
|
-
const columns = (0, import_drizzle_orm3.getTableColumns)(table);
|
|
654
|
-
const columnEntries = Object.entries(columns);
|
|
655
|
-
const insertFields = Object.fromEntries(
|
|
656
|
-
columnEntries.map(([columnName, columnDescription]) => [
|
|
657
|
-
columnName,
|
|
658
|
-
drizzleColumnToGraphQLType(
|
|
659
|
-
columnDescription,
|
|
660
|
-
columnName,
|
|
661
|
-
tableName,
|
|
662
|
-
false,
|
|
663
|
-
true,
|
|
664
|
-
true
|
|
665
|
-
)
|
|
666
|
-
])
|
|
667
|
-
);
|
|
668
|
-
const updateFields = Object.fromEntries(
|
|
669
|
-
columnEntries.map(([columnName, columnDescription]) => [
|
|
670
|
-
columnName,
|
|
671
|
-
drizzleColumnToGraphQLType(
|
|
672
|
-
columnDescription,
|
|
673
|
-
columnName,
|
|
674
|
-
tableName,
|
|
675
|
-
true,
|
|
676
|
-
false,
|
|
677
|
-
true
|
|
678
|
-
)
|
|
679
|
-
])
|
|
680
|
-
);
|
|
681
|
-
const insertInput = new import_graphql3.GraphQLInputObjectType({
|
|
682
|
-
name: `${stylizedName}InsertInput`,
|
|
683
|
-
fields: insertFields
|
|
684
|
-
});
|
|
685
|
-
const tableFieldsInterface = generateTableInterfaceTypeCached(
|
|
686
|
-
table,
|
|
687
|
-
tableName
|
|
688
|
-
);
|
|
689
|
-
const selectSingleOutput = new import_graphql3.GraphQLObjectType({
|
|
690
|
-
name: `${stylizedName}SelectItem`,
|
|
691
|
-
fields: { ...tableFields, ...relationFields },
|
|
692
|
-
interfaces: [tableFieldsInterface]
|
|
693
|
-
});
|
|
694
|
-
const selectArrOutput = new import_graphql3.GraphQLNonNull(
|
|
695
|
-
new import_graphql3.GraphQLList(new import_graphql3.GraphQLNonNull(selectSingleOutput))
|
|
696
|
-
);
|
|
697
|
-
const singleTableItemOutput = withReturning ? new import_graphql3.GraphQLObjectType({
|
|
698
|
-
name: `${stylizedName}Item`,
|
|
699
|
-
fields: tableFields,
|
|
700
|
-
interfaces: [tableFieldsInterface]
|
|
701
|
-
}) : void 0;
|
|
702
|
-
const arrTableItemOutput = withReturning ? new import_graphql3.GraphQLNonNull(
|
|
703
|
-
new import_graphql3.GraphQLList(new import_graphql3.GraphQLNonNull(singleTableItemOutput))
|
|
704
|
-
) : void 0;
|
|
705
|
-
const updateInput = new import_graphql3.GraphQLInputObjectType({
|
|
706
|
-
name: `${stylizedName}UpdateInput`,
|
|
707
|
-
fields: updateFields
|
|
708
|
-
});
|
|
709
|
-
const inputs = {
|
|
710
|
-
insertInput,
|
|
711
|
-
updateInput,
|
|
712
|
-
tableOrder: order,
|
|
713
|
-
tableFilters: filters
|
|
714
|
-
};
|
|
715
|
-
const outputs = withReturning ? {
|
|
716
|
-
selectSingleOutput,
|
|
717
|
-
selectArrOutput,
|
|
718
|
-
singleTableItemOutput,
|
|
719
|
-
arrTableItemOutput,
|
|
720
|
-
tableFieldsInterface
|
|
721
|
-
} : {
|
|
722
|
-
selectSingleOutput,
|
|
723
|
-
selectArrOutput,
|
|
724
|
-
tableFieldsInterface
|
|
725
|
-
};
|
|
726
|
-
return {
|
|
727
|
-
inputs,
|
|
728
|
-
outputs
|
|
729
|
-
};
|
|
730
|
-
};
|
|
731
|
-
var extractOrderBy = (table, orderArgs) => {
|
|
732
|
-
const res = [];
|
|
733
|
-
for (const [column, config] of Object.entries(orderArgs).sort(
|
|
734
|
-
(a, b) => (b[1]?.priority ?? 0) - (a[1]?.priority ?? 0)
|
|
735
|
-
)) {
|
|
736
|
-
if (!config)
|
|
737
|
-
continue;
|
|
738
|
-
const { direction } = config;
|
|
739
|
-
res.push(
|
|
740
|
-
direction === "asc" ? (0, import_drizzle_orm3.asc)((0, import_drizzle_orm3.getTableColumns)(table)[column]) : (0, import_drizzle_orm3.desc)((0, import_drizzle_orm3.getTableColumns)(table)[column])
|
|
741
|
-
);
|
|
742
|
-
}
|
|
743
|
-
return res;
|
|
744
|
-
};
|
|
745
|
-
var extractFiltersColumn = (column, columnName, operators) => {
|
|
746
|
-
if (!operators.OR?.length)
|
|
747
|
-
delete operators.OR;
|
|
748
|
-
const entries = Object.entries(
|
|
749
|
-
operators
|
|
750
|
-
);
|
|
751
|
-
if (operators.OR) {
|
|
752
|
-
if (entries.length > 1) {
|
|
753
|
-
throw new import_graphql3.GraphQLError(
|
|
754
|
-
`WHERE ${columnName}: Cannot specify both fields and 'OR' in column operators!`
|
|
755
|
-
);
|
|
756
|
-
}
|
|
757
|
-
const variants2 = [];
|
|
758
|
-
for (const variant of operators.OR) {
|
|
759
|
-
const extracted = extractFiltersColumn(column, columnName, variant);
|
|
760
|
-
if (extracted)
|
|
761
|
-
variants2.push(extracted);
|
|
762
|
-
}
|
|
763
|
-
return variants2.length ? variants2.length > 1 ? (0, import_drizzle_orm3.or)(...variants2) : variants2[0] : void 0;
|
|
764
|
-
}
|
|
765
|
-
const variants = [];
|
|
766
|
-
for (const [operatorName, operatorValue] of entries) {
|
|
767
|
-
if (operatorValue === null || operatorValue === false)
|
|
768
|
-
continue;
|
|
769
|
-
let operator;
|
|
770
|
-
switch (operatorName) {
|
|
771
|
-
case "eq":
|
|
772
|
-
operator = operator ?? import_drizzle_orm3.eq;
|
|
773
|
-
case "ne":
|
|
774
|
-
operator = operator ?? import_drizzle_orm3.ne;
|
|
775
|
-
case "gt":
|
|
776
|
-
operator = operator ?? import_drizzle_orm3.gt;
|
|
777
|
-
case "gte":
|
|
778
|
-
operator = operator ?? import_drizzle_orm3.gte;
|
|
779
|
-
case "lt":
|
|
780
|
-
operator = operator ?? import_drizzle_orm3.lt;
|
|
781
|
-
case "lte":
|
|
782
|
-
operator = operator ?? import_drizzle_orm3.lte;
|
|
783
|
-
const singleValue = remapFromGraphQLCore(
|
|
784
|
-
operatorValue,
|
|
785
|
-
column,
|
|
786
|
-
columnName
|
|
787
|
-
);
|
|
788
|
-
variants.push(operator(column, singleValue));
|
|
789
|
-
break;
|
|
790
|
-
case "like":
|
|
791
|
-
operator = operator ?? import_drizzle_orm3.like;
|
|
792
|
-
case "notLike":
|
|
793
|
-
operator = operator ?? import_drizzle_orm3.notLike;
|
|
794
|
-
case "ilike":
|
|
795
|
-
operator = operator ?? import_drizzle_orm3.ilike;
|
|
796
|
-
case "notIlike":
|
|
797
|
-
operator = operator ?? import_drizzle_orm3.notIlike;
|
|
798
|
-
variants.push(operator(column, operatorValue));
|
|
799
|
-
break;
|
|
800
|
-
case "inArray":
|
|
801
|
-
operator = operator ?? import_drizzle_orm3.inArray;
|
|
802
|
-
case "notInArray":
|
|
803
|
-
operator = operator ?? import_drizzle_orm3.notInArray;
|
|
804
|
-
if (!operatorValue.length) {
|
|
805
|
-
throw new import_graphql3.GraphQLError(
|
|
806
|
-
`WHERE ${columnName}: Unable to use operator ${operatorName} with an empty array!`
|
|
807
|
-
);
|
|
808
|
-
}
|
|
809
|
-
const arrayValue = operatorValue.map(
|
|
810
|
-
(val) => remapFromGraphQLCore(val, column, columnName)
|
|
811
|
-
);
|
|
812
|
-
variants.push(operator(column, arrayValue));
|
|
813
|
-
break;
|
|
814
|
-
case "isNull":
|
|
815
|
-
operator = operator ?? import_drizzle_orm3.isNull;
|
|
816
|
-
case "isNotNull":
|
|
817
|
-
operator = operator ?? import_drizzle_orm3.isNotNull;
|
|
818
|
-
variants.push(operator(column));
|
|
819
|
-
}
|
|
820
|
-
}
|
|
821
|
-
return variants.length ? variants.length > 1 ? (0, import_drizzle_orm3.and)(...variants) : variants[0] : void 0;
|
|
822
|
-
};
|
|
823
|
-
var extractFilters = (table, tableName, filters) => {
|
|
824
|
-
if (!filters.OR?.length)
|
|
825
|
-
delete filters.OR;
|
|
826
|
-
const entries = Object.entries(filters);
|
|
827
|
-
if (!entries.length)
|
|
828
|
-
return;
|
|
829
|
-
if (filters.OR) {
|
|
830
|
-
if (entries.length > 1) {
|
|
831
|
-
throw new import_graphql3.GraphQLError(
|
|
832
|
-
`WHERE ${tableName}: Cannot specify both fields and 'OR' in table filters!`
|
|
833
|
-
);
|
|
834
|
-
}
|
|
835
|
-
const variants2 = [];
|
|
836
|
-
for (const variant of filters.OR) {
|
|
837
|
-
const extracted = extractFilters(table, tableName, variant);
|
|
838
|
-
if (extracted)
|
|
839
|
-
variants2.push(extracted);
|
|
840
|
-
}
|
|
841
|
-
return variants2.length ? variants2.length > 1 ? (0, import_drizzle_orm3.or)(...variants2) : variants2[0] : void 0;
|
|
842
|
-
}
|
|
843
|
-
const variants = [];
|
|
844
|
-
for (const [columnName, operators] of entries) {
|
|
845
|
-
if (operators === null)
|
|
846
|
-
continue;
|
|
847
|
-
const column = (0, import_drizzle_orm3.getTableColumns)(table)[columnName];
|
|
848
|
-
variants.push(extractFiltersColumn(column, columnName, operators));
|
|
849
|
-
}
|
|
850
|
-
return variants.length ? variants.length > 1 ? (0, import_drizzle_orm3.and)(...variants) : variants[0] : void 0;
|
|
851
|
-
};
|
|
852
|
-
var extractRelationsParamsInner = (relationMap, tables, tableName, typeName, originField, isInitial = false) => {
|
|
853
|
-
const relations = relationMap[tableName];
|
|
854
|
-
if (!relations)
|
|
855
|
-
return void 0;
|
|
856
|
-
const baseField = Object.entries(originField.fieldsByTypeName).find(
|
|
857
|
-
([key, value]) => key === typeName
|
|
858
|
-
)?.[1];
|
|
859
|
-
if (!baseField)
|
|
860
|
-
return void 0;
|
|
861
|
-
const args = {};
|
|
862
|
-
for (const [relName, { targetTableName, relation }] of Object.entries(
|
|
863
|
-
relations
|
|
864
|
-
)) {
|
|
865
|
-
const relTypeName = `${isInitial ? capitalize(tableName) : typeName}${capitalize(relName)}Relation`;
|
|
866
|
-
const relationField = Object.values(baseField).find(
|
|
867
|
-
(field) => field.name === relName
|
|
868
|
-
);
|
|
869
|
-
if (!relationField)
|
|
870
|
-
continue;
|
|
871
|
-
const allFields = {};
|
|
872
|
-
if (relationField.fieldsByTypeName) {
|
|
873
|
-
for (const [typeName2, typeFields] of Object.entries(
|
|
874
|
-
relationField.fieldsByTypeName
|
|
875
|
-
)) {
|
|
876
|
-
Object.assign(allFields, typeFields);
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
const columns = extractSelectedColumnsFromTree(
|
|
880
|
-
allFields,
|
|
881
|
-
tables[targetTableName]
|
|
882
|
-
);
|
|
883
|
-
const thisRecord = {};
|
|
884
|
-
thisRecord.columns = columns;
|
|
885
|
-
const relationArgs = relationField?.args;
|
|
886
|
-
const orderBy = relationArgs?.orderBy ? extractOrderBy(tables[targetTableName], relationArgs.orderBy) : void 0;
|
|
887
|
-
const where = relationArgs?.where ? extractFilters(tables[targetTableName], relName, relationArgs?.where) : void 0;
|
|
888
|
-
const offset = relationArgs?.offset ?? void 0;
|
|
889
|
-
const limit = relationArgs?.limit ?? void 0;
|
|
890
|
-
thisRecord.orderBy = orderBy;
|
|
891
|
-
thisRecord.where = where;
|
|
892
|
-
thisRecord.offset = offset;
|
|
893
|
-
thisRecord.limit = limit;
|
|
894
|
-
const relWith = relationField ? extractRelationsParamsInner(
|
|
895
|
-
relationMap,
|
|
896
|
-
tables,
|
|
897
|
-
targetTableName,
|
|
898
|
-
relTypeName,
|
|
899
|
-
relationField
|
|
900
|
-
) : void 0;
|
|
901
|
-
thisRecord.with = relWith;
|
|
902
|
-
args[relName] = thisRecord;
|
|
903
|
-
}
|
|
904
|
-
return args;
|
|
905
|
-
};
|
|
906
|
-
var extractRelationsParams = (relationMap, tables, tableName, info, typeName) => {
|
|
907
|
-
if (!info)
|
|
908
|
-
return void 0;
|
|
909
|
-
return extractRelationsParamsInner(
|
|
910
|
-
relationMap,
|
|
911
|
-
tables,
|
|
912
|
-
tableName,
|
|
913
|
-
typeName,
|
|
914
|
-
info,
|
|
915
|
-
true
|
|
916
|
-
);
|
|
917
|
-
};
|
|
918
|
-
|
|
919
|
-
// src/util/builders/mysql.ts
|
|
920
|
-
var import_graphql_parse_resolve_info = require("graphql-parse-resolve-info");
|
|
921
|
-
var generateSelectArray = (db, tableName, tables, relationMap, orderArgs, filterArgs) => {
|
|
922
|
-
const queryName = `${uncapitalize(tableName)}`;
|
|
923
|
-
const queryBase = db.query[tableName];
|
|
924
|
-
if (!queryBase) {
|
|
925
|
-
throw new Error(
|
|
926
|
-
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
927
|
-
);
|
|
928
|
-
}
|
|
929
|
-
const queryArgs = {
|
|
930
|
-
offset: {
|
|
931
|
-
type: import_graphql4.GraphQLInt
|
|
932
|
-
},
|
|
933
|
-
limit: {
|
|
934
|
-
type: import_graphql4.GraphQLInt
|
|
935
|
-
},
|
|
936
|
-
orderBy: {
|
|
937
|
-
type: orderArgs
|
|
938
|
-
},
|
|
939
|
-
where: {
|
|
940
|
-
type: filterArgs
|
|
941
|
-
}
|
|
942
|
-
};
|
|
943
|
-
const typeName = `${capitalize(tableName)}SelectItem`;
|
|
944
|
-
const table = tables[tableName];
|
|
945
|
-
return {
|
|
946
|
-
name: queryName,
|
|
947
|
-
resolver: async (source, args, context, info) => {
|
|
948
|
-
try {
|
|
949
|
-
const { offset, limit, orderBy, where } = args;
|
|
950
|
-
const parsedInfo = (0, import_graphql_parse_resolve_info.parseResolveInfo)(info, {
|
|
951
|
-
deep: true
|
|
952
|
-
});
|
|
953
|
-
const allFields = {};
|
|
954
|
-
if (parsedInfo.fieldsByTypeName) {
|
|
955
|
-
for (const fields of Object.values(parsedInfo.fieldsByTypeName)) {
|
|
956
|
-
Object.assign(allFields, fields);
|
|
957
|
-
}
|
|
958
|
-
}
|
|
959
|
-
const query = queryBase.findMany({
|
|
960
|
-
columns: extractSelectedColumnsFromTree(allFields, table),
|
|
961
|
-
offset,
|
|
962
|
-
limit,
|
|
963
|
-
orderBy: orderBy ? extractOrderBy(table, orderBy) : void 0,
|
|
964
|
-
where: where ? extractFilters(table, tableName, where) : void 0,
|
|
965
|
-
with: relationMap[tableName] ? extractRelationsParams(
|
|
966
|
-
relationMap,
|
|
967
|
-
tables,
|
|
968
|
-
tableName,
|
|
969
|
-
parsedInfo,
|
|
970
|
-
typeName
|
|
971
|
-
) : void 0
|
|
972
|
-
});
|
|
973
|
-
const result = await query;
|
|
974
|
-
return remapToGraphQLArrayOutput(result, tableName, table, relationMap);
|
|
975
|
-
} catch (e) {
|
|
976
|
-
if (typeof e === "object" && typeof e.message === "string") {
|
|
977
|
-
throw new import_graphql4.GraphQLError(e.message);
|
|
978
|
-
}
|
|
979
|
-
throw e;
|
|
980
|
-
}
|
|
981
|
-
},
|
|
982
|
-
args: queryArgs
|
|
983
|
-
};
|
|
984
|
-
};
|
|
985
|
-
var generateSelectSingle = (db, tableName, tables, relationMap, orderArgs, filterArgs) => {
|
|
986
|
-
const queryName = `${uncapitalize(tableName)}Single`;
|
|
987
|
-
const queryBase = db.query[tableName];
|
|
988
|
-
if (!queryBase) {
|
|
989
|
-
throw new Error(
|
|
990
|
-
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
991
|
-
);
|
|
992
|
-
}
|
|
993
|
-
const queryArgs = {
|
|
994
|
-
offset: {
|
|
995
|
-
type: import_graphql4.GraphQLInt
|
|
996
|
-
},
|
|
997
|
-
orderBy: {
|
|
998
|
-
type: orderArgs
|
|
999
|
-
},
|
|
1000
|
-
where: {
|
|
1001
|
-
type: filterArgs
|
|
1002
|
-
}
|
|
1003
|
-
};
|
|
1004
|
-
const typeName = `${capitalize(tableName)}SelectItem`;
|
|
1005
|
-
const table = tables[tableName];
|
|
1006
|
-
return {
|
|
1007
|
-
name: queryName,
|
|
1008
|
-
resolver: async (source, args, context, info) => {
|
|
1009
|
-
try {
|
|
1010
|
-
const { offset, orderBy, where } = args;
|
|
1011
|
-
const parsedInfo = (0, import_graphql_parse_resolve_info.parseResolveInfo)(info, {
|
|
1012
|
-
deep: true
|
|
1013
|
-
});
|
|
1014
|
-
const allFields = {};
|
|
1015
|
-
if (parsedInfo.fieldsByTypeName) {
|
|
1016
|
-
for (const fields of Object.values(parsedInfo.fieldsByTypeName)) {
|
|
1017
|
-
Object.assign(allFields, fields);
|
|
1018
|
-
}
|
|
1019
|
-
}
|
|
1020
|
-
const query = queryBase.findFirst({
|
|
1021
|
-
columns: extractSelectedColumnsFromTree(allFields, table),
|
|
1022
|
-
offset,
|
|
1023
|
-
orderBy: orderBy ? extractOrderBy(table, orderBy) : void 0,
|
|
1024
|
-
where: where ? extractFilters(table, tableName, where) : void 0,
|
|
1025
|
-
with: relationMap[tableName] ? extractRelationsParams(
|
|
1026
|
-
relationMap,
|
|
1027
|
-
tables,
|
|
1028
|
-
tableName,
|
|
1029
|
-
parsedInfo,
|
|
1030
|
-
typeName
|
|
1031
|
-
) : void 0
|
|
1032
|
-
});
|
|
1033
|
-
const result = await query;
|
|
1034
|
-
if (!result)
|
|
1035
|
-
return void 0;
|
|
1036
|
-
return remapToGraphQLSingleOutput(
|
|
1037
|
-
result,
|
|
1038
|
-
tableName,
|
|
1039
|
-
table,
|
|
1040
|
-
relationMap
|
|
1041
|
-
);
|
|
1042
|
-
} catch (e) {
|
|
1043
|
-
if (typeof e === "object" && typeof e.message === "string") {
|
|
1044
|
-
throw new import_graphql4.GraphQLError(e.message);
|
|
1045
|
-
}
|
|
1046
|
-
throw e;
|
|
1047
|
-
}
|
|
1048
|
-
},
|
|
1049
|
-
args: queryArgs
|
|
1050
|
-
};
|
|
1051
|
-
};
|
|
1052
|
-
var generateInsertArray = (db, tableName, table, baseType) => {
|
|
1053
|
-
const queryName = `insertInto${capitalize(tableName)}`;
|
|
1054
|
-
const queryArgs = {
|
|
1055
|
-
values: {
|
|
1056
|
-
type: new import_graphql4.GraphQLNonNull(new import_graphql4.GraphQLList(new import_graphql4.GraphQLNonNull(baseType)))
|
|
1057
|
-
}
|
|
1058
|
-
};
|
|
1059
|
-
return {
|
|
1060
|
-
name: queryName,
|
|
1061
|
-
resolver: async (source, args, context, info) => {
|
|
1062
|
-
try {
|
|
1063
|
-
const input = remapFromGraphQLArrayInput(args.values, table);
|
|
1064
|
-
if (!input.length)
|
|
1065
|
-
throw new import_graphql4.GraphQLError("No values were provided!");
|
|
1066
|
-
await db.insert(table).values(input);
|
|
1067
|
-
return { isSuccess: true };
|
|
1068
|
-
} catch (e) {
|
|
1069
|
-
if (typeof e === "object" && typeof e.message === "string") {
|
|
1070
|
-
throw new import_graphql4.GraphQLError(e.message);
|
|
1071
|
-
}
|
|
1072
|
-
throw e;
|
|
1073
|
-
}
|
|
1074
|
-
},
|
|
1075
|
-
args: queryArgs
|
|
1076
|
-
};
|
|
1077
|
-
};
|
|
1078
|
-
var generateInsertSingle = (db, tableName, table, baseType) => {
|
|
1079
|
-
const queryName = `insertInto${capitalize(tableName)}Single`;
|
|
1080
|
-
const queryArgs = {
|
|
1081
|
-
values: {
|
|
1082
|
-
type: new import_graphql4.GraphQLNonNull(baseType)
|
|
1083
|
-
}
|
|
1084
|
-
};
|
|
1085
|
-
return {
|
|
1086
|
-
name: queryName,
|
|
1087
|
-
resolver: async (source, args, context, info) => {
|
|
1088
|
-
try {
|
|
1089
|
-
const input = remapFromGraphQLSingleInput(args.values, table);
|
|
1090
|
-
await db.insert(table).values(input);
|
|
1091
|
-
return { isSuccess: true };
|
|
1092
|
-
} catch (e) {
|
|
1093
|
-
if (typeof e === "object" && typeof e.message === "string") {
|
|
1094
|
-
throw new import_graphql4.GraphQLError(e.message);
|
|
1095
|
-
}
|
|
1096
|
-
throw e;
|
|
1097
|
-
}
|
|
1098
|
-
},
|
|
1099
|
-
args: queryArgs
|
|
1100
|
-
};
|
|
1101
|
-
};
|
|
1102
|
-
var generateUpdate = (db, tableName, table, setArgs, filterArgs) => {
|
|
1103
|
-
const queryName = `update${capitalize(tableName)}`;
|
|
1104
|
-
const queryArgs = {
|
|
1105
|
-
set: {
|
|
1106
|
-
type: new import_graphql4.GraphQLNonNull(setArgs)
|
|
1107
|
-
},
|
|
1108
|
-
where: {
|
|
1109
|
-
type: filterArgs
|
|
1110
|
-
}
|
|
1111
|
-
};
|
|
1112
|
-
return {
|
|
1113
|
-
name: queryName,
|
|
1114
|
-
resolver: async (source, args, context, info) => {
|
|
1115
|
-
try {
|
|
1116
|
-
const { where, set } = args;
|
|
1117
|
-
const input = remapFromGraphQLSingleInput(set, table);
|
|
1118
|
-
if (!Object.keys(input).length)
|
|
1119
|
-
throw new import_graphql4.GraphQLError("Unable to update with no values specified!");
|
|
1120
|
-
let query = db.update(table).set(input);
|
|
1121
|
-
if (where) {
|
|
1122
|
-
const filters = extractFilters(table, tableName, where);
|
|
1123
|
-
query = query.where(filters);
|
|
1124
|
-
}
|
|
1125
|
-
await query;
|
|
1126
|
-
return { isSuccess: true };
|
|
1127
|
-
} catch (e) {
|
|
1128
|
-
if (typeof e === "object" && typeof e.message === "string") {
|
|
1129
|
-
throw new import_graphql4.GraphQLError(e.message);
|
|
1130
|
-
}
|
|
1131
|
-
throw e;
|
|
1132
|
-
}
|
|
1133
|
-
},
|
|
1134
|
-
args: queryArgs
|
|
1135
|
-
};
|
|
1136
|
-
};
|
|
1137
|
-
var generateDelete = (db, tableName, table, filterArgs) => {
|
|
1138
|
-
const queryName = `deleteFrom${tableName}`;
|
|
1139
|
-
const queryArgs = {
|
|
1140
|
-
where: {
|
|
1141
|
-
type: filterArgs
|
|
1142
|
-
}
|
|
1143
|
-
};
|
|
1144
|
-
return {
|
|
1145
|
-
name: queryName,
|
|
1146
|
-
resolver: async (source, args, context, info) => {
|
|
1147
|
-
try {
|
|
1148
|
-
const { where } = args;
|
|
1149
|
-
let query = db.delete(table);
|
|
1150
|
-
if (where) {
|
|
1151
|
-
const filters = extractFilters(table, tableName, where);
|
|
1152
|
-
query = query.where(filters);
|
|
1153
|
-
}
|
|
1154
|
-
await query;
|
|
1155
|
-
return { isSuccess: true };
|
|
1156
|
-
} catch (e) {
|
|
1157
|
-
if (typeof e === "object" && typeof e.message === "string") {
|
|
1158
|
-
throw new import_graphql4.GraphQLError(e.message);
|
|
1159
|
-
}
|
|
1160
|
-
throw e;
|
|
1161
|
-
}
|
|
1162
|
-
},
|
|
1163
|
-
args: queryArgs
|
|
1164
|
-
};
|
|
1165
|
-
};
|
|
1166
|
-
var generateSchemaData = (db, schema, relationsDepthLimit) => {
|
|
1167
|
-
const rawSchema = schema;
|
|
1168
|
-
const schemaEntries = Object.entries(rawSchema);
|
|
1169
|
-
const tableEntries = schemaEntries.filter(
|
|
1170
|
-
([key, value]) => (0, import_drizzle_orm4.is)(value, import_mysql_core2.MySqlTable)
|
|
1171
|
-
);
|
|
1172
|
-
const tables = Object.fromEntries(tableEntries);
|
|
1173
|
-
if (!tableEntries.length) {
|
|
1174
|
-
throw new Error(
|
|
1175
|
-
"Drizzle-GraphQL Error: No tables detected in Drizzle-ORM's database instance. Did you forget to pass schema to drizzle constructor?"
|
|
1176
|
-
);
|
|
1177
|
-
}
|
|
1178
|
-
const rawRelations = schemaEntries.filter(([key, value]) => (0, import_drizzle_orm4.is)(value, import_drizzle_orm4.Relations)).map(([key, value]) => [
|
|
1179
|
-
tableEntries.find(
|
|
1180
|
-
([tableName, tableValue]) => tableValue === value.table
|
|
1181
|
-
)[0],
|
|
1182
|
-
value
|
|
1183
|
-
]).map(([tableName, relValue]) => [
|
|
1184
|
-
tableName,
|
|
1185
|
-
relValue.config((0, import_drizzle_orm4.createTableRelationsHelpers)(tables[tableName]))
|
|
1186
|
-
]);
|
|
1187
|
-
const namedRelations = Object.fromEntries(
|
|
1188
|
-
rawRelations.map(([relName, config]) => {
|
|
1189
|
-
const namedConfig = Object.fromEntries(
|
|
1190
|
-
Object.entries(config).map(([innerRelName, innerRelValue]) => [
|
|
1191
|
-
innerRelName,
|
|
1192
|
-
{
|
|
1193
|
-
relation: innerRelValue,
|
|
1194
|
-
targetTableName: tableEntries.find(
|
|
1195
|
-
([tableName, tableValue]) => tableValue === innerRelValue.referencedTable
|
|
1196
|
-
)[0]
|
|
1197
|
-
}
|
|
1198
|
-
])
|
|
1199
|
-
);
|
|
1200
|
-
return [relName, namedConfig];
|
|
1201
|
-
})
|
|
1202
|
-
);
|
|
1203
|
-
const queries = {};
|
|
1204
|
-
const mutations = {};
|
|
1205
|
-
const gqlSchemaTypes = Object.fromEntries(
|
|
1206
|
-
Object.entries(tables).map(([tableName, table]) => [
|
|
1207
|
-
tableName,
|
|
1208
|
-
generateTableTypes(
|
|
1209
|
-
tableName,
|
|
1210
|
-
tables,
|
|
1211
|
-
namedRelations,
|
|
1212
|
-
false,
|
|
1213
|
-
relationsDepthLimit
|
|
1214
|
-
)
|
|
1215
|
-
])
|
|
1216
|
-
);
|
|
1217
|
-
const mutationReturnType = new import_graphql4.GraphQLObjectType({
|
|
1218
|
-
name: `MutationReturn`,
|
|
1219
|
-
fields: {
|
|
1220
|
-
isSuccess: {
|
|
1221
|
-
type: new import_graphql4.GraphQLNonNull(import_graphql4.GraphQLBoolean)
|
|
1222
|
-
}
|
|
1223
|
-
}
|
|
1224
|
-
});
|
|
1225
|
-
const inputs = {};
|
|
1226
|
-
const interfaces = {};
|
|
1227
|
-
const outputs = {
|
|
1228
|
-
MutationReturn: mutationReturnType
|
|
1229
|
-
};
|
|
1230
|
-
for (const [tableName, tableTypes] of Object.entries(gqlSchemaTypes)) {
|
|
1231
|
-
const { insertInput, updateInput, tableFilters, tableOrder } = tableTypes.inputs;
|
|
1232
|
-
const { selectSingleOutput, selectArrOutput, tableFieldsInterface } = tableTypes.outputs;
|
|
1233
|
-
const selectArrGenerated = generateSelectArray(
|
|
1234
|
-
db,
|
|
1235
|
-
tableName,
|
|
1236
|
-
tables,
|
|
1237
|
-
namedRelations,
|
|
1238
|
-
tableOrder,
|
|
1239
|
-
tableFilters
|
|
1240
|
-
);
|
|
1241
|
-
const selectSingleGenerated = generateSelectSingle(
|
|
1242
|
-
db,
|
|
1243
|
-
tableName,
|
|
1244
|
-
tables,
|
|
1245
|
-
namedRelations,
|
|
1246
|
-
tableOrder,
|
|
1247
|
-
tableFilters
|
|
1248
|
-
);
|
|
1249
|
-
const insertArrGenerated = generateInsertArray(
|
|
1250
|
-
db,
|
|
1251
|
-
tableName,
|
|
1252
|
-
schema[tableName],
|
|
1253
|
-
insertInput
|
|
1254
|
-
);
|
|
1255
|
-
const insertSingleGenerated = generateInsertSingle(
|
|
1256
|
-
db,
|
|
1257
|
-
tableName,
|
|
1258
|
-
schema[tableName],
|
|
1259
|
-
insertInput
|
|
1260
|
-
);
|
|
1261
|
-
const updateGenerated = generateUpdate(
|
|
1262
|
-
db,
|
|
1263
|
-
tableName,
|
|
1264
|
-
schema[tableName],
|
|
1265
|
-
updateInput,
|
|
1266
|
-
tableFilters
|
|
1267
|
-
);
|
|
1268
|
-
const deleteGenerated = generateDelete(
|
|
1269
|
-
db,
|
|
1270
|
-
tableName,
|
|
1271
|
-
schema[tableName],
|
|
1272
|
-
tableFilters
|
|
1273
|
-
);
|
|
1274
|
-
queries[selectArrGenerated.name] = {
|
|
1275
|
-
type: selectArrOutput,
|
|
1276
|
-
args: selectArrGenerated.args,
|
|
1277
|
-
resolve: selectArrGenerated.resolver
|
|
1278
|
-
};
|
|
1279
|
-
queries[selectSingleGenerated.name] = {
|
|
1280
|
-
type: selectSingleOutput,
|
|
1281
|
-
args: selectSingleGenerated.args,
|
|
1282
|
-
resolve: selectSingleGenerated.resolver
|
|
1283
|
-
};
|
|
1284
|
-
mutations[insertArrGenerated.name] = {
|
|
1285
|
-
type: mutationReturnType,
|
|
1286
|
-
args: insertArrGenerated.args,
|
|
1287
|
-
resolve: insertArrGenerated.resolver
|
|
1288
|
-
};
|
|
1289
|
-
mutations[insertSingleGenerated.name] = {
|
|
1290
|
-
type: mutationReturnType,
|
|
1291
|
-
args: insertSingleGenerated.args,
|
|
1292
|
-
resolve: insertSingleGenerated.resolver
|
|
1293
|
-
};
|
|
1294
|
-
mutations[updateGenerated.name] = {
|
|
1295
|
-
type: mutationReturnType,
|
|
1296
|
-
args: updateGenerated.args,
|
|
1297
|
-
resolve: updateGenerated.resolver
|
|
1298
|
-
};
|
|
1299
|
-
mutations[deleteGenerated.name] = {
|
|
1300
|
-
type: mutationReturnType,
|
|
1301
|
-
args: deleteGenerated.args,
|
|
1302
|
-
resolve: deleteGenerated.resolver
|
|
1303
|
-
};
|
|
1304
|
-
[insertInput, updateInput, tableFilters, tableOrder].forEach(
|
|
1305
|
-
(e) => inputs[e.name] = e
|
|
1306
|
-
);
|
|
1307
|
-
outputs[selectSingleOutput.name] = selectSingleOutput;
|
|
1308
|
-
interfaces[tableFieldsInterface.name] = tableFieldsInterface;
|
|
1309
|
-
}
|
|
1310
|
-
return { queries, mutations, inputs, interfaces, types: outputs };
|
|
1311
|
-
};
|
|
1312
|
-
|
|
1313
|
-
// src/util/builders/pg.ts
|
|
1314
|
-
var import_drizzle_orm5 = require("drizzle-orm");
|
|
1315
|
-
var import_pg_core2 = require("drizzle-orm/pg-core");
|
|
1316
|
-
var import_graphql5 = require("graphql");
|
|
1317
|
-
var import_graphql_parse_resolve_info2 = require("graphql-parse-resolve-info");
|
|
1318
|
-
var generateSelectArray2 = (db, tableName, tables, relationMap, orderArgs, filterArgs) => {
|
|
1319
|
-
const queryName = `${uncapitalize(tableName)}`;
|
|
1320
|
-
const queryBase = db.query[tableName];
|
|
1321
|
-
if (!queryBase) {
|
|
1322
|
-
throw new Error(
|
|
1323
|
-
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
1324
|
-
);
|
|
1325
|
-
}
|
|
1326
|
-
const queryArgs = {
|
|
1327
|
-
offset: {
|
|
1328
|
-
type: import_graphql5.GraphQLInt
|
|
1329
|
-
},
|
|
1330
|
-
limit: {
|
|
1331
|
-
type: import_graphql5.GraphQLInt
|
|
1332
|
-
},
|
|
1333
|
-
orderBy: {
|
|
1334
|
-
type: orderArgs
|
|
1335
|
-
},
|
|
1336
|
-
where: {
|
|
1337
|
-
type: filterArgs
|
|
1338
|
-
}
|
|
1339
|
-
};
|
|
1340
|
-
const typeName = `${capitalize(tableName)}SelectItem`;
|
|
1341
|
-
const table = tables[tableName];
|
|
1342
|
-
return {
|
|
1343
|
-
name: queryName,
|
|
1344
|
-
resolver: async (source, args, context, info) => {
|
|
1345
|
-
try {
|
|
1346
|
-
const { offset, limit, orderBy, where } = args;
|
|
1347
|
-
const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, {
|
|
1348
|
-
deep: true
|
|
1349
|
-
});
|
|
1350
|
-
const query = queryBase.findMany({
|
|
1351
|
-
columns: extractSelectedColumnsFromTree(
|
|
1352
|
-
parsedInfo.fieldsByTypeName[typeName],
|
|
1353
|
-
table
|
|
1354
|
-
),
|
|
1355
|
-
offset,
|
|
1356
|
-
limit,
|
|
1357
|
-
orderBy: orderBy ? extractOrderBy(table, orderBy) : void 0,
|
|
1358
|
-
where: where ? extractFilters(table, tableName, where) : void 0,
|
|
1359
|
-
with: relationMap[tableName] ? extractRelationsParams(
|
|
1360
|
-
relationMap,
|
|
1361
|
-
tables,
|
|
1362
|
-
tableName,
|
|
1363
|
-
parsedInfo,
|
|
1364
|
-
typeName
|
|
1365
|
-
) : void 0
|
|
1366
|
-
});
|
|
1367
|
-
const result = await query;
|
|
1368
|
-
return remapToGraphQLArrayOutput(result, tableName, table, relationMap);
|
|
1369
|
-
} catch (e) {
|
|
1370
|
-
if (typeof e === "object" && typeof e.message === "string") {
|
|
1371
|
-
throw new import_graphql5.GraphQLError(e.message);
|
|
1372
|
-
}
|
|
1373
|
-
throw e;
|
|
1374
|
-
}
|
|
1375
|
-
},
|
|
1376
|
-
args: queryArgs
|
|
1377
|
-
};
|
|
1378
|
-
};
|
|
1379
|
-
var generateSelectSingle2 = (db, tableName, tables, relationMap, orderArgs, filterArgs) => {
|
|
1380
|
-
const queryName = `${uncapitalize(tableName)}Single`;
|
|
1381
|
-
const queryBase = db.query[tableName];
|
|
1382
|
-
if (!queryBase) {
|
|
1383
|
-
throw new Error(
|
|
1384
|
-
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
1385
|
-
);
|
|
1386
|
-
}
|
|
1387
|
-
const queryArgs = {
|
|
1388
|
-
offset: {
|
|
1389
|
-
type: import_graphql5.GraphQLInt
|
|
1390
|
-
},
|
|
1391
|
-
orderBy: {
|
|
1392
|
-
type: orderArgs
|
|
1393
|
-
},
|
|
1394
|
-
where: {
|
|
1395
|
-
type: filterArgs
|
|
1396
|
-
}
|
|
1397
|
-
};
|
|
1398
|
-
const typeName = `${capitalize(tableName)}SelectItem`;
|
|
1399
|
-
const table = tables[tableName];
|
|
1400
|
-
return {
|
|
1401
|
-
name: queryName,
|
|
1402
|
-
resolver: async (source, args, context, info) => {
|
|
1403
|
-
try {
|
|
1404
|
-
const { offset, orderBy, where } = args;
|
|
1405
|
-
const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, {
|
|
1406
|
-
deep: true
|
|
1407
|
-
});
|
|
1408
|
-
const allFields = {};
|
|
1409
|
-
if (parsedInfo.fieldsByTypeName) {
|
|
1410
|
-
for (const fields of Object.values(parsedInfo.fieldsByTypeName)) {
|
|
1411
|
-
Object.assign(allFields, fields);
|
|
1412
|
-
}
|
|
1413
|
-
}
|
|
1414
|
-
const query = queryBase.findFirst({
|
|
1415
|
-
columns: extractSelectedColumnsFromTree(allFields, table),
|
|
1416
|
-
offset,
|
|
1417
|
-
orderBy: orderBy ? extractOrderBy(table, orderBy) : void 0,
|
|
1418
|
-
where: where ? extractFilters(table, tableName, where) : void 0,
|
|
1419
|
-
with: relationMap[tableName] ? extractRelationsParams(
|
|
1420
|
-
relationMap,
|
|
1421
|
-
tables,
|
|
1422
|
-
tableName,
|
|
1423
|
-
parsedInfo,
|
|
1424
|
-
typeName
|
|
1425
|
-
) : void 0
|
|
1426
|
-
});
|
|
1427
|
-
const result = await query;
|
|
1428
|
-
if (!result)
|
|
1429
|
-
return void 0;
|
|
1430
|
-
return remapToGraphQLSingleOutput(
|
|
1431
|
-
result,
|
|
1432
|
-
tableName,
|
|
1433
|
-
table,
|
|
1434
|
-
relationMap
|
|
1435
|
-
);
|
|
1436
|
-
} catch (e) {
|
|
1437
|
-
if (typeof e === "object" && typeof e.message === "string") {
|
|
1438
|
-
throw new import_graphql5.GraphQLError(e.message);
|
|
1439
|
-
}
|
|
1440
|
-
throw e;
|
|
1441
|
-
}
|
|
1442
|
-
},
|
|
1443
|
-
args: queryArgs
|
|
1444
|
-
};
|
|
1445
|
-
};
|
|
1446
|
-
var generateInsertArray2 = (db, tableName, table, baseType) => {
|
|
1447
|
-
const queryName = `insertInto${capitalize(tableName)}`;
|
|
1448
|
-
const typeName = `${capitalize(tableName)}Item`;
|
|
1449
|
-
const queryArgs = {
|
|
1450
|
-
values: {
|
|
1451
|
-
type: new import_graphql5.GraphQLNonNull(new import_graphql5.GraphQLList(new import_graphql5.GraphQLNonNull(baseType)))
|
|
1452
|
-
}
|
|
1453
|
-
};
|
|
1454
|
-
return {
|
|
1455
|
-
name: queryName,
|
|
1456
|
-
resolver: async (source, args, context, info) => {
|
|
1457
|
-
try {
|
|
1458
|
-
const input = remapFromGraphQLArrayInput(args.values, table);
|
|
1459
|
-
if (!input.length)
|
|
1460
|
-
throw new import_graphql5.GraphQLError("No values were provided!");
|
|
1461
|
-
const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, {
|
|
1462
|
-
deep: true
|
|
1463
|
-
});
|
|
1464
|
-
const columns = extractSelectedColumnsFromTreeSQLFormat(
|
|
1465
|
-
parsedInfo.fieldsByTypeName[typeName],
|
|
1466
|
-
table
|
|
1467
|
-
);
|
|
1468
|
-
const result = await db.insert(table).values(input).returning(columns).onConflictDoNothing();
|
|
1469
|
-
return remapToGraphQLArrayOutput(result, tableName, table);
|
|
1470
|
-
} catch (e) {
|
|
1471
|
-
if (typeof e === "object" && typeof e.message === "string") {
|
|
1472
|
-
throw new import_graphql5.GraphQLError(e.message);
|
|
1473
|
-
}
|
|
1474
|
-
throw e;
|
|
1475
|
-
}
|
|
1476
|
-
},
|
|
1477
|
-
args: queryArgs
|
|
1478
|
-
};
|
|
1479
|
-
};
|
|
1480
|
-
var generateInsertSingle2 = (db, tableName, table, baseType) => {
|
|
1481
|
-
const queryName = `insertInto${capitalize(tableName)}Single`;
|
|
1482
|
-
const typeName = `${capitalize(tableName)}Item`;
|
|
1483
|
-
const queryArgs = {
|
|
1484
|
-
values: {
|
|
1485
|
-
type: new import_graphql5.GraphQLNonNull(baseType)
|
|
1486
|
-
}
|
|
1487
|
-
};
|
|
1488
|
-
return {
|
|
1489
|
-
name: queryName,
|
|
1490
|
-
resolver: async (source, args, context, info) => {
|
|
1491
|
-
try {
|
|
1492
|
-
const input = remapFromGraphQLSingleInput(args.values, table);
|
|
1493
|
-
const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, {
|
|
1494
|
-
deep: true
|
|
1495
|
-
});
|
|
1496
|
-
const columns = extractSelectedColumnsFromTreeSQLFormat(
|
|
1497
|
-
parsedInfo.fieldsByTypeName[typeName],
|
|
1498
|
-
table
|
|
1499
|
-
);
|
|
1500
|
-
const result = await db.insert(table).values(input).returning(columns).onConflictDoNothing();
|
|
1501
|
-
if (!result[0])
|
|
1502
|
-
return void 0;
|
|
1503
|
-
return remapToGraphQLSingleOutput(result[0], tableName, table);
|
|
1504
|
-
} catch (e) {
|
|
1505
|
-
if (typeof e === "object" && typeof e.message === "string") {
|
|
1506
|
-
throw new import_graphql5.GraphQLError(e.message);
|
|
1507
|
-
}
|
|
1508
|
-
throw e;
|
|
1509
|
-
}
|
|
1510
|
-
},
|
|
1511
|
-
args: queryArgs
|
|
1512
|
-
};
|
|
1513
|
-
};
|
|
1514
|
-
var generateUpdate2 = (db, tableName, table, setArgs, filterArgs) => {
|
|
1515
|
-
const queryName = `update${capitalize(tableName)}`;
|
|
1516
|
-
const typeName = `${capitalize(tableName)}Item`;
|
|
1517
|
-
const queryArgs = {
|
|
1518
|
-
set: {
|
|
1519
|
-
type: new import_graphql5.GraphQLNonNull(setArgs)
|
|
1520
|
-
},
|
|
1521
|
-
where: {
|
|
1522
|
-
type: filterArgs
|
|
1523
|
-
}
|
|
1524
|
-
};
|
|
1525
|
-
return {
|
|
1526
|
-
name: queryName,
|
|
1527
|
-
resolver: async (source, args, context, info) => {
|
|
1528
|
-
try {
|
|
1529
|
-
const { where, set } = args;
|
|
1530
|
-
const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, {
|
|
1531
|
-
deep: true
|
|
1532
|
-
});
|
|
1533
|
-
const columns = extractSelectedColumnsFromTreeSQLFormat(
|
|
1534
|
-
parsedInfo.fieldsByTypeName[typeName],
|
|
1535
|
-
table
|
|
1536
|
-
);
|
|
1537
|
-
const input = remapFromGraphQLSingleInput(set, table);
|
|
1538
|
-
if (!Object.keys(input).length)
|
|
1539
|
-
throw new import_graphql5.GraphQLError("Unable to update with no values specified!");
|
|
1540
|
-
let query = db.update(table).set(input);
|
|
1541
|
-
if (where) {
|
|
1542
|
-
const filters = extractFilters(table, tableName, where);
|
|
1543
|
-
query = query.where(filters);
|
|
1544
|
-
}
|
|
1545
|
-
query = query.returning(columns);
|
|
1546
|
-
const result = await query;
|
|
1547
|
-
return remapToGraphQLArrayOutput(result, tableName, table);
|
|
1548
|
-
} catch (e) {
|
|
1549
|
-
if (typeof e === "object" && typeof e.message === "string") {
|
|
1550
|
-
throw new import_graphql5.GraphQLError(e.message);
|
|
1551
|
-
}
|
|
1552
|
-
throw e;
|
|
1553
|
-
}
|
|
1554
|
-
},
|
|
1555
|
-
args: queryArgs
|
|
1556
|
-
};
|
|
1557
|
-
};
|
|
1558
|
-
var generateDelete2 = (db, tableName, table, filterArgs) => {
|
|
1559
|
-
const queryName = `deleteFrom${capitalize(tableName)}`;
|
|
1560
|
-
const typeName = `${capitalize(tableName)}Item`;
|
|
1561
|
-
const queryArgs = {
|
|
1562
|
-
where: {
|
|
1563
|
-
type: filterArgs
|
|
1564
|
-
}
|
|
1565
|
-
};
|
|
1566
|
-
return {
|
|
1567
|
-
name: queryName,
|
|
1568
|
-
resolver: async (source, args, context, info) => {
|
|
1569
|
-
try {
|
|
1570
|
-
const { where } = args;
|
|
1571
|
-
const parsedInfo = (0, import_graphql_parse_resolve_info2.parseResolveInfo)(info, {
|
|
1572
|
-
deep: true
|
|
1573
|
-
});
|
|
1574
|
-
const columns = extractSelectedColumnsFromTreeSQLFormat(
|
|
1575
|
-
parsedInfo.fieldsByTypeName[typeName],
|
|
1576
|
-
table
|
|
1577
|
-
);
|
|
1578
|
-
let query = db.delete(table);
|
|
1579
|
-
if (where) {
|
|
1580
|
-
const filters = extractFilters(table, tableName, where);
|
|
1581
|
-
query = query.where(filters);
|
|
1582
|
-
}
|
|
1583
|
-
query = query.returning(columns);
|
|
1584
|
-
const result = await query;
|
|
1585
|
-
return remapToGraphQLArrayOutput(result, tableName, table);
|
|
1586
|
-
} catch (e) {
|
|
1587
|
-
if (typeof e === "object" && typeof e.message === "string") {
|
|
1588
|
-
throw new import_graphql5.GraphQLError(e.message);
|
|
1589
|
-
}
|
|
1590
|
-
throw e;
|
|
1591
|
-
}
|
|
1592
|
-
},
|
|
1593
|
-
args: queryArgs
|
|
1594
|
-
};
|
|
1595
|
-
};
|
|
1596
|
-
var generateSchemaData2 = (db, schema, relationsDepthLimit) => {
|
|
1597
|
-
const rawSchema = schema;
|
|
1598
|
-
const schemaEntries = Object.entries(rawSchema);
|
|
1599
|
-
const tableEntries = schemaEntries.filter(
|
|
1600
|
-
([key, value]) => (0, import_drizzle_orm5.is)(value, import_pg_core2.PgTable)
|
|
1601
|
-
);
|
|
1602
|
-
const tables = Object.fromEntries(tableEntries);
|
|
1603
|
-
if (!tableEntries.length) {
|
|
1604
|
-
throw new Error(
|
|
1605
|
-
"Drizzle-GraphQL Error: No tables detected in Drizzle-ORM's database instance. Did you forget to pass schema to drizzle constructor?"
|
|
1606
|
-
);
|
|
1607
|
-
}
|
|
1608
|
-
const rawRelations = schemaEntries.filter(([key, value]) => (0, import_drizzle_orm5.is)(value, import_drizzle_orm5.Relations)).map(([key, value]) => [
|
|
1609
|
-
tableEntries.find(
|
|
1610
|
-
([tableName, tableValue]) => tableValue === value.table
|
|
1611
|
-
)[0],
|
|
1612
|
-
value
|
|
1613
|
-
]).map(([tableName, relValue]) => [
|
|
1614
|
-
tableName,
|
|
1615
|
-
relValue.config((0, import_drizzle_orm5.createTableRelationsHelpers)(tables[tableName]))
|
|
1616
|
-
]);
|
|
1617
|
-
const namedRelations = Object.fromEntries(
|
|
1618
|
-
rawRelations.map(([relName, config]) => {
|
|
1619
|
-
const namedConfig = Object.fromEntries(
|
|
1620
|
-
Object.entries(config).map(([innerRelName, innerRelValue]) => [
|
|
1621
|
-
innerRelName,
|
|
1622
|
-
{
|
|
1623
|
-
relation: innerRelValue,
|
|
1624
|
-
targetTableName: tableEntries.find(
|
|
1625
|
-
([tableName, tableValue]) => tableValue === innerRelValue.referencedTable
|
|
1626
|
-
)[0]
|
|
1627
|
-
}
|
|
1628
|
-
])
|
|
1629
|
-
);
|
|
1630
|
-
return [relName, namedConfig];
|
|
1631
|
-
})
|
|
1632
|
-
);
|
|
1633
|
-
const queries = {};
|
|
1634
|
-
const mutations = {};
|
|
1635
|
-
const gqlSchemaTypes = Object.fromEntries(
|
|
1636
|
-
Object.entries(tables).map(([tableName, table]) => [
|
|
1637
|
-
tableName,
|
|
1638
|
-
generateTableTypes(
|
|
1639
|
-
tableName,
|
|
1640
|
-
tables,
|
|
1641
|
-
namedRelations,
|
|
1642
|
-
true,
|
|
1643
|
-
relationsDepthLimit
|
|
1644
|
-
)
|
|
1645
|
-
])
|
|
1646
|
-
);
|
|
1647
|
-
const inputs = {};
|
|
1648
|
-
const interfaces = {};
|
|
1649
|
-
const outputs = {};
|
|
1650
|
-
for (const [tableName, tableTypes] of Object.entries(gqlSchemaTypes)) {
|
|
1651
|
-
const { insertInput, updateInput, tableFilters, tableOrder } = tableTypes.inputs;
|
|
1652
|
-
const {
|
|
1653
|
-
selectSingleOutput,
|
|
1654
|
-
selectArrOutput,
|
|
1655
|
-
singleTableItemOutput,
|
|
1656
|
-
arrTableItemOutput,
|
|
1657
|
-
tableFieldsInterface
|
|
1658
|
-
} = tableTypes.outputs;
|
|
1659
|
-
const selectArrGenerated = generateSelectArray2(
|
|
1660
|
-
db,
|
|
1661
|
-
tableName,
|
|
1662
|
-
tables,
|
|
1663
|
-
namedRelations,
|
|
1664
|
-
tableOrder,
|
|
1665
|
-
tableFilters
|
|
1666
|
-
);
|
|
1667
|
-
const selectSingleGenerated = generateSelectSingle2(
|
|
1668
|
-
db,
|
|
1669
|
-
tableName,
|
|
1670
|
-
tables,
|
|
1671
|
-
namedRelations,
|
|
1672
|
-
tableOrder,
|
|
1673
|
-
tableFilters
|
|
1674
|
-
);
|
|
1675
|
-
const insertArrGenerated = generateInsertArray2(
|
|
1676
|
-
db,
|
|
1677
|
-
tableName,
|
|
1678
|
-
schema[tableName],
|
|
1679
|
-
insertInput
|
|
1680
|
-
);
|
|
1681
|
-
const insertSingleGenerated = generateInsertSingle2(
|
|
1682
|
-
db,
|
|
1683
|
-
tableName,
|
|
1684
|
-
schema[tableName],
|
|
1685
|
-
insertInput
|
|
1686
|
-
);
|
|
1687
|
-
const updateGenerated = generateUpdate2(
|
|
1688
|
-
db,
|
|
1689
|
-
tableName,
|
|
1690
|
-
schema[tableName],
|
|
1691
|
-
updateInput,
|
|
1692
|
-
tableFilters
|
|
1693
|
-
);
|
|
1694
|
-
const deleteGenerated = generateDelete2(
|
|
1695
|
-
db,
|
|
1696
|
-
tableName,
|
|
1697
|
-
schema[tableName],
|
|
1698
|
-
tableFilters
|
|
1699
|
-
);
|
|
1700
|
-
queries[selectArrGenerated.name] = {
|
|
1701
|
-
type: selectArrOutput,
|
|
1702
|
-
args: selectArrGenerated.args,
|
|
1703
|
-
resolve: selectArrGenerated.resolver
|
|
1704
|
-
};
|
|
1705
|
-
queries[selectSingleGenerated.name] = {
|
|
1706
|
-
type: selectSingleOutput,
|
|
1707
|
-
args: selectSingleGenerated.args,
|
|
1708
|
-
resolve: selectSingleGenerated.resolver
|
|
1709
|
-
};
|
|
1710
|
-
mutations[insertArrGenerated.name] = {
|
|
1711
|
-
type: arrTableItemOutput,
|
|
1712
|
-
args: insertArrGenerated.args,
|
|
1713
|
-
resolve: insertArrGenerated.resolver
|
|
1714
|
-
};
|
|
1715
|
-
mutations[insertSingleGenerated.name] = {
|
|
1716
|
-
type: singleTableItemOutput,
|
|
1717
|
-
args: insertSingleGenerated.args,
|
|
1718
|
-
resolve: insertSingleGenerated.resolver
|
|
1719
|
-
};
|
|
1720
|
-
mutations[updateGenerated.name] = {
|
|
1721
|
-
type: arrTableItemOutput,
|
|
1722
|
-
args: updateGenerated.args,
|
|
1723
|
-
resolve: updateGenerated.resolver
|
|
1724
|
-
};
|
|
1725
|
-
mutations[deleteGenerated.name] = {
|
|
1726
|
-
type: arrTableItemOutput,
|
|
1727
|
-
args: deleteGenerated.args,
|
|
1728
|
-
resolve: deleteGenerated.resolver
|
|
1729
|
-
};
|
|
1730
|
-
[insertInput, updateInput, tableFilters, tableOrder].forEach(
|
|
1731
|
-
(e) => inputs[e.name] = e
|
|
1732
|
-
);
|
|
1733
|
-
outputs[selectSingleOutput.name] = selectSingleOutput;
|
|
1734
|
-
outputs[singleTableItemOutput.name] = singleTableItemOutput;
|
|
1735
|
-
interfaces[tableFieldsInterface.name] = tableFieldsInterface;
|
|
1736
|
-
}
|
|
1737
|
-
return { queries, mutations, inputs, interfaces, types: outputs };
|
|
1738
|
-
};
|
|
1739
|
-
|
|
1740
|
-
// src/util/builders/sqlite.ts
|
|
1741
|
-
var import_drizzle_orm6 = require("drizzle-orm");
|
|
1742
|
-
var import_sqlite_core2 = require("drizzle-orm/sqlite-core");
|
|
1743
|
-
var import_graphql6 = require("graphql");
|
|
1744
|
-
var import_graphql_parse_resolve_info3 = require("graphql-parse-resolve-info");
|
|
1745
|
-
var generateSelectArray3 = (db, tableName, tables, relationMap, orderArgs, filterArgs) => {
|
|
1746
|
-
const queryName = `${uncapitalize(tableName)}`;
|
|
1747
|
-
const queryBase = db.query[tableName];
|
|
1748
|
-
if (!queryBase) {
|
|
1749
|
-
throw new Error(
|
|
1750
|
-
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
1751
|
-
);
|
|
1752
|
-
}
|
|
1753
|
-
const queryArgs = {
|
|
1754
|
-
offset: {
|
|
1755
|
-
type: import_graphql6.GraphQLInt
|
|
1756
|
-
},
|
|
1757
|
-
limit: {
|
|
1758
|
-
type: import_graphql6.GraphQLInt
|
|
1759
|
-
},
|
|
1760
|
-
orderBy: {
|
|
1761
|
-
type: orderArgs
|
|
1762
|
-
},
|
|
1763
|
-
where: {
|
|
1764
|
-
type: filterArgs
|
|
1765
|
-
}
|
|
1766
|
-
};
|
|
1767
|
-
const typeName = `${capitalize(tableName)}SelectItem`;
|
|
1768
|
-
const table = tables[tableName];
|
|
1769
|
-
return {
|
|
1770
|
-
name: queryName,
|
|
1771
|
-
resolver: async (source, args, context, info) => {
|
|
1772
|
-
try {
|
|
1773
|
-
const { offset, limit, orderBy, where } = args;
|
|
1774
|
-
const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
|
|
1775
|
-
deep: true
|
|
1776
|
-
});
|
|
1777
|
-
const allFields = {};
|
|
1778
|
-
if (parsedInfo.fieldsByTypeName) {
|
|
1779
|
-
for (const fields of Object.values(parsedInfo.fieldsByTypeName)) {
|
|
1780
|
-
Object.assign(allFields, fields);
|
|
1781
|
-
}
|
|
1782
|
-
}
|
|
1783
|
-
const query = queryBase.findMany({
|
|
1784
|
-
columns: extractSelectedColumnsFromTree(allFields, table),
|
|
1785
|
-
offset,
|
|
1786
|
-
limit,
|
|
1787
|
-
orderBy: orderBy ? extractOrderBy(table, orderBy) : void 0,
|
|
1788
|
-
where: where ? extractFilters(table, tableName, where) : void 0,
|
|
1789
|
-
with: relationMap[tableName] ? extractRelationsParams(
|
|
1790
|
-
relationMap,
|
|
1791
|
-
tables,
|
|
1792
|
-
tableName,
|
|
1793
|
-
parsedInfo,
|
|
1794
|
-
typeName
|
|
1795
|
-
) : void 0
|
|
1796
|
-
});
|
|
1797
|
-
const result = await query;
|
|
1798
|
-
return remapToGraphQLArrayOutput(result, tableName, table, relationMap);
|
|
1799
|
-
} catch (e) {
|
|
1800
|
-
if (typeof e === "object" && typeof e.message === "string") {
|
|
1801
|
-
throw new import_graphql6.GraphQLError(e.message);
|
|
1802
|
-
}
|
|
1803
|
-
throw e;
|
|
1804
|
-
}
|
|
1805
|
-
},
|
|
1806
|
-
args: queryArgs
|
|
1807
|
-
};
|
|
1808
|
-
};
|
|
1809
|
-
var generateSelectSingle3 = (db, tableName, tables, relationMap, orderArgs, filterArgs) => {
|
|
1810
|
-
const queryName = `${uncapitalize(tableName)}Single`;
|
|
1811
|
-
const queryBase = db.query[tableName];
|
|
1812
|
-
if (!queryBase) {
|
|
1813
|
-
throw new Error(
|
|
1814
|
-
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
1815
|
-
);
|
|
1816
|
-
}
|
|
1817
|
-
const queryArgs = {
|
|
1818
|
-
offset: {
|
|
1819
|
-
type: import_graphql6.GraphQLInt
|
|
1820
|
-
},
|
|
1821
|
-
orderBy: {
|
|
1822
|
-
type: orderArgs
|
|
1823
|
-
},
|
|
1824
|
-
where: {
|
|
1825
|
-
type: filterArgs
|
|
1826
|
-
}
|
|
1827
|
-
};
|
|
1828
|
-
const typeName = `${capitalize(tableName)}SelectItem`;
|
|
1829
|
-
const table = tables[tableName];
|
|
1830
|
-
return {
|
|
1831
|
-
name: queryName,
|
|
1832
|
-
resolver: async (source, args, context, info) => {
|
|
1833
|
-
try {
|
|
1834
|
-
const { offset, orderBy, where } = args;
|
|
1835
|
-
const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
|
|
1836
|
-
deep: true
|
|
1837
|
-
});
|
|
1838
|
-
const allFields = {};
|
|
1839
|
-
if (parsedInfo.fieldsByTypeName) {
|
|
1840
|
-
for (const fields of Object.values(parsedInfo.fieldsByTypeName)) {
|
|
1841
|
-
Object.assign(allFields, fields);
|
|
1842
|
-
}
|
|
1843
|
-
}
|
|
1844
|
-
const query = queryBase.findFirst({
|
|
1845
|
-
columns: extractSelectedColumnsFromTree(allFields, table),
|
|
1846
|
-
offset,
|
|
1847
|
-
orderBy: orderBy ? extractOrderBy(table, orderBy) : void 0,
|
|
1848
|
-
where: where ? extractFilters(table, tableName, where) : void 0,
|
|
1849
|
-
with: relationMap[tableName] ? extractRelationsParams(
|
|
1850
|
-
relationMap,
|
|
1851
|
-
tables,
|
|
1852
|
-
tableName,
|
|
1853
|
-
parsedInfo,
|
|
1854
|
-
typeName
|
|
1855
|
-
) : void 0
|
|
1856
|
-
});
|
|
1857
|
-
const result = await query;
|
|
1858
|
-
if (!result)
|
|
1859
|
-
return void 0;
|
|
1860
|
-
return remapToGraphQLSingleOutput(
|
|
1861
|
-
result,
|
|
1862
|
-
tableName,
|
|
1863
|
-
table,
|
|
1864
|
-
relationMap
|
|
1865
|
-
);
|
|
1866
|
-
} catch (e) {
|
|
1867
|
-
if (typeof e === "object" && typeof e.message === "string") {
|
|
1868
|
-
throw new import_graphql6.GraphQLError(e.message);
|
|
1869
|
-
}
|
|
1870
|
-
throw e;
|
|
1871
|
-
}
|
|
1872
|
-
},
|
|
1873
|
-
args: queryArgs
|
|
1874
|
-
};
|
|
1875
|
-
};
|
|
1876
|
-
var generateInsertArray3 = (db, tableName, table, baseType) => {
|
|
1877
|
-
const queryName = `insertInto${capitalize(tableName)}`;
|
|
1878
|
-
const typeName = `${capitalize(tableName)}Item`;
|
|
1879
|
-
const queryArgs = {
|
|
1880
|
-
values: {
|
|
1881
|
-
type: new import_graphql6.GraphQLNonNull(new import_graphql6.GraphQLList(new import_graphql6.GraphQLNonNull(baseType)))
|
|
1882
|
-
}
|
|
1883
|
-
};
|
|
1884
|
-
return {
|
|
1885
|
-
name: queryName,
|
|
1886
|
-
resolver: async (source, args, context, info) => {
|
|
1887
|
-
try {
|
|
1888
|
-
const input = remapFromGraphQLArrayInput(args.values, table);
|
|
1889
|
-
if (!input.length)
|
|
1890
|
-
throw new import_graphql6.GraphQLError("No values were provided!");
|
|
1891
|
-
const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
|
|
1892
|
-
deep: true
|
|
1893
|
-
});
|
|
1894
|
-
const columns = extractSelectedColumnsFromTreeSQLFormat(
|
|
1895
|
-
parsedInfo.fieldsByTypeName[typeName],
|
|
1896
|
-
table
|
|
1897
|
-
);
|
|
1898
|
-
const result = await db.insert(table).values(input).returning(columns).onConflictDoNothing();
|
|
1899
|
-
return remapToGraphQLArrayOutput(result, tableName, table);
|
|
1900
|
-
} catch (e) {
|
|
1901
|
-
if (typeof e === "object" && typeof e.message === "string") {
|
|
1902
|
-
throw new import_graphql6.GraphQLError(e.message);
|
|
1903
|
-
}
|
|
1904
|
-
throw e;
|
|
1905
|
-
}
|
|
1906
|
-
},
|
|
1907
|
-
args: queryArgs
|
|
1908
|
-
};
|
|
1909
|
-
};
|
|
1910
|
-
var generateInsertSingle3 = (db, tableName, table, baseType) => {
|
|
1911
|
-
const queryName = `insertInto${capitalize(tableName)}Single`;
|
|
1912
|
-
const typeName = `${capitalize(tableName)}Item`;
|
|
1913
|
-
const queryArgs = {
|
|
1914
|
-
values: {
|
|
1915
|
-
type: new import_graphql6.GraphQLNonNull(baseType)
|
|
1916
|
-
}
|
|
1917
|
-
};
|
|
1918
|
-
return {
|
|
1919
|
-
name: queryName,
|
|
1920
|
-
resolver: async (source, args, context, info) => {
|
|
1921
|
-
try {
|
|
1922
|
-
const input = remapFromGraphQLSingleInput(args.values, table);
|
|
1923
|
-
const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
|
|
1924
|
-
deep: true
|
|
1925
|
-
});
|
|
1926
|
-
const columns = extractSelectedColumnsFromTreeSQLFormat(
|
|
1927
|
-
parsedInfo.fieldsByTypeName[typeName],
|
|
1928
|
-
table
|
|
1929
|
-
);
|
|
1930
|
-
const result = await db.insert(table).values(input).returning(columns).onConflictDoNothing();
|
|
1931
|
-
if (!result[0])
|
|
1932
|
-
return void 0;
|
|
1933
|
-
return remapToGraphQLSingleOutput(result[0], tableName, table);
|
|
1934
|
-
} catch (e) {
|
|
1935
|
-
if (typeof e === "object" && typeof e.message === "string") {
|
|
1936
|
-
throw new import_graphql6.GraphQLError(e.message);
|
|
1937
|
-
}
|
|
1938
|
-
throw e;
|
|
1939
|
-
}
|
|
1940
|
-
},
|
|
1941
|
-
args: queryArgs
|
|
1942
|
-
};
|
|
1943
|
-
};
|
|
1944
|
-
var generateUpdate3 = (db, tableName, table, setArgs, filterArgs) => {
|
|
1945
|
-
const queryName = `update${capitalize(tableName)}`;
|
|
1946
|
-
const typeName = `${capitalize(tableName)}Item`;
|
|
1947
|
-
const queryArgs = {
|
|
1948
|
-
set: {
|
|
1949
|
-
type: new import_graphql6.GraphQLNonNull(setArgs)
|
|
1950
|
-
},
|
|
1951
|
-
where: {
|
|
1952
|
-
type: filterArgs
|
|
1953
|
-
}
|
|
1954
|
-
};
|
|
1955
|
-
return {
|
|
1956
|
-
name: queryName,
|
|
1957
|
-
resolver: async (source, args, context, info) => {
|
|
1958
|
-
try {
|
|
1959
|
-
const { where, set } = args;
|
|
1960
|
-
const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
|
|
1961
|
-
deep: true
|
|
1962
|
-
});
|
|
1963
|
-
const columns = extractSelectedColumnsFromTreeSQLFormat(
|
|
1964
|
-
parsedInfo.fieldsByTypeName[typeName],
|
|
1965
|
-
table
|
|
1966
|
-
);
|
|
1967
|
-
const input = remapFromGraphQLSingleInput(set, table);
|
|
1968
|
-
if (!Object.keys(input).length)
|
|
1969
|
-
throw new import_graphql6.GraphQLError("Unable to update with no values specified!");
|
|
1970
|
-
let query = db.update(table).set(input);
|
|
1971
|
-
if (where) {
|
|
1972
|
-
const filters = extractFilters(table, tableName, where);
|
|
1973
|
-
query = query.where(filters);
|
|
1974
|
-
}
|
|
1975
|
-
query = query.returning(columns);
|
|
1976
|
-
const result = await query;
|
|
1977
|
-
return remapToGraphQLArrayOutput(result, tableName, table);
|
|
1978
|
-
} catch (e) {
|
|
1979
|
-
if (typeof e === "object" && typeof e.message === "string") {
|
|
1980
|
-
throw new import_graphql6.GraphQLError(e.message);
|
|
1981
|
-
}
|
|
1982
|
-
throw e;
|
|
1983
|
-
}
|
|
1984
|
-
},
|
|
1985
|
-
args: queryArgs
|
|
1986
|
-
};
|
|
1987
|
-
};
|
|
1988
|
-
var generateDelete3 = (db, tableName, table, filterArgs) => {
|
|
1989
|
-
const queryName = `deleteFrom${capitalize(tableName)}`;
|
|
1990
|
-
const typeName = `${capitalize(tableName)}Item`;
|
|
1991
|
-
const queryArgs = {
|
|
1992
|
-
where: {
|
|
1993
|
-
type: filterArgs
|
|
1994
|
-
}
|
|
1995
|
-
};
|
|
1996
|
-
return {
|
|
1997
|
-
name: queryName,
|
|
1998
|
-
resolver: async (source, args, context, info) => {
|
|
1999
|
-
try {
|
|
2000
|
-
const { where } = args;
|
|
2001
|
-
const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
|
|
2002
|
-
deep: true
|
|
2003
|
-
});
|
|
2004
|
-
const columns = extractSelectedColumnsFromTreeSQLFormat(
|
|
2005
|
-
parsedInfo.fieldsByTypeName[typeName],
|
|
2006
|
-
table
|
|
2007
|
-
);
|
|
2008
|
-
let query = db.delete(table);
|
|
2009
|
-
if (where) {
|
|
2010
|
-
const filters = extractFilters(table, tableName, where);
|
|
2011
|
-
query = query.where(filters);
|
|
2012
|
-
}
|
|
2013
|
-
query = query.returning(columns);
|
|
2014
|
-
const result = await query;
|
|
2015
|
-
return remapToGraphQLArrayOutput(result, tableName, table);
|
|
2016
|
-
} catch (e) {
|
|
2017
|
-
if (typeof e === "object" && typeof e.message === "string") {
|
|
2018
|
-
throw new import_graphql6.GraphQLError(e.message);
|
|
2019
|
-
}
|
|
2020
|
-
throw e;
|
|
2021
|
-
}
|
|
2022
|
-
},
|
|
2023
|
-
args: queryArgs
|
|
2024
|
-
};
|
|
2025
|
-
};
|
|
2026
|
-
var generateSchemaData3 = (db, schema, relationsDepthLimit) => {
|
|
2027
|
-
const rawSchema = schema;
|
|
2028
|
-
const schemaEntries = Object.entries(rawSchema);
|
|
2029
|
-
const tableEntries = schemaEntries.filter(
|
|
2030
|
-
([key, value]) => (0, import_drizzle_orm6.is)(value, import_sqlite_core2.SQLiteTable)
|
|
2031
|
-
);
|
|
2032
|
-
const tables = Object.fromEntries(tableEntries);
|
|
2033
|
-
if (!tableEntries.length) {
|
|
2034
|
-
throw new Error(
|
|
2035
|
-
"Drizzle-GraphQL Error: No tables detected in Drizzle-ORM's database instance. Did you forget to pass schema to drizzle constructor?"
|
|
2036
|
-
);
|
|
2037
|
-
}
|
|
2038
|
-
const rawRelations = schemaEntries.filter(([key, value]) => (0, import_drizzle_orm6.is)(value, import_drizzle_orm6.Relations)).map(([key, value]) => [
|
|
2039
|
-
tableEntries.find(
|
|
2040
|
-
([tableName, tableValue]) => tableValue === value.table
|
|
2041
|
-
)[0],
|
|
2042
|
-
value
|
|
2043
|
-
]).map(([tableName, relValue]) => [
|
|
2044
|
-
tableName,
|
|
2045
|
-
relValue.config((0, import_drizzle_orm6.createTableRelationsHelpers)(tables[tableName]))
|
|
2046
|
-
]);
|
|
2047
|
-
const namedRelations = Object.fromEntries(
|
|
2048
|
-
rawRelations.map(([relName, config]) => {
|
|
2049
|
-
const namedConfig = Object.fromEntries(
|
|
2050
|
-
Object.entries(config).map(([innerRelName, innerRelValue]) => [
|
|
2051
|
-
innerRelName,
|
|
2052
|
-
{
|
|
2053
|
-
relation: innerRelValue,
|
|
2054
|
-
targetTableName: tableEntries.find(
|
|
2055
|
-
([tableName, tableValue]) => tableValue === innerRelValue.referencedTable
|
|
2056
|
-
)[0]
|
|
2057
|
-
}
|
|
2058
|
-
])
|
|
2059
|
-
);
|
|
2060
|
-
return [relName, namedConfig];
|
|
2061
|
-
})
|
|
2062
|
-
);
|
|
2063
|
-
const queries = {};
|
|
2064
|
-
const mutations = {};
|
|
2065
|
-
const gqlSchemaTypes = Object.fromEntries(
|
|
2066
|
-
Object.entries(tables).map(([tableName, table]) => [
|
|
2067
|
-
tableName,
|
|
2068
|
-
generateTableTypes(
|
|
2069
|
-
tableName,
|
|
2070
|
-
tables,
|
|
2071
|
-
namedRelations,
|
|
2072
|
-
true,
|
|
2073
|
-
relationsDepthLimit
|
|
2074
|
-
)
|
|
2075
|
-
])
|
|
2076
|
-
);
|
|
2077
|
-
const inputs = {};
|
|
2078
|
-
const interfaces = {};
|
|
2079
|
-
const outputs = {};
|
|
2080
|
-
for (const [tableName, tableTypes] of Object.entries(gqlSchemaTypes)) {
|
|
2081
|
-
const { insertInput, updateInput, tableFilters, tableOrder } = tableTypes.inputs;
|
|
2082
|
-
const {
|
|
2083
|
-
selectSingleOutput,
|
|
2084
|
-
selectArrOutput,
|
|
2085
|
-
singleTableItemOutput,
|
|
2086
|
-
arrTableItemOutput,
|
|
2087
|
-
tableFieldsInterface
|
|
2088
|
-
} = tableTypes.outputs;
|
|
2089
|
-
const selectArrGenerated = generateSelectArray3(
|
|
2090
|
-
db,
|
|
2091
|
-
tableName,
|
|
2092
|
-
tables,
|
|
2093
|
-
namedRelations,
|
|
2094
|
-
tableOrder,
|
|
2095
|
-
tableFilters
|
|
2096
|
-
);
|
|
2097
|
-
const selectSingleGenerated = generateSelectSingle3(
|
|
2098
|
-
db,
|
|
2099
|
-
tableName,
|
|
2100
|
-
tables,
|
|
2101
|
-
namedRelations,
|
|
2102
|
-
tableOrder,
|
|
2103
|
-
tableFilters
|
|
2104
|
-
);
|
|
2105
|
-
const insertArrGenerated = generateInsertArray3(
|
|
2106
|
-
db,
|
|
2107
|
-
tableName,
|
|
2108
|
-
schema[tableName],
|
|
2109
|
-
insertInput
|
|
2110
|
-
);
|
|
2111
|
-
const insertSingleGenerated = generateInsertSingle3(
|
|
2112
|
-
db,
|
|
2113
|
-
tableName,
|
|
2114
|
-
schema[tableName],
|
|
2115
|
-
insertInput
|
|
2116
|
-
);
|
|
2117
|
-
const updateGenerated = generateUpdate3(
|
|
2118
|
-
db,
|
|
2119
|
-
tableName,
|
|
2120
|
-
schema[tableName],
|
|
2121
|
-
updateInput,
|
|
2122
|
-
tableFilters
|
|
2123
|
-
);
|
|
2124
|
-
const deleteGenerated = generateDelete3(
|
|
2125
|
-
db,
|
|
2126
|
-
tableName,
|
|
2127
|
-
schema[tableName],
|
|
2128
|
-
tableFilters
|
|
2129
|
-
);
|
|
2130
|
-
queries[selectArrGenerated.name] = {
|
|
2131
|
-
type: selectArrOutput,
|
|
2132
|
-
args: selectArrGenerated.args,
|
|
2133
|
-
resolve: selectArrGenerated.resolver
|
|
2134
|
-
};
|
|
2135
|
-
queries[selectSingleGenerated.name] = {
|
|
2136
|
-
type: selectSingleOutput,
|
|
2137
|
-
args: selectSingleGenerated.args,
|
|
2138
|
-
resolve: selectSingleGenerated.resolver
|
|
2139
|
-
};
|
|
2140
|
-
mutations[insertArrGenerated.name] = {
|
|
2141
|
-
type: arrTableItemOutput,
|
|
2142
|
-
args: insertArrGenerated.args,
|
|
2143
|
-
resolve: insertArrGenerated.resolver
|
|
2144
|
-
};
|
|
2145
|
-
mutations[insertSingleGenerated.name] = {
|
|
2146
|
-
type: singleTableItemOutput,
|
|
2147
|
-
args: insertSingleGenerated.args,
|
|
2148
|
-
resolve: insertSingleGenerated.resolver
|
|
2149
|
-
};
|
|
2150
|
-
mutations[updateGenerated.name] = {
|
|
2151
|
-
type: arrTableItemOutput,
|
|
2152
|
-
args: updateGenerated.args,
|
|
2153
|
-
resolve: updateGenerated.resolver
|
|
2154
|
-
};
|
|
2155
|
-
mutations[deleteGenerated.name] = {
|
|
2156
|
-
type: arrTableItemOutput,
|
|
2157
|
-
args: deleteGenerated.args,
|
|
2158
|
-
resolve: deleteGenerated.resolver
|
|
2159
|
-
};
|
|
2160
|
-
[insertInput, updateInput, tableFilters, tableOrder].forEach(
|
|
2161
|
-
(e) => inputs[e.name] = e
|
|
2162
|
-
);
|
|
2163
|
-
outputs[selectSingleOutput.name] = selectSingleOutput;
|
|
2164
|
-
outputs[singleTableItemOutput.name] = singleTableItemOutput;
|
|
2165
|
-
interfaces[tableFieldsInterface.name] = tableFieldsInterface;
|
|
2166
|
-
}
|
|
2167
|
-
return { queries, mutations, inputs, interfaces, types: outputs };
|
|
2168
|
-
};
|
|
2169
|
-
|
|
2170
|
-
// src/buildSchema.ts
|
|
2171
|
-
var buildSchema = (db, config) => {
|
|
2172
|
-
const schema = db._.fullSchema;
|
|
2173
|
-
if (!schema) {
|
|
2174
|
-
throw new Error(
|
|
2175
|
-
"Drizzle-GraphQL Error: Schema not found in drizzle instance. Make sure you're using drizzle-orm v0.30.9 or above and schema is passed to drizzle constructor!"
|
|
2176
|
-
);
|
|
2177
|
-
}
|
|
2178
|
-
if (typeof config?.relationsDepthLimit === "number") {
|
|
2179
|
-
if (config.relationsDepthLimit < 0) {
|
|
2180
|
-
throw new Error(
|
|
2181
|
-
"Drizzle-GraphQL Error: config.relationsDepthLimit is supposed to be nonnegative integer or undefined!"
|
|
2182
|
-
);
|
|
2183
|
-
}
|
|
2184
|
-
if (config.relationsDepthLimit !== ~~config.relationsDepthLimit) {
|
|
2185
|
-
throw new Error(
|
|
2186
|
-
"Drizzle-GraphQL Error: config.relationsDepthLimit is supposed to be nonnegative integer or undefined!"
|
|
2187
|
-
);
|
|
2188
|
-
}
|
|
2189
|
-
}
|
|
2190
|
-
let generatorOutput;
|
|
2191
|
-
if ((0, import_drizzle_orm7.is)(db, import_mysql_core3.MySqlDatabase)) {
|
|
2192
|
-
generatorOutput = generateSchemaData(db, schema, config?.relationsDepthLimit);
|
|
2193
|
-
} else if ((0, import_drizzle_orm7.is)(db, import_pg_core3.PgDatabase)) {
|
|
2194
|
-
generatorOutput = generateSchemaData2(db, schema, config?.relationsDepthLimit);
|
|
2195
|
-
} else if ((0, import_drizzle_orm7.is)(db, import_sqlite_core3.BaseSQLiteDatabase)) {
|
|
2196
|
-
generatorOutput = generateSchemaData3(db, schema, config?.relationsDepthLimit);
|
|
2197
|
-
} else
|
|
2198
|
-
throw new Error("Drizzle-GraphQL Error: Unknown database instance type");
|
|
2199
|
-
const { queries, mutations, inputs, types } = generatorOutput;
|
|
2200
|
-
const graphQLSchemaConfig = {
|
|
2201
|
-
types: [...Object.values(inputs), ...Object.values(types)],
|
|
2202
|
-
query: new import_graphql7.GraphQLObjectType({
|
|
2203
|
-
name: "Query",
|
|
2204
|
-
fields: queries
|
|
2205
|
-
})
|
|
2206
|
-
};
|
|
2207
|
-
if (config?.mutations !== false) {
|
|
2208
|
-
const mutation = new import_graphql7.GraphQLObjectType({
|
|
2209
|
-
name: "Mutation",
|
|
2210
|
-
fields: mutations
|
|
2211
|
-
});
|
|
2212
|
-
graphQLSchemaConfig.mutation = mutation;
|
|
2213
|
-
}
|
|
2214
|
-
const outputSchema = new import_graphql7.GraphQLSchema(graphQLSchemaConfig);
|
|
2215
|
-
return { schema: outputSchema, entities: generatorOutput };
|
|
2216
|
-
};
|
|
2217
|
-
|
|
2218
|
-
// src/buildSchemaSDL/index.ts
|
|
2219
|
-
var import_drizzle_orm12 = require("drizzle-orm");
|
|
2220
|
-
var import_sqlite_core5 = require("drizzle-orm/sqlite-core");
|
|
2221
|
-
|
|
2222
|
-
// src/buildSchemaSDL/generator/schema/generation.ts
|
|
2223
|
-
var import_drizzle_orm8 = require("drizzle-orm");
|
|
2224
|
-
var generateTypes = (db, schema) => {
|
|
2225
|
-
const tables = {};
|
|
2226
|
-
const schemaEntries = Object.entries(schema);
|
|
2227
|
-
const tableEntries = [];
|
|
2228
|
-
for (const [key, value] of schemaEntries) {
|
|
2229
|
-
if (value && typeof value === "object" && "getSQL" in value) {
|
|
2230
|
-
const table = value;
|
|
2231
|
-
const tableName = key;
|
|
2232
|
-
const columns = (0, import_drizzle_orm8.getTableColumns)(table);
|
|
2233
|
-
tables[tableName] = {
|
|
2234
|
-
name: tableName,
|
|
2235
|
-
table,
|
|
2236
|
-
columns
|
|
2237
|
-
};
|
|
2238
|
-
tableEntries.push([tableName, table]);
|
|
2239
|
-
}
|
|
2240
|
-
}
|
|
2241
|
-
const rawRelations = schemaEntries.filter(([key, value]) => (0, import_drizzle_orm8.is)(value, import_drizzle_orm8.Relations)).map(([key, value]) => [
|
|
2242
|
-
tableEntries.find(
|
|
2243
|
-
([tableName, tableValue]) => tableValue === value.table
|
|
2244
|
-
)[0],
|
|
2245
|
-
value
|
|
2246
|
-
]).map(([tableName, relValue]) => [
|
|
2247
|
-
tableName,
|
|
2248
|
-
relValue.config((0, import_drizzle_orm8.createTableRelationsHelpers)(tables[tableName].table))
|
|
2249
|
-
]);
|
|
2250
|
-
const namedRelations = Object.fromEntries(
|
|
2251
|
-
rawRelations.map(([relName, config]) => {
|
|
2252
|
-
const namedConfig = Object.fromEntries(
|
|
2253
|
-
Object.entries(config).map(([innerRelName, innerRelValue]) => [
|
|
2254
|
-
innerRelName,
|
|
2255
|
-
{
|
|
2256
|
-
relation: innerRelValue,
|
|
2257
|
-
targetTableName: tableEntries.find(
|
|
2258
|
-
([tableName, tableValue]) => tableValue === innerRelValue.referencedTable
|
|
2259
|
-
)[0]
|
|
2260
|
-
}
|
|
2261
|
-
])
|
|
2262
|
-
);
|
|
2263
|
-
return [relName, namedConfig];
|
|
2264
|
-
})
|
|
2265
|
-
);
|
|
2266
|
-
return { tables, relations: namedRelations };
|
|
2267
|
-
};
|
|
2268
|
-
|
|
2269
|
-
// src/buildSchemaSDL/generator/schema/type-defs.ts
|
|
2270
|
-
var import_drizzle_orm9 = require("drizzle-orm");
|
|
2271
|
-
var import_mysql_core4 = require("drizzle-orm/mysql-core");
|
|
2272
|
-
var import_pg_core4 = require("drizzle-orm/pg-core");
|
|
2273
|
-
var import_sqlite_core4 = require("drizzle-orm/sqlite-core");
|
|
2274
|
-
var allowedNameChars2 = /^[a-zA-Z0-9_]+$/;
|
|
2275
|
-
var customScalars = /* @__PURE__ */ new Set();
|
|
2276
|
-
var enumDefinitions = /* @__PURE__ */ new Map();
|
|
2277
|
-
var requiredFieldFilters = /* @__PURE__ */ new Set();
|
|
2278
|
-
var foreignKeyTypes = /* @__PURE__ */ new Map();
|
|
2279
|
-
var columnToSDL = (column, columnName, tableName, forceNullable = false, isPrimaryKey = false) => {
|
|
2280
|
-
let baseType;
|
|
2281
|
-
if (column.customGraphqlType) {
|
|
2282
|
-
baseType = column.customGraphqlType;
|
|
2283
|
-
} else if (isPrimaryKey) {
|
|
2284
|
-
baseType = "ID";
|
|
2285
|
-
} else {
|
|
2286
|
-
const foreignKeyType = foreignKeyTypes.get(`${tableName}.${columnName}`);
|
|
2287
|
-
if (foreignKeyType) {
|
|
2288
|
-
baseType = foreignKeyType;
|
|
2289
|
-
} else {
|
|
2290
|
-
switch (column.dataType) {
|
|
2291
|
-
case "boolean":
|
|
2292
|
-
baseType = "Boolean";
|
|
2293
|
-
break;
|
|
2294
|
-
case "json":
|
|
2295
|
-
if (column.columnType === "PgGeometryObject") {
|
|
2296
|
-
baseType = "PgGeometryObject";
|
|
2297
|
-
customScalars.add("PgGeometryObject");
|
|
2298
|
-
} else {
|
|
2299
|
-
baseType = "JSON";
|
|
2300
|
-
customScalars.add("JSON");
|
|
2301
|
-
}
|
|
2302
|
-
break;
|
|
2303
|
-
case "date":
|
|
2304
|
-
baseType = "Date";
|
|
2305
|
-
customScalars.add("Date");
|
|
2306
|
-
break;
|
|
2307
|
-
case "string":
|
|
2308
|
-
if (column.enumValues?.length) {
|
|
2309
|
-
const enumName = `${capitalize(tableName)}${capitalize(
|
|
2310
|
-
columnName
|
|
2311
|
-
)}Enum`;
|
|
2312
|
-
baseType = enumName;
|
|
2313
|
-
if (!enumDefinitions.has(enumName)) {
|
|
2314
|
-
enumDefinitions.set(enumName, {
|
|
2315
|
-
name: enumName,
|
|
2316
|
-
values: column.enumValues.map(
|
|
2317
|
-
(e, index) => allowedNameChars2.test(e) ? e : `Option${index}`
|
|
2318
|
-
)
|
|
2319
|
-
});
|
|
2320
|
-
}
|
|
2321
|
-
} else {
|
|
2322
|
-
baseType = "String";
|
|
2323
|
-
}
|
|
2324
|
-
break;
|
|
2325
|
-
case "bigint":
|
|
2326
|
-
baseType = "BigInt";
|
|
2327
|
-
customScalars.add("BigInt");
|
|
2328
|
-
break;
|
|
2329
|
-
case "number":
|
|
2330
|
-
if ((0, import_drizzle_orm9.is)(column, import_pg_core4.PgInteger) || (0, import_drizzle_orm9.is)(column, import_pg_core4.PgSerial) || (0, import_drizzle_orm9.is)(column, import_mysql_core4.MySqlInt) || (0, import_drizzle_orm9.is)(column, import_mysql_core4.MySqlSerial) || (0, import_drizzle_orm9.is)(column, import_sqlite_core4.SQLiteInteger)) {
|
|
2331
|
-
baseType = "Int";
|
|
2332
|
-
} else {
|
|
2333
|
-
baseType = "Float";
|
|
2334
|
-
}
|
|
2335
|
-
break;
|
|
2336
|
-
case "buffer":
|
|
2337
|
-
baseType = "[Int!]";
|
|
2338
|
-
break;
|
|
2339
|
-
case "array":
|
|
2340
|
-
if (column.columnType === "PgVector") {
|
|
2341
|
-
baseType = "[Float!]";
|
|
2342
|
-
} else if (column.columnType === "PgGeometry") {
|
|
2343
|
-
baseType = "[Float!]";
|
|
2344
|
-
} else {
|
|
2345
|
-
const scalarName = `${capitalize(tableName)}${capitalize(
|
|
2346
|
-
columnName
|
|
2347
|
-
)}Array`;
|
|
2348
|
-
baseType = scalarName;
|
|
2349
|
-
customScalars.add(scalarName);
|
|
2350
|
-
}
|
|
2351
|
-
break;
|
|
2352
|
-
case "custom":
|
|
2353
|
-
default:
|
|
2354
|
-
if (column.columnType) {
|
|
2355
|
-
baseType = column.columnType;
|
|
2356
|
-
customScalars.add(column.columnType);
|
|
2357
|
-
} else {
|
|
2358
|
-
const customScalarName = `${capitalize(tableName)}${capitalize(
|
|
2359
|
-
columnName
|
|
2360
|
-
)}`;
|
|
2361
|
-
baseType = customScalarName;
|
|
2362
|
-
customScalars.add(customScalarName);
|
|
2363
|
-
}
|
|
2364
|
-
break;
|
|
2365
|
-
}
|
|
2366
|
-
}
|
|
2367
|
-
}
|
|
2368
|
-
if (!forceNullable && column.notNull) {
|
|
2369
|
-
return `${baseType}!`;
|
|
2370
|
-
}
|
|
2371
|
-
return baseType;
|
|
2372
|
-
};
|
|
2373
|
-
var generateTypeDefs = (tables, relations) => {
|
|
2374
|
-
const typeDefs = [];
|
|
2375
|
-
customScalars.clear();
|
|
2376
|
-
enumDefinitions.clear();
|
|
2377
|
-
requiredFieldFilters.clear();
|
|
2378
|
-
foreignKeyTypes.clear();
|
|
2379
|
-
for (const [tableName, tableRelations] of Object.entries(relations)) {
|
|
2380
|
-
const tableInfo = tables[tableName];
|
|
2381
|
-
if (!tableInfo)
|
|
2382
|
-
continue;
|
|
2383
|
-
for (const [relationName, relationInfo] of Object.entries(tableRelations)) {
|
|
2384
|
-
const relation = relationInfo.relation;
|
|
2385
|
-
if (!(0, import_drizzle_orm9.is)(relation, import_drizzle_orm9.One))
|
|
2386
|
-
continue;
|
|
2387
|
-
const config = relation.config;
|
|
2388
|
-
if (!config?.fields || !config?.references)
|
|
2389
|
-
continue;
|
|
2390
|
-
const referencedTableName = relationInfo.targetTableName;
|
|
2391
|
-
const referencedTable = tables[referencedTableName];
|
|
2392
|
-
if (!referencedTable)
|
|
2393
|
-
continue;
|
|
2394
|
-
for (let i = 0; i < config.fields.length; i++) {
|
|
2395
|
-
const field = config.fields[i];
|
|
2396
|
-
const reference = config.references[i];
|
|
2397
|
-
if (!field || !reference)
|
|
2398
|
-
continue;
|
|
2399
|
-
const fieldColumnName = field.name;
|
|
2400
|
-
const referenceColumnName = reference.name;
|
|
2401
|
-
const foreignKeyColumn = Object.values(tableInfo.columns).find(
|
|
2402
|
-
(col) => col.name === fieldColumnName
|
|
2403
|
-
);
|
|
2404
|
-
const referencedColumn = Object.values(referencedTable.columns).find(
|
|
2405
|
-
(col) => col.name === referenceColumnName
|
|
2406
|
-
);
|
|
2407
|
-
if (!foreignKeyColumn || !referencedColumn)
|
|
2408
|
-
continue;
|
|
2409
|
-
const foreignKeyPropertyName = Object.keys(tableInfo.columns).find(
|
|
2410
|
-
(key) => tableInfo.columns[key] === foreignKeyColumn
|
|
2411
|
-
);
|
|
2412
|
-
if (!foreignKeyPropertyName)
|
|
2413
|
-
continue;
|
|
2414
|
-
const referencedCustomType = referencedColumn.customGraphqlType;
|
|
2415
|
-
const foreignKeyHasCustomType = !!foreignKeyColumn.customGraphqlType;
|
|
2416
|
-
const referencedIsPrimaryKey = referencedColumn.primary || false;
|
|
2417
|
-
if (!foreignKeyHasCustomType) {
|
|
2418
|
-
if (referencedCustomType) {
|
|
2419
|
-
foreignKeyTypes.set(
|
|
2420
|
-
`${tableName}.${foreignKeyPropertyName}`,
|
|
2421
|
-
referencedCustomType
|
|
2422
|
-
);
|
|
2423
|
-
} else if (referencedIsPrimaryKey) {
|
|
2424
|
-
foreignKeyTypes.set(`${tableName}.${foreignKeyPropertyName}`, "ID");
|
|
2425
|
-
}
|
|
2426
|
-
}
|
|
2427
|
-
}
|
|
2428
|
-
}
|
|
2429
|
-
}
|
|
2430
|
-
for (const [tableName, tableInfo] of Object.entries(tables)) {
|
|
2431
|
-
const typeName = capitalize(tableName);
|
|
2432
|
-
const fields = [];
|
|
2433
|
-
for (const [columnName, column] of Object.entries(tableInfo.columns)) {
|
|
2434
|
-
const isPrimaryKey = column.primary || false;
|
|
2435
|
-
const typeStr = columnToSDL(
|
|
2436
|
-
column,
|
|
2437
|
-
columnName,
|
|
2438
|
-
tableName,
|
|
2439
|
-
false,
|
|
2440
|
-
isPrimaryKey
|
|
2441
|
-
);
|
|
2442
|
-
const description = column.customGraphqlDescription;
|
|
2443
|
-
if (description) {
|
|
2444
|
-
fields.push(` """${description}"""`);
|
|
2445
|
-
}
|
|
2446
|
-
fields.push(` ${columnName}: ${typeStr}`);
|
|
2447
|
-
}
|
|
2448
|
-
const tableRelations = relations[tableName];
|
|
2449
|
-
if (tableRelations) {
|
|
2450
|
-
for (const [relationName, relationInfo] of Object.entries(
|
|
2451
|
-
tableRelations
|
|
2452
|
-
)) {
|
|
2453
|
-
const isOne = (0, import_drizzle_orm9.is)(relationInfo.relation, import_drizzle_orm9.One);
|
|
2454
|
-
const targetTableName = relationInfo.targetTableName;
|
|
2455
|
-
const targetTypeName = capitalize(targetTableName);
|
|
2456
|
-
if (isOne) {
|
|
2457
|
-
fields.push(
|
|
2458
|
-
` ${relationName}(where: ${targetTypeName}Filters): ${targetTypeName}`
|
|
2459
|
-
);
|
|
2460
|
-
} else {
|
|
2461
|
-
fields.push(
|
|
2462
|
-
` ${relationName}(where: ${targetTypeName}Filters, orderBy: ${targetTypeName}OrderBy, limit: Int, offset: Int): [${targetTypeName}!]!`
|
|
2463
|
-
);
|
|
2464
|
-
}
|
|
2465
|
-
}
|
|
2466
|
-
}
|
|
2467
|
-
typeDefs.push(`type ${typeName} {
|
|
2468
|
-
${fields.join("\n")}
|
|
2469
|
-
}`);
|
|
2470
|
-
const insertFields = [];
|
|
2471
|
-
for (const [columnName, column] of Object.entries(tableInfo.columns)) {
|
|
2472
|
-
const isPrimaryKey = column.primary || false;
|
|
2473
|
-
const typeStr = columnToSDL(
|
|
2474
|
-
column,
|
|
2475
|
-
columnName,
|
|
2476
|
-
tableName,
|
|
2477
|
-
false,
|
|
2478
|
-
isPrimaryKey
|
|
2479
|
-
);
|
|
2480
|
-
const hasDefault = column.hasDefault || column.default !== void 0;
|
|
2481
|
-
const isAutoIncrement = column.autoIncrement || column.generatedAlwaysAs;
|
|
2482
|
-
const isNotNull4 = column.notNull;
|
|
2483
|
-
const shouldBeOptional = hasDefault || isAutoIncrement || isPrimaryKey && !isNotNull4;
|
|
2484
|
-
let insertFieldType;
|
|
2485
|
-
if (shouldBeOptional) {
|
|
2486
|
-
insertFieldType = typeStr.endsWith("!") ? typeStr.slice(0, -1) : typeStr;
|
|
2487
|
-
} else {
|
|
2488
|
-
insertFieldType = isNotNull4 && !typeStr.endsWith("!") ? `${typeStr}!` : typeStr;
|
|
2489
|
-
}
|
|
2490
|
-
insertFields.push(` ${columnName}: ${insertFieldType}`);
|
|
2491
|
-
}
|
|
2492
|
-
if (insertFields.length > 0) {
|
|
2493
|
-
typeDefs.push(
|
|
2494
|
-
`input ${typeName}InsertInput {
|
|
2495
|
-
${insertFields.join("\n")}
|
|
2496
|
-
}`
|
|
2497
|
-
);
|
|
2498
|
-
}
|
|
2499
|
-
const updateFields = [];
|
|
2500
|
-
for (const [columnName, column] of Object.entries(tableInfo.columns)) {
|
|
2501
|
-
const isPrimaryKey = column.primary || false;
|
|
2502
|
-
const typeStr = columnToSDL(
|
|
2503
|
-
column,
|
|
2504
|
-
columnName,
|
|
2505
|
-
tableName,
|
|
2506
|
-
true,
|
|
2507
|
-
isPrimaryKey
|
|
2508
|
-
);
|
|
2509
|
-
updateFields.push(` ${columnName}: ${typeStr}`);
|
|
2510
|
-
}
|
|
2511
|
-
typeDefs.push(
|
|
2512
|
-
`input ${typeName}UpdateInput {
|
|
2513
|
-
${updateFields.join("\n")}
|
|
2514
|
-
}`
|
|
2515
|
-
);
|
|
2516
|
-
const whereFields = [];
|
|
2517
|
-
for (const [columnName, column] of Object.entries(tableInfo.columns)) {
|
|
2518
|
-
const isPrimaryKey = column.primary || false;
|
|
2519
|
-
const typeStr = columnToSDL(
|
|
2520
|
-
column,
|
|
2521
|
-
columnName,
|
|
2522
|
-
tableName,
|
|
2523
|
-
true,
|
|
2524
|
-
isPrimaryKey
|
|
2525
|
-
);
|
|
2526
|
-
const normalizedType = typeStr.replace(/[^a-zA-Z0-9]/g, "");
|
|
2527
|
-
const filterTypeName = `${normalizedType}FieldFilter`;
|
|
2528
|
-
requiredFieldFilters.add(
|
|
2529
|
-
JSON.stringify({ normalizedType, baseType: typeStr })
|
|
2530
|
-
);
|
|
2531
|
-
whereFields.push(` ${columnName}: ${filterTypeName}`);
|
|
2532
|
-
}
|
|
2533
|
-
whereFields.push(` OR: [${typeName}Filters!]`);
|
|
2534
|
-
typeDefs.push(`input ${typeName}Filters {
|
|
2535
|
-
${whereFields.join("\n")}
|
|
2536
|
-
}`);
|
|
2537
|
-
const orderByFields = [];
|
|
2538
|
-
for (const columnName of Object.keys(tableInfo.columns)) {
|
|
2539
|
-
orderByFields.push(` ${columnName}: InnerOrder`);
|
|
2540
|
-
}
|
|
2541
|
-
typeDefs.push(`input ${typeName}OrderBy {
|
|
2542
|
-
${orderByFields.join("\n")}
|
|
2543
|
-
}`);
|
|
2544
|
-
typeDefs.push(`type ${typeName}DeleteResult {
|
|
2545
|
-
deletedItems: [DeletedItem!]!
|
|
2546
|
-
${tableName}FindMany(where: ${typeName}Filters, orderBy: ${typeName}OrderBy, limit: Int, offset: Int): [${typeName}!]!
|
|
2547
|
-
}`);
|
|
2548
|
-
}
|
|
2549
|
-
const allDefs = [];
|
|
2550
|
-
if (customScalars.size > 0) {
|
|
2551
|
-
for (const scalarName of Array.from(customScalars).sort()) {
|
|
2552
|
-
allDefs.push(`scalar ${scalarName}`);
|
|
2553
|
-
}
|
|
2554
|
-
}
|
|
2555
|
-
if (enumDefinitions.size > 0) {
|
|
2556
|
-
for (const enumDef of enumDefinitions.values()) {
|
|
2557
|
-
const valueStrings = enumDef.values.map((v) => ` ${v}`);
|
|
2558
|
-
allDefs.push(`enum ${enumDef.name} {
|
|
2559
|
-
${valueStrings.join("\n")}
|
|
2560
|
-
}`);
|
|
2561
|
-
}
|
|
2562
|
-
}
|
|
2563
|
-
allDefs.push(`enum OrderByDirection {
|
|
2564
|
-
asc
|
|
2565
|
-
desc
|
|
2566
|
-
}`);
|
|
2567
|
-
allDefs.push(`type DeletedItem {
|
|
2568
|
-
id: ID!
|
|
2569
|
-
}`);
|
|
2570
|
-
allDefs.push(`input InnerOrder {
|
|
2571
|
-
direction: OrderByDirection!
|
|
2572
|
-
priority: Int!
|
|
2573
|
-
}`);
|
|
2574
|
-
const filterTypesAdded = /* @__PURE__ */ new Set();
|
|
2575
|
-
for (const filterInfoJson of requiredFieldFilters) {
|
|
2576
|
-
const { normalizedType, baseType } = JSON.parse(filterInfoJson);
|
|
2577
|
-
const filterTypeName = `${normalizedType}FieldFilter`;
|
|
2578
|
-
if (filterTypesAdded.has(filterTypeName))
|
|
2579
|
-
continue;
|
|
2580
|
-
filterTypesAdded.add(filterTypeName);
|
|
2581
|
-
const filterFields = [];
|
|
2582
|
-
filterFields.push(` eq: ${baseType}`);
|
|
2583
|
-
filterFields.push(` ne: ${baseType}`);
|
|
2584
|
-
filterFields.push(` lt: ${baseType}`);
|
|
2585
|
-
filterFields.push(` lte: ${baseType}`);
|
|
2586
|
-
filterFields.push(` gt: ${baseType}`);
|
|
2587
|
-
filterFields.push(` gte: ${baseType}`);
|
|
2588
|
-
filterFields.push(` like: String`);
|
|
2589
|
-
filterFields.push(` notLike: String`);
|
|
2590
|
-
filterFields.push(` ilike: String`);
|
|
2591
|
-
filterFields.push(` notIlike: String`);
|
|
2592
|
-
filterFields.push(` inArray: [${baseType}!]`);
|
|
2593
|
-
filterFields.push(` notInArray: [${baseType}!]`);
|
|
2594
|
-
filterFields.push(` isNull: Boolean`);
|
|
2595
|
-
filterFields.push(` isNotNull: Boolean`);
|
|
2596
|
-
filterFields.push(` OR: [${filterTypeName}!]`);
|
|
2597
|
-
allDefs.push(`input ${filterTypeName} {
|
|
2598
|
-
${filterFields.join("\n")}
|
|
2599
|
-
}`);
|
|
2600
|
-
}
|
|
2601
|
-
allDefs.push(...typeDefs);
|
|
2602
|
-
return allDefs.join("\n\n");
|
|
2603
|
-
};
|
|
2604
|
-
var generateQueryTypeDefs = (tables) => {
|
|
2605
|
-
const queryFields = [];
|
|
2606
|
-
for (const tableName of Object.keys(tables)) {
|
|
2607
|
-
const typeName = capitalize(tableName);
|
|
2608
|
-
queryFields.push(
|
|
2609
|
-
` ${tableName}FindMany(where: ${typeName}Filters, orderBy: ${typeName}OrderBy, limit: Int, offset: Int): [${typeName}!]!`
|
|
2610
|
-
);
|
|
2611
|
-
queryFields.push(
|
|
2612
|
-
` ${tableName}FindFirst(where: ${typeName}Filters, orderBy: ${typeName}OrderBy): ${typeName}`
|
|
2613
|
-
);
|
|
2614
|
-
}
|
|
2615
|
-
return `type Query {
|
|
2616
|
-
${queryFields.join("\n")}
|
|
2617
|
-
}`;
|
|
2618
|
-
};
|
|
2619
|
-
var generateMutationTypeDefs = (tables) => {
|
|
2620
|
-
const mutationFields = [];
|
|
2621
|
-
for (const tableName of Object.keys(tables)) {
|
|
2622
|
-
const typeName = capitalize(tableName);
|
|
2623
|
-
mutationFields.push(
|
|
2624
|
-
` ${tableName}InsertMany(values: [${typeName}InsertInput!]!): [${typeName}!]!`
|
|
2625
|
-
);
|
|
2626
|
-
mutationFields.push(
|
|
2627
|
-
` ${tableName}UpdateMany(where: ${typeName}Filters, set: ${typeName}UpdateInput!): [${typeName}!]!`
|
|
2628
|
-
);
|
|
2629
|
-
mutationFields.push(
|
|
2630
|
-
` ${tableName}DeleteMany(where: ${typeName}Filters): ${typeName}DeleteResult!`
|
|
2631
|
-
);
|
|
2632
|
-
}
|
|
2633
|
-
return `type Mutation {
|
|
2634
|
-
${mutationFields.join("\n")}
|
|
2635
|
-
}`;
|
|
2636
|
-
};
|
|
2637
|
-
|
|
2638
|
-
// src/buildSchemaSDL/generator/queries/resolvers.ts
|
|
2639
|
-
var import_graphql9 = require("graphql");
|
|
2640
|
-
var import_graphql_parse_resolve_info4 = require("graphql-parse-resolve-info");
|
|
2641
|
-
|
|
2642
|
-
// src/buildSchemaSDL/generator/utils/selection.ts
|
|
2643
|
-
var import_drizzle_orm11 = require("drizzle-orm");
|
|
2644
|
-
|
|
2645
|
-
// src/buildSchemaSDL/generator/utils/filters.ts
|
|
2646
|
-
var import_drizzle_orm10 = require("drizzle-orm");
|
|
2647
|
-
var import_graphql8 = require("graphql");
|
|
2648
|
-
var extractFiltersColumn2 = (column, columnName, operators) => {
|
|
2649
|
-
const entries = Object.entries(operators);
|
|
2650
|
-
if (!entries.length)
|
|
2651
|
-
return void 0;
|
|
2652
|
-
if (operators.OR && operators.OR.length > 0) {
|
|
2653
|
-
if (entries.length > 1) {
|
|
2654
|
-
throw new import_graphql8.GraphQLError(
|
|
2655
|
-
`WHERE ${columnName}: Cannot specify both fields and 'OR' in column operators!`
|
|
2656
|
-
);
|
|
2657
|
-
}
|
|
2658
|
-
const variants2 = [];
|
|
2659
|
-
for (const variant of operators.OR) {
|
|
2660
|
-
const extracted = extractFiltersColumn2(column, columnName, variant);
|
|
2661
|
-
if (extracted)
|
|
2662
|
-
variants2.push(extracted);
|
|
2663
|
-
}
|
|
2664
|
-
return variants2.length ? variants2.length > 1 ? (0, import_drizzle_orm10.or)(...variants2) : variants2[0] : void 0;
|
|
2665
|
-
}
|
|
2666
|
-
const variants = [];
|
|
2667
|
-
for (const [operatorName, operatorValue] of entries) {
|
|
2668
|
-
if (operatorValue === null || operatorValue === false)
|
|
2669
|
-
continue;
|
|
2670
|
-
switch (operatorName) {
|
|
2671
|
-
case "eq":
|
|
2672
|
-
case "ne":
|
|
2673
|
-
case "gt":
|
|
2674
|
-
case "gte":
|
|
2675
|
-
case "lt":
|
|
2676
|
-
case "lte": {
|
|
2677
|
-
const singleValue = remapFromGraphQLCore(
|
|
2678
|
-
operatorValue,
|
|
2679
|
-
column,
|
|
2680
|
-
columnName
|
|
2681
|
-
);
|
|
2682
|
-
const opMap = { eq: import_drizzle_orm10.eq, ne: import_drizzle_orm10.ne, gt: import_drizzle_orm10.gt, gte: import_drizzle_orm10.gte, lt: import_drizzle_orm10.lt, lte: import_drizzle_orm10.lte };
|
|
2683
|
-
variants.push(opMap[operatorName](column, singleValue));
|
|
2684
|
-
break;
|
|
2685
|
-
}
|
|
2686
|
-
case "like":
|
|
2687
|
-
case "notLike":
|
|
2688
|
-
case "ilike":
|
|
2689
|
-
case "notIlike": {
|
|
2690
|
-
const opMap = { like: import_drizzle_orm10.like, notLike: import_drizzle_orm10.notLike, ilike: import_drizzle_orm10.ilike, notIlike: import_drizzle_orm10.notIlike };
|
|
2691
|
-
variants.push(opMap[operatorName](column, operatorValue));
|
|
2692
|
-
break;
|
|
2693
|
-
}
|
|
2694
|
-
case "inArray":
|
|
2695
|
-
case "notInArray": {
|
|
2696
|
-
if (!operatorValue.length) {
|
|
2697
|
-
throw new import_graphql8.GraphQLError(
|
|
2698
|
-
`WHERE ${columnName}: Unable to use operator ${operatorName} with an empty array!`
|
|
2699
|
-
);
|
|
2700
|
-
}
|
|
2701
|
-
const arrayValue = operatorValue.map(
|
|
2702
|
-
(val) => remapFromGraphQLCore(val, column, columnName)
|
|
2703
|
-
);
|
|
2704
|
-
const opMap = { inArray: import_drizzle_orm10.inArray, notInArray: import_drizzle_orm10.notInArray };
|
|
2705
|
-
variants.push(opMap[operatorName](column, arrayValue));
|
|
2706
|
-
break;
|
|
2707
|
-
}
|
|
2708
|
-
case "isNull":
|
|
2709
|
-
case "isNotNull": {
|
|
2710
|
-
const opMap = { isNull: import_drizzle_orm10.isNull, isNotNull: import_drizzle_orm10.isNotNull };
|
|
2711
|
-
variants.push(opMap[operatorName](column));
|
|
2712
|
-
break;
|
|
2713
|
-
}
|
|
2714
|
-
}
|
|
2715
|
-
}
|
|
2716
|
-
return variants.length ? variants.length > 1 ? (0, import_drizzle_orm10.and)(...variants) : variants[0] : void 0;
|
|
2717
|
-
};
|
|
2718
|
-
var buildWhereClause = (tableInfo, where) => {
|
|
2719
|
-
if (!where || Object.keys(where).length === 0) {
|
|
2720
|
-
return void 0;
|
|
2721
|
-
}
|
|
2722
|
-
if (where.OR && where.OR.length > 0) {
|
|
2723
|
-
if (Object.keys(where).length > 1) {
|
|
2724
|
-
throw new import_graphql8.GraphQLError(
|
|
2725
|
-
`WHERE ${tableInfo.name}: Cannot specify both fields and 'OR' in table filters!`
|
|
2726
|
-
);
|
|
2727
|
-
}
|
|
2728
|
-
const variants = [];
|
|
2729
|
-
for (const variant of where.OR) {
|
|
2730
|
-
const extracted = buildWhereClause(tableInfo, variant);
|
|
2731
|
-
if (extracted)
|
|
2732
|
-
variants.push(extracted);
|
|
2733
|
-
}
|
|
2734
|
-
return variants.length ? variants.length > 1 ? (0, import_drizzle_orm10.or)(...variants) : variants[0] : void 0;
|
|
2735
|
-
}
|
|
2736
|
-
const conditions = [];
|
|
2737
|
-
for (const [columnName, operators] of Object.entries(where)) {
|
|
2738
|
-
if (columnName === "OR")
|
|
2739
|
-
continue;
|
|
2740
|
-
if (!operators || Object.keys(operators).length === 0)
|
|
2741
|
-
continue;
|
|
2742
|
-
const column = tableInfo.columns[columnName];
|
|
2743
|
-
if (!column)
|
|
2744
|
-
continue;
|
|
2745
|
-
const extracted = extractFiltersColumn2(
|
|
2746
|
-
column,
|
|
2747
|
-
columnName,
|
|
2748
|
-
operators
|
|
2749
|
-
);
|
|
2750
|
-
if (extracted)
|
|
2751
|
-
conditions.push(extracted);
|
|
2752
|
-
}
|
|
2753
|
-
if (conditions.length === 0)
|
|
2754
|
-
return void 0;
|
|
2755
|
-
if (conditions.length === 1)
|
|
2756
|
-
return conditions[0];
|
|
2757
|
-
return (0, import_drizzle_orm10.and)(...conditions);
|
|
2758
|
-
};
|
|
2759
|
-
|
|
2760
|
-
// src/export-tool/utils.ts
|
|
2761
|
-
function isExportVariable(value) {
|
|
2762
|
-
return typeof value === "string" && value.startsWith("$_") && value.length > 2;
|
|
2763
|
-
}
|
|
2764
|
-
function getVariableName(value) {
|
|
2765
|
-
if (!isExportVariable(value)) {
|
|
2766
|
-
return null;
|
|
2767
|
-
}
|
|
2768
|
-
return value.slice(2);
|
|
2769
|
-
}
|
|
2770
|
-
async function resolveExportVariables(args, exportStore, timeout, allowNull = true) {
|
|
2771
|
-
if (isExportVariable(args)) {
|
|
2772
|
-
const varName = getVariableName(args);
|
|
2773
|
-
return await exportStore.waitFor(varName, timeout, allowNull);
|
|
2774
|
-
}
|
|
2775
|
-
if (Array.isArray(args)) {
|
|
2776
|
-
const resolved = await Promise.all(
|
|
2777
|
-
args.map(async (item) => {
|
|
2778
|
-
if (isExportVariable(item)) {
|
|
2779
|
-
const varName = getVariableName(item);
|
|
2780
|
-
return await exportStore.waitFor(varName, timeout, allowNull);
|
|
2781
|
-
} else if (typeof item === "object" && item !== null) {
|
|
2782
|
-
return await resolveExportVariables(item, exportStore, timeout, allowNull);
|
|
2783
|
-
}
|
|
2784
|
-
return item;
|
|
2785
|
-
})
|
|
2786
|
-
);
|
|
2787
|
-
return resolved;
|
|
2788
|
-
}
|
|
2789
|
-
if (typeof args === "object" && args !== null) {
|
|
2790
|
-
const resolved = {};
|
|
2791
|
-
for (const [key, value] of Object.entries(args)) {
|
|
2792
|
-
if (isExportVariable(value)) {
|
|
2793
|
-
const varName = getVariableName(value);
|
|
2794
|
-
resolved[key] = await exportStore.waitFor(varName, timeout, allowNull);
|
|
2795
|
-
} else if (Array.isArray(value)) {
|
|
2796
|
-
resolved[key] = await resolveExportVariables(
|
|
2797
|
-
value,
|
|
2798
|
-
exportStore,
|
|
2799
|
-
timeout,
|
|
2800
|
-
allowNull
|
|
2801
|
-
);
|
|
2802
|
-
} else if (typeof value === "object" && value !== null) {
|
|
2803
|
-
resolved[key] = await resolveExportVariables(
|
|
2804
|
-
value,
|
|
2805
|
-
exportStore,
|
|
2806
|
-
timeout,
|
|
2807
|
-
allowNull
|
|
2808
|
-
);
|
|
2809
|
-
} else {
|
|
2810
|
-
resolved[key] = value;
|
|
2811
|
-
}
|
|
2812
|
-
}
|
|
2813
|
-
return resolved;
|
|
2814
|
-
}
|
|
2815
|
-
return args;
|
|
2816
|
-
}
|
|
2817
|
-
function getExportDirective(fieldNode) {
|
|
2818
|
-
const node = fieldNode;
|
|
2819
|
-
if (!node || !node.directives) {
|
|
2820
|
-
return null;
|
|
2821
|
-
}
|
|
2822
|
-
const exportDirective = node.directives.find(
|
|
2823
|
-
(directive) => directive.name.value === "export"
|
|
2824
|
-
);
|
|
2825
|
-
if (!exportDirective) {
|
|
2826
|
-
return null;
|
|
2827
|
-
}
|
|
2828
|
-
const asArg = exportDirective.arguments?.find(
|
|
2829
|
-
(arg) => arg.name.value === "as"
|
|
2830
|
-
);
|
|
2831
|
-
if (!asArg || asArg.value.kind !== "StringValue") {
|
|
2832
|
-
return null;
|
|
2833
|
-
}
|
|
2834
|
-
return asArg.value.value;
|
|
2835
|
-
}
|
|
2836
|
-
function hasExportVariables(args) {
|
|
2837
|
-
if (isExportVariable(args)) {
|
|
2838
|
-
return true;
|
|
2839
|
-
}
|
|
2840
|
-
if (Array.isArray(args)) {
|
|
2841
|
-
return args.some((item) => hasExportVariables(item));
|
|
2842
|
-
}
|
|
2843
|
-
if (typeof args === "object" && args !== null) {
|
|
2844
|
-
for (const value of Object.values(args)) {
|
|
2845
|
-
if (hasExportVariables(value)) {
|
|
2846
|
-
return true;
|
|
2847
|
-
}
|
|
2848
|
-
}
|
|
2849
|
-
}
|
|
2850
|
-
return false;
|
|
2851
|
-
}
|
|
2852
|
-
function processExports(result, selectionSet, exportStore) {
|
|
2853
|
-
if (!result || !selectionSet)
|
|
2854
|
-
return;
|
|
2855
|
-
for (const selection of selectionSet.selections) {
|
|
2856
|
-
if (selection.kind !== "Field")
|
|
2857
|
-
continue;
|
|
2858
|
-
const resultKey = selection.alias?.value ?? selection.name.value;
|
|
2859
|
-
if (!(resultKey in result))
|
|
2860
|
-
continue;
|
|
2861
|
-
const value = result[resultKey];
|
|
2862
|
-
const exportName = getExportDirective(selection);
|
|
2863
|
-
if (exportName) {
|
|
2864
|
-
exportStore.set(exportName, value);
|
|
2865
|
-
}
|
|
2866
|
-
if (selection.selectionSet && value !== null && value !== void 0) {
|
|
2867
|
-
if (Array.isArray(value)) {
|
|
2868
|
-
value.forEach((item) => {
|
|
2869
|
-
if (item && typeof item === "object") {
|
|
2870
|
-
processExports(item, selection.selectionSet, exportStore);
|
|
2871
|
-
}
|
|
2872
|
-
});
|
|
2873
|
-
} else if (typeof value === "object") {
|
|
2874
|
-
processExports(value, selection.selectionSet, exportStore);
|
|
2875
|
-
}
|
|
2876
|
-
}
|
|
2877
|
-
}
|
|
2878
|
-
}
|
|
2879
|
-
|
|
2880
|
-
// src/buildSchemaSDL/generator/utils/selection.ts
|
|
2881
|
-
var buildOrderByClause = (tableInfo, orderBy) => {
|
|
2882
|
-
if (!orderBy || Object.keys(orderBy).length === 0) {
|
|
2883
|
-
return void 0;
|
|
2884
|
-
}
|
|
2885
|
-
const orderEntries = Object.entries(orderBy).map(([columnName, field]) => ({
|
|
2886
|
-
columnName,
|
|
2887
|
-
...field
|
|
2888
|
-
}));
|
|
2889
|
-
orderEntries.sort((a, b) => a.priority - b.priority);
|
|
2890
|
-
const orderClauses = [];
|
|
2891
|
-
for (const entry of orderEntries) {
|
|
2892
|
-
const column = tableInfo.columns[entry.columnName];
|
|
2893
|
-
if (column) {
|
|
2894
|
-
orderClauses.push(
|
|
2895
|
-
entry.direction === "desc" ? (0, import_drizzle_orm11.desc)(column) : (0, import_drizzle_orm11.asc)(column)
|
|
2896
|
-
);
|
|
2897
|
-
}
|
|
2898
|
-
}
|
|
2899
|
-
return orderClauses.length > 0 ? orderClauses : void 0;
|
|
2900
|
-
};
|
|
2901
|
-
var extractSelectedColumns = (fields, tableInfo) => {
|
|
2902
|
-
const columns = {};
|
|
2903
|
-
for (const fieldName of Object.keys(fields)) {
|
|
2904
|
-
if (tableInfo.columns[fieldName]) {
|
|
2905
|
-
columns[fieldName] = true;
|
|
2906
|
-
}
|
|
2907
|
-
}
|
|
2908
|
-
return Object.keys(columns).length > 0 ? columns : {};
|
|
2909
|
-
};
|
|
2910
|
-
var extractRelationsParams2 = async (relationMap, tables, tableName, fields, context) => {
|
|
2911
|
-
const relations = relationMap[tableName];
|
|
2912
|
-
if (!relations)
|
|
2913
|
-
return void 0;
|
|
2914
|
-
const args = {};
|
|
2915
|
-
for (const [relName, { targetTableName, relation }] of Object.entries(
|
|
2916
|
-
relations
|
|
2917
|
-
)) {
|
|
2918
|
-
const relationField = fields[relName];
|
|
2919
|
-
if (!relationField)
|
|
2920
|
-
continue;
|
|
2921
|
-
const allFields = {};
|
|
2922
|
-
if (relationField.fieldsByTypeName) {
|
|
2923
|
-
for (const typeFields of Object.values(relationField.fieldsByTypeName)) {
|
|
2924
|
-
Object.assign(allFields, typeFields);
|
|
2925
|
-
}
|
|
2926
|
-
}
|
|
2927
|
-
const targetTable = tables[targetTableName];
|
|
2928
|
-
if (!targetTable)
|
|
2929
|
-
continue;
|
|
2930
|
-
const thisRecord = {
|
|
2931
|
-
columns: extractSelectedColumns(allFields, targetTable)
|
|
2932
|
-
};
|
|
2933
|
-
const relationArgs = relationField.args;
|
|
2934
|
-
if (relationArgs) {
|
|
2935
|
-
if (relationArgs["where"]) {
|
|
2936
|
-
let whereClause = relationArgs["where"];
|
|
2937
|
-
if (context?.exportStore && hasExportVariables(whereClause)) {
|
|
2938
|
-
try {
|
|
2939
|
-
whereClause = await resolveExportVariables(whereClause, context.exportStore);
|
|
2940
|
-
} catch (error) {
|
|
2941
|
-
console.warn(`Failed to resolve export variables in nested relation ${relName}:`, error);
|
|
2942
|
-
}
|
|
2943
|
-
}
|
|
2944
|
-
thisRecord.where = buildWhereClause(targetTable, whereClause);
|
|
2945
|
-
}
|
|
2946
|
-
if (relationArgs["orderBy"]) {
|
|
2947
|
-
thisRecord.orderBy = buildOrderByClause(
|
|
2948
|
-
targetTable,
|
|
2949
|
-
relationArgs["orderBy"]
|
|
2950
|
-
);
|
|
2951
|
-
}
|
|
2952
|
-
if (relationArgs["limit"] !== void 0) {
|
|
2953
|
-
thisRecord.limit = relationArgs["limit"];
|
|
2954
|
-
}
|
|
2955
|
-
if (relationArgs["offset"] !== void 0) {
|
|
2956
|
-
thisRecord.offset = relationArgs["offset"];
|
|
2957
|
-
}
|
|
2958
|
-
}
|
|
2959
|
-
const nestedWith = await extractRelationsParams2(
|
|
2960
|
-
relationMap,
|
|
2961
|
-
tables,
|
|
2962
|
-
targetTableName,
|
|
2963
|
-
allFields,
|
|
2964
|
-
context
|
|
2965
|
-
);
|
|
2966
|
-
if (nestedWith) {
|
|
2967
|
-
thisRecord.with = nestedWith;
|
|
2968
|
-
}
|
|
2969
|
-
args[relName] = thisRecord;
|
|
2970
|
-
}
|
|
2971
|
-
return Object.keys(args).length > 0 ? args : void 0;
|
|
2972
|
-
};
|
|
2973
|
-
|
|
2974
|
-
// src/buildSchemaSDL/generator/queries/resolvers.ts
|
|
2975
|
-
var createFindManyResolver = (queryBase, tableInfo, tables, relations) => {
|
|
2976
|
-
return async (parent, args, context, info) => {
|
|
2977
|
-
try {
|
|
2978
|
-
const { where, orderBy, limit, offset } = args;
|
|
2979
|
-
const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
|
|
2980
|
-
deep: true
|
|
2981
|
-
});
|
|
2982
|
-
const allFields = {};
|
|
2983
|
-
if (parsedInfo.fieldsByTypeName) {
|
|
2984
|
-
for (const fields of Object.values(parsedInfo.fieldsByTypeName)) {
|
|
2985
|
-
Object.assign(allFields, fields);
|
|
2986
|
-
}
|
|
2987
|
-
}
|
|
2988
|
-
const result = await queryBase.findMany({
|
|
2989
|
-
columns: extractSelectedColumns(allFields, tableInfo),
|
|
2990
|
-
offset,
|
|
2991
|
-
limit,
|
|
2992
|
-
orderBy: buildOrderByClause(tableInfo, orderBy),
|
|
2993
|
-
where: buildWhereClause(tableInfo, where),
|
|
2994
|
-
with: await extractRelationsParams2(
|
|
2995
|
-
relations,
|
|
2996
|
-
tables,
|
|
2997
|
-
tableInfo.name,
|
|
2998
|
-
allFields,
|
|
2999
|
-
context
|
|
3000
|
-
)
|
|
3001
|
-
});
|
|
3002
|
-
return result;
|
|
3003
|
-
} catch (e) {
|
|
3004
|
-
if (typeof e === "object" && e !== null && "message" in e) {
|
|
3005
|
-
throw new import_graphql9.GraphQLError(String(e.message));
|
|
3006
|
-
}
|
|
3007
|
-
throw e;
|
|
3008
|
-
}
|
|
3009
|
-
};
|
|
3010
|
-
};
|
|
3011
|
-
var createFindFirstResolver = (queryBase, tableInfo, tables, relations) => {
|
|
3012
|
-
return async (parent, args, context, info) => {
|
|
3013
|
-
try {
|
|
3014
|
-
const { where, orderBy } = args;
|
|
3015
|
-
const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
|
|
3016
|
-
deep: true
|
|
3017
|
-
});
|
|
3018
|
-
const allFields = {};
|
|
3019
|
-
if (parsedInfo.fieldsByTypeName) {
|
|
3020
|
-
for (const fields of Object.values(parsedInfo.fieldsByTypeName)) {
|
|
3021
|
-
Object.assign(allFields, fields);
|
|
3022
|
-
}
|
|
3023
|
-
}
|
|
3024
|
-
const result = await queryBase.findFirst({
|
|
3025
|
-
columns: extractSelectedColumns(allFields, tableInfo),
|
|
3026
|
-
orderBy: buildOrderByClause(tableInfo, orderBy),
|
|
3027
|
-
where: buildWhereClause(tableInfo, where),
|
|
3028
|
-
with: await extractRelationsParams2(
|
|
3029
|
-
relations,
|
|
3030
|
-
tables,
|
|
3031
|
-
tableInfo.name,
|
|
3032
|
-
allFields,
|
|
3033
|
-
context
|
|
3034
|
-
)
|
|
3035
|
-
});
|
|
3036
|
-
return result || null;
|
|
3037
|
-
} catch (e) {
|
|
3038
|
-
if (typeof e === "object" && e !== null && "message" in e) {
|
|
3039
|
-
throw new import_graphql9.GraphQLError(String(e.message));
|
|
3040
|
-
}
|
|
3041
|
-
throw e;
|
|
3042
|
-
}
|
|
3043
|
-
};
|
|
3044
|
-
};
|
|
3045
|
-
|
|
3046
|
-
// src/buildSchemaSDL/generator/queries/index.ts
|
|
3047
|
-
var generateQueries = (db, tables, relations) => {
|
|
3048
|
-
const queries = {};
|
|
3049
|
-
for (const [tableName, tableInfo] of Object.entries(tables)) {
|
|
3050
|
-
const queryBase = db.query[tableName];
|
|
3051
|
-
if (!queryBase) {
|
|
3052
|
-
throw new Error(
|
|
3053
|
-
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
3054
|
-
);
|
|
3055
|
-
}
|
|
3056
|
-
queries[`${tableName}FindMany`] = createFindManyResolver(
|
|
3057
|
-
queryBase,
|
|
3058
|
-
tableInfo,
|
|
3059
|
-
tables,
|
|
3060
|
-
relations
|
|
3061
|
-
);
|
|
3062
|
-
queries[`${tableName}FindFirst`] = createFindFirstResolver(
|
|
3063
|
-
queryBase,
|
|
3064
|
-
tableInfo,
|
|
3065
|
-
tables,
|
|
3066
|
-
relations
|
|
3067
|
-
);
|
|
3068
|
-
}
|
|
3069
|
-
return queries;
|
|
3070
|
-
};
|
|
3071
|
-
|
|
3072
|
-
// src/buildSchemaSDL/generator/mutations/resolvers.ts
|
|
3073
|
-
var import_graphql10 = require("graphql");
|
|
3074
|
-
var createInsertManyResolver = (db, queryBase, tableInfo, tables, relations, primaryKeyColumn) => {
|
|
3075
|
-
const queryResolver = createFindManyResolver(
|
|
3076
|
-
queryBase,
|
|
3077
|
-
tableInfo,
|
|
3078
|
-
tables,
|
|
3079
|
-
relations
|
|
3080
|
-
);
|
|
3081
|
-
return async (parent, args, context, info) => {
|
|
3082
|
-
try {
|
|
3083
|
-
const { values } = args;
|
|
3084
|
-
if (!values || values.length === 0) {
|
|
3085
|
-
throw new import_graphql10.GraphQLError("No values provided for insert");
|
|
3086
|
-
}
|
|
3087
|
-
const remappedValues = remapFromGraphQLArrayInput(
|
|
3088
|
-
values,
|
|
3089
|
-
tableInfo.table
|
|
3090
|
-
);
|
|
3091
|
-
const insertedRows = await db.insert(tableInfo.table).values(remappedValues).returning({
|
|
3092
|
-
[primaryKeyColumn.name]: primaryKeyColumn
|
|
3093
|
-
});
|
|
3094
|
-
const insertedIds = insertedRows.map(
|
|
3095
|
-
(row) => row[primaryKeyColumn.name]
|
|
3096
|
-
);
|
|
3097
|
-
const result = await queryResolver(
|
|
3098
|
-
parent,
|
|
3099
|
-
{
|
|
3100
|
-
where: {
|
|
3101
|
-
[primaryKeyColumn.name]: { inArray: insertedIds }
|
|
3102
|
-
}
|
|
3103
|
-
},
|
|
3104
|
-
context,
|
|
3105
|
-
info
|
|
3106
|
-
);
|
|
3107
|
-
return result;
|
|
3108
|
-
} catch (e) {
|
|
3109
|
-
if (typeof e === "object" && e !== null && "message" in e) {
|
|
3110
|
-
throw new import_graphql10.GraphQLError(String(e.message));
|
|
3111
|
-
}
|
|
3112
|
-
throw e;
|
|
3113
|
-
}
|
|
3114
|
-
};
|
|
3115
|
-
};
|
|
3116
|
-
var createUpdateManyResolver = (db, queryBase, tableInfo, tables, relations, primaryKeyColumn) => {
|
|
3117
|
-
const queryResolver = createFindManyResolver(
|
|
3118
|
-
queryBase,
|
|
3119
|
-
tableInfo,
|
|
3120
|
-
tables,
|
|
3121
|
-
relations
|
|
3122
|
-
);
|
|
3123
|
-
return async (parent, args, context, info) => {
|
|
3124
|
-
try {
|
|
3125
|
-
const { where, set } = args;
|
|
3126
|
-
if (!set || Object.keys(set).length === 0) {
|
|
3127
|
-
throw new import_graphql10.GraphQLError("No values provided for update");
|
|
3128
|
-
}
|
|
3129
|
-
const remappedSet = remapFromGraphQLSingleInput(set, tableInfo.table);
|
|
3130
|
-
const whereClause = buildWhereClause(tableInfo, where);
|
|
3131
|
-
let query = db.update(tableInfo.table).set(remappedSet);
|
|
3132
|
-
if (whereClause) {
|
|
3133
|
-
query = query.where(whereClause);
|
|
3134
|
-
}
|
|
3135
|
-
const updatedRows = await query.returning({
|
|
3136
|
-
[primaryKeyColumn.name]: primaryKeyColumn
|
|
3137
|
-
});
|
|
3138
|
-
const updatedIds = updatedRows.map(
|
|
3139
|
-
(row) => row[primaryKeyColumn.name]
|
|
3140
|
-
);
|
|
3141
|
-
const result = await queryResolver(
|
|
3142
|
-
parent,
|
|
3143
|
-
{
|
|
3144
|
-
where: {
|
|
3145
|
-
[primaryKeyColumn.name]: { inArray: updatedIds }
|
|
3146
|
-
}
|
|
3147
|
-
},
|
|
3148
|
-
context,
|
|
3149
|
-
info
|
|
3150
|
-
);
|
|
3151
|
-
return result;
|
|
3152
|
-
} catch (e) {
|
|
3153
|
-
if (typeof e === "object" && e !== null && "message" in e) {
|
|
3154
|
-
throw new import_graphql10.GraphQLError(String(e.message));
|
|
3155
|
-
}
|
|
3156
|
-
throw e;
|
|
3157
|
-
}
|
|
3158
|
-
};
|
|
3159
|
-
};
|
|
3160
|
-
var createDeleteManyResolver = (db, queryBase, tableInfo, tables, relations, primaryKeyColumn) => {
|
|
3161
|
-
const queryResolver = createFindManyResolver(
|
|
3162
|
-
queryBase,
|
|
3163
|
-
tableInfo,
|
|
3164
|
-
tables,
|
|
3165
|
-
relations
|
|
3166
|
-
);
|
|
3167
|
-
return async (parent, args, context, info) => {
|
|
3168
|
-
try {
|
|
3169
|
-
const { where } = args;
|
|
3170
|
-
const whereClause = buildWhereClause(tableInfo, where);
|
|
3171
|
-
let deleteQuery = db.delete(tableInfo.table);
|
|
3172
|
-
if (whereClause) {
|
|
3173
|
-
deleteQuery = deleteQuery.where(whereClause);
|
|
3174
|
-
}
|
|
3175
|
-
const deletedRows = await deleteQuery.returning({
|
|
3176
|
-
[primaryKeyColumn.name]: primaryKeyColumn
|
|
3177
|
-
});
|
|
3178
|
-
const deletedItems = deletedRows.map((row) => ({
|
|
3179
|
-
id: row[primaryKeyColumn.name]
|
|
3180
|
-
}));
|
|
3181
|
-
return {
|
|
3182
|
-
deletedItems,
|
|
3183
|
-
[tableInfo.name + "FindMany"]: queryResolver
|
|
3184
|
-
};
|
|
3185
|
-
} catch (e) {
|
|
3186
|
-
if (typeof e === "object" && e !== null && "message" in e) {
|
|
3187
|
-
throw new import_graphql10.GraphQLError(String(e.message));
|
|
3188
|
-
}
|
|
3189
|
-
throw e;
|
|
3190
|
-
}
|
|
3191
|
-
};
|
|
3192
|
-
};
|
|
3193
|
-
|
|
3194
|
-
// src/buildSchemaSDL/generator/mutations/index.ts
|
|
3195
|
-
var generateMutations = (db, tables, relations) => {
|
|
3196
|
-
const mutations = {};
|
|
3197
|
-
const deleteResultResolvers = {};
|
|
3198
|
-
for (const [tableName, tableInfo] of Object.entries(tables)) {
|
|
3199
|
-
const capitalizedName = capitalize(tableName);
|
|
3200
|
-
const queryBase = db.query[tableName];
|
|
3201
|
-
if (!queryBase) {
|
|
3202
|
-
throw new Error(
|
|
3203
|
-
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
3204
|
-
);
|
|
3205
|
-
}
|
|
3206
|
-
const primaryKeyColumn = Object.values(tableInfo.columns).find(
|
|
3207
|
-
(col) => col.primary || col.name === "id"
|
|
3208
|
-
);
|
|
3209
|
-
if (!primaryKeyColumn) {
|
|
3210
|
-
throw new Error(
|
|
3211
|
-
`Drizzle-GraphQL Error: Table ${tableName} does not have a primary key column`
|
|
3212
|
-
);
|
|
3213
|
-
}
|
|
3214
|
-
mutations[`${tableName}InsertMany`] = createInsertManyResolver(
|
|
3215
|
-
db,
|
|
3216
|
-
queryBase,
|
|
3217
|
-
tableInfo,
|
|
3218
|
-
tables,
|
|
3219
|
-
relations,
|
|
3220
|
-
primaryKeyColumn
|
|
3221
|
-
);
|
|
3222
|
-
mutations[`${tableName}UpdateMany`] = createUpdateManyResolver(
|
|
3223
|
-
db,
|
|
3224
|
-
queryBase,
|
|
3225
|
-
tableInfo,
|
|
3226
|
-
tables,
|
|
3227
|
-
relations,
|
|
3228
|
-
primaryKeyColumn
|
|
3229
|
-
);
|
|
3230
|
-
mutations[`${tableName}DeleteMany`] = createDeleteManyResolver(
|
|
3231
|
-
db,
|
|
3232
|
-
queryBase,
|
|
3233
|
-
tableInfo,
|
|
3234
|
-
tables,
|
|
3235
|
-
relations,
|
|
3236
|
-
primaryKeyColumn
|
|
3237
|
-
);
|
|
3238
|
-
deleteResultResolvers[`${capitalizedName}DeleteResult`] = {
|
|
3239
|
-
[`${tableName}FindMany`]: (parent, args, context, info) => {
|
|
3240
|
-
const resolverFn = parent[`${tableName}FindMany`];
|
|
3241
|
-
if (typeof resolverFn === "function") {
|
|
3242
|
-
return resolverFn(parent, args, context, info);
|
|
3243
|
-
}
|
|
3244
|
-
return [];
|
|
3245
|
-
}
|
|
3246
|
-
};
|
|
3247
|
-
}
|
|
3248
|
-
return { mutations, deleteResultResolvers };
|
|
3249
|
-
};
|
|
3250
|
-
|
|
3251
|
-
// src/buildSchemaSDL/index.ts
|
|
3252
|
-
var buildSchemaSDL = (db, config) => {
|
|
3253
|
-
const schema = db._.fullSchema;
|
|
3254
|
-
if (!schema) {
|
|
3255
|
-
throw new Error(
|
|
3256
|
-
"Drizzle-GraphQL Error: Schema not found in drizzle instance. Make sure you're using drizzle-orm v0.30.9 or above and schema is passed to drizzle constructor!"
|
|
3257
|
-
);
|
|
139
|
+
return exports2;
|
|
140
|
+
}
|
|
141
|
+
function hasExportVariables(args) {
|
|
142
|
+
if (isExportVariable(args)) {
|
|
143
|
+
return true;
|
|
3258
144
|
}
|
|
3259
|
-
if (
|
|
3260
|
-
|
|
3261
|
-
"Drizzle-GraphQL Error: buildSchemaSDL currently only supports SQLite databases"
|
|
3262
|
-
);
|
|
145
|
+
if (Array.isArray(args)) {
|
|
146
|
+
return args.some((item) => hasExportVariables(item));
|
|
3263
147
|
}
|
|
3264
|
-
|
|
3265
|
-
|
|
3266
|
-
|
|
3267
|
-
|
|
3268
|
-
|
|
3269
|
-
const typeDefs = typeDefsArray.join("\n\n");
|
|
3270
|
-
const queries = generateQueries(db, tables, relations);
|
|
3271
|
-
const { mutations, deleteResultResolvers } = generateMutations(
|
|
3272
|
-
db,
|
|
3273
|
-
tables,
|
|
3274
|
-
relations
|
|
3275
|
-
);
|
|
3276
|
-
return {
|
|
3277
|
-
typeDefs,
|
|
3278
|
-
resolvers: {
|
|
3279
|
-
Query: queries,
|
|
3280
|
-
Mutation: mutations,
|
|
3281
|
-
...deleteResultResolvers
|
|
148
|
+
if (typeof args === "object" && args !== null) {
|
|
149
|
+
for (const value of Object.values(args)) {
|
|
150
|
+
if (hasExportVariables(value)) {
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
3282
153
|
}
|
|
3283
|
-
}
|
|
3284
|
-
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
function processExports(result, selectionSet, exportStore, isArrayItem = false) {
|
|
158
|
+
if (!result || !selectionSet)
|
|
159
|
+
return;
|
|
160
|
+
for (const selection of selectionSet.selections) {
|
|
161
|
+
if (selection.kind !== "Field")
|
|
162
|
+
continue;
|
|
163
|
+
const resultKey = selection.alias?.value ?? selection.name.value;
|
|
164
|
+
if (!(resultKey in result))
|
|
165
|
+
continue;
|
|
166
|
+
const value = result[resultKey];
|
|
167
|
+
const exportName = getExportDirective(selection);
|
|
168
|
+
if (exportName) {
|
|
169
|
+
if (isArrayItem) {
|
|
170
|
+
exportStore.accumulate(exportName, value);
|
|
171
|
+
} else {
|
|
172
|
+
exportStore.set(exportName, value);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (selection.selectionSet && value !== null && value !== void 0) {
|
|
176
|
+
if (Array.isArray(value)) {
|
|
177
|
+
value.forEach((item) => {
|
|
178
|
+
if (item && typeof item === "object") {
|
|
179
|
+
processExports(item, selection.selectionSet, exportStore, true);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
} else if (typeof value === "object") {
|
|
183
|
+
processExports(value, selection.selectionSet, exportStore, isArrayItem);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
var init_utils = __esm({
|
|
189
|
+
"src/export-tool/utils.ts"() {
|
|
190
|
+
"use strict";
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// src/index.ts
|
|
195
|
+
var src_exports = {};
|
|
196
|
+
__export(src_exports, {
|
|
197
|
+
buildSchemaSDLWithDataLoader: () => buildSchemaSDL,
|
|
198
|
+
cleanupDataLoaderContext: () => cleanupDataLoaderContext,
|
|
199
|
+
createDataLoaderContext: () => createDataLoaderContext,
|
|
200
|
+
createExportMiddleware: () => createExportMiddleware,
|
|
201
|
+
exportDirectiveTypeDefs: () => exportDirectiveTypeDefs,
|
|
202
|
+
makeScalarAcceptExports: () => makeScalarAcceptExports,
|
|
203
|
+
setCustomGraphQL: () => setCustomGraphQL,
|
|
204
|
+
setCustomGraphQLTypes: () => setCustomGraphQLTypes,
|
|
205
|
+
useDataLoaderCleanup: () => useDataLoaderCleanup,
|
|
206
|
+
useDataLoaderCleanupOnly: () => useDataLoaderCleanupOnly,
|
|
207
|
+
useDataLoaderContext: () => useDataLoaderContext
|
|
208
|
+
});
|
|
209
|
+
module.exports = __toCommonJS(src_exports);
|
|
3285
210
|
|
|
3286
211
|
// src/build-schema-sdl-with-dl/index.ts
|
|
3287
|
-
var
|
|
3288
|
-
var
|
|
212
|
+
var import_drizzle_orm8 = require("drizzle-orm");
|
|
213
|
+
var import_sqlite_core2 = require("drizzle-orm/sqlite-core");
|
|
3289
214
|
|
|
3290
215
|
// src/build-schema-sdl-with-dl/generator/schema/generation.ts
|
|
3291
|
-
var
|
|
3292
|
-
var
|
|
216
|
+
var import_drizzle_orm = require("drizzle-orm");
|
|
217
|
+
var generateTypes = (db, schema) => {
|
|
3293
218
|
const tables = {};
|
|
3294
219
|
const schemaEntries = Object.entries(schema);
|
|
3295
220
|
const tableEntries = [];
|
|
@@ -3297,7 +222,7 @@ var generateTypes2 = (db, schema) => {
|
|
|
3297
222
|
if (value && typeof value === "object" && "getSQL" in value) {
|
|
3298
223
|
const table = value;
|
|
3299
224
|
const tableName = key;
|
|
3300
|
-
const columns = (0,
|
|
225
|
+
const columns = (0, import_drizzle_orm.getTableColumns)(table);
|
|
3301
226
|
tables[tableName] = {
|
|
3302
227
|
name: tableName,
|
|
3303
228
|
table,
|
|
@@ -3306,14 +231,14 @@ var generateTypes2 = (db, schema) => {
|
|
|
3306
231
|
tableEntries.push([tableName, table]);
|
|
3307
232
|
}
|
|
3308
233
|
}
|
|
3309
|
-
const rawRelations = schemaEntries.filter(([key, value]) => (0,
|
|
234
|
+
const rawRelations = schemaEntries.filter(([key, value]) => (0, import_drizzle_orm.is)(value, import_drizzle_orm.Relations)).map(([key, value]) => [
|
|
3310
235
|
tableEntries.find(
|
|
3311
236
|
([tableName, tableValue]) => tableValue === value.table
|
|
3312
237
|
)[0],
|
|
3313
238
|
value
|
|
3314
239
|
]).map(([tableName, relValue]) => [
|
|
3315
240
|
tableName,
|
|
3316
|
-
relValue.config((0,
|
|
241
|
+
relValue.config((0, import_drizzle_orm.createTableRelationsHelpers)(tables[tableName].table))
|
|
3317
242
|
]);
|
|
3318
243
|
const namedRelations = Object.fromEntries(
|
|
3319
244
|
rawRelations.map(([relName, config]) => {
|
|
@@ -3335,23 +260,28 @@ var generateTypes2 = (db, schema) => {
|
|
|
3335
260
|
};
|
|
3336
261
|
|
|
3337
262
|
// src/build-schema-sdl-with-dl/generator/schema/type-defs.ts
|
|
3338
|
-
var
|
|
3339
|
-
var
|
|
3340
|
-
var
|
|
3341
|
-
var
|
|
3342
|
-
|
|
3343
|
-
|
|
3344
|
-
var
|
|
3345
|
-
|
|
3346
|
-
|
|
3347
|
-
var
|
|
263
|
+
var import_drizzle_orm2 = require("drizzle-orm");
|
|
264
|
+
var import_mysql_core = require("drizzle-orm/mysql-core");
|
|
265
|
+
var import_pg_core = require("drizzle-orm/pg-core");
|
|
266
|
+
var import_sqlite_core = require("drizzle-orm/sqlite-core");
|
|
267
|
+
|
|
268
|
+
// src/util/case-ops/index.ts
|
|
269
|
+
var capitalize = (input) => input.length ? `${input[0].toLocaleUpperCase()}${input.length > 1 ? input.slice(1, input.length) : ""}` : input;
|
|
270
|
+
|
|
271
|
+
// src/build-schema-sdl-with-dl/generator/schema/type-defs.ts
|
|
272
|
+
var allowedNameChars = /^[a-zA-Z0-9_]+$/;
|
|
273
|
+
var customScalars = /* @__PURE__ */ new Set();
|
|
274
|
+
var enumDefinitions = /* @__PURE__ */ new Map();
|
|
275
|
+
var requiredFieldFilters = /* @__PURE__ */ new Set();
|
|
276
|
+
var foreignKeyTypes = /* @__PURE__ */ new Map();
|
|
277
|
+
var columnToSDL = (column, columnName, tableName, forceNullable = false, isPrimaryKey = false) => {
|
|
3348
278
|
let baseType;
|
|
3349
279
|
if (column.customGraphqlType) {
|
|
3350
280
|
baseType = column.customGraphqlType;
|
|
3351
281
|
} else if (isPrimaryKey) {
|
|
3352
282
|
baseType = "ID";
|
|
3353
283
|
} else {
|
|
3354
|
-
const foreignKeyType =
|
|
284
|
+
const foreignKeyType = foreignKeyTypes.get(`${tableName}.${columnName}`);
|
|
3355
285
|
if (foreignKeyType) {
|
|
3356
286
|
baseType = foreignKeyType;
|
|
3357
287
|
} else {
|
|
@@ -3362,15 +292,15 @@ var columnToSDL2 = (column, columnName, tableName, forceNullable = false, isPrim
|
|
|
3362
292
|
case "json":
|
|
3363
293
|
if (column.columnType === "PgGeometryObject") {
|
|
3364
294
|
baseType = "PgGeometryObject";
|
|
3365
|
-
|
|
295
|
+
customScalars.add("PgGeometryObject");
|
|
3366
296
|
} else {
|
|
3367
297
|
baseType = "JSON";
|
|
3368
|
-
|
|
298
|
+
customScalars.add("JSON");
|
|
3369
299
|
}
|
|
3370
300
|
break;
|
|
3371
301
|
case "date":
|
|
3372
302
|
baseType = "Date";
|
|
3373
|
-
|
|
303
|
+
customScalars.add("Date");
|
|
3374
304
|
break;
|
|
3375
305
|
case "string":
|
|
3376
306
|
if (column.enumValues?.length) {
|
|
@@ -3378,11 +308,11 @@ var columnToSDL2 = (column, columnName, tableName, forceNullable = false, isPrim
|
|
|
3378
308
|
columnName
|
|
3379
309
|
)}Enum`;
|
|
3380
310
|
baseType = enumName;
|
|
3381
|
-
if (!
|
|
3382
|
-
|
|
311
|
+
if (!enumDefinitions.has(enumName)) {
|
|
312
|
+
enumDefinitions.set(enumName, {
|
|
3383
313
|
name: enumName,
|
|
3384
314
|
values: column.enumValues.map(
|
|
3385
|
-
(e, index) =>
|
|
315
|
+
(e, index) => allowedNameChars.test(e) ? e : `Option${index}`
|
|
3386
316
|
)
|
|
3387
317
|
});
|
|
3388
318
|
}
|
|
@@ -3392,10 +322,10 @@ var columnToSDL2 = (column, columnName, tableName, forceNullable = false, isPrim
|
|
|
3392
322
|
break;
|
|
3393
323
|
case "bigint":
|
|
3394
324
|
baseType = "BigInt";
|
|
3395
|
-
|
|
325
|
+
customScalars.add("BigInt");
|
|
3396
326
|
break;
|
|
3397
327
|
case "number":
|
|
3398
|
-
if ((0,
|
|
328
|
+
if ((0, import_drizzle_orm2.is)(column, import_pg_core.PgInteger) || (0, import_drizzle_orm2.is)(column, import_pg_core.PgSerial) || (0, import_drizzle_orm2.is)(column, import_mysql_core.MySqlInt) || (0, import_drizzle_orm2.is)(column, import_mysql_core.MySqlSerial) || (0, import_drizzle_orm2.is)(column, import_sqlite_core.SQLiteInteger)) {
|
|
3399
329
|
baseType = "Int";
|
|
3400
330
|
} else {
|
|
3401
331
|
baseType = "Float";
|
|
@@ -3414,20 +344,20 @@ var columnToSDL2 = (column, columnName, tableName, forceNullable = false, isPrim
|
|
|
3414
344
|
columnName
|
|
3415
345
|
)}Array`;
|
|
3416
346
|
baseType = scalarName;
|
|
3417
|
-
|
|
347
|
+
customScalars.add(scalarName);
|
|
3418
348
|
}
|
|
3419
349
|
break;
|
|
3420
350
|
case "custom":
|
|
3421
351
|
default:
|
|
3422
352
|
if (column.columnType) {
|
|
3423
353
|
baseType = column.columnType;
|
|
3424
|
-
|
|
354
|
+
customScalars.add(column.columnType);
|
|
3425
355
|
} else {
|
|
3426
356
|
const customScalarName = `${capitalize(tableName)}${capitalize(
|
|
3427
357
|
columnName
|
|
3428
358
|
)}`;
|
|
3429
359
|
baseType = customScalarName;
|
|
3430
|
-
|
|
360
|
+
customScalars.add(customScalarName);
|
|
3431
361
|
}
|
|
3432
362
|
break;
|
|
3433
363
|
}
|
|
@@ -3438,19 +368,19 @@ var columnToSDL2 = (column, columnName, tableName, forceNullable = false, isPrim
|
|
|
3438
368
|
}
|
|
3439
369
|
return baseType;
|
|
3440
370
|
};
|
|
3441
|
-
var
|
|
371
|
+
var generateTypeDefs = (tables, relations) => {
|
|
3442
372
|
const typeDefs = [];
|
|
3443
|
-
|
|
3444
|
-
|
|
3445
|
-
|
|
3446
|
-
|
|
373
|
+
customScalars.clear();
|
|
374
|
+
enumDefinitions.clear();
|
|
375
|
+
requiredFieldFilters.clear();
|
|
376
|
+
foreignKeyTypes.clear();
|
|
3447
377
|
for (const [tableName, tableRelations] of Object.entries(relations)) {
|
|
3448
378
|
const tableInfo = tables[tableName];
|
|
3449
379
|
if (!tableInfo)
|
|
3450
380
|
continue;
|
|
3451
381
|
for (const [relationName, relationInfo] of Object.entries(tableRelations)) {
|
|
3452
382
|
const relation = relationInfo.relation;
|
|
3453
|
-
if (!(0,
|
|
383
|
+
if (!(0, import_drizzle_orm2.is)(relation, import_drizzle_orm2.One))
|
|
3454
384
|
continue;
|
|
3455
385
|
const config = relation.config;
|
|
3456
386
|
if (!config?.fields || !config?.references)
|
|
@@ -3484,12 +414,12 @@ var generateTypeDefs2 = (tables, relations) => {
|
|
|
3484
414
|
const referencedIsPrimaryKey = referencedColumn.primary || false;
|
|
3485
415
|
if (!foreignKeyHasCustomType) {
|
|
3486
416
|
if (referencedCustomType) {
|
|
3487
|
-
|
|
417
|
+
foreignKeyTypes.set(
|
|
3488
418
|
`${tableName}.${foreignKeyPropertyName}`,
|
|
3489
419
|
referencedCustomType
|
|
3490
420
|
);
|
|
3491
421
|
} else if (referencedIsPrimaryKey) {
|
|
3492
|
-
|
|
422
|
+
foreignKeyTypes.set(`${tableName}.${foreignKeyPropertyName}`, "ID");
|
|
3493
423
|
}
|
|
3494
424
|
}
|
|
3495
425
|
}
|
|
@@ -3500,7 +430,7 @@ var generateTypeDefs2 = (tables, relations) => {
|
|
|
3500
430
|
const fields = [];
|
|
3501
431
|
for (const [columnName, column] of Object.entries(tableInfo.columns)) {
|
|
3502
432
|
const isPrimaryKey = column.primary || false;
|
|
3503
|
-
const typeStr =
|
|
433
|
+
const typeStr = columnToSDL(
|
|
3504
434
|
column,
|
|
3505
435
|
columnName,
|
|
3506
436
|
tableName,
|
|
@@ -3518,7 +448,7 @@ var generateTypeDefs2 = (tables, relations) => {
|
|
|
3518
448
|
for (const [relationName, relationInfo] of Object.entries(
|
|
3519
449
|
tableRelations
|
|
3520
450
|
)) {
|
|
3521
|
-
const isOne = (0,
|
|
451
|
+
const isOne = (0, import_drizzle_orm2.is)(relationInfo.relation, import_drizzle_orm2.One);
|
|
3522
452
|
const targetTableName = relationInfo.targetTableName;
|
|
3523
453
|
const targetTypeName = capitalize(targetTableName);
|
|
3524
454
|
if (isOne) {
|
|
@@ -3538,7 +468,7 @@ ${fields.join("\n")}
|
|
|
3538
468
|
const insertFields = [];
|
|
3539
469
|
for (const [columnName, column] of Object.entries(tableInfo.columns)) {
|
|
3540
470
|
const isPrimaryKey = column.primary || false;
|
|
3541
|
-
const typeStr =
|
|
471
|
+
const typeStr = columnToSDL(
|
|
3542
472
|
column,
|
|
3543
473
|
columnName,
|
|
3544
474
|
tableName,
|
|
@@ -3547,13 +477,13 @@ ${fields.join("\n")}
|
|
|
3547
477
|
);
|
|
3548
478
|
const hasDefault = column.hasDefault || column.default !== void 0;
|
|
3549
479
|
const isAutoIncrement = column.autoIncrement || column.generatedAlwaysAs;
|
|
3550
|
-
const
|
|
3551
|
-
const shouldBeOptional = hasDefault || isAutoIncrement || isPrimaryKey && !
|
|
480
|
+
const isNotNull2 = column.notNull;
|
|
481
|
+
const shouldBeOptional = hasDefault || isAutoIncrement || isPrimaryKey && !isNotNull2;
|
|
3552
482
|
let insertFieldType;
|
|
3553
483
|
if (shouldBeOptional) {
|
|
3554
484
|
insertFieldType = typeStr.endsWith("!") ? typeStr.slice(0, -1) : typeStr;
|
|
3555
485
|
} else {
|
|
3556
|
-
insertFieldType =
|
|
486
|
+
insertFieldType = isNotNull2 && !typeStr.endsWith("!") ? `${typeStr}!` : typeStr;
|
|
3557
487
|
}
|
|
3558
488
|
insertFields.push(` ${columnName}: ${insertFieldType}`);
|
|
3559
489
|
}
|
|
@@ -3567,7 +497,7 @@ ${insertFields.join("\n")}
|
|
|
3567
497
|
const updateFields = [];
|
|
3568
498
|
for (const [columnName, column] of Object.entries(tableInfo.columns)) {
|
|
3569
499
|
const isPrimaryKey = column.primary || false;
|
|
3570
|
-
const typeStr =
|
|
500
|
+
const typeStr = columnToSDL(
|
|
3571
501
|
column,
|
|
3572
502
|
columnName,
|
|
3573
503
|
tableName,
|
|
@@ -3584,7 +514,7 @@ ${updateFields.join("\n")}
|
|
|
3584
514
|
const whereFields = [];
|
|
3585
515
|
for (const [columnName, column] of Object.entries(tableInfo.columns)) {
|
|
3586
516
|
const isPrimaryKey = column.primary || false;
|
|
3587
|
-
const typeStr =
|
|
517
|
+
const typeStr = columnToSDL(
|
|
3588
518
|
column,
|
|
3589
519
|
columnName,
|
|
3590
520
|
tableName,
|
|
@@ -3593,7 +523,7 @@ ${updateFields.join("\n")}
|
|
|
3593
523
|
);
|
|
3594
524
|
const normalizedType = typeStr.replace(/[^a-zA-Z0-9]/g, "");
|
|
3595
525
|
const filterTypeName = `${normalizedType}FieldFilter`;
|
|
3596
|
-
|
|
526
|
+
requiredFieldFilters.add(
|
|
3597
527
|
JSON.stringify({ normalizedType, baseType: typeStr })
|
|
3598
528
|
);
|
|
3599
529
|
whereFields.push(` ${columnName}: ${filterTypeName}`);
|
|
@@ -3615,14 +545,14 @@ ${orderByFields.join("\n")}
|
|
|
3615
545
|
}`);
|
|
3616
546
|
}
|
|
3617
547
|
const allDefs = [];
|
|
3618
|
-
|
|
3619
|
-
if (
|
|
3620
|
-
for (const scalarName of Array.from(
|
|
548
|
+
customScalars.add("JSON");
|
|
549
|
+
if (customScalars.size > 0) {
|
|
550
|
+
for (const scalarName of Array.from(customScalars).sort()) {
|
|
3621
551
|
allDefs.push(`scalar ${scalarName}`);
|
|
3622
552
|
}
|
|
3623
553
|
}
|
|
3624
|
-
if (
|
|
3625
|
-
for (const enumDef of Array.from(
|
|
554
|
+
if (enumDefinitions.size > 0) {
|
|
555
|
+
for (const enumDef of Array.from(enumDefinitions.values())) {
|
|
3626
556
|
const valueStrings = enumDef.values.map((v) => ` ${v}`);
|
|
3627
557
|
allDefs.push(`enum ${enumDef.name} {
|
|
3628
558
|
${valueStrings.join("\n")}
|
|
@@ -3641,7 +571,7 @@ ${valueStrings.join("\n")}
|
|
|
3641
571
|
priority: Int!
|
|
3642
572
|
}`);
|
|
3643
573
|
const filterTypesAdded = /* @__PURE__ */ new Set();
|
|
3644
|
-
for (const filterInfoJson of Array.from(
|
|
574
|
+
for (const filterInfoJson of Array.from(requiredFieldFilters)) {
|
|
3645
575
|
const { normalizedType, baseType } = JSON.parse(filterInfoJson);
|
|
3646
576
|
const filterTypeName = `${normalizedType}FieldFilter`;
|
|
3647
577
|
if (filterTypesAdded.has(filterTypeName))
|
|
@@ -3670,7 +600,7 @@ ${filterFields.join("\n")}
|
|
|
3670
600
|
allDefs.push(...typeDefs);
|
|
3671
601
|
return allDefs.join("\n\n");
|
|
3672
602
|
};
|
|
3673
|
-
var
|
|
603
|
+
var generateQueryTypeDefs = (tables) => {
|
|
3674
604
|
const queryFields = [];
|
|
3675
605
|
for (const tableName of Object.keys(tables)) {
|
|
3676
606
|
const typeName = capitalize(tableName);
|
|
@@ -3685,7 +615,7 @@ var generateQueryTypeDefs2 = (tables) => {
|
|
|
3685
615
|
${queryFields.join("\n")}
|
|
3686
616
|
}`;
|
|
3687
617
|
};
|
|
3688
|
-
var
|
|
618
|
+
var generateMutationTypeDefs = (tables) => {
|
|
3689
619
|
const mutationFields = [];
|
|
3690
620
|
for (const tableName of Object.keys(tables)) {
|
|
3691
621
|
const typeName = capitalize(tableName);
|
|
@@ -3705,33 +635,110 @@ ${mutationFields.join("\n")}
|
|
|
3705
635
|
};
|
|
3706
636
|
|
|
3707
637
|
// src/build-schema-sdl-with-dl/generator/queries/dataloader-resolvers.ts
|
|
3708
|
-
var
|
|
3709
|
-
var
|
|
3710
|
-
var
|
|
638
|
+
var import_graphql3 = require("graphql");
|
|
639
|
+
var import_graphql_parse_resolve_info = require("graphql-parse-resolve-info");
|
|
640
|
+
var import_drizzle_orm7 = require("drizzle-orm");
|
|
3711
641
|
|
|
3712
642
|
// src/build-schema-sdl-with-dl/generator/utils/selection.ts
|
|
3713
|
-
var
|
|
643
|
+
var import_drizzle_orm5 = require("drizzle-orm");
|
|
644
|
+
|
|
645
|
+
// src/build-schema-sdl-with-dl/generator/utils/filters.ts
|
|
646
|
+
var import_drizzle_orm4 = require("drizzle-orm");
|
|
647
|
+
var import_graphql2 = require("graphql");
|
|
648
|
+
|
|
649
|
+
// src/util/data-mappers/index.ts
|
|
650
|
+
var import_drizzle_orm3 = require("drizzle-orm");
|
|
651
|
+
var import_graphql = require("graphql");
|
|
652
|
+
var remapFromGraphQLCore = (value, column, columnName) => {
|
|
653
|
+
switch (column.dataType) {
|
|
654
|
+
case "date": {
|
|
655
|
+
const formatted = new Date(value);
|
|
656
|
+
if (Number.isNaN(formatted.getTime()))
|
|
657
|
+
throw new import_graphql.GraphQLError(`Field '${columnName}' is not a valid date!`);
|
|
658
|
+
return formatted;
|
|
659
|
+
}
|
|
660
|
+
case "buffer": {
|
|
661
|
+
if (!Array.isArray(value)) {
|
|
662
|
+
throw new import_graphql.GraphQLError(`Field '${columnName}' is not an array!`);
|
|
663
|
+
}
|
|
664
|
+
return Buffer.from(value);
|
|
665
|
+
}
|
|
666
|
+
case "json": {
|
|
667
|
+
if (column.columnType === "PgGeometryObject")
|
|
668
|
+
return value;
|
|
669
|
+
try {
|
|
670
|
+
return JSON.parse(value);
|
|
671
|
+
} catch (e) {
|
|
672
|
+
throw new import_graphql.GraphQLError(
|
|
673
|
+
`Invalid JSON in field '${columnName}':
|
|
674
|
+
${e instanceof Error ? e.message : "Unknown error"}`
|
|
675
|
+
);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
case "array": {
|
|
679
|
+
if (!Array.isArray(value)) {
|
|
680
|
+
throw new import_graphql.GraphQLError(`Field '${columnName}' is not an array!`);
|
|
681
|
+
}
|
|
682
|
+
if (column.columnType === "PgGeometry" && value.length !== 2) {
|
|
683
|
+
throw new import_graphql.GraphQLError(
|
|
684
|
+
`Invalid float tuple in field '${columnName}': expected array with length of 2, received ${value.length}`
|
|
685
|
+
);
|
|
686
|
+
}
|
|
687
|
+
return value;
|
|
688
|
+
}
|
|
689
|
+
case "bigint": {
|
|
690
|
+
try {
|
|
691
|
+
return BigInt(value);
|
|
692
|
+
} catch (error) {
|
|
693
|
+
throw new import_graphql.GraphQLError(`Field '${columnName}' is not a BigInt!`);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
default: {
|
|
697
|
+
return value;
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
};
|
|
701
|
+
var remapFromGraphQLSingleInput = (queryInput, table) => {
|
|
702
|
+
for (const [key, value] of Object.entries(queryInput)) {
|
|
703
|
+
if (value === void 0) {
|
|
704
|
+
delete queryInput[key];
|
|
705
|
+
} else {
|
|
706
|
+
const column = (0, import_drizzle_orm3.getTableColumns)(table)[key];
|
|
707
|
+
if (!column)
|
|
708
|
+
throw new import_graphql.GraphQLError(`Unknown column: ${key}`);
|
|
709
|
+
if (value === null && column.notNull) {
|
|
710
|
+
delete queryInput[key];
|
|
711
|
+
continue;
|
|
712
|
+
}
|
|
713
|
+
queryInput[key] = remapFromGraphQLCore(value, column, key);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
return queryInput;
|
|
717
|
+
};
|
|
718
|
+
var remapFromGraphQLArrayInput = (queryInput, table) => {
|
|
719
|
+
for (const entry of queryInput)
|
|
720
|
+
remapFromGraphQLSingleInput(entry, table);
|
|
721
|
+
return queryInput;
|
|
722
|
+
};
|
|
3714
723
|
|
|
3715
724
|
// src/build-schema-sdl-with-dl/generator/utils/filters.ts
|
|
3716
|
-
var
|
|
3717
|
-
var import_graphql11 = require("graphql");
|
|
3718
|
-
var extractFiltersColumn3 = (column, columnName, operators) => {
|
|
725
|
+
var extractFiltersColumn = (column, columnName, operators) => {
|
|
3719
726
|
const entries = Object.entries(operators);
|
|
3720
727
|
if (!entries.length)
|
|
3721
728
|
return void 0;
|
|
3722
729
|
if (operators.OR && operators.OR.length > 0) {
|
|
3723
730
|
if (entries.length > 1) {
|
|
3724
|
-
throw new
|
|
731
|
+
throw new import_graphql2.GraphQLError(
|
|
3725
732
|
`WHERE ${columnName}: Cannot specify both fields and 'OR' in column operators!`
|
|
3726
733
|
);
|
|
3727
734
|
}
|
|
3728
735
|
const variants2 = [];
|
|
3729
736
|
for (const variant of operators.OR) {
|
|
3730
|
-
const extracted =
|
|
737
|
+
const extracted = extractFiltersColumn(column, columnName, variant);
|
|
3731
738
|
if (extracted)
|
|
3732
739
|
variants2.push(extracted);
|
|
3733
740
|
}
|
|
3734
|
-
return variants2.length ? variants2.length > 1 ? (0,
|
|
741
|
+
return variants2.length ? variants2.length > 1 ? (0, import_drizzle_orm4.or)(...variants2) : variants2[0] : void 0;
|
|
3735
742
|
}
|
|
3736
743
|
const variants = [];
|
|
3737
744
|
for (const [operatorName, operatorValue] of entries) {
|
|
@@ -3749,7 +756,7 @@ var extractFiltersColumn3 = (column, columnName, operators) => {
|
|
|
3749
756
|
column,
|
|
3750
757
|
columnName
|
|
3751
758
|
);
|
|
3752
|
-
const opMap = { eq:
|
|
759
|
+
const opMap = { eq: import_drizzle_orm4.eq, ne: import_drizzle_orm4.ne, gt: import_drizzle_orm4.gt, gte: import_drizzle_orm4.gte, lt: import_drizzle_orm4.lt, lte: import_drizzle_orm4.lte };
|
|
3753
760
|
variants.push(opMap[operatorName](column, singleValue));
|
|
3754
761
|
break;
|
|
3755
762
|
}
|
|
@@ -3757,51 +764,51 @@ var extractFiltersColumn3 = (column, columnName, operators) => {
|
|
|
3757
764
|
case "notLike":
|
|
3758
765
|
case "ilike":
|
|
3759
766
|
case "notIlike": {
|
|
3760
|
-
const opMap = { like:
|
|
767
|
+
const opMap = { like: import_drizzle_orm4.like, notLike: import_drizzle_orm4.notLike, ilike: import_drizzle_orm4.ilike, notIlike: import_drizzle_orm4.notIlike };
|
|
3761
768
|
variants.push(opMap[operatorName](column, operatorValue));
|
|
3762
769
|
break;
|
|
3763
770
|
}
|
|
3764
771
|
case "inArray":
|
|
3765
772
|
case "notInArray": {
|
|
3766
773
|
if (!operatorValue.length) {
|
|
3767
|
-
throw new
|
|
774
|
+
throw new import_graphql2.GraphQLError(
|
|
3768
775
|
`WHERE ${columnName}: Unable to use operator ${operatorName} with an empty array!`
|
|
3769
776
|
);
|
|
3770
777
|
}
|
|
3771
778
|
const arrayValue = operatorValue.map(
|
|
3772
779
|
(val) => remapFromGraphQLCore(val, column, columnName)
|
|
3773
780
|
);
|
|
3774
|
-
const opMap = { inArray:
|
|
781
|
+
const opMap = { inArray: import_drizzle_orm4.inArray, notInArray: import_drizzle_orm4.notInArray };
|
|
3775
782
|
variants.push(opMap[operatorName](column, arrayValue));
|
|
3776
783
|
break;
|
|
3777
784
|
}
|
|
3778
785
|
case "isNull":
|
|
3779
786
|
case "isNotNull": {
|
|
3780
|
-
const opMap = { isNull:
|
|
787
|
+
const opMap = { isNull: import_drizzle_orm4.isNull, isNotNull: import_drizzle_orm4.isNotNull };
|
|
3781
788
|
variants.push(opMap[operatorName](column));
|
|
3782
789
|
break;
|
|
3783
790
|
}
|
|
3784
791
|
}
|
|
3785
792
|
}
|
|
3786
|
-
return variants.length ? variants.length > 1 ? (0,
|
|
793
|
+
return variants.length ? variants.length > 1 ? (0, import_drizzle_orm4.and)(...variants) : variants[0] : void 0;
|
|
3787
794
|
};
|
|
3788
|
-
var
|
|
795
|
+
var buildWhereClause = (tableInfo, where) => {
|
|
3789
796
|
if (!where || Object.keys(where).length === 0) {
|
|
3790
797
|
return void 0;
|
|
3791
798
|
}
|
|
3792
799
|
if (where.OR && where.OR.length > 0) {
|
|
3793
800
|
if (Object.keys(where).length > 1) {
|
|
3794
|
-
throw new
|
|
801
|
+
throw new import_graphql2.GraphQLError(
|
|
3795
802
|
`WHERE ${tableInfo.name}: Cannot specify both fields and 'OR' in table filters!`
|
|
3796
803
|
);
|
|
3797
804
|
}
|
|
3798
805
|
const variants = [];
|
|
3799
806
|
for (const variant of where.OR) {
|
|
3800
|
-
const extracted =
|
|
807
|
+
const extracted = buildWhereClause(tableInfo, variant);
|
|
3801
808
|
if (extracted)
|
|
3802
809
|
variants.push(extracted);
|
|
3803
810
|
}
|
|
3804
|
-
return variants.length ? variants.length > 1 ? (0,
|
|
811
|
+
return variants.length ? variants.length > 1 ? (0, import_drizzle_orm4.or)(...variants) : variants[0] : void 0;
|
|
3805
812
|
}
|
|
3806
813
|
const conditions = [];
|
|
3807
814
|
for (const [columnName, operators] of Object.entries(where)) {
|
|
@@ -3812,7 +819,7 @@ var buildWhereClause2 = (tableInfo, where) => {
|
|
|
3812
819
|
const column = tableInfo.columns[columnName];
|
|
3813
820
|
if (!column)
|
|
3814
821
|
continue;
|
|
3815
|
-
const extracted =
|
|
822
|
+
const extracted = extractFiltersColumn(
|
|
3816
823
|
column,
|
|
3817
824
|
columnName,
|
|
3818
825
|
operators
|
|
@@ -3824,11 +831,11 @@ var buildWhereClause2 = (tableInfo, where) => {
|
|
|
3824
831
|
return void 0;
|
|
3825
832
|
if (conditions.length === 1)
|
|
3826
833
|
return conditions[0];
|
|
3827
|
-
return (0,
|
|
834
|
+
return (0, import_drizzle_orm4.and)(...conditions);
|
|
3828
835
|
};
|
|
3829
836
|
|
|
3830
837
|
// src/build-schema-sdl-with-dl/generator/utils/selection.ts
|
|
3831
|
-
var
|
|
838
|
+
var buildOrderByClause = (tableInfo, orderBy) => {
|
|
3832
839
|
if (!orderBy || Object.keys(orderBy).length === 0) {
|
|
3833
840
|
return void 0;
|
|
3834
841
|
}
|
|
@@ -3842,7 +849,7 @@ var buildOrderByClause2 = (tableInfo, orderBy) => {
|
|
|
3842
849
|
const column = tableInfo.columns[entry.columnName];
|
|
3843
850
|
if (column) {
|
|
3844
851
|
orderClauses.push(
|
|
3845
|
-
entry.direction === "desc" ? (0,
|
|
852
|
+
entry.direction === "desc" ? (0, import_drizzle_orm5.desc)(column) : (0, import_drizzle_orm5.asc)(column)
|
|
3846
853
|
);
|
|
3847
854
|
}
|
|
3848
855
|
}
|
|
@@ -3851,12 +858,14 @@ var buildOrderByClause2 = (tableInfo, orderBy) => {
|
|
|
3851
858
|
|
|
3852
859
|
// src/build-schema-sdl-with-dl/generator/utils/dataloader.ts
|
|
3853
860
|
var import_dataloader = __toESM(require("dataloader"), 1);
|
|
3854
|
-
var
|
|
861
|
+
var import_drizzle_orm6 = require("drizzle-orm");
|
|
3855
862
|
var RelationDataLoader = class {
|
|
3856
|
-
constructor(queryBase, tableInfo, relations) {
|
|
863
|
+
constructor(queryBase, tableInfo, relations, context, debugConfig) {
|
|
3857
864
|
this.queryBase = queryBase;
|
|
3858
865
|
this.tableInfo = tableInfo;
|
|
3859
866
|
this.relations = relations;
|
|
867
|
+
this.context = context;
|
|
868
|
+
this.debugConfig = debugConfig;
|
|
3860
869
|
}
|
|
3861
870
|
loaders = /* @__PURE__ */ new Map();
|
|
3862
871
|
createLoaderKey(key) {
|
|
@@ -3897,11 +906,31 @@ var RelationDataLoader = class {
|
|
|
3897
906
|
}
|
|
3898
907
|
const allParentIds = groupKeys.flatMap((key) => key.parentIds);
|
|
3899
908
|
const uniqueParentIds = Array.from(new Set(allParentIds));
|
|
909
|
+
let resolvedWhere = firstKey.where;
|
|
910
|
+
if (resolvedWhere && this.context?.exportStore) {
|
|
911
|
+
const { hasExportVariables: hasExportVariables2, resolveExportVariables: resolveExportVariables2 } = await Promise.resolve().then(() => (init_utils(), utils_exports));
|
|
912
|
+
if (hasExportVariables2(resolvedWhere)) {
|
|
913
|
+
try {
|
|
914
|
+
if (this.debugConfig?.exportVariables) {
|
|
915
|
+
console.log(`\u{1F50D} DataLoader: Resolving export variables in where clause:`, resolvedWhere);
|
|
916
|
+
}
|
|
917
|
+
resolvedWhere = await resolveExportVariables2(resolvedWhere, this.context.exportStore);
|
|
918
|
+
if (this.debugConfig?.exportVariables) {
|
|
919
|
+
console.log(`\u2705 DataLoader: Successfully resolved to:`, resolvedWhere);
|
|
920
|
+
}
|
|
921
|
+
} catch (error) {
|
|
922
|
+
if (this.debugConfig?.exportVariables) {
|
|
923
|
+
console.warn(`\u274C DataLoader: Failed to resolve export variables:`, error);
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
}
|
|
3900
928
|
const whereClause = this.buildBatchWhereClause(
|
|
3901
929
|
uniqueParentIds,
|
|
3902
930
|
firstKey.isReversedRelation,
|
|
3903
931
|
firstKey.foreignKey,
|
|
3904
|
-
|
|
932
|
+
resolvedWhere
|
|
933
|
+
// Use resolved where clause
|
|
3905
934
|
);
|
|
3906
935
|
if (!whereClause) {
|
|
3907
936
|
results.push(...groupKeys.map(() => []));
|
|
@@ -3914,13 +943,17 @@ var RelationDataLoader = class {
|
|
|
3914
943
|
[firstKey.foreignKey]: true
|
|
3915
944
|
},
|
|
3916
945
|
where: whereClause,
|
|
3917
|
-
orderBy: firstKey.orderBy ?
|
|
946
|
+
orderBy: firstKey.orderBy ? buildOrderByClause(this.tableInfo, firstKey.orderBy) : void 0,
|
|
3918
947
|
limit: firstKey.limit,
|
|
3919
948
|
offset: firstKey.offset
|
|
3920
949
|
});
|
|
3921
|
-
|
|
950
|
+
if (this.debugConfig?.dataLoader) {
|
|
951
|
+
console.log(`DataLoader executing query for relation ${firstKey.relationName} with foreign key ${firstKey.foreignKey}`);
|
|
952
|
+
}
|
|
3922
953
|
const batchResults = await query;
|
|
3923
|
-
|
|
954
|
+
if (this.debugConfig?.dataLoader) {
|
|
955
|
+
console.log(`DataLoader got ${batchResults.length} results:`, batchResults);
|
|
956
|
+
}
|
|
3924
957
|
const resultsByParentId = /* @__PURE__ */ new Map();
|
|
3925
958
|
for (const result of batchResults) {
|
|
3926
959
|
const parentId = result[firstKey.foreignKey];
|
|
@@ -3946,12 +979,12 @@ var RelationDataLoader = class {
|
|
|
3946
979
|
console.error(`Available columns:`, Object.keys(this.tableInfo.columns));
|
|
3947
980
|
return void 0;
|
|
3948
981
|
}
|
|
3949
|
-
const parentIdClause = (0,
|
|
982
|
+
const parentIdClause = (0, import_drizzle_orm6.inArray)(foreignKeyColumn, parentIds);
|
|
3950
983
|
if (!additionalWhere) {
|
|
3951
984
|
return parentIdClause;
|
|
3952
985
|
}
|
|
3953
|
-
const additionalClause =
|
|
3954
|
-
return additionalClause ? (0,
|
|
986
|
+
const additionalClause = buildWhereClause(this.tableInfo, additionalWhere);
|
|
987
|
+
return additionalClause ? (0, import_drizzle_orm6.and)(parentIdClause, additionalClause) : parentIdClause;
|
|
3955
988
|
}
|
|
3956
989
|
async loadRelation(relationName, parentIds, foreignKey, options = {}, isReversedRelation = false) {
|
|
3957
990
|
const key = {
|
|
@@ -3974,22 +1007,23 @@ var RelationDataLoader = class {
|
|
|
3974
1007
|
this.loaders.clear();
|
|
3975
1008
|
}
|
|
3976
1009
|
};
|
|
3977
|
-
function getRelationLoader(context, tableName, queryBase, tableInfo, relations) {
|
|
1010
|
+
function getRelationLoader(context, tableName, queryBase, tableInfo, relations, debugConfig) {
|
|
3978
1011
|
if (!context.relationLoaders.has(tableName)) {
|
|
3979
1012
|
context.relationLoaders.set(
|
|
3980
1013
|
tableName,
|
|
3981
|
-
new RelationDataLoader(queryBase, tableInfo, relations)
|
|
1014
|
+
new RelationDataLoader(queryBase, tableInfo, relations, context, debugConfig)
|
|
1015
|
+
// Pass debug config
|
|
3982
1016
|
);
|
|
3983
1017
|
}
|
|
3984
1018
|
return context.relationLoaders.get(tableName);
|
|
3985
1019
|
}
|
|
3986
1020
|
|
|
3987
1021
|
// src/build-schema-sdl-with-dl/generator/queries/dataloader-resolvers.ts
|
|
3988
|
-
var createDataLoaderFindManyResolver = (queryBase, tableInfo, tables, relations) => {
|
|
1022
|
+
var createDataLoaderFindManyResolver = (queryBase, tableInfo, tables, relations, debugConfig) => {
|
|
3989
1023
|
return async (parent, args, context, info) => {
|
|
3990
1024
|
try {
|
|
3991
1025
|
const { where, orderBy, limit, offset } = args;
|
|
3992
|
-
const parsedInfo = (0,
|
|
1026
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info.parseResolveInfo)(info, {
|
|
3993
1027
|
deep: true
|
|
3994
1028
|
});
|
|
3995
1029
|
const allFields = {};
|
|
@@ -4006,8 +1040,8 @@ var createDataLoaderFindManyResolver = (queryBase, tableInfo, tables, relations)
|
|
|
4006
1040
|
columns: allColumns,
|
|
4007
1041
|
offset,
|
|
4008
1042
|
limit,
|
|
4009
|
-
orderBy:
|
|
4010
|
-
where:
|
|
1043
|
+
orderBy: buildOrderByClause(tableInfo, orderBy),
|
|
1044
|
+
where: buildWhereClause(tableInfo, where)
|
|
4011
1045
|
// No 'with' clause - we'll load relations separately
|
|
4012
1046
|
});
|
|
4013
1047
|
if (mainResults.length === 0) {
|
|
@@ -4019,22 +1053,23 @@ var createDataLoaderFindManyResolver = (queryBase, tableInfo, tables, relations)
|
|
|
4019
1053
|
tables,
|
|
4020
1054
|
relations,
|
|
4021
1055
|
allFields,
|
|
4022
|
-
context
|
|
1056
|
+
context,
|
|
1057
|
+
debugConfig
|
|
4023
1058
|
);
|
|
4024
1059
|
return enhancedResults;
|
|
4025
1060
|
} catch (e) {
|
|
4026
1061
|
if (typeof e === "object" && e !== null && "message" in e) {
|
|
4027
|
-
throw new
|
|
1062
|
+
throw new import_graphql3.GraphQLError(String(e.message));
|
|
4028
1063
|
}
|
|
4029
1064
|
throw e;
|
|
4030
1065
|
}
|
|
4031
1066
|
};
|
|
4032
1067
|
};
|
|
4033
|
-
var createDataLoaderFindFirstResolver = (queryBase, tableInfo, tables, relations) => {
|
|
1068
|
+
var createDataLoaderFindFirstResolver = (queryBase, tableInfo, tables, relations, debugConfig) => {
|
|
4034
1069
|
return async (parent, args, context, info) => {
|
|
4035
1070
|
try {
|
|
4036
1071
|
const { where, orderBy } = args;
|
|
4037
|
-
const parsedInfo = (0,
|
|
1072
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info.parseResolveInfo)(info, {
|
|
4038
1073
|
deep: true
|
|
4039
1074
|
});
|
|
4040
1075
|
const allFields = {};
|
|
@@ -4049,8 +1084,8 @@ var createDataLoaderFindFirstResolver = (queryBase, tableInfo, tables, relations
|
|
|
4049
1084
|
}
|
|
4050
1085
|
const mainResult = await queryBase.findFirst({
|
|
4051
1086
|
columns: allColumns,
|
|
4052
|
-
orderBy:
|
|
4053
|
-
where:
|
|
1087
|
+
orderBy: buildOrderByClause(tableInfo, orderBy),
|
|
1088
|
+
where: buildWhereClause(tableInfo, where)
|
|
4054
1089
|
// No 'with' clause
|
|
4055
1090
|
});
|
|
4056
1091
|
if (!mainResult) {
|
|
@@ -4062,18 +1097,19 @@ var createDataLoaderFindFirstResolver = (queryBase, tableInfo, tables, relations
|
|
|
4062
1097
|
tables,
|
|
4063
1098
|
relations,
|
|
4064
1099
|
allFields,
|
|
4065
|
-
context
|
|
1100
|
+
context,
|
|
1101
|
+
debugConfig
|
|
4066
1102
|
);
|
|
4067
1103
|
return enhancedResult || null;
|
|
4068
1104
|
} catch (e) {
|
|
4069
1105
|
if (typeof e === "object" && e !== null && "message" in e) {
|
|
4070
|
-
throw new
|
|
1106
|
+
throw new import_graphql3.GraphQLError(String(e.message));
|
|
4071
1107
|
}
|
|
4072
1108
|
throw e;
|
|
4073
1109
|
}
|
|
4074
1110
|
};
|
|
4075
1111
|
};
|
|
4076
|
-
async function loadRelationsWithDataLoader(mainResults, tableInfo, tables, relations, fields, context) {
|
|
1112
|
+
async function loadRelationsWithDataLoader(mainResults, tableInfo, tables, relations, fields, context, debugConfig) {
|
|
4077
1113
|
const tableRelations = relations[tableInfo.name];
|
|
4078
1114
|
if (!tableRelations) {
|
|
4079
1115
|
return mainResults;
|
|
@@ -4083,7 +1119,7 @@ async function loadRelationsWithDataLoader(mainResults, tableInfo, tables, relat
|
|
|
4083
1119
|
const relationField = fields[relName];
|
|
4084
1120
|
if (!relationField)
|
|
4085
1121
|
continue;
|
|
4086
|
-
if ((0,
|
|
1122
|
+
if ((0, import_drizzle_orm7.is)(relation, import_drizzle_orm7.One)) {
|
|
4087
1123
|
mainResult[relName] = null;
|
|
4088
1124
|
} else {
|
|
4089
1125
|
mainResult[relName] = [];
|
|
@@ -4124,9 +1160,11 @@ async function loadRelationsWithDataLoader(mainResults, tableInfo, tables, relat
|
|
|
4124
1160
|
let foreignKeyName;
|
|
4125
1161
|
let isReversedRelation = false;
|
|
4126
1162
|
const relationConfig = relation.config;
|
|
4127
|
-
|
|
1163
|
+
if (debugConfig?.dataLoader) {
|
|
1164
|
+
console.log(`Processing relation ${relName} for table ${tableInfo.name} -> ${targetTableName}`);
|
|
1165
|
+
}
|
|
4128
1166
|
if (relationConfig?.fields && relationConfig.fields.length > 0) {
|
|
4129
|
-
if ((0,
|
|
1167
|
+
if ((0, import_drizzle_orm7.is)(relation, import_drizzle_orm7.One)) {
|
|
4130
1168
|
const fieldColumn = relationConfig.fields[0];
|
|
4131
1169
|
const referenceColumn = relationConfig.references[0];
|
|
4132
1170
|
const referenceKeyName = Object.keys(targetTable.columns).find(
|
|
@@ -4134,7 +1172,9 @@ async function loadRelationsWithDataLoader(mainResults, tableInfo, tables, relat
|
|
|
4134
1172
|
) || referenceColumn.name;
|
|
4135
1173
|
foreignKeyName = referenceKeyName;
|
|
4136
1174
|
isReversedRelation = false;
|
|
4137
|
-
|
|
1175
|
+
if (debugConfig?.dataLoader) {
|
|
1176
|
+
console.log(`One-to-one relation: foreignKey=${foreignKeyName}, isReversed=${isReversedRelation}`);
|
|
1177
|
+
}
|
|
4138
1178
|
} else {
|
|
4139
1179
|
const fieldColumn = relationConfig.fields[0];
|
|
4140
1180
|
const referenceColumn = relationConfig.references[0];
|
|
@@ -4142,14 +1182,18 @@ async function loadRelationsWithDataLoader(mainResults, tableInfo, tables, relat
|
|
|
4142
1182
|
(key) => targetTable.columns[key] === referenceColumn
|
|
4143
1183
|
) || referenceColumn.name;
|
|
4144
1184
|
isReversedRelation = false;
|
|
4145
|
-
|
|
1185
|
+
if (debugConfig?.dataLoader) {
|
|
1186
|
+
console.log(`Many-to-one relation: foreignKey=${foreignKeyName}, isReversed=${isReversedRelation}`);
|
|
1187
|
+
}
|
|
4146
1188
|
}
|
|
4147
1189
|
} else if (relationConfig?.references && relationConfig.references.length > 0) {
|
|
4148
1190
|
foreignKeyName = relationConfig.references[0].name;
|
|
4149
1191
|
isReversedRelation = true;
|
|
4150
|
-
|
|
1192
|
+
if (debugConfig?.dataLoader) {
|
|
1193
|
+
console.log(`References-only relation: foreignKey=${foreignKeyName}, isReversed=${isReversedRelation}`);
|
|
1194
|
+
}
|
|
4151
1195
|
} else {
|
|
4152
|
-
if ((0,
|
|
1196
|
+
if ((0, import_drizzle_orm7.is)(relation, import_drizzle_orm7.One)) {
|
|
4153
1197
|
let possibleForeignKeys = Object.entries(targetTable.columns).filter(([name, col]) => {
|
|
4154
1198
|
const lowerName = name.toLowerCase();
|
|
4155
1199
|
const tableName = tableInfo.name.toLowerCase();
|
|
@@ -4199,7 +1243,9 @@ async function loadRelationsWithDataLoader(mainResults, tableInfo, tables, relat
|
|
|
4199
1243
|
) || fieldColumn.name;
|
|
4200
1244
|
if (tableInfo.columns[fieldKeyName]) {
|
|
4201
1245
|
actualParentIds = mainResults.map((result) => result[fieldKeyName]).filter((id) => id != null);
|
|
4202
|
-
|
|
1246
|
+
if (debugConfig?.dataLoader) {
|
|
1247
|
+
console.log(`Extracted foreign key values for many-to-one relation:`, actualParentIds);
|
|
1248
|
+
}
|
|
4203
1249
|
}
|
|
4204
1250
|
}
|
|
4205
1251
|
const targetQueryBase = context.db?.query?.[targetTableName];
|
|
@@ -4212,9 +1258,13 @@ async function loadRelationsWithDataLoader(mainResults, tableInfo, tables, relat
|
|
|
4212
1258
|
targetTableName,
|
|
4213
1259
|
targetQueryBase,
|
|
4214
1260
|
targetTable,
|
|
4215
|
-
relations[targetTableName] || {}
|
|
1261
|
+
relations[targetTableName] || {},
|
|
1262
|
+
debugConfig
|
|
1263
|
+
// Pass debug config
|
|
4216
1264
|
);
|
|
4217
|
-
|
|
1265
|
+
if (debugConfig?.dataLoader) {
|
|
1266
|
+
console.log(`Created relation loader for ${targetTableName}, calling loadRelation with parentIds:`, actualParentIds);
|
|
1267
|
+
}
|
|
4218
1268
|
const relationPromise = relationLoader.loadRelation(
|
|
4219
1269
|
relName,
|
|
4220
1270
|
actualParentIds,
|
|
@@ -4222,7 +1272,9 @@ async function loadRelationsWithDataLoader(mainResults, tableInfo, tables, relat
|
|
|
4222
1272
|
relationOptions,
|
|
4223
1273
|
isReversedRelation
|
|
4224
1274
|
).then(async (relationResults) => {
|
|
4225
|
-
|
|
1275
|
+
if (debugConfig?.dataLoader) {
|
|
1276
|
+
console.log(`Relation ${relName} loaded, got ${relationResults.length} results:`, relationResults);
|
|
1277
|
+
}
|
|
4226
1278
|
const relationMap = /* @__PURE__ */ new Map();
|
|
4227
1279
|
for (const result of relationResults) {
|
|
4228
1280
|
relationMap.set(result.parentId, result.data);
|
|
@@ -4238,7 +1290,8 @@ async function loadRelationsWithDataLoader(mainResults, tableInfo, tables, relat
|
|
|
4238
1290
|
tables,
|
|
4239
1291
|
relations,
|
|
4240
1292
|
relationFields,
|
|
4241
|
-
context
|
|
1293
|
+
context,
|
|
1294
|
+
debugConfig
|
|
4242
1295
|
);
|
|
4243
1296
|
let dataIndex = 0;
|
|
4244
1297
|
for (const result of relationResults) {
|
|
@@ -4262,7 +1315,7 @@ async function loadRelationsWithDataLoader(mainResults, tableInfo, tables, relat
|
|
|
4262
1315
|
}
|
|
4263
1316
|
}
|
|
4264
1317
|
const relationData = relationMap.get(parentId) || [];
|
|
4265
|
-
if ((0,
|
|
1318
|
+
if ((0, import_drizzle_orm7.is)(relation, import_drizzle_orm7.One)) {
|
|
4266
1319
|
mainResult[relName] = relationData[0] || null;
|
|
4267
1320
|
} else {
|
|
4268
1321
|
mainResult[relName] = relationData;
|
|
@@ -4276,7 +1329,7 @@ async function loadRelationsWithDataLoader(mainResults, tableInfo, tables, relat
|
|
|
4276
1329
|
}
|
|
4277
1330
|
|
|
4278
1331
|
// src/build-schema-sdl-with-dl/generator/queries/index.ts
|
|
4279
|
-
var
|
|
1332
|
+
var generateQueries = (db, tables, relations, debugConfig) => {
|
|
4280
1333
|
const queries = {};
|
|
4281
1334
|
for (const [tableName, tableInfo] of Object.entries(tables)) {
|
|
4282
1335
|
const queryBase = db.query[tableName];
|
|
@@ -4289,27 +1342,29 @@ var generateQueries2 = (db, tables, relations) => {
|
|
|
4289
1342
|
queryBase,
|
|
4290
1343
|
tableInfo,
|
|
4291
1344
|
tables,
|
|
4292
|
-
relations
|
|
1345
|
+
relations,
|
|
1346
|
+
debugConfig
|
|
4293
1347
|
);
|
|
4294
1348
|
queries[`${tableName}FindFirst`] = createDataLoaderFindFirstResolver(
|
|
4295
1349
|
queryBase,
|
|
4296
1350
|
tableInfo,
|
|
4297
1351
|
tables,
|
|
4298
|
-
relations
|
|
1352
|
+
relations,
|
|
1353
|
+
debugConfig
|
|
4299
1354
|
);
|
|
4300
1355
|
}
|
|
4301
1356
|
return queries;
|
|
4302
1357
|
};
|
|
4303
1358
|
|
|
4304
1359
|
// src/build-schema-sdl-with-dl/generator/mutations/resolvers.ts
|
|
4305
|
-
var
|
|
4306
|
-
var
|
|
4307
|
-
const queryResolver = createDataLoaderFindManyResolver(queryBase, tableInfo, tables, relations);
|
|
1360
|
+
var import_graphql4 = require("graphql");
|
|
1361
|
+
var createInsertManyResolver = (db, queryBase, tableInfo, tables, relations, primaryKeyColumn, debugConfig) => {
|
|
1362
|
+
const queryResolver = createDataLoaderFindManyResolver(queryBase, tableInfo, tables, relations, debugConfig);
|
|
4308
1363
|
return async (parent, args, context, info) => {
|
|
4309
1364
|
try {
|
|
4310
1365
|
const { values } = args;
|
|
4311
1366
|
if (!values || values.length === 0) {
|
|
4312
|
-
throw new
|
|
1367
|
+
throw new import_graphql4.GraphQLError("No values provided for insert");
|
|
4313
1368
|
}
|
|
4314
1369
|
const remappedValues = remapFromGraphQLArrayInput(
|
|
4315
1370
|
values,
|
|
@@ -4334,22 +1389,22 @@ var createInsertManyResolver2 = (db, queryBase, tableInfo, tables, relations, pr
|
|
|
4334
1389
|
return result;
|
|
4335
1390
|
} catch (e) {
|
|
4336
1391
|
if (typeof e === "object" && e !== null && "message" in e) {
|
|
4337
|
-
throw new
|
|
1392
|
+
throw new import_graphql4.GraphQLError(String(e.message));
|
|
4338
1393
|
}
|
|
4339
1394
|
throw e;
|
|
4340
1395
|
}
|
|
4341
1396
|
};
|
|
4342
1397
|
};
|
|
4343
|
-
var
|
|
4344
|
-
const queryResolver = createDataLoaderFindManyResolver(queryBase, tableInfo, tables, relations);
|
|
1398
|
+
var createUpdateManyResolver = (db, queryBase, tableInfo, tables, relations, primaryKeyColumn, debugConfig) => {
|
|
1399
|
+
const queryResolver = createDataLoaderFindManyResolver(queryBase, tableInfo, tables, relations, debugConfig);
|
|
4345
1400
|
return async (parent, args, context, info) => {
|
|
4346
1401
|
try {
|
|
4347
1402
|
const { where, set } = args;
|
|
4348
1403
|
if (!set || Object.keys(set).length === 0) {
|
|
4349
|
-
throw new
|
|
1404
|
+
throw new import_graphql4.GraphQLError("No values provided for update");
|
|
4350
1405
|
}
|
|
4351
1406
|
const remappedSet = remapFromGraphQLSingleInput(set, tableInfo.table);
|
|
4352
|
-
const whereClause =
|
|
1407
|
+
const whereClause = buildWhereClause(tableInfo, where);
|
|
4353
1408
|
let query = db.update(tableInfo.table).set(remappedSet);
|
|
4354
1409
|
if (whereClause) {
|
|
4355
1410
|
query = query.where(whereClause);
|
|
@@ -4373,18 +1428,18 @@ var createUpdateManyResolver2 = (db, queryBase, tableInfo, tables, relations, pr
|
|
|
4373
1428
|
return result;
|
|
4374
1429
|
} catch (e) {
|
|
4375
1430
|
if (typeof e === "object" && e !== null && "message" in e) {
|
|
4376
|
-
throw new
|
|
1431
|
+
throw new import_graphql4.GraphQLError(String(e.message));
|
|
4377
1432
|
}
|
|
4378
1433
|
throw e;
|
|
4379
1434
|
}
|
|
4380
1435
|
};
|
|
4381
1436
|
};
|
|
4382
|
-
var
|
|
4383
|
-
const queryResolver = createDataLoaderFindManyResolver(queryBase, tableInfo, tables, relations);
|
|
1437
|
+
var createDeleteManyResolver = (db, queryBase, tableInfo, tables, relations, primaryKeyColumn, debugConfig) => {
|
|
1438
|
+
const queryResolver = createDataLoaderFindManyResolver(queryBase, tableInfo, tables, relations, debugConfig);
|
|
4384
1439
|
return async (parent, args, context, info) => {
|
|
4385
1440
|
try {
|
|
4386
1441
|
const { where } = args;
|
|
4387
|
-
const whereClause =
|
|
1442
|
+
const whereClause = buildWhereClause(tableInfo, where);
|
|
4388
1443
|
let deleteQuery = db.delete(tableInfo.table);
|
|
4389
1444
|
if (whereClause) {
|
|
4390
1445
|
deleteQuery = deleteQuery.where(whereClause);
|
|
@@ -4401,7 +1456,7 @@ var createDeleteManyResolver2 = (db, queryBase, tableInfo, tables, relations, pr
|
|
|
4401
1456
|
};
|
|
4402
1457
|
} catch (e) {
|
|
4403
1458
|
if (typeof e === "object" && e !== null && "message" in e) {
|
|
4404
|
-
throw new
|
|
1459
|
+
throw new import_graphql4.GraphQLError(String(e.message));
|
|
4405
1460
|
}
|
|
4406
1461
|
throw e;
|
|
4407
1462
|
}
|
|
@@ -4409,7 +1464,7 @@ var createDeleteManyResolver2 = (db, queryBase, tableInfo, tables, relations, pr
|
|
|
4409
1464
|
};
|
|
4410
1465
|
|
|
4411
1466
|
// src/build-schema-sdl-with-dl/generator/mutations/index.ts
|
|
4412
|
-
var
|
|
1467
|
+
var generateMutations = (db, tables, relations, debugConfig) => {
|
|
4413
1468
|
const mutations = {};
|
|
4414
1469
|
const deleteResultResolvers = {};
|
|
4415
1470
|
for (const [tableName, tableInfo] of Object.entries(tables)) {
|
|
@@ -4428,29 +1483,32 @@ var generateMutations2 = (db, tables, relations) => {
|
|
|
4428
1483
|
`Drizzle-GraphQL Error: Table ${tableName} does not have a primary key column`
|
|
4429
1484
|
);
|
|
4430
1485
|
}
|
|
4431
|
-
mutations[`${tableName}InsertMany`] =
|
|
1486
|
+
mutations[`${tableName}InsertMany`] = createInsertManyResolver(
|
|
4432
1487
|
db,
|
|
4433
1488
|
queryBase,
|
|
4434
1489
|
tableInfo,
|
|
4435
1490
|
tables,
|
|
4436
1491
|
relations,
|
|
4437
|
-
primaryKeyColumn
|
|
1492
|
+
primaryKeyColumn,
|
|
1493
|
+
debugConfig
|
|
4438
1494
|
);
|
|
4439
|
-
mutations[`${tableName}UpdateMany`] =
|
|
1495
|
+
mutations[`${tableName}UpdateMany`] = createUpdateManyResolver(
|
|
4440
1496
|
db,
|
|
4441
1497
|
queryBase,
|
|
4442
1498
|
tableInfo,
|
|
4443
1499
|
tables,
|
|
4444
1500
|
relations,
|
|
4445
|
-
primaryKeyColumn
|
|
1501
|
+
primaryKeyColumn,
|
|
1502
|
+
debugConfig
|
|
4446
1503
|
);
|
|
4447
|
-
mutations[`${tableName}DeleteMany`] =
|
|
1504
|
+
mutations[`${tableName}DeleteMany`] = createDeleteManyResolver(
|
|
4448
1505
|
db,
|
|
4449
1506
|
queryBase,
|
|
4450
1507
|
tableInfo,
|
|
4451
1508
|
tables,
|
|
4452
1509
|
relations,
|
|
4453
|
-
primaryKeyColumn
|
|
1510
|
+
primaryKeyColumn,
|
|
1511
|
+
debugConfig
|
|
4454
1512
|
);
|
|
4455
1513
|
deleteResultResolvers[`${capitalizedName}DeleteResult`] = {
|
|
4456
1514
|
[`${tableName}FindMany`]: (parent, args, context, info) => {
|
|
@@ -4469,30 +1527,31 @@ var generateMutations2 = (db, tables, relations) => {
|
|
|
4469
1527
|
var exportDirectiveTypeDefs = `directive @export(as: String!) on FIELD`;
|
|
4470
1528
|
|
|
4471
1529
|
// src/build-schema-sdl-with-dl/index.ts
|
|
4472
|
-
var
|
|
4473
|
-
var
|
|
1530
|
+
var import_schema2 = require("@graphql-tools/schema");
|
|
1531
|
+
var buildSchemaSDL = (db, config) => {
|
|
4474
1532
|
const schema = db._.fullSchema;
|
|
4475
1533
|
if (!schema) {
|
|
4476
1534
|
throw new Error(
|
|
4477
1535
|
"Drizzle-GraphQL Error: Schema not found in drizzle instance. Make sure you're using drizzle-orm v0.30.9 or above and schema is passed to drizzle constructor!"
|
|
4478
1536
|
);
|
|
4479
1537
|
}
|
|
4480
|
-
if (!(0,
|
|
1538
|
+
if (!(0, import_drizzle_orm8.is)(db, import_sqlite_core2.BaseSQLiteDatabase)) {
|
|
4481
1539
|
throw new Error(
|
|
4482
1540
|
"Drizzle-GraphQL Error: buildSchemaSDL currently only supports SQLite databases"
|
|
4483
1541
|
);
|
|
4484
1542
|
}
|
|
4485
|
-
const { tables, relations } =
|
|
1543
|
+
const { tables, relations } = generateTypes(db, schema);
|
|
4486
1544
|
const typeDefsArray = [];
|
|
4487
|
-
typeDefsArray.push(
|
|
4488
|
-
typeDefsArray.push(
|
|
4489
|
-
typeDefsArray.push(
|
|
1545
|
+
typeDefsArray.push(generateTypeDefs(tables, relations));
|
|
1546
|
+
typeDefsArray.push(generateQueryTypeDefs(tables));
|
|
1547
|
+
typeDefsArray.push(generateMutationTypeDefs(tables));
|
|
4490
1548
|
const typeDefs = typeDefsArray.join("\n\n");
|
|
4491
|
-
const queries =
|
|
4492
|
-
const { mutations, deleteResultResolvers } =
|
|
1549
|
+
const queries = generateQueries(db, tables, relations, config?.debug);
|
|
1550
|
+
const { mutations, deleteResultResolvers } = generateMutations(
|
|
4493
1551
|
db,
|
|
4494
1552
|
tables,
|
|
4495
|
-
relations
|
|
1553
|
+
relations,
|
|
1554
|
+
config?.debug
|
|
4496
1555
|
);
|
|
4497
1556
|
const resolvers = {
|
|
4498
1557
|
Query: queries,
|
|
@@ -4562,6 +1621,7 @@ var useDataLoaderCleanupOnly = () => ({
|
|
|
4562
1621
|
var ExportStore = class {
|
|
4563
1622
|
store = /* @__PURE__ */ new Map();
|
|
4564
1623
|
pending = /* @__PURE__ */ new Map();
|
|
1624
|
+
accumulators = /* @__PURE__ */ new Map();
|
|
4565
1625
|
/**
|
|
4566
1626
|
* Store a value for later retrieval
|
|
4567
1627
|
* Resolves any pending promises waiting for this value
|
|
@@ -4574,6 +1634,32 @@ var ExportStore = class {
|
|
|
4574
1634
|
this.pending.delete(name);
|
|
4575
1635
|
}
|
|
4576
1636
|
}
|
|
1637
|
+
/**
|
|
1638
|
+
* Accumulate values into an array with deduplication
|
|
1639
|
+
* First call initializes an array, subsequent calls add to it
|
|
1640
|
+
* Use this when the same export variable is used in multiple array items
|
|
1641
|
+
*/
|
|
1642
|
+
accumulate(name, value) {
|
|
1643
|
+
if (!this.accumulators.has(name)) {
|
|
1644
|
+
this.accumulators.set(name, /* @__PURE__ */ new Set());
|
|
1645
|
+
}
|
|
1646
|
+
const accumulator = this.accumulators.get(name);
|
|
1647
|
+
if (Array.isArray(value)) {
|
|
1648
|
+
value.forEach((item) => {
|
|
1649
|
+
if (item !== null && item !== void 0) {
|
|
1650
|
+
accumulator.add(item);
|
|
1651
|
+
}
|
|
1652
|
+
});
|
|
1653
|
+
} else if (value !== null && value !== void 0) {
|
|
1654
|
+
accumulator.add(value);
|
|
1655
|
+
}
|
|
1656
|
+
const accumulatedArray = Array.from(accumulator);
|
|
1657
|
+
this.store.set(name, accumulatedArray);
|
|
1658
|
+
const callbacks = this.pending.get(name);
|
|
1659
|
+
if (callbacks) {
|
|
1660
|
+
callbacks.forEach((resolve) => resolve(accumulatedArray));
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
4577
1663
|
/**
|
|
4578
1664
|
* Get a value if it exists, otherwise return undefined
|
|
4579
1665
|
*/
|
|
@@ -4604,7 +1690,9 @@ var ExportStore = class {
|
|
|
4604
1690
|
if (allowNull) {
|
|
4605
1691
|
resolve(null);
|
|
4606
1692
|
} else {
|
|
4607
|
-
reject(
|
|
1693
|
+
reject(
|
|
1694
|
+
new Error(`Timeout waiting for export variable "${name}"`)
|
|
1695
|
+
);
|
|
4608
1696
|
}
|
|
4609
1697
|
}
|
|
4610
1698
|
}
|
|
@@ -4623,6 +1711,7 @@ var ExportStore = class {
|
|
|
4623
1711
|
clear() {
|
|
4624
1712
|
this.store.clear();
|
|
4625
1713
|
this.pending.clear();
|
|
1714
|
+
this.accumulators.clear();
|
|
4626
1715
|
}
|
|
4627
1716
|
/**
|
|
4628
1717
|
* Get all stored values
|
|
@@ -4633,6 +1722,7 @@ var ExportStore = class {
|
|
|
4633
1722
|
};
|
|
4634
1723
|
|
|
4635
1724
|
// src/export-tool/middleware.ts
|
|
1725
|
+
init_utils();
|
|
4636
1726
|
function createExportMiddleware() {
|
|
4637
1727
|
return (next) => {
|
|
4638
1728
|
return async (source, args, context, info) => {
|
|
@@ -4661,17 +1751,23 @@ function createExportMiddleware() {
|
|
|
4661
1751
|
return result;
|
|
4662
1752
|
const selfExportName = getExportDirective(fieldNode);
|
|
4663
1753
|
if (selfExportName && result !== void 0) {
|
|
4664
|
-
|
|
1754
|
+
if (Array.isArray(result)) {
|
|
1755
|
+
result.forEach(
|
|
1756
|
+
(value) => exportStore.accumulate(selfExportName, value)
|
|
1757
|
+
);
|
|
1758
|
+
} else {
|
|
1759
|
+
exportStore.set(selfExportName, result);
|
|
1760
|
+
}
|
|
4665
1761
|
}
|
|
4666
1762
|
if (fieldNode.selectionSet && result !== void 0 && result !== null) {
|
|
4667
1763
|
if (Array.isArray(result)) {
|
|
4668
1764
|
result.forEach((item) => {
|
|
4669
1765
|
if (item && typeof item === "object") {
|
|
4670
|
-
processExports(item, fieldNode.selectionSet, exportStore);
|
|
1766
|
+
processExports(item, fieldNode.selectionSet, exportStore, true);
|
|
4671
1767
|
}
|
|
4672
1768
|
});
|
|
4673
1769
|
} else if (typeof result === "object") {
|
|
4674
|
-
processExports(result, fieldNode.selectionSet, exportStore);
|
|
1770
|
+
processExports(result, fieldNode.selectionSet, exportStore, false);
|
|
4675
1771
|
}
|
|
4676
1772
|
}
|
|
4677
1773
|
return result;
|
|
@@ -4679,11 +1775,14 @@ function createExportMiddleware() {
|
|
|
4679
1775
|
};
|
|
4680
1776
|
}
|
|
4681
1777
|
|
|
1778
|
+
// src/export-tool/index.ts
|
|
1779
|
+
init_utils();
|
|
1780
|
+
|
|
4682
1781
|
// src/export-tool/makeScalarAcceptExports.ts
|
|
4683
|
-
var
|
|
1782
|
+
var import_graphql5 = require("graphql");
|
|
4684
1783
|
function makeScalarAcceptExports(originalScalar) {
|
|
4685
1784
|
const config = originalScalar.toConfig();
|
|
4686
|
-
return new
|
|
1785
|
+
return new import_graphql5.GraphQLScalarType({
|
|
4687
1786
|
...config,
|
|
4688
1787
|
name: config.name,
|
|
4689
1788
|
// Keep original name to override it in schema
|
|
@@ -4699,7 +1798,7 @@ function makeScalarAcceptExports(originalScalar) {
|
|
|
4699
1798
|
return value;
|
|
4700
1799
|
},
|
|
4701
1800
|
parseLiteral(ast, variables) {
|
|
4702
|
-
if (ast.kind ===
|
|
1801
|
+
if (ast.kind === import_graphql5.Kind.STRING) {
|
|
4703
1802
|
if (ast.value.startsWith("$_") || ast.value === "") {
|
|
4704
1803
|
return ast.value;
|
|
4705
1804
|
}
|
|
@@ -4713,13 +1812,13 @@ function makeScalarAcceptExports(originalScalar) {
|
|
|
4713
1812
|
}
|
|
4714
1813
|
|
|
4715
1814
|
// src/helpers.ts
|
|
4716
|
-
var
|
|
1815
|
+
var import_drizzle_orm9 = require("drizzle-orm");
|
|
4717
1816
|
function setCustomGraphQL(table, columnConfig) {
|
|
4718
1817
|
for (const [columnName, config] of Object.entries(columnConfig)) {
|
|
4719
1818
|
const column = table[columnName];
|
|
4720
1819
|
if (!column) {
|
|
4721
1820
|
console.warn(
|
|
4722
|
-
`Warning: Column "${columnName}" not found in table "${(0,
|
|
1821
|
+
`Warning: Column "${columnName}" not found in table "${(0, import_drizzle_orm9.getTableName)(
|
|
4723
1822
|
table
|
|
4724
1823
|
)}"`
|
|
4725
1824
|
);
|
|
@@ -4741,8 +1840,6 @@ function setCustomGraphQLTypes(table, columnTypes) {
|
|
|
4741
1840
|
}
|
|
4742
1841
|
// Annotate the CommonJS export names for ESM import in node:
|
|
4743
1842
|
0 && (module.exports = {
|
|
4744
|
-
buildSchema,
|
|
4745
|
-
buildSchemaSDL,
|
|
4746
1843
|
buildSchemaSDLWithDataLoader,
|
|
4747
1844
|
cleanupDataLoaderContext,
|
|
4748
1845
|
createDataLoaderContext,
|