drizzle-graphql-plus 0.8.36 → 0.8.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs +328 -3323
- package/index.cjs.map +1 -1
- package/index.d.cts +4 -88
- package/index.d.ts +4 -88
- package/index.js +332 -3427
- package/index.js.map +1 -1
- package/package.json +3 -12
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
|
};
|
|
@@ -62,7 +62,12 @@ async function resolveExportVariables(args, exportStore, timeout, allowNull = tr
|
|
|
62
62
|
const varName = getVariableName(item);
|
|
63
63
|
return await exportStore.waitFor(varName, timeout, allowNull);
|
|
64
64
|
} else if (typeof item === "object" && item !== null) {
|
|
65
|
-
return await resolveExportVariables(
|
|
65
|
+
return await resolveExportVariables(
|
|
66
|
+
item,
|
|
67
|
+
exportStore,
|
|
68
|
+
timeout,
|
|
69
|
+
allowNull
|
|
70
|
+
);
|
|
66
71
|
}
|
|
67
72
|
return item;
|
|
68
73
|
})
|
|
@@ -124,3208 +129,92 @@ function extractExportDirectives(info) {
|
|
|
124
129
|
}
|
|
125
130
|
for (const selection of fieldNode.selectionSet.selections) {
|
|
126
131
|
if (selection.kind === "Field") {
|
|
127
|
-
const exportName = getExportDirective(selection);
|
|
128
|
-
if (exportName) {
|
|
129
|
-
const fieldName = selection.name.value;
|
|
130
|
-
exports2.set(fieldName, exportName);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
return exports2;
|
|
135
|
-
}
|
|
136
|
-
function hasExportVariables(args) {
|
|
137
|
-
if (isExportVariable(args)) {
|
|
138
|
-
return true;
|
|
139
|
-
}
|
|
140
|
-
if (Array.isArray(args)) {
|
|
141
|
-
return args.some((item) => hasExportVariables(item));
|
|
142
|
-
}
|
|
143
|
-
if (typeof args === "object" && args !== null) {
|
|
144
|
-
for (const value of Object.values(args)) {
|
|
145
|
-
if (hasExportVariables(value)) {
|
|
146
|
-
return true;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
return false;
|
|
151
|
-
}
|
|
152
|
-
function processExports(result, selectionSet, exportStore) {
|
|
153
|
-
if (!result || !selectionSet)
|
|
154
|
-
return;
|
|
155
|
-
for (const selection of selectionSet.selections) {
|
|
156
|
-
if (selection.kind !== "Field")
|
|
157
|
-
continue;
|
|
158
|
-
const resultKey = selection.alias?.value ?? selection.name.value;
|
|
159
|
-
if (!(resultKey in result))
|
|
160
|
-
continue;
|
|
161
|
-
const value = result[resultKey];
|
|
162
|
-
const exportName = getExportDirective(selection);
|
|
163
|
-
if (exportName) {
|
|
164
|
-
exportStore.set(exportName, value);
|
|
165
|
-
}
|
|
166
|
-
if (selection.selectionSet && value !== null && value !== void 0) {
|
|
167
|
-
if (Array.isArray(value)) {
|
|
168
|
-
value.forEach((item) => {
|
|
169
|
-
if (item && typeof item === "object") {
|
|
170
|
-
processExports(item, selection.selectionSet, exportStore);
|
|
171
|
-
}
|
|
172
|
-
});
|
|
173
|
-
} else if (typeof value === "object") {
|
|
174
|
-
processExports(value, selection.selectionSet, exportStore);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
var init_utils = __esm({
|
|
180
|
-
"src/export-tool/utils.ts"() {
|
|
181
|
-
"use strict";
|
|
182
|
-
}
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
// src/index.ts
|
|
186
|
-
var src_exports = {};
|
|
187
|
-
__export(src_exports, {
|
|
188
|
-
buildSchema: () => buildSchema,
|
|
189
|
-
buildSchemaSDL: () => buildSchemaSDL,
|
|
190
|
-
buildSchemaSDLWithDataLoader: () => buildSchemaSDL2,
|
|
191
|
-
cleanupDataLoaderContext: () => cleanupDataLoaderContext,
|
|
192
|
-
createDataLoaderContext: () => createDataLoaderContext,
|
|
193
|
-
createExportMiddleware: () => createExportMiddleware,
|
|
194
|
-
exportDirectiveTypeDefs: () => exportDirectiveTypeDefs,
|
|
195
|
-
makeScalarAcceptExports: () => makeScalarAcceptExports,
|
|
196
|
-
setCustomGraphQL: () => setCustomGraphQL,
|
|
197
|
-
setCustomGraphQLTypes: () => setCustomGraphQLTypes,
|
|
198
|
-
useDataLoaderCleanup: () => useDataLoaderCleanup,
|
|
199
|
-
useDataLoaderCleanupOnly: () => useDataLoaderCleanupOnly,
|
|
200
|
-
useDataLoaderContext: () => useDataLoaderContext
|
|
201
|
-
});
|
|
202
|
-
module.exports = __toCommonJS(src_exports);
|
|
203
|
-
|
|
204
|
-
// src/buildSchema.ts
|
|
205
|
-
var import_drizzle_orm7 = require("drizzle-orm");
|
|
206
|
-
var import_mysql_core3 = require("drizzle-orm/mysql-core");
|
|
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 [];
|
|
132
|
+
const exportName = getExportDirective(selection);
|
|
133
|
+
if (exportName) {
|
|
134
|
+
const fieldName = selection.name.value;
|
|
135
|
+
exports2.set(fieldName, exportName);
|
|
3281
136
|
}
|
|
3282
|
-
}
|
|
137
|
+
}
|
|
3283
138
|
}
|
|
3284
|
-
return
|
|
3285
|
-
}
|
|
3286
|
-
|
|
3287
|
-
|
|
3288
|
-
|
|
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
|
-
);
|
|
139
|
+
return exports2;
|
|
140
|
+
}
|
|
141
|
+
function hasExportVariables(args) {
|
|
142
|
+
if (isExportVariable(args)) {
|
|
143
|
+
return true;
|
|
3294
144
|
}
|
|
3295
|
-
if (
|
|
3296
|
-
|
|
3297
|
-
"Drizzle-GraphQL Error: buildSchemaSDL currently only supports SQLite databases"
|
|
3298
|
-
);
|
|
145
|
+
if (Array.isArray(args)) {
|
|
146
|
+
return args.some((item) => hasExportVariables(item));
|
|
3299
147
|
}
|
|
3300
|
-
|
|
3301
|
-
|
|
3302
|
-
|
|
3303
|
-
|
|
3304
|
-
|
|
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
|
|
148
|
+
if (typeof args === "object" && args !== null) {
|
|
149
|
+
for (const value of Object.values(args)) {
|
|
150
|
+
if (hasExportVariables(value)) {
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
3318
153
|
}
|
|
3319
|
-
}
|
|
3320
|
-
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
function processExports(result, selectionSet, exportStore, isArrayItem = false) {
|
|
158
|
+
if (!result || !selectionSet)
|
|
159
|
+
return;
|
|
160
|
+
for (const selection of selectionSet.selections) {
|
|
161
|
+
if (selection.kind !== "Field")
|
|
162
|
+
continue;
|
|
163
|
+
const resultKey = selection.alias?.value ?? selection.name.value;
|
|
164
|
+
if (!(resultKey in result))
|
|
165
|
+
continue;
|
|
166
|
+
const value = result[resultKey];
|
|
167
|
+
const exportName = getExportDirective(selection);
|
|
168
|
+
if (exportName) {
|
|
169
|
+
if (isArrayItem) {
|
|
170
|
+
exportStore.accumulate(exportName, value);
|
|
171
|
+
} else {
|
|
172
|
+
exportStore.set(exportName, value);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (selection.selectionSet && value !== null && value !== void 0) {
|
|
176
|
+
if (Array.isArray(value)) {
|
|
177
|
+
value.forEach((item) => {
|
|
178
|
+
if (item && typeof item === "object") {
|
|
179
|
+
processExports(item, selection.selectionSet, exportStore, true);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
} else if (typeof value === "object") {
|
|
183
|
+
processExports(value, selection.selectionSet, exportStore, isArrayItem);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
var init_utils = __esm({
|
|
189
|
+
"src/export-tool/utils.ts"() {
|
|
190
|
+
"use strict";
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// src/index.ts
|
|
195
|
+
var src_exports = {};
|
|
196
|
+
__export(src_exports, {
|
|
197
|
+
buildSchemaSDLWithDataLoader: () => buildSchemaSDL,
|
|
198
|
+
cleanupDataLoaderContext: () => cleanupDataLoaderContext,
|
|
199
|
+
createDataLoaderContext: () => createDataLoaderContext,
|
|
200
|
+
createExportMiddleware: () => createExportMiddleware,
|
|
201
|
+
exportDirectiveTypeDefs: () => exportDirectiveTypeDefs,
|
|
202
|
+
makeScalarAcceptExports: () => makeScalarAcceptExports,
|
|
203
|
+
setCustomGraphQL: () => setCustomGraphQL,
|
|
204
|
+
setCustomGraphQLTypes: () => setCustomGraphQLTypes,
|
|
205
|
+
useDataLoaderCleanup: () => useDataLoaderCleanup,
|
|
206
|
+
useDataLoaderCleanupOnly: () => useDataLoaderCleanupOnly,
|
|
207
|
+
useDataLoaderContext: () => useDataLoaderContext
|
|
208
|
+
});
|
|
209
|
+
module.exports = __toCommonJS(src_exports);
|
|
3321
210
|
|
|
3322
211
|
// src/build-schema-sdl-with-dl/index.ts
|
|
3323
|
-
var
|
|
3324
|
-
var
|
|
212
|
+
var import_drizzle_orm8 = require("drizzle-orm");
|
|
213
|
+
var import_sqlite_core2 = require("drizzle-orm/sqlite-core");
|
|
3325
214
|
|
|
3326
215
|
// src/build-schema-sdl-with-dl/generator/schema/generation.ts
|
|
3327
|
-
var
|
|
3328
|
-
var
|
|
216
|
+
var import_drizzle_orm = require("drizzle-orm");
|
|
217
|
+
var generateTypes = (db, schema) => {
|
|
3329
218
|
const tables = {};
|
|
3330
219
|
const schemaEntries = Object.entries(schema);
|
|
3331
220
|
const tableEntries = [];
|
|
@@ -3333,7 +222,7 @@ var generateTypes2 = (db, schema) => {
|
|
|
3333
222
|
if (value && typeof value === "object" && "getSQL" in value) {
|
|
3334
223
|
const table = value;
|
|
3335
224
|
const tableName = key;
|
|
3336
|
-
const columns = (0,
|
|
225
|
+
const columns = (0, import_drizzle_orm.getTableColumns)(table);
|
|
3337
226
|
tables[tableName] = {
|
|
3338
227
|
name: tableName,
|
|
3339
228
|
table,
|
|
@@ -3342,14 +231,14 @@ var generateTypes2 = (db, schema) => {
|
|
|
3342
231
|
tableEntries.push([tableName, table]);
|
|
3343
232
|
}
|
|
3344
233
|
}
|
|
3345
|
-
const rawRelations = schemaEntries.filter(([key, value]) => (0,
|
|
234
|
+
const rawRelations = schemaEntries.filter(([key, value]) => (0, import_drizzle_orm.is)(value, import_drizzle_orm.Relations)).map(([key, value]) => [
|
|
3346
235
|
tableEntries.find(
|
|
3347
236
|
([tableName, tableValue]) => tableValue === value.table
|
|
3348
237
|
)[0],
|
|
3349
238
|
value
|
|
3350
239
|
]).map(([tableName, relValue]) => [
|
|
3351
240
|
tableName,
|
|
3352
|
-
relValue.config((0,
|
|
241
|
+
relValue.config((0, import_drizzle_orm.createTableRelationsHelpers)(tables[tableName].table))
|
|
3353
242
|
]);
|
|
3354
243
|
const namedRelations = Object.fromEntries(
|
|
3355
244
|
rawRelations.map(([relName, config]) => {
|
|
@@ -3371,23 +260,28 @@ var generateTypes2 = (db, schema) => {
|
|
|
3371
260
|
};
|
|
3372
261
|
|
|
3373
262
|
// 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
|
|
263
|
+
var import_drizzle_orm2 = require("drizzle-orm");
|
|
264
|
+
var import_mysql_core = require("drizzle-orm/mysql-core");
|
|
265
|
+
var import_pg_core = require("drizzle-orm/pg-core");
|
|
266
|
+
var import_sqlite_core = require("drizzle-orm/sqlite-core");
|
|
267
|
+
|
|
268
|
+
// src/util/case-ops/index.ts
|
|
269
|
+
var capitalize = (input) => input.length ? `${input[0].toLocaleUpperCase()}${input.length > 1 ? input.slice(1, input.length) : ""}` : input;
|
|
270
|
+
|
|
271
|
+
// src/build-schema-sdl-with-dl/generator/schema/type-defs.ts
|
|
272
|
+
var allowedNameChars = /^[a-zA-Z0-9_]+$/;
|
|
273
|
+
var customScalars = /* @__PURE__ */ new Set();
|
|
274
|
+
var enumDefinitions = /* @__PURE__ */ new Map();
|
|
275
|
+
var requiredFieldFilters = /* @__PURE__ */ new Set();
|
|
276
|
+
var foreignKeyTypes = /* @__PURE__ */ new Map();
|
|
277
|
+
var columnToSDL = (column, columnName, tableName, forceNullable = false, isPrimaryKey = false) => {
|
|
3384
278
|
let baseType;
|
|
3385
279
|
if (column.customGraphqlType) {
|
|
3386
280
|
baseType = column.customGraphqlType;
|
|
3387
281
|
} else if (isPrimaryKey) {
|
|
3388
282
|
baseType = "ID";
|
|
3389
283
|
} else {
|
|
3390
|
-
const foreignKeyType =
|
|
284
|
+
const foreignKeyType = foreignKeyTypes.get(`${tableName}.${columnName}`);
|
|
3391
285
|
if (foreignKeyType) {
|
|
3392
286
|
baseType = foreignKeyType;
|
|
3393
287
|
} else {
|
|
@@ -3398,15 +292,15 @@ var columnToSDL2 = (column, columnName, tableName, forceNullable = false, isPrim
|
|
|
3398
292
|
case "json":
|
|
3399
293
|
if (column.columnType === "PgGeometryObject") {
|
|
3400
294
|
baseType = "PgGeometryObject";
|
|
3401
|
-
|
|
295
|
+
customScalars.add("PgGeometryObject");
|
|
3402
296
|
} else {
|
|
3403
297
|
baseType = "JSON";
|
|
3404
|
-
|
|
298
|
+
customScalars.add("JSON");
|
|
3405
299
|
}
|
|
3406
300
|
break;
|
|
3407
301
|
case "date":
|
|
3408
302
|
baseType = "Date";
|
|
3409
|
-
|
|
303
|
+
customScalars.add("Date");
|
|
3410
304
|
break;
|
|
3411
305
|
case "string":
|
|
3412
306
|
if (column.enumValues?.length) {
|
|
@@ -3414,11 +308,11 @@ var columnToSDL2 = (column, columnName, tableName, forceNullable = false, isPrim
|
|
|
3414
308
|
columnName
|
|
3415
309
|
)}Enum`;
|
|
3416
310
|
baseType = enumName;
|
|
3417
|
-
if (!
|
|
3418
|
-
|
|
311
|
+
if (!enumDefinitions.has(enumName)) {
|
|
312
|
+
enumDefinitions.set(enumName, {
|
|
3419
313
|
name: enumName,
|
|
3420
314
|
values: column.enumValues.map(
|
|
3421
|
-
(e, index) =>
|
|
315
|
+
(e, index) => allowedNameChars.test(e) ? e : `Option${index}`
|
|
3422
316
|
)
|
|
3423
317
|
});
|
|
3424
318
|
}
|
|
@@ -3428,10 +322,10 @@ var columnToSDL2 = (column, columnName, tableName, forceNullable = false, isPrim
|
|
|
3428
322
|
break;
|
|
3429
323
|
case "bigint":
|
|
3430
324
|
baseType = "BigInt";
|
|
3431
|
-
|
|
325
|
+
customScalars.add("BigInt");
|
|
3432
326
|
break;
|
|
3433
327
|
case "number":
|
|
3434
|
-
if ((0,
|
|
328
|
+
if ((0, import_drizzle_orm2.is)(column, import_pg_core.PgInteger) || (0, import_drizzle_orm2.is)(column, import_pg_core.PgSerial) || (0, import_drizzle_orm2.is)(column, import_mysql_core.MySqlInt) || (0, import_drizzle_orm2.is)(column, import_mysql_core.MySqlSerial) || (0, import_drizzle_orm2.is)(column, import_sqlite_core.SQLiteInteger)) {
|
|
3435
329
|
baseType = "Int";
|
|
3436
330
|
} else {
|
|
3437
331
|
baseType = "Float";
|
|
@@ -3450,20 +344,20 @@ var columnToSDL2 = (column, columnName, tableName, forceNullable = false, isPrim
|
|
|
3450
344
|
columnName
|
|
3451
345
|
)}Array`;
|
|
3452
346
|
baseType = scalarName;
|
|
3453
|
-
|
|
347
|
+
customScalars.add(scalarName);
|
|
3454
348
|
}
|
|
3455
349
|
break;
|
|
3456
350
|
case "custom":
|
|
3457
351
|
default:
|
|
3458
352
|
if (column.columnType) {
|
|
3459
353
|
baseType = column.columnType;
|
|
3460
|
-
|
|
354
|
+
customScalars.add(column.columnType);
|
|
3461
355
|
} else {
|
|
3462
356
|
const customScalarName = `${capitalize(tableName)}${capitalize(
|
|
3463
357
|
columnName
|
|
3464
358
|
)}`;
|
|
3465
359
|
baseType = customScalarName;
|
|
3466
|
-
|
|
360
|
+
customScalars.add(customScalarName);
|
|
3467
361
|
}
|
|
3468
362
|
break;
|
|
3469
363
|
}
|
|
@@ -3474,19 +368,19 @@ var columnToSDL2 = (column, columnName, tableName, forceNullable = false, isPrim
|
|
|
3474
368
|
}
|
|
3475
369
|
return baseType;
|
|
3476
370
|
};
|
|
3477
|
-
var
|
|
371
|
+
var generateTypeDefs = (tables, relations) => {
|
|
3478
372
|
const typeDefs = [];
|
|
3479
|
-
|
|
3480
|
-
|
|
3481
|
-
|
|
3482
|
-
|
|
373
|
+
customScalars.clear();
|
|
374
|
+
enumDefinitions.clear();
|
|
375
|
+
requiredFieldFilters.clear();
|
|
376
|
+
foreignKeyTypes.clear();
|
|
3483
377
|
for (const [tableName, tableRelations] of Object.entries(relations)) {
|
|
3484
378
|
const tableInfo = tables[tableName];
|
|
3485
379
|
if (!tableInfo)
|
|
3486
380
|
continue;
|
|
3487
381
|
for (const [relationName, relationInfo] of Object.entries(tableRelations)) {
|
|
3488
382
|
const relation = relationInfo.relation;
|
|
3489
|
-
if (!(0,
|
|
383
|
+
if (!(0, import_drizzle_orm2.is)(relation, import_drizzle_orm2.One))
|
|
3490
384
|
continue;
|
|
3491
385
|
const config = relation.config;
|
|
3492
386
|
if (!config?.fields || !config?.references)
|
|
@@ -3520,12 +414,12 @@ var generateTypeDefs2 = (tables, relations) => {
|
|
|
3520
414
|
const referencedIsPrimaryKey = referencedColumn.primary || false;
|
|
3521
415
|
if (!foreignKeyHasCustomType) {
|
|
3522
416
|
if (referencedCustomType) {
|
|
3523
|
-
|
|
417
|
+
foreignKeyTypes.set(
|
|
3524
418
|
`${tableName}.${foreignKeyPropertyName}`,
|
|
3525
419
|
referencedCustomType
|
|
3526
420
|
);
|
|
3527
421
|
} else if (referencedIsPrimaryKey) {
|
|
3528
|
-
|
|
422
|
+
foreignKeyTypes.set(`${tableName}.${foreignKeyPropertyName}`, "ID");
|
|
3529
423
|
}
|
|
3530
424
|
}
|
|
3531
425
|
}
|
|
@@ -3536,7 +430,7 @@ var generateTypeDefs2 = (tables, relations) => {
|
|
|
3536
430
|
const fields = [];
|
|
3537
431
|
for (const [columnName, column] of Object.entries(tableInfo.columns)) {
|
|
3538
432
|
const isPrimaryKey = column.primary || false;
|
|
3539
|
-
const typeStr =
|
|
433
|
+
const typeStr = columnToSDL(
|
|
3540
434
|
column,
|
|
3541
435
|
columnName,
|
|
3542
436
|
tableName,
|
|
@@ -3554,7 +448,7 @@ var generateTypeDefs2 = (tables, relations) => {
|
|
|
3554
448
|
for (const [relationName, relationInfo] of Object.entries(
|
|
3555
449
|
tableRelations
|
|
3556
450
|
)) {
|
|
3557
|
-
const isOne = (0,
|
|
451
|
+
const isOne = (0, import_drizzle_orm2.is)(relationInfo.relation, import_drizzle_orm2.One);
|
|
3558
452
|
const targetTableName = relationInfo.targetTableName;
|
|
3559
453
|
const targetTypeName = capitalize(targetTableName);
|
|
3560
454
|
if (isOne) {
|
|
@@ -3574,7 +468,7 @@ ${fields.join("\n")}
|
|
|
3574
468
|
const insertFields = [];
|
|
3575
469
|
for (const [columnName, column] of Object.entries(tableInfo.columns)) {
|
|
3576
470
|
const isPrimaryKey = column.primary || false;
|
|
3577
|
-
const typeStr =
|
|
471
|
+
const typeStr = columnToSDL(
|
|
3578
472
|
column,
|
|
3579
473
|
columnName,
|
|
3580
474
|
tableName,
|
|
@@ -3583,13 +477,13 @@ ${fields.join("\n")}
|
|
|
3583
477
|
);
|
|
3584
478
|
const hasDefault = column.hasDefault || column.default !== void 0;
|
|
3585
479
|
const isAutoIncrement = column.autoIncrement || column.generatedAlwaysAs;
|
|
3586
|
-
const
|
|
3587
|
-
const shouldBeOptional = hasDefault || isAutoIncrement || isPrimaryKey && !
|
|
480
|
+
const isNotNull2 = column.notNull;
|
|
481
|
+
const shouldBeOptional = hasDefault || isAutoIncrement || isPrimaryKey && !isNotNull2;
|
|
3588
482
|
let insertFieldType;
|
|
3589
483
|
if (shouldBeOptional) {
|
|
3590
484
|
insertFieldType = typeStr.endsWith("!") ? typeStr.slice(0, -1) : typeStr;
|
|
3591
485
|
} else {
|
|
3592
|
-
insertFieldType =
|
|
486
|
+
insertFieldType = isNotNull2 && !typeStr.endsWith("!") ? `${typeStr}!` : typeStr;
|
|
3593
487
|
}
|
|
3594
488
|
insertFields.push(` ${columnName}: ${insertFieldType}`);
|
|
3595
489
|
}
|
|
@@ -3603,7 +497,7 @@ ${insertFields.join("\n")}
|
|
|
3603
497
|
const updateFields = [];
|
|
3604
498
|
for (const [columnName, column] of Object.entries(tableInfo.columns)) {
|
|
3605
499
|
const isPrimaryKey = column.primary || false;
|
|
3606
|
-
const typeStr =
|
|
500
|
+
const typeStr = columnToSDL(
|
|
3607
501
|
column,
|
|
3608
502
|
columnName,
|
|
3609
503
|
tableName,
|
|
@@ -3620,7 +514,7 @@ ${updateFields.join("\n")}
|
|
|
3620
514
|
const whereFields = [];
|
|
3621
515
|
for (const [columnName, column] of Object.entries(tableInfo.columns)) {
|
|
3622
516
|
const isPrimaryKey = column.primary || false;
|
|
3623
|
-
const typeStr =
|
|
517
|
+
const typeStr = columnToSDL(
|
|
3624
518
|
column,
|
|
3625
519
|
columnName,
|
|
3626
520
|
tableName,
|
|
@@ -3629,7 +523,7 @@ ${updateFields.join("\n")}
|
|
|
3629
523
|
);
|
|
3630
524
|
const normalizedType = typeStr.replace(/[^a-zA-Z0-9]/g, "");
|
|
3631
525
|
const filterTypeName = `${normalizedType}FieldFilter`;
|
|
3632
|
-
|
|
526
|
+
requiredFieldFilters.add(
|
|
3633
527
|
JSON.stringify({ normalizedType, baseType: typeStr })
|
|
3634
528
|
);
|
|
3635
529
|
whereFields.push(` ${columnName}: ${filterTypeName}`);
|
|
@@ -3651,14 +545,14 @@ ${orderByFields.join("\n")}
|
|
|
3651
545
|
}`);
|
|
3652
546
|
}
|
|
3653
547
|
const allDefs = [];
|
|
3654
|
-
|
|
3655
|
-
if (
|
|
3656
|
-
for (const scalarName of Array.from(
|
|
548
|
+
customScalars.add("JSON");
|
|
549
|
+
if (customScalars.size > 0) {
|
|
550
|
+
for (const scalarName of Array.from(customScalars).sort()) {
|
|
3657
551
|
allDefs.push(`scalar ${scalarName}`);
|
|
3658
552
|
}
|
|
3659
553
|
}
|
|
3660
|
-
if (
|
|
3661
|
-
for (const enumDef of Array.from(
|
|
554
|
+
if (enumDefinitions.size > 0) {
|
|
555
|
+
for (const enumDef of Array.from(enumDefinitions.values())) {
|
|
3662
556
|
const valueStrings = enumDef.values.map((v) => ` ${v}`);
|
|
3663
557
|
allDefs.push(`enum ${enumDef.name} {
|
|
3664
558
|
${valueStrings.join("\n")}
|
|
@@ -3677,7 +571,7 @@ ${valueStrings.join("\n")}
|
|
|
3677
571
|
priority: Int!
|
|
3678
572
|
}`);
|
|
3679
573
|
const filterTypesAdded = /* @__PURE__ */ new Set();
|
|
3680
|
-
for (const filterInfoJson of Array.from(
|
|
574
|
+
for (const filterInfoJson of Array.from(requiredFieldFilters)) {
|
|
3681
575
|
const { normalizedType, baseType } = JSON.parse(filterInfoJson);
|
|
3682
576
|
const filterTypeName = `${normalizedType}FieldFilter`;
|
|
3683
577
|
if (filterTypesAdded.has(filterTypeName))
|
|
@@ -3706,7 +600,7 @@ ${filterFields.join("\n")}
|
|
|
3706
600
|
allDefs.push(...typeDefs);
|
|
3707
601
|
return allDefs.join("\n\n");
|
|
3708
602
|
};
|
|
3709
|
-
var
|
|
603
|
+
var generateQueryTypeDefs = (tables) => {
|
|
3710
604
|
const queryFields = [];
|
|
3711
605
|
for (const tableName of Object.keys(tables)) {
|
|
3712
606
|
const typeName = capitalize(tableName);
|
|
@@ -3721,7 +615,7 @@ var generateQueryTypeDefs2 = (tables) => {
|
|
|
3721
615
|
${queryFields.join("\n")}
|
|
3722
616
|
}`;
|
|
3723
617
|
};
|
|
3724
|
-
var
|
|
618
|
+
var generateMutationTypeDefs = (tables) => {
|
|
3725
619
|
const mutationFields = [];
|
|
3726
620
|
for (const tableName of Object.keys(tables)) {
|
|
3727
621
|
const typeName = capitalize(tableName);
|
|
@@ -3741,33 +635,110 @@ ${mutationFields.join("\n")}
|
|
|
3741
635
|
};
|
|
3742
636
|
|
|
3743
637
|
// src/build-schema-sdl-with-dl/generator/queries/dataloader-resolvers.ts
|
|
3744
|
-
var
|
|
3745
|
-
var
|
|
3746
|
-
var
|
|
638
|
+
var import_graphql3 = require("graphql");
|
|
639
|
+
var import_graphql_parse_resolve_info = require("graphql-parse-resolve-info");
|
|
640
|
+
var import_drizzle_orm7 = require("drizzle-orm");
|
|
3747
641
|
|
|
3748
642
|
// src/build-schema-sdl-with-dl/generator/utils/selection.ts
|
|
3749
|
-
var
|
|
643
|
+
var import_drizzle_orm5 = require("drizzle-orm");
|
|
644
|
+
|
|
645
|
+
// src/build-schema-sdl-with-dl/generator/utils/filters.ts
|
|
646
|
+
var import_drizzle_orm4 = require("drizzle-orm");
|
|
647
|
+
var import_graphql2 = require("graphql");
|
|
648
|
+
|
|
649
|
+
// src/util/data-mappers/index.ts
|
|
650
|
+
var import_drizzle_orm3 = require("drizzle-orm");
|
|
651
|
+
var import_graphql = require("graphql");
|
|
652
|
+
var remapFromGraphQLCore = (value, column, columnName) => {
|
|
653
|
+
switch (column.dataType) {
|
|
654
|
+
case "date": {
|
|
655
|
+
const formatted = new Date(value);
|
|
656
|
+
if (Number.isNaN(formatted.getTime()))
|
|
657
|
+
throw new import_graphql.GraphQLError(`Field '${columnName}' is not a valid date!`);
|
|
658
|
+
return formatted;
|
|
659
|
+
}
|
|
660
|
+
case "buffer": {
|
|
661
|
+
if (!Array.isArray(value)) {
|
|
662
|
+
throw new import_graphql.GraphQLError(`Field '${columnName}' is not an array!`);
|
|
663
|
+
}
|
|
664
|
+
return Buffer.from(value);
|
|
665
|
+
}
|
|
666
|
+
case "json": {
|
|
667
|
+
if (column.columnType === "PgGeometryObject")
|
|
668
|
+
return value;
|
|
669
|
+
try {
|
|
670
|
+
return JSON.parse(value);
|
|
671
|
+
} catch (e) {
|
|
672
|
+
throw new import_graphql.GraphQLError(
|
|
673
|
+
`Invalid JSON in field '${columnName}':
|
|
674
|
+
${e instanceof Error ? e.message : "Unknown error"}`
|
|
675
|
+
);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
case "array": {
|
|
679
|
+
if (!Array.isArray(value)) {
|
|
680
|
+
throw new import_graphql.GraphQLError(`Field '${columnName}' is not an array!`);
|
|
681
|
+
}
|
|
682
|
+
if (column.columnType === "PgGeometry" && value.length !== 2) {
|
|
683
|
+
throw new import_graphql.GraphQLError(
|
|
684
|
+
`Invalid float tuple in field '${columnName}': expected array with length of 2, received ${value.length}`
|
|
685
|
+
);
|
|
686
|
+
}
|
|
687
|
+
return value;
|
|
688
|
+
}
|
|
689
|
+
case "bigint": {
|
|
690
|
+
try {
|
|
691
|
+
return BigInt(value);
|
|
692
|
+
} catch (error) {
|
|
693
|
+
throw new import_graphql.GraphQLError(`Field '${columnName}' is not a BigInt!`);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
default: {
|
|
697
|
+
return value;
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
};
|
|
701
|
+
var remapFromGraphQLSingleInput = (queryInput, table) => {
|
|
702
|
+
for (const [key, value] of Object.entries(queryInput)) {
|
|
703
|
+
if (value === void 0) {
|
|
704
|
+
delete queryInput[key];
|
|
705
|
+
} else {
|
|
706
|
+
const column = (0, import_drizzle_orm3.getTableColumns)(table)[key];
|
|
707
|
+
if (!column)
|
|
708
|
+
throw new import_graphql.GraphQLError(`Unknown column: ${key}`);
|
|
709
|
+
if (value === null && column.notNull) {
|
|
710
|
+
delete queryInput[key];
|
|
711
|
+
continue;
|
|
712
|
+
}
|
|
713
|
+
queryInput[key] = remapFromGraphQLCore(value, column, key);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
return queryInput;
|
|
717
|
+
};
|
|
718
|
+
var remapFromGraphQLArrayInput = (queryInput, table) => {
|
|
719
|
+
for (const entry of queryInput)
|
|
720
|
+
remapFromGraphQLSingleInput(entry, table);
|
|
721
|
+
return queryInput;
|
|
722
|
+
};
|
|
3750
723
|
|
|
3751
724
|
// src/build-schema-sdl-with-dl/generator/utils/filters.ts
|
|
3752
|
-
var
|
|
3753
|
-
var import_graphql11 = require("graphql");
|
|
3754
|
-
var extractFiltersColumn3 = (column, columnName, operators) => {
|
|
725
|
+
var extractFiltersColumn = (column, columnName, operators) => {
|
|
3755
726
|
const entries = Object.entries(operators);
|
|
3756
727
|
if (!entries.length)
|
|
3757
728
|
return void 0;
|
|
3758
729
|
if (operators.OR && operators.OR.length > 0) {
|
|
3759
730
|
if (entries.length > 1) {
|
|
3760
|
-
throw new
|
|
731
|
+
throw new import_graphql2.GraphQLError(
|
|
3761
732
|
`WHERE ${columnName}: Cannot specify both fields and 'OR' in column operators!`
|
|
3762
733
|
);
|
|
3763
734
|
}
|
|
3764
735
|
const variants2 = [];
|
|
3765
736
|
for (const variant of operators.OR) {
|
|
3766
|
-
const extracted =
|
|
737
|
+
const extracted = extractFiltersColumn(column, columnName, variant);
|
|
3767
738
|
if (extracted)
|
|
3768
739
|
variants2.push(extracted);
|
|
3769
740
|
}
|
|
3770
|
-
return variants2.length ? variants2.length > 1 ? (0,
|
|
741
|
+
return variants2.length ? variants2.length > 1 ? (0, import_drizzle_orm4.or)(...variants2) : variants2[0] : void 0;
|
|
3771
742
|
}
|
|
3772
743
|
const variants = [];
|
|
3773
744
|
for (const [operatorName, operatorValue] of entries) {
|
|
@@ -3785,7 +756,7 @@ var extractFiltersColumn3 = (column, columnName, operators) => {
|
|
|
3785
756
|
column,
|
|
3786
757
|
columnName
|
|
3787
758
|
);
|
|
3788
|
-
const opMap = { eq:
|
|
759
|
+
const opMap = { eq: import_drizzle_orm4.eq, ne: import_drizzle_orm4.ne, gt: import_drizzle_orm4.gt, gte: import_drizzle_orm4.gte, lt: import_drizzle_orm4.lt, lte: import_drizzle_orm4.lte };
|
|
3789
760
|
variants.push(opMap[operatorName](column, singleValue));
|
|
3790
761
|
break;
|
|
3791
762
|
}
|
|
@@ -3793,51 +764,51 @@ var extractFiltersColumn3 = (column, columnName, operators) => {
|
|
|
3793
764
|
case "notLike":
|
|
3794
765
|
case "ilike":
|
|
3795
766
|
case "notIlike": {
|
|
3796
|
-
const opMap = { like:
|
|
767
|
+
const opMap = { like: import_drizzle_orm4.like, notLike: import_drizzle_orm4.notLike, ilike: import_drizzle_orm4.ilike, notIlike: import_drizzle_orm4.notIlike };
|
|
3797
768
|
variants.push(opMap[operatorName](column, operatorValue));
|
|
3798
769
|
break;
|
|
3799
770
|
}
|
|
3800
771
|
case "inArray":
|
|
3801
772
|
case "notInArray": {
|
|
3802
773
|
if (!operatorValue.length) {
|
|
3803
|
-
throw new
|
|
774
|
+
throw new import_graphql2.GraphQLError(
|
|
3804
775
|
`WHERE ${columnName}: Unable to use operator ${operatorName} with an empty array!`
|
|
3805
776
|
);
|
|
3806
777
|
}
|
|
3807
778
|
const arrayValue = operatorValue.map(
|
|
3808
779
|
(val) => remapFromGraphQLCore(val, column, columnName)
|
|
3809
780
|
);
|
|
3810
|
-
const opMap = { inArray:
|
|
781
|
+
const opMap = { inArray: import_drizzle_orm4.inArray, notInArray: import_drizzle_orm4.notInArray };
|
|
3811
782
|
variants.push(opMap[operatorName](column, arrayValue));
|
|
3812
783
|
break;
|
|
3813
784
|
}
|
|
3814
785
|
case "isNull":
|
|
3815
786
|
case "isNotNull": {
|
|
3816
|
-
const opMap = { isNull:
|
|
787
|
+
const opMap = { isNull: import_drizzle_orm4.isNull, isNotNull: import_drizzle_orm4.isNotNull };
|
|
3817
788
|
variants.push(opMap[operatorName](column));
|
|
3818
789
|
break;
|
|
3819
790
|
}
|
|
3820
791
|
}
|
|
3821
792
|
}
|
|
3822
|
-
return variants.length ? variants.length > 1 ? (0,
|
|
793
|
+
return variants.length ? variants.length > 1 ? (0, import_drizzle_orm4.and)(...variants) : variants[0] : void 0;
|
|
3823
794
|
};
|
|
3824
|
-
var
|
|
795
|
+
var buildWhereClause = (tableInfo, where) => {
|
|
3825
796
|
if (!where || Object.keys(where).length === 0) {
|
|
3826
797
|
return void 0;
|
|
3827
798
|
}
|
|
3828
799
|
if (where.OR && where.OR.length > 0) {
|
|
3829
800
|
if (Object.keys(where).length > 1) {
|
|
3830
|
-
throw new
|
|
801
|
+
throw new import_graphql2.GraphQLError(
|
|
3831
802
|
`WHERE ${tableInfo.name}: Cannot specify both fields and 'OR' in table filters!`
|
|
3832
803
|
);
|
|
3833
804
|
}
|
|
3834
805
|
const variants = [];
|
|
3835
806
|
for (const variant of where.OR) {
|
|
3836
|
-
const extracted =
|
|
807
|
+
const extracted = buildWhereClause(tableInfo, variant);
|
|
3837
808
|
if (extracted)
|
|
3838
809
|
variants.push(extracted);
|
|
3839
810
|
}
|
|
3840
|
-
return variants.length ? variants.length > 1 ? (0,
|
|
811
|
+
return variants.length ? variants.length > 1 ? (0, import_drizzle_orm4.or)(...variants) : variants[0] : void 0;
|
|
3841
812
|
}
|
|
3842
813
|
const conditions = [];
|
|
3843
814
|
for (const [columnName, operators] of Object.entries(where)) {
|
|
@@ -3848,7 +819,7 @@ var buildWhereClause2 = (tableInfo, where) => {
|
|
|
3848
819
|
const column = tableInfo.columns[columnName];
|
|
3849
820
|
if (!column)
|
|
3850
821
|
continue;
|
|
3851
|
-
const extracted =
|
|
822
|
+
const extracted = extractFiltersColumn(
|
|
3852
823
|
column,
|
|
3853
824
|
columnName,
|
|
3854
825
|
operators
|
|
@@ -3860,11 +831,11 @@ var buildWhereClause2 = (tableInfo, where) => {
|
|
|
3860
831
|
return void 0;
|
|
3861
832
|
if (conditions.length === 1)
|
|
3862
833
|
return conditions[0];
|
|
3863
|
-
return (0,
|
|
834
|
+
return (0, import_drizzle_orm4.and)(...conditions);
|
|
3864
835
|
};
|
|
3865
836
|
|
|
3866
837
|
// src/build-schema-sdl-with-dl/generator/utils/selection.ts
|
|
3867
|
-
var
|
|
838
|
+
var buildOrderByClause = (tableInfo, orderBy) => {
|
|
3868
839
|
if (!orderBy || Object.keys(orderBy).length === 0) {
|
|
3869
840
|
return void 0;
|
|
3870
841
|
}
|
|
@@ -3878,7 +849,7 @@ var buildOrderByClause2 = (tableInfo, orderBy) => {
|
|
|
3878
849
|
const column = tableInfo.columns[entry.columnName];
|
|
3879
850
|
if (column) {
|
|
3880
851
|
orderClauses.push(
|
|
3881
|
-
entry.direction === "desc" ? (0,
|
|
852
|
+
entry.direction === "desc" ? (0, import_drizzle_orm5.desc)(column) : (0, import_drizzle_orm5.asc)(column)
|
|
3882
853
|
);
|
|
3883
854
|
}
|
|
3884
855
|
}
|
|
@@ -3887,7 +858,7 @@ var buildOrderByClause2 = (tableInfo, orderBy) => {
|
|
|
3887
858
|
|
|
3888
859
|
// src/build-schema-sdl-with-dl/generator/utils/dataloader.ts
|
|
3889
860
|
var import_dataloader = __toESM(require("dataloader"), 1);
|
|
3890
|
-
var
|
|
861
|
+
var import_drizzle_orm6 = require("drizzle-orm");
|
|
3891
862
|
var RelationDataLoader = class {
|
|
3892
863
|
constructor(queryBase, tableInfo, relations, context, debugConfig) {
|
|
3893
864
|
this.queryBase = queryBase;
|
|
@@ -3972,7 +943,7 @@ var RelationDataLoader = class {
|
|
|
3972
943
|
[firstKey.foreignKey]: true
|
|
3973
944
|
},
|
|
3974
945
|
where: whereClause,
|
|
3975
|
-
orderBy: firstKey.orderBy ?
|
|
946
|
+
orderBy: firstKey.orderBy ? buildOrderByClause(this.tableInfo, firstKey.orderBy) : void 0,
|
|
3976
947
|
limit: firstKey.limit,
|
|
3977
948
|
offset: firstKey.offset
|
|
3978
949
|
});
|
|
@@ -4008,12 +979,12 @@ var RelationDataLoader = class {
|
|
|
4008
979
|
console.error(`Available columns:`, Object.keys(this.tableInfo.columns));
|
|
4009
980
|
return void 0;
|
|
4010
981
|
}
|
|
4011
|
-
const parentIdClause = (0,
|
|
982
|
+
const parentIdClause = (0, import_drizzle_orm6.inArray)(foreignKeyColumn, parentIds);
|
|
4012
983
|
if (!additionalWhere) {
|
|
4013
984
|
return parentIdClause;
|
|
4014
985
|
}
|
|
4015
|
-
const additionalClause =
|
|
4016
|
-
return additionalClause ? (0,
|
|
986
|
+
const additionalClause = buildWhereClause(this.tableInfo, additionalWhere);
|
|
987
|
+
return additionalClause ? (0, import_drizzle_orm6.and)(parentIdClause, additionalClause) : parentIdClause;
|
|
4017
988
|
}
|
|
4018
989
|
async loadRelation(relationName, parentIds, foreignKey, options = {}, isReversedRelation = false) {
|
|
4019
990
|
const key = {
|
|
@@ -4052,7 +1023,7 @@ var createDataLoaderFindManyResolver = (queryBase, tableInfo, tables, relations,
|
|
|
4052
1023
|
return async (parent, args, context, info) => {
|
|
4053
1024
|
try {
|
|
4054
1025
|
const { where, orderBy, limit, offset } = args;
|
|
4055
|
-
const parsedInfo = (0,
|
|
1026
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info.parseResolveInfo)(info, {
|
|
4056
1027
|
deep: true
|
|
4057
1028
|
});
|
|
4058
1029
|
const allFields = {};
|
|
@@ -4069,8 +1040,8 @@ var createDataLoaderFindManyResolver = (queryBase, tableInfo, tables, relations,
|
|
|
4069
1040
|
columns: allColumns,
|
|
4070
1041
|
offset,
|
|
4071
1042
|
limit,
|
|
4072
|
-
orderBy:
|
|
4073
|
-
where:
|
|
1043
|
+
orderBy: buildOrderByClause(tableInfo, orderBy),
|
|
1044
|
+
where: buildWhereClause(tableInfo, where)
|
|
4074
1045
|
// No 'with' clause - we'll load relations separately
|
|
4075
1046
|
});
|
|
4076
1047
|
if (mainResults.length === 0) {
|
|
@@ -4088,7 +1059,7 @@ var createDataLoaderFindManyResolver = (queryBase, tableInfo, tables, relations,
|
|
|
4088
1059
|
return enhancedResults;
|
|
4089
1060
|
} catch (e) {
|
|
4090
1061
|
if (typeof e === "object" && e !== null && "message" in e) {
|
|
4091
|
-
throw new
|
|
1062
|
+
throw new import_graphql3.GraphQLError(String(e.message));
|
|
4092
1063
|
}
|
|
4093
1064
|
throw e;
|
|
4094
1065
|
}
|
|
@@ -4098,7 +1069,7 @@ var createDataLoaderFindFirstResolver = (queryBase, tableInfo, tables, relations
|
|
|
4098
1069
|
return async (parent, args, context, info) => {
|
|
4099
1070
|
try {
|
|
4100
1071
|
const { where, orderBy } = args;
|
|
4101
|
-
const parsedInfo = (0,
|
|
1072
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info.parseResolveInfo)(info, {
|
|
4102
1073
|
deep: true
|
|
4103
1074
|
});
|
|
4104
1075
|
const allFields = {};
|
|
@@ -4113,8 +1084,8 @@ var createDataLoaderFindFirstResolver = (queryBase, tableInfo, tables, relations
|
|
|
4113
1084
|
}
|
|
4114
1085
|
const mainResult = await queryBase.findFirst({
|
|
4115
1086
|
columns: allColumns,
|
|
4116
|
-
orderBy:
|
|
4117
|
-
where:
|
|
1087
|
+
orderBy: buildOrderByClause(tableInfo, orderBy),
|
|
1088
|
+
where: buildWhereClause(tableInfo, where)
|
|
4118
1089
|
// No 'with' clause
|
|
4119
1090
|
});
|
|
4120
1091
|
if (!mainResult) {
|
|
@@ -4132,7 +1103,7 @@ var createDataLoaderFindFirstResolver = (queryBase, tableInfo, tables, relations
|
|
|
4132
1103
|
return enhancedResult || null;
|
|
4133
1104
|
} catch (e) {
|
|
4134
1105
|
if (typeof e === "object" && e !== null && "message" in e) {
|
|
4135
|
-
throw new
|
|
1106
|
+
throw new import_graphql3.GraphQLError(String(e.message));
|
|
4136
1107
|
}
|
|
4137
1108
|
throw e;
|
|
4138
1109
|
}
|
|
@@ -4148,7 +1119,7 @@ async function loadRelationsWithDataLoader(mainResults, tableInfo, tables, relat
|
|
|
4148
1119
|
const relationField = fields[relName];
|
|
4149
1120
|
if (!relationField)
|
|
4150
1121
|
continue;
|
|
4151
|
-
if ((0,
|
|
1122
|
+
if ((0, import_drizzle_orm7.is)(relation, import_drizzle_orm7.One)) {
|
|
4152
1123
|
mainResult[relName] = null;
|
|
4153
1124
|
} else {
|
|
4154
1125
|
mainResult[relName] = [];
|
|
@@ -4193,7 +1164,7 @@ async function loadRelationsWithDataLoader(mainResults, tableInfo, tables, relat
|
|
|
4193
1164
|
console.log(`Processing relation ${relName} for table ${tableInfo.name} -> ${targetTableName}`);
|
|
4194
1165
|
}
|
|
4195
1166
|
if (relationConfig?.fields && relationConfig.fields.length > 0) {
|
|
4196
|
-
if ((0,
|
|
1167
|
+
if ((0, import_drizzle_orm7.is)(relation, import_drizzle_orm7.One)) {
|
|
4197
1168
|
const fieldColumn = relationConfig.fields[0];
|
|
4198
1169
|
const referenceColumn = relationConfig.references[0];
|
|
4199
1170
|
const referenceKeyName = Object.keys(targetTable.columns).find(
|
|
@@ -4222,7 +1193,7 @@ async function loadRelationsWithDataLoader(mainResults, tableInfo, tables, relat
|
|
|
4222
1193
|
console.log(`References-only relation: foreignKey=${foreignKeyName}, isReversed=${isReversedRelation}`);
|
|
4223
1194
|
}
|
|
4224
1195
|
} else {
|
|
4225
|
-
if ((0,
|
|
1196
|
+
if ((0, import_drizzle_orm7.is)(relation, import_drizzle_orm7.One)) {
|
|
4226
1197
|
let possibleForeignKeys = Object.entries(targetTable.columns).filter(([name, col]) => {
|
|
4227
1198
|
const lowerName = name.toLowerCase();
|
|
4228
1199
|
const tableName = tableInfo.name.toLowerCase();
|
|
@@ -4344,7 +1315,7 @@ async function loadRelationsWithDataLoader(mainResults, tableInfo, tables, relat
|
|
|
4344
1315
|
}
|
|
4345
1316
|
}
|
|
4346
1317
|
const relationData = relationMap.get(parentId) || [];
|
|
4347
|
-
if ((0,
|
|
1318
|
+
if ((0, import_drizzle_orm7.is)(relation, import_drizzle_orm7.One)) {
|
|
4348
1319
|
mainResult[relName] = relationData[0] || null;
|
|
4349
1320
|
} else {
|
|
4350
1321
|
mainResult[relName] = relationData;
|
|
@@ -4358,7 +1329,7 @@ async function loadRelationsWithDataLoader(mainResults, tableInfo, tables, relat
|
|
|
4358
1329
|
}
|
|
4359
1330
|
|
|
4360
1331
|
// src/build-schema-sdl-with-dl/generator/queries/index.ts
|
|
4361
|
-
var
|
|
1332
|
+
var generateQueries = (db, tables, relations, debugConfig) => {
|
|
4362
1333
|
const queries = {};
|
|
4363
1334
|
for (const [tableName, tableInfo] of Object.entries(tables)) {
|
|
4364
1335
|
const queryBase = db.query[tableName];
|
|
@@ -4386,14 +1357,14 @@ var generateQueries2 = (db, tables, relations, debugConfig) => {
|
|
|
4386
1357
|
};
|
|
4387
1358
|
|
|
4388
1359
|
// src/build-schema-sdl-with-dl/generator/mutations/resolvers.ts
|
|
4389
|
-
var
|
|
4390
|
-
var
|
|
1360
|
+
var import_graphql4 = require("graphql");
|
|
1361
|
+
var createInsertManyResolver = (db, queryBase, tableInfo, tables, relations, primaryKeyColumn, debugConfig) => {
|
|
4391
1362
|
const queryResolver = createDataLoaderFindManyResolver(queryBase, tableInfo, tables, relations, debugConfig);
|
|
4392
1363
|
return async (parent, args, context, info) => {
|
|
4393
1364
|
try {
|
|
4394
1365
|
const { values } = args;
|
|
4395
1366
|
if (!values || values.length === 0) {
|
|
4396
|
-
throw new
|
|
1367
|
+
throw new import_graphql4.GraphQLError("No values provided for insert");
|
|
4397
1368
|
}
|
|
4398
1369
|
const remappedValues = remapFromGraphQLArrayInput(
|
|
4399
1370
|
values,
|
|
@@ -4418,22 +1389,22 @@ var createInsertManyResolver2 = (db, queryBase, tableInfo, tables, relations, pr
|
|
|
4418
1389
|
return result;
|
|
4419
1390
|
} catch (e) {
|
|
4420
1391
|
if (typeof e === "object" && e !== null && "message" in e) {
|
|
4421
|
-
throw new
|
|
1392
|
+
throw new import_graphql4.GraphQLError(String(e.message));
|
|
4422
1393
|
}
|
|
4423
1394
|
throw e;
|
|
4424
1395
|
}
|
|
4425
1396
|
};
|
|
4426
1397
|
};
|
|
4427
|
-
var
|
|
1398
|
+
var createUpdateManyResolver = (db, queryBase, tableInfo, tables, relations, primaryKeyColumn, debugConfig) => {
|
|
4428
1399
|
const queryResolver = createDataLoaderFindManyResolver(queryBase, tableInfo, tables, relations, debugConfig);
|
|
4429
1400
|
return async (parent, args, context, info) => {
|
|
4430
1401
|
try {
|
|
4431
1402
|
const { where, set } = args;
|
|
4432
1403
|
if (!set || Object.keys(set).length === 0) {
|
|
4433
|
-
throw new
|
|
1404
|
+
throw new import_graphql4.GraphQLError("No values provided for update");
|
|
4434
1405
|
}
|
|
4435
1406
|
const remappedSet = remapFromGraphQLSingleInput(set, tableInfo.table);
|
|
4436
|
-
const whereClause =
|
|
1407
|
+
const whereClause = buildWhereClause(tableInfo, where);
|
|
4437
1408
|
let query = db.update(tableInfo.table).set(remappedSet);
|
|
4438
1409
|
if (whereClause) {
|
|
4439
1410
|
query = query.where(whereClause);
|
|
@@ -4457,18 +1428,18 @@ var createUpdateManyResolver2 = (db, queryBase, tableInfo, tables, relations, pr
|
|
|
4457
1428
|
return result;
|
|
4458
1429
|
} catch (e) {
|
|
4459
1430
|
if (typeof e === "object" && e !== null && "message" in e) {
|
|
4460
|
-
throw new
|
|
1431
|
+
throw new import_graphql4.GraphQLError(String(e.message));
|
|
4461
1432
|
}
|
|
4462
1433
|
throw e;
|
|
4463
1434
|
}
|
|
4464
1435
|
};
|
|
4465
1436
|
};
|
|
4466
|
-
var
|
|
1437
|
+
var createDeleteManyResolver = (db, queryBase, tableInfo, tables, relations, primaryKeyColumn, debugConfig) => {
|
|
4467
1438
|
const queryResolver = createDataLoaderFindManyResolver(queryBase, tableInfo, tables, relations, debugConfig);
|
|
4468
1439
|
return async (parent, args, context, info) => {
|
|
4469
1440
|
try {
|
|
4470
1441
|
const { where } = args;
|
|
4471
|
-
const whereClause =
|
|
1442
|
+
const whereClause = buildWhereClause(tableInfo, where);
|
|
4472
1443
|
let deleteQuery = db.delete(tableInfo.table);
|
|
4473
1444
|
if (whereClause) {
|
|
4474
1445
|
deleteQuery = deleteQuery.where(whereClause);
|
|
@@ -4485,7 +1456,7 @@ var createDeleteManyResolver2 = (db, queryBase, tableInfo, tables, relations, pr
|
|
|
4485
1456
|
};
|
|
4486
1457
|
} catch (e) {
|
|
4487
1458
|
if (typeof e === "object" && e !== null && "message" in e) {
|
|
4488
|
-
throw new
|
|
1459
|
+
throw new import_graphql4.GraphQLError(String(e.message));
|
|
4489
1460
|
}
|
|
4490
1461
|
throw e;
|
|
4491
1462
|
}
|
|
@@ -4493,7 +1464,7 @@ var createDeleteManyResolver2 = (db, queryBase, tableInfo, tables, relations, pr
|
|
|
4493
1464
|
};
|
|
4494
1465
|
|
|
4495
1466
|
// src/build-schema-sdl-with-dl/generator/mutations/index.ts
|
|
4496
|
-
var
|
|
1467
|
+
var generateMutations = (db, tables, relations, debugConfig) => {
|
|
4497
1468
|
const mutations = {};
|
|
4498
1469
|
const deleteResultResolvers = {};
|
|
4499
1470
|
for (const [tableName, tableInfo] of Object.entries(tables)) {
|
|
@@ -4512,7 +1483,7 @@ var generateMutations2 = (db, tables, relations, debugConfig) => {
|
|
|
4512
1483
|
`Drizzle-GraphQL Error: Table ${tableName} does not have a primary key column`
|
|
4513
1484
|
);
|
|
4514
1485
|
}
|
|
4515
|
-
mutations[`${tableName}InsertMany`] =
|
|
1486
|
+
mutations[`${tableName}InsertMany`] = createInsertManyResolver(
|
|
4516
1487
|
db,
|
|
4517
1488
|
queryBase,
|
|
4518
1489
|
tableInfo,
|
|
@@ -4521,7 +1492,7 @@ var generateMutations2 = (db, tables, relations, debugConfig) => {
|
|
|
4521
1492
|
primaryKeyColumn,
|
|
4522
1493
|
debugConfig
|
|
4523
1494
|
);
|
|
4524
|
-
mutations[`${tableName}UpdateMany`] =
|
|
1495
|
+
mutations[`${tableName}UpdateMany`] = createUpdateManyResolver(
|
|
4525
1496
|
db,
|
|
4526
1497
|
queryBase,
|
|
4527
1498
|
tableInfo,
|
|
@@ -4530,7 +1501,7 @@ var generateMutations2 = (db, tables, relations, debugConfig) => {
|
|
|
4530
1501
|
primaryKeyColumn,
|
|
4531
1502
|
debugConfig
|
|
4532
1503
|
);
|
|
4533
|
-
mutations[`${tableName}DeleteMany`] =
|
|
1504
|
+
mutations[`${tableName}DeleteMany`] = createDeleteManyResolver(
|
|
4534
1505
|
db,
|
|
4535
1506
|
queryBase,
|
|
4536
1507
|
tableInfo,
|
|
@@ -4556,27 +1527,27 @@ var generateMutations2 = (db, tables, relations, debugConfig) => {
|
|
|
4556
1527
|
var exportDirectiveTypeDefs = `directive @export(as: String!) on FIELD`;
|
|
4557
1528
|
|
|
4558
1529
|
// src/build-schema-sdl-with-dl/index.ts
|
|
4559
|
-
var
|
|
4560
|
-
var
|
|
1530
|
+
var import_schema2 = require("@graphql-tools/schema");
|
|
1531
|
+
var buildSchemaSDL = (db, config) => {
|
|
4561
1532
|
const schema = db._.fullSchema;
|
|
4562
1533
|
if (!schema) {
|
|
4563
1534
|
throw new Error(
|
|
4564
1535
|
"Drizzle-GraphQL Error: Schema not found in drizzle instance. Make sure you're using drizzle-orm v0.30.9 or above and schema is passed to drizzle constructor!"
|
|
4565
1536
|
);
|
|
4566
1537
|
}
|
|
4567
|
-
if (!(0,
|
|
1538
|
+
if (!(0, import_drizzle_orm8.is)(db, import_sqlite_core2.BaseSQLiteDatabase)) {
|
|
4568
1539
|
throw new Error(
|
|
4569
1540
|
"Drizzle-GraphQL Error: buildSchemaSDL currently only supports SQLite databases"
|
|
4570
1541
|
);
|
|
4571
1542
|
}
|
|
4572
|
-
const { tables, relations } =
|
|
1543
|
+
const { tables, relations } = generateTypes(db, schema);
|
|
4573
1544
|
const typeDefsArray = [];
|
|
4574
|
-
typeDefsArray.push(
|
|
4575
|
-
typeDefsArray.push(
|
|
4576
|
-
typeDefsArray.push(
|
|
1545
|
+
typeDefsArray.push(generateTypeDefs(tables, relations));
|
|
1546
|
+
typeDefsArray.push(generateQueryTypeDefs(tables));
|
|
1547
|
+
typeDefsArray.push(generateMutationTypeDefs(tables));
|
|
4577
1548
|
const typeDefs = typeDefsArray.join("\n\n");
|
|
4578
|
-
const queries =
|
|
4579
|
-
const { mutations, deleteResultResolvers } =
|
|
1549
|
+
const queries = generateQueries(db, tables, relations, config?.debug);
|
|
1550
|
+
const { mutations, deleteResultResolvers } = generateMutations(
|
|
4580
1551
|
db,
|
|
4581
1552
|
tables,
|
|
4582
1553
|
relations,
|
|
@@ -4650,6 +1621,7 @@ var useDataLoaderCleanupOnly = () => ({
|
|
|
4650
1621
|
var ExportStore = class {
|
|
4651
1622
|
store = /* @__PURE__ */ new Map();
|
|
4652
1623
|
pending = /* @__PURE__ */ new Map();
|
|
1624
|
+
accumulators = /* @__PURE__ */ new Map();
|
|
4653
1625
|
/**
|
|
4654
1626
|
* Store a value for later retrieval
|
|
4655
1627
|
* Resolves any pending promises waiting for this value
|
|
@@ -4662,6 +1634,32 @@ var ExportStore = class {
|
|
|
4662
1634
|
this.pending.delete(name);
|
|
4663
1635
|
}
|
|
4664
1636
|
}
|
|
1637
|
+
/**
|
|
1638
|
+
* Accumulate values into an array with deduplication
|
|
1639
|
+
* First call initializes an array, subsequent calls add to it
|
|
1640
|
+
* Use this when the same export variable is used in multiple array items
|
|
1641
|
+
*/
|
|
1642
|
+
accumulate(name, value) {
|
|
1643
|
+
if (!this.accumulators.has(name)) {
|
|
1644
|
+
this.accumulators.set(name, /* @__PURE__ */ new Set());
|
|
1645
|
+
}
|
|
1646
|
+
const accumulator = this.accumulators.get(name);
|
|
1647
|
+
if (Array.isArray(value)) {
|
|
1648
|
+
value.forEach((item) => {
|
|
1649
|
+
if (item !== null && item !== void 0) {
|
|
1650
|
+
accumulator.add(item);
|
|
1651
|
+
}
|
|
1652
|
+
});
|
|
1653
|
+
} else if (value !== null && value !== void 0) {
|
|
1654
|
+
accumulator.add(value);
|
|
1655
|
+
}
|
|
1656
|
+
const accumulatedArray = Array.from(accumulator);
|
|
1657
|
+
this.store.set(name, accumulatedArray);
|
|
1658
|
+
const callbacks = this.pending.get(name);
|
|
1659
|
+
if (callbacks) {
|
|
1660
|
+
callbacks.forEach((resolve) => resolve(accumulatedArray));
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
4665
1663
|
/**
|
|
4666
1664
|
* Get a value if it exists, otherwise return undefined
|
|
4667
1665
|
*/
|
|
@@ -4692,7 +1690,9 @@ var ExportStore = class {
|
|
|
4692
1690
|
if (allowNull) {
|
|
4693
1691
|
resolve(null);
|
|
4694
1692
|
} else {
|
|
4695
|
-
reject(
|
|
1693
|
+
reject(
|
|
1694
|
+
new Error(`Timeout waiting for export variable "${name}"`)
|
|
1695
|
+
);
|
|
4696
1696
|
}
|
|
4697
1697
|
}
|
|
4698
1698
|
}
|
|
@@ -4711,6 +1711,7 @@ var ExportStore = class {
|
|
|
4711
1711
|
clear() {
|
|
4712
1712
|
this.store.clear();
|
|
4713
1713
|
this.pending.clear();
|
|
1714
|
+
this.accumulators.clear();
|
|
4714
1715
|
}
|
|
4715
1716
|
/**
|
|
4716
1717
|
* Get all stored values
|
|
@@ -4750,17 +1751,23 @@ function createExportMiddleware() {
|
|
|
4750
1751
|
return result;
|
|
4751
1752
|
const selfExportName = getExportDirective(fieldNode);
|
|
4752
1753
|
if (selfExportName && result !== void 0) {
|
|
4753
|
-
|
|
1754
|
+
if (Array.isArray(result)) {
|
|
1755
|
+
result.forEach(
|
|
1756
|
+
(value) => exportStore.accumulate(selfExportName, value)
|
|
1757
|
+
);
|
|
1758
|
+
} else {
|
|
1759
|
+
exportStore.set(selfExportName, result);
|
|
1760
|
+
}
|
|
4754
1761
|
}
|
|
4755
1762
|
if (fieldNode.selectionSet && result !== void 0 && result !== null) {
|
|
4756
1763
|
if (Array.isArray(result)) {
|
|
4757
1764
|
result.forEach((item) => {
|
|
4758
1765
|
if (item && typeof item === "object") {
|
|
4759
|
-
processExports(item, fieldNode.selectionSet, exportStore);
|
|
1766
|
+
processExports(item, fieldNode.selectionSet, exportStore, true);
|
|
4760
1767
|
}
|
|
4761
1768
|
});
|
|
4762
1769
|
} else if (typeof result === "object") {
|
|
4763
|
-
processExports(result, fieldNode.selectionSet, exportStore);
|
|
1770
|
+
processExports(result, fieldNode.selectionSet, exportStore, false);
|
|
4764
1771
|
}
|
|
4765
1772
|
}
|
|
4766
1773
|
return result;
|
|
@@ -4772,10 +1779,10 @@ function createExportMiddleware() {
|
|
|
4772
1779
|
init_utils();
|
|
4773
1780
|
|
|
4774
1781
|
// src/export-tool/makeScalarAcceptExports.ts
|
|
4775
|
-
var
|
|
1782
|
+
var import_graphql5 = require("graphql");
|
|
4776
1783
|
function makeScalarAcceptExports(originalScalar) {
|
|
4777
1784
|
const config = originalScalar.toConfig();
|
|
4778
|
-
return new
|
|
1785
|
+
return new import_graphql5.GraphQLScalarType({
|
|
4779
1786
|
...config,
|
|
4780
1787
|
name: config.name,
|
|
4781
1788
|
// Keep original name to override it in schema
|
|
@@ -4791,7 +1798,7 @@ function makeScalarAcceptExports(originalScalar) {
|
|
|
4791
1798
|
return value;
|
|
4792
1799
|
},
|
|
4793
1800
|
parseLiteral(ast, variables) {
|
|
4794
|
-
if (ast.kind ===
|
|
1801
|
+
if (ast.kind === import_graphql5.Kind.STRING) {
|
|
4795
1802
|
if (ast.value.startsWith("$_") || ast.value === "") {
|
|
4796
1803
|
return ast.value;
|
|
4797
1804
|
}
|
|
@@ -4805,13 +1812,13 @@ function makeScalarAcceptExports(originalScalar) {
|
|
|
4805
1812
|
}
|
|
4806
1813
|
|
|
4807
1814
|
// src/helpers.ts
|
|
4808
|
-
var
|
|
1815
|
+
var import_drizzle_orm9 = require("drizzle-orm");
|
|
4809
1816
|
function setCustomGraphQL(table, columnConfig) {
|
|
4810
1817
|
for (const [columnName, config] of Object.entries(columnConfig)) {
|
|
4811
1818
|
const column = table[columnName];
|
|
4812
1819
|
if (!column) {
|
|
4813
1820
|
console.warn(
|
|
4814
|
-
`Warning: Column "${columnName}" not found in table "${(0,
|
|
1821
|
+
`Warning: Column "${columnName}" not found in table "${(0, import_drizzle_orm9.getTableName)(
|
|
4815
1822
|
table
|
|
4816
1823
|
)}"`
|
|
4817
1824
|
);
|
|
@@ -4833,8 +1840,6 @@ function setCustomGraphQLTypes(table, columnTypes) {
|
|
|
4833
1840
|
}
|
|
4834
1841
|
// Annotate the CommonJS export names for ESM import in node:
|
|
4835
1842
|
0 && (module.exports = {
|
|
4836
|
-
buildSchema,
|
|
4837
|
-
buildSchemaSDL,
|
|
4838
1843
|
buildSchemaSDLWithDataLoader,
|
|
4839
1844
|
cleanupDataLoaderContext,
|
|
4840
1845
|
createDataLoaderContext,
|