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