drizzle-graphql-plus 0.8.9 → 0.8.10
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 +2125 -0
- package/index.cjs.map +1 -0
- package/index.d.cts +251 -0
- package/index.d.ts +251 -0
- package/index.js +2177 -0
- package/index.js.map +1 -0
- package/package.json +16 -15
- package/.github/workflows/release.yaml +0 -74
- package/LICENSE +0 -201
- package/dprint.json +0 -31
- package/drizzle.test-config.ts +0 -29
- package/scripts/build.ts +0 -28
- package/src/index.ts +0 -74
- package/src/types.ts +0 -535
- package/src/util/builders/common.ts +0 -842
- package/src/util/builders/index.ts +0 -5
- package/src/util/builders/mysql.ts +0 -567
- package/src/util/builders/pg.ts +0 -616
- package/src/util/builders/sqlite.ts +0 -622
- package/src/util/builders/types.ts +0 -317
- package/src/util/case-ops/index.ts +0 -9
- package/src/util/data-mappers/index.ts +0 -162
- package/src/util/type-converter/index.ts +0 -148
- package/src/util/type-converter/types.ts +0 -54
- package/tests/mysql-custom.test.ts +0 -2009
- package/tests/mysql.test.ts +0 -4600
- package/tests/pg-custom.test.ts +0 -2054
- package/tests/pg.test.ts +0 -4285
- package/tests/schema/mysql.ts +0 -72
- package/tests/schema/pg.ts +0 -83
- package/tests/schema/sqlite.ts +0 -64
- package/tests/sqlite-custom.test.ts +0 -1956
- package/tests/sqlite.test.ts +0 -3749
- package/tests/tsconfig.json +0 -11
- package/tests/util/query/index.ts +0 -30
- package/tsconfig.build.json +0 -4
- package/tsconfig.dts.json +0 -13
- package/tsconfig.json +0 -48
- package/vitest.config.ts +0 -17
package/index.js
ADDED
|
@@ -0,0 +1,2177 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { is as is6 } from "drizzle-orm";
|
|
3
|
+
import { MySqlDatabase as MySqlDatabase2 } from "drizzle-orm/mysql-core";
|
|
4
|
+
import { PgDatabase as PgDatabase2 } from "drizzle-orm/pg-core";
|
|
5
|
+
import { BaseSQLiteDatabase as BaseSQLiteDatabase2 } from "drizzle-orm/sqlite-core";
|
|
6
|
+
import {
|
|
7
|
+
GraphQLObjectType as GraphQLObjectType6,
|
|
8
|
+
GraphQLSchema
|
|
9
|
+
} from "graphql";
|
|
10
|
+
|
|
11
|
+
// src/util/builders/mysql.ts
|
|
12
|
+
import {
|
|
13
|
+
createTableRelationsHelpers,
|
|
14
|
+
is as is3,
|
|
15
|
+
Relations
|
|
16
|
+
} from "drizzle-orm";
|
|
17
|
+
import { MySqlTable } from "drizzle-orm/mysql-core";
|
|
18
|
+
import {
|
|
19
|
+
GraphQLBoolean as GraphQLBoolean3,
|
|
20
|
+
GraphQLError as GraphQLError3,
|
|
21
|
+
GraphQLInt as GraphQLInt3,
|
|
22
|
+
GraphQLList as GraphQLList3,
|
|
23
|
+
GraphQLNonNull as GraphQLNonNull3,
|
|
24
|
+
GraphQLObjectType as GraphQLObjectType3
|
|
25
|
+
} from "graphql";
|
|
26
|
+
|
|
27
|
+
// src/util/builders/common.ts
|
|
28
|
+
import {
|
|
29
|
+
and,
|
|
30
|
+
asc,
|
|
31
|
+
desc,
|
|
32
|
+
eq,
|
|
33
|
+
getTableColumns as getTableColumns2,
|
|
34
|
+
gt,
|
|
35
|
+
gte,
|
|
36
|
+
ilike,
|
|
37
|
+
inArray,
|
|
38
|
+
is as is2,
|
|
39
|
+
isNotNull,
|
|
40
|
+
isNull,
|
|
41
|
+
like,
|
|
42
|
+
lt,
|
|
43
|
+
lte,
|
|
44
|
+
ne,
|
|
45
|
+
notIlike,
|
|
46
|
+
notInArray,
|
|
47
|
+
notLike,
|
|
48
|
+
One,
|
|
49
|
+
or
|
|
50
|
+
} from "drizzle-orm";
|
|
51
|
+
import {
|
|
52
|
+
GraphQLBoolean as GraphQLBoolean2,
|
|
53
|
+
GraphQLEnumType as GraphQLEnumType2,
|
|
54
|
+
GraphQLError as GraphQLError2,
|
|
55
|
+
GraphQLInputObjectType as GraphQLInputObjectType2,
|
|
56
|
+
GraphQLInt as GraphQLInt2,
|
|
57
|
+
GraphQLList as GraphQLList2,
|
|
58
|
+
GraphQLNonNull as GraphQLNonNull2,
|
|
59
|
+
GraphQLObjectType as GraphQLObjectType2,
|
|
60
|
+
GraphQLString as GraphQLString2,
|
|
61
|
+
GraphQLInterfaceType
|
|
62
|
+
} from "graphql";
|
|
63
|
+
|
|
64
|
+
// src/util/case-ops/index.ts
|
|
65
|
+
var uncapitalize = (input) => input.length ? `${input[0].toLocaleLowerCase()}${input.length > 1 ? input.slice(1, input.length) : ""}` : input;
|
|
66
|
+
var capitalize = (input) => input.length ? `${input[0].toLocaleUpperCase()}${input.length > 1 ? input.slice(1, input.length) : ""}` : input;
|
|
67
|
+
|
|
68
|
+
// src/util/data-mappers/index.ts
|
|
69
|
+
import { getTableColumns } from "drizzle-orm";
|
|
70
|
+
import { GraphQLError } from "graphql";
|
|
71
|
+
var remapToGraphQLCore = (key, value, tableName, column, relationMap) => {
|
|
72
|
+
if (value instanceof Date)
|
|
73
|
+
return value.toISOString();
|
|
74
|
+
if (value instanceof Buffer)
|
|
75
|
+
return Array.from(value);
|
|
76
|
+
if (typeof value === "bigint")
|
|
77
|
+
return value.toString();
|
|
78
|
+
if (Array.isArray(value)) {
|
|
79
|
+
const relations = relationMap?.[tableName];
|
|
80
|
+
if (relations?.[key]) {
|
|
81
|
+
return remapToGraphQLArrayOutput(
|
|
82
|
+
value,
|
|
83
|
+
relations[key].targetTableName,
|
|
84
|
+
relations[key].relation.referencedTable,
|
|
85
|
+
relationMap
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
if (column.columnType === "PgGeometry" || column.columnType === "PgVector")
|
|
89
|
+
return value;
|
|
90
|
+
return value.map((arrVal) => remapToGraphQLCore(key, arrVal, tableName, column, relationMap));
|
|
91
|
+
}
|
|
92
|
+
if (typeof value === "object") {
|
|
93
|
+
const relations = relationMap?.[tableName];
|
|
94
|
+
if (relations?.[key]) {
|
|
95
|
+
return remapToGraphQLSingleOutput(
|
|
96
|
+
value,
|
|
97
|
+
relations[key].targetTableName,
|
|
98
|
+
relations[key].relation.referencedTable,
|
|
99
|
+
relationMap
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
if (column.columnType === "PgGeometryObject")
|
|
103
|
+
return value;
|
|
104
|
+
return JSON.stringify(value);
|
|
105
|
+
}
|
|
106
|
+
return value;
|
|
107
|
+
};
|
|
108
|
+
var remapToGraphQLSingleOutput = (queryOutput, tableName, table, relationMap) => {
|
|
109
|
+
for (const [key, value] of Object.entries(queryOutput)) {
|
|
110
|
+
if (value === void 0 || value === null) {
|
|
111
|
+
delete queryOutput[key];
|
|
112
|
+
} else {
|
|
113
|
+
queryOutput[key] = remapToGraphQLCore(key, value, tableName, table[key], relationMap);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return queryOutput;
|
|
117
|
+
};
|
|
118
|
+
var remapToGraphQLArrayOutput = (queryOutput, tableName, table, relationMap) => {
|
|
119
|
+
for (const entry of queryOutput) {
|
|
120
|
+
remapToGraphQLSingleOutput(entry, tableName, table, relationMap);
|
|
121
|
+
}
|
|
122
|
+
return queryOutput;
|
|
123
|
+
};
|
|
124
|
+
var remapFromGraphQLCore = (value, column, columnName) => {
|
|
125
|
+
switch (column.dataType) {
|
|
126
|
+
case "date": {
|
|
127
|
+
const formatted = new Date(value);
|
|
128
|
+
if (Number.isNaN(formatted.getTime()))
|
|
129
|
+
throw new GraphQLError(`Field '${columnName}' is not a valid date!`);
|
|
130
|
+
return formatted;
|
|
131
|
+
}
|
|
132
|
+
case "buffer": {
|
|
133
|
+
if (!Array.isArray(value)) {
|
|
134
|
+
throw new GraphQLError(`Field '${columnName}' is not an array!`);
|
|
135
|
+
}
|
|
136
|
+
return Buffer.from(value);
|
|
137
|
+
}
|
|
138
|
+
case "json": {
|
|
139
|
+
if (column.columnType === "PgGeometryObject")
|
|
140
|
+
return value;
|
|
141
|
+
try {
|
|
142
|
+
return JSON.parse(value);
|
|
143
|
+
} catch (e) {
|
|
144
|
+
throw new GraphQLError(
|
|
145
|
+
`Invalid JSON in field '${columnName}':
|
|
146
|
+
${e instanceof Error ? e.message : "Unknown error"}`
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
case "array": {
|
|
151
|
+
if (!Array.isArray(value)) {
|
|
152
|
+
throw new GraphQLError(`Field '${columnName}' is not an array!`);
|
|
153
|
+
}
|
|
154
|
+
if (column.columnType === "PgGeometry" && value.length !== 2) {
|
|
155
|
+
throw new GraphQLError(
|
|
156
|
+
`Invalid float tuple in field '${columnName}': expected array with length of 2, received ${value.length}`
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
return value;
|
|
160
|
+
}
|
|
161
|
+
case "bigint": {
|
|
162
|
+
try {
|
|
163
|
+
return BigInt(value);
|
|
164
|
+
} catch (error) {
|
|
165
|
+
throw new GraphQLError(`Field '${columnName}' is not a BigInt!`);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
default: {
|
|
169
|
+
return value;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
var remapFromGraphQLSingleInput = (queryInput, table) => {
|
|
174
|
+
for (const [key, value] of Object.entries(queryInput)) {
|
|
175
|
+
if (value === void 0) {
|
|
176
|
+
delete queryInput[key];
|
|
177
|
+
} else {
|
|
178
|
+
const column = getTableColumns(table)[key];
|
|
179
|
+
if (!column)
|
|
180
|
+
throw new GraphQLError(`Unknown column: ${key}`);
|
|
181
|
+
if (value === null && column.notNull) {
|
|
182
|
+
delete queryInput[key];
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
queryInput[key] = remapFromGraphQLCore(value, column, key);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return queryInput;
|
|
189
|
+
};
|
|
190
|
+
var remapFromGraphQLArrayInput = (queryInput, table) => {
|
|
191
|
+
for (const entry of queryInput)
|
|
192
|
+
remapFromGraphQLSingleInput(entry, table);
|
|
193
|
+
return queryInput;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
// src/util/type-converter/index.ts
|
|
197
|
+
import { is } from "drizzle-orm";
|
|
198
|
+
import { MySqlInt, MySqlSerial } from "drizzle-orm/mysql-core";
|
|
199
|
+
import { PgInteger, PgSerial } from "drizzle-orm/pg-core";
|
|
200
|
+
import { SQLiteInteger } from "drizzle-orm/sqlite-core";
|
|
201
|
+
import {
|
|
202
|
+
GraphQLBoolean,
|
|
203
|
+
GraphQLEnumType,
|
|
204
|
+
GraphQLFloat,
|
|
205
|
+
GraphQLInputObjectType,
|
|
206
|
+
GraphQLInt,
|
|
207
|
+
GraphQLList,
|
|
208
|
+
GraphQLNonNull,
|
|
209
|
+
GraphQLObjectType,
|
|
210
|
+
GraphQLString
|
|
211
|
+
} from "graphql";
|
|
212
|
+
var allowedNameChars = /^[a-zA-Z0-9_]+$/;
|
|
213
|
+
var enumMap = /* @__PURE__ */ new WeakMap();
|
|
214
|
+
var generateEnumCached = (column, columnName, tableName) => {
|
|
215
|
+
if (enumMap.has(column))
|
|
216
|
+
return enumMap.get(column);
|
|
217
|
+
const gqlEnum = new GraphQLEnumType({
|
|
218
|
+
name: `${capitalize(tableName)}${capitalize(columnName)}Enum`,
|
|
219
|
+
values: Object.fromEntries(column.enumValues.map((e, index) => [allowedNameChars.test(e) ? e : `Option${index}`, {
|
|
220
|
+
value: e,
|
|
221
|
+
description: `Value: ${e}`
|
|
222
|
+
}]))
|
|
223
|
+
});
|
|
224
|
+
enumMap.set(column, gqlEnum);
|
|
225
|
+
return gqlEnum;
|
|
226
|
+
};
|
|
227
|
+
var geoXyType = new GraphQLObjectType({
|
|
228
|
+
name: "PgGeometryObject",
|
|
229
|
+
fields: {
|
|
230
|
+
x: { type: GraphQLFloat },
|
|
231
|
+
y: { type: GraphQLFloat }
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
var geoXyInputType = new GraphQLInputObjectType({
|
|
235
|
+
name: "PgGeometryObjectInput",
|
|
236
|
+
fields: {
|
|
237
|
+
x: { type: GraphQLFloat },
|
|
238
|
+
y: { type: GraphQLFloat }
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
var columnToGraphQLCore = (column, columnName, tableName, isInput) => {
|
|
242
|
+
switch (column.dataType) {
|
|
243
|
+
case "boolean":
|
|
244
|
+
return { type: GraphQLBoolean, description: "Boolean" };
|
|
245
|
+
case "json":
|
|
246
|
+
return column.columnType === "PgGeometryObject" ? {
|
|
247
|
+
type: isInput ? geoXyInputType : geoXyType,
|
|
248
|
+
description: "Geometry points XY"
|
|
249
|
+
} : { type: GraphQLString, description: "JSON" };
|
|
250
|
+
case "date":
|
|
251
|
+
return { type: GraphQLString, description: "Date" };
|
|
252
|
+
case "string":
|
|
253
|
+
if (column.enumValues?.length)
|
|
254
|
+
return { type: generateEnumCached(column, columnName, tableName) };
|
|
255
|
+
return { type: GraphQLString, description: "String" };
|
|
256
|
+
case "bigint":
|
|
257
|
+
return { type: GraphQLString, description: "BigInt" };
|
|
258
|
+
case "number":
|
|
259
|
+
return is(column, PgInteger) || is(column, PgSerial) || is(column, MySqlInt) || is(column, MySqlSerial) || is(column, SQLiteInteger) ? { type: GraphQLInt, description: "Integer" } : { type: GraphQLFloat, description: "Float" };
|
|
260
|
+
case "buffer":
|
|
261
|
+
return { type: new GraphQLList(new GraphQLNonNull(GraphQLInt)), description: "Buffer" };
|
|
262
|
+
case "array": {
|
|
263
|
+
if (column.columnType === "PgVector") {
|
|
264
|
+
return {
|
|
265
|
+
type: new GraphQLList(new GraphQLNonNull(GraphQLFloat)),
|
|
266
|
+
description: "Array<Float>"
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
if (column.columnType === "PgGeometry") {
|
|
270
|
+
return {
|
|
271
|
+
type: new GraphQLList(new GraphQLNonNull(GraphQLFloat)),
|
|
272
|
+
description: "Tuple<[Float, Float]>"
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
const innerType = columnToGraphQLCore(
|
|
276
|
+
column.baseColumn,
|
|
277
|
+
columnName,
|
|
278
|
+
tableName,
|
|
279
|
+
isInput
|
|
280
|
+
);
|
|
281
|
+
return {
|
|
282
|
+
type: new GraphQLList(new GraphQLNonNull(innerType.type)),
|
|
283
|
+
description: `Array<${innerType.description}>`
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
case "custom":
|
|
287
|
+
default:
|
|
288
|
+
throw new Error(`Drizzle-GraphQL Error: Type ${column.dataType} is not implemented!`);
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
var drizzleColumnToGraphQLType = (column, columnName, tableName, forceNullable = false, defaultIsNullable = false, isInput = false) => {
|
|
292
|
+
const typeDesc = columnToGraphQLCore(column, columnName, tableName, isInput);
|
|
293
|
+
const noDesc = ["string", "boolean", "number"];
|
|
294
|
+
if (noDesc.find((e) => e === column.dataType))
|
|
295
|
+
delete typeDesc.description;
|
|
296
|
+
if (forceNullable)
|
|
297
|
+
return typeDesc;
|
|
298
|
+
if (column.notNull && !(defaultIsNullable && (column.hasDefault || column.defaultFn))) {
|
|
299
|
+
return {
|
|
300
|
+
type: new GraphQLNonNull(typeDesc.type),
|
|
301
|
+
description: typeDesc.description
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
return typeDesc;
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
// src/util/builders/common.ts
|
|
308
|
+
var rqbCrashTypes = ["SQLiteBigInt", "SQLiteBlobJson", "SQLiteBlobBuffer"];
|
|
309
|
+
var extractSelectedColumnsFromTree = (tree, table) => {
|
|
310
|
+
const tableColumns = getTableColumns2(table);
|
|
311
|
+
const treeEntries = Object.entries(tree);
|
|
312
|
+
const selectedColumns = [];
|
|
313
|
+
for (const [fieldName, fieldData] of treeEntries) {
|
|
314
|
+
if (!tableColumns[fieldData.name])
|
|
315
|
+
continue;
|
|
316
|
+
selectedColumns.push([fieldData.name, true]);
|
|
317
|
+
}
|
|
318
|
+
if (!selectedColumns.length) {
|
|
319
|
+
const columnKeys = Object.entries(tableColumns);
|
|
320
|
+
const columnName = columnKeys.find(
|
|
321
|
+
(e) => rqbCrashTypes.find((haram) => e[1].columnType !== haram)
|
|
322
|
+
)?.[0] ?? columnKeys[0][0];
|
|
323
|
+
selectedColumns.push([columnName, true]);
|
|
324
|
+
}
|
|
325
|
+
return Object.fromEntries(selectedColumns);
|
|
326
|
+
};
|
|
327
|
+
var extractSelectedColumnsFromTreeSQLFormat = (tree, table) => {
|
|
328
|
+
const tableColumns = getTableColumns2(table);
|
|
329
|
+
const treeEntries = Object.entries(tree);
|
|
330
|
+
const selectedColumns = [];
|
|
331
|
+
for (const [fieldName, fieldData] of treeEntries) {
|
|
332
|
+
if (!tableColumns[fieldData.name])
|
|
333
|
+
continue;
|
|
334
|
+
selectedColumns.push([fieldData.name, tableColumns[fieldData.name]]);
|
|
335
|
+
}
|
|
336
|
+
if (!selectedColumns.length) {
|
|
337
|
+
const columnKeys = Object.entries(tableColumns);
|
|
338
|
+
const columnName = columnKeys.find(
|
|
339
|
+
(e) => rqbCrashTypes.find((haram) => e[1].columnType !== haram)
|
|
340
|
+
)?.[0] ?? columnKeys[0][0];
|
|
341
|
+
selectedColumns.push([columnName, tableColumns[columnName]]);
|
|
342
|
+
}
|
|
343
|
+
return Object.fromEntries(selectedColumns);
|
|
344
|
+
};
|
|
345
|
+
var innerOrder = new GraphQLInputObjectType2({
|
|
346
|
+
name: "InnerOrder",
|
|
347
|
+
fields: {
|
|
348
|
+
direction: {
|
|
349
|
+
type: new GraphQLNonNull2(
|
|
350
|
+
new GraphQLEnumType2({
|
|
351
|
+
name: "OrderDirection",
|
|
352
|
+
description: "Order by direction",
|
|
353
|
+
values: {
|
|
354
|
+
asc: {
|
|
355
|
+
value: "asc",
|
|
356
|
+
description: "Ascending order"
|
|
357
|
+
},
|
|
358
|
+
desc: {
|
|
359
|
+
value: "desc",
|
|
360
|
+
description: "Descending order"
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
})
|
|
364
|
+
)
|
|
365
|
+
},
|
|
366
|
+
priority: {
|
|
367
|
+
type: new GraphQLNonNull2(GraphQLInt2),
|
|
368
|
+
description: "Priority of current field"
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
var generateColumnFilterValues = (column, tableName, columnName) => {
|
|
373
|
+
const columnGraphQLType = drizzleColumnToGraphQLType(
|
|
374
|
+
column,
|
|
375
|
+
columnName,
|
|
376
|
+
tableName,
|
|
377
|
+
true,
|
|
378
|
+
false,
|
|
379
|
+
true
|
|
380
|
+
);
|
|
381
|
+
const columnArr = new GraphQLList2(new GraphQLNonNull2(columnGraphQLType.type));
|
|
382
|
+
const baseFields = {
|
|
383
|
+
eq: {
|
|
384
|
+
type: columnGraphQLType.type,
|
|
385
|
+
description: columnGraphQLType.description
|
|
386
|
+
},
|
|
387
|
+
ne: {
|
|
388
|
+
type: columnGraphQLType.type,
|
|
389
|
+
description: columnGraphQLType.description
|
|
390
|
+
},
|
|
391
|
+
lt: {
|
|
392
|
+
type: columnGraphQLType.type,
|
|
393
|
+
description: columnGraphQLType.description
|
|
394
|
+
},
|
|
395
|
+
lte: {
|
|
396
|
+
type: columnGraphQLType.type,
|
|
397
|
+
description: columnGraphQLType.description
|
|
398
|
+
},
|
|
399
|
+
gt: {
|
|
400
|
+
type: columnGraphQLType.type,
|
|
401
|
+
description: columnGraphQLType.description
|
|
402
|
+
},
|
|
403
|
+
gte: {
|
|
404
|
+
type: columnGraphQLType.type,
|
|
405
|
+
description: columnGraphQLType.description
|
|
406
|
+
},
|
|
407
|
+
like: { type: GraphQLString2 },
|
|
408
|
+
notLike: { type: GraphQLString2 },
|
|
409
|
+
ilike: { type: GraphQLString2 },
|
|
410
|
+
notIlike: { type: GraphQLString2 },
|
|
411
|
+
inArray: {
|
|
412
|
+
type: columnArr,
|
|
413
|
+
description: `Array<${columnGraphQLType.description}>`
|
|
414
|
+
},
|
|
415
|
+
notInArray: {
|
|
416
|
+
type: columnArr,
|
|
417
|
+
description: `Array<${columnGraphQLType.description}>`
|
|
418
|
+
},
|
|
419
|
+
isNull: { type: GraphQLBoolean2 },
|
|
420
|
+
isNotNull: { type: GraphQLBoolean2 }
|
|
421
|
+
};
|
|
422
|
+
const type = new GraphQLInputObjectType2({
|
|
423
|
+
name: `${capitalize(tableName)}${capitalize(columnName)}Filters`,
|
|
424
|
+
fields: {
|
|
425
|
+
...baseFields,
|
|
426
|
+
OR: {
|
|
427
|
+
type: new GraphQLList2(
|
|
428
|
+
new GraphQLNonNull2(
|
|
429
|
+
new GraphQLInputObjectType2({
|
|
430
|
+
name: `${capitalize(tableName)}${capitalize(
|
|
431
|
+
columnName
|
|
432
|
+
)}filtersOr`,
|
|
433
|
+
fields: {
|
|
434
|
+
...baseFields
|
|
435
|
+
}
|
|
436
|
+
})
|
|
437
|
+
)
|
|
438
|
+
)
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
});
|
|
442
|
+
return type;
|
|
443
|
+
};
|
|
444
|
+
var orderMap = /* @__PURE__ */ new WeakMap();
|
|
445
|
+
var generateTableOrderCached = (table) => {
|
|
446
|
+
if (orderMap.has(table))
|
|
447
|
+
return orderMap.get(table);
|
|
448
|
+
const columns = getTableColumns2(table);
|
|
449
|
+
const columnEntries = Object.entries(columns);
|
|
450
|
+
const remapped = Object.fromEntries(
|
|
451
|
+
columnEntries.map(([columnName, columnDescription]) => [
|
|
452
|
+
columnName,
|
|
453
|
+
{ type: innerOrder }
|
|
454
|
+
])
|
|
455
|
+
);
|
|
456
|
+
orderMap.set(table, remapped);
|
|
457
|
+
return remapped;
|
|
458
|
+
};
|
|
459
|
+
var filterMap = /* @__PURE__ */ new WeakMap();
|
|
460
|
+
var generateTableFilterValuesCached = (table, tableName) => {
|
|
461
|
+
if (filterMap.has(table))
|
|
462
|
+
return filterMap.get(table);
|
|
463
|
+
const columns = getTableColumns2(table);
|
|
464
|
+
const columnEntries = Object.entries(columns);
|
|
465
|
+
const remapped = Object.fromEntries(
|
|
466
|
+
columnEntries.map(([columnName, columnDescription]) => [
|
|
467
|
+
columnName,
|
|
468
|
+
{
|
|
469
|
+
type: generateColumnFilterValues(
|
|
470
|
+
columnDescription,
|
|
471
|
+
tableName,
|
|
472
|
+
columnName
|
|
473
|
+
)
|
|
474
|
+
}
|
|
475
|
+
])
|
|
476
|
+
);
|
|
477
|
+
filterMap.set(table, remapped);
|
|
478
|
+
return remapped;
|
|
479
|
+
};
|
|
480
|
+
var fieldMap = /* @__PURE__ */ new WeakMap();
|
|
481
|
+
var generateTableSelectTypeFieldsCached = (table, tableName) => {
|
|
482
|
+
if (fieldMap.has(table))
|
|
483
|
+
return fieldMap.get(table);
|
|
484
|
+
const columns = getTableColumns2(table);
|
|
485
|
+
const columnEntries = Object.entries(columns);
|
|
486
|
+
const remapped = Object.fromEntries(
|
|
487
|
+
columnEntries.map(([columnName, columnDescription]) => [
|
|
488
|
+
columnName,
|
|
489
|
+
drizzleColumnToGraphQLType(columnDescription, columnName, tableName)
|
|
490
|
+
])
|
|
491
|
+
);
|
|
492
|
+
fieldMap.set(table, remapped);
|
|
493
|
+
return remapped;
|
|
494
|
+
};
|
|
495
|
+
var orderTypeMap = /* @__PURE__ */ new WeakMap();
|
|
496
|
+
var generateTableOrderTypeCached = (table, tableName) => {
|
|
497
|
+
if (orderTypeMap.has(table))
|
|
498
|
+
return orderTypeMap.get(table);
|
|
499
|
+
const orderColumns = generateTableOrderCached(table);
|
|
500
|
+
const order = new GraphQLInputObjectType2({
|
|
501
|
+
name: `${capitalize(tableName)}OrderBy`,
|
|
502
|
+
fields: orderColumns
|
|
503
|
+
});
|
|
504
|
+
orderTypeMap.set(table, order);
|
|
505
|
+
return order;
|
|
506
|
+
};
|
|
507
|
+
var filterTypeMap = /* @__PURE__ */ new WeakMap();
|
|
508
|
+
var generateTableFilterTypeCached = (table, tableName) => {
|
|
509
|
+
if (filterTypeMap.has(table))
|
|
510
|
+
return filterTypeMap.get(table);
|
|
511
|
+
const filterColumns = generateTableFilterValuesCached(table, tableName);
|
|
512
|
+
const filters = new GraphQLInputObjectType2({
|
|
513
|
+
name: `${capitalize(tableName)}Filters`,
|
|
514
|
+
fields: {
|
|
515
|
+
...filterColumns,
|
|
516
|
+
OR: {
|
|
517
|
+
type: new GraphQLList2(
|
|
518
|
+
new GraphQLNonNull2(
|
|
519
|
+
new GraphQLInputObjectType2({
|
|
520
|
+
name: `${capitalize(tableName)}FiltersOr`,
|
|
521
|
+
fields: filterColumns
|
|
522
|
+
})
|
|
523
|
+
)
|
|
524
|
+
)
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
});
|
|
528
|
+
filterTypeMap.set(table, filters);
|
|
529
|
+
return filters;
|
|
530
|
+
};
|
|
531
|
+
var generateSelectFields = (tables, tableName, relationMap, typeName, withOrder, relationsDepthLimit, currentDepth = 0, usedTables = /* @__PURE__ */ new Set()) => {
|
|
532
|
+
const relations = relationMap[tableName];
|
|
533
|
+
const relationEntries = relations ? Object.entries(relations) : [];
|
|
534
|
+
const table = tables[tableName];
|
|
535
|
+
const order = withOrder ? generateTableOrderTypeCached(table, tableName) : void 0;
|
|
536
|
+
const filters = generateTableFilterTypeCached(table, tableName);
|
|
537
|
+
const tableFields = generateTableSelectTypeFieldsCached(table, tableName);
|
|
538
|
+
if (usedTables.has(tableName) || typeof relationsDepthLimit === "number" && currentDepth >= relationsDepthLimit || !relationEntries.length) {
|
|
539
|
+
return {
|
|
540
|
+
order,
|
|
541
|
+
filters,
|
|
542
|
+
tableFields,
|
|
543
|
+
relationFields: {}
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
const rawRelationFields = [];
|
|
547
|
+
const updatedUsedTables = new Set(usedTables).add(tableName);
|
|
548
|
+
const newDepth = currentDepth + 1;
|
|
549
|
+
for (const [relationName, { targetTableName, relation }] of relationEntries) {
|
|
550
|
+
const relTypeName = `${typeName}${capitalize(relationName)}Relation`;
|
|
551
|
+
const isOne = is2(relation, One);
|
|
552
|
+
const relData = generateSelectFields(
|
|
553
|
+
tables,
|
|
554
|
+
targetTableName,
|
|
555
|
+
relationMap,
|
|
556
|
+
relTypeName,
|
|
557
|
+
!isOne,
|
|
558
|
+
relationsDepthLimit,
|
|
559
|
+
newDepth,
|
|
560
|
+
updatedUsedTables
|
|
561
|
+
);
|
|
562
|
+
const relType = new GraphQLObjectType2({
|
|
563
|
+
name: relTypeName,
|
|
564
|
+
fields: { ...relData.tableFields, ...relData.relationFields }
|
|
565
|
+
});
|
|
566
|
+
if (isOne) {
|
|
567
|
+
rawRelationFields.push([
|
|
568
|
+
relationName,
|
|
569
|
+
{
|
|
570
|
+
type: relType,
|
|
571
|
+
args: {
|
|
572
|
+
where: { type: relData.filters }
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
]);
|
|
576
|
+
continue;
|
|
577
|
+
}
|
|
578
|
+
rawRelationFields.push([
|
|
579
|
+
relationName,
|
|
580
|
+
{
|
|
581
|
+
type: new GraphQLNonNull2(new GraphQLList2(new GraphQLNonNull2(relType))),
|
|
582
|
+
args: {
|
|
583
|
+
where: { type: relData.filters },
|
|
584
|
+
orderBy: { type: relData.order },
|
|
585
|
+
offset: { type: GraphQLInt2 },
|
|
586
|
+
limit: { type: GraphQLInt2 }
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
]);
|
|
590
|
+
}
|
|
591
|
+
const relationFields = Object.fromEntries(rawRelationFields);
|
|
592
|
+
return {
|
|
593
|
+
order,
|
|
594
|
+
filters,
|
|
595
|
+
tableFields,
|
|
596
|
+
relationFields
|
|
597
|
+
};
|
|
598
|
+
};
|
|
599
|
+
var generateTableTypes = (tableName, tables, relationMap, withReturning, relationsDepthLimit) => {
|
|
600
|
+
const stylizedName = capitalize(tableName);
|
|
601
|
+
const { tableFields, relationFields, filters, order } = generateSelectFields(
|
|
602
|
+
tables,
|
|
603
|
+
tableName,
|
|
604
|
+
relationMap,
|
|
605
|
+
stylizedName,
|
|
606
|
+
true,
|
|
607
|
+
relationsDepthLimit
|
|
608
|
+
);
|
|
609
|
+
const table = tables[tableName];
|
|
610
|
+
const columns = getTableColumns2(table);
|
|
611
|
+
const columnEntries = Object.entries(columns);
|
|
612
|
+
const insertFields = Object.fromEntries(
|
|
613
|
+
columnEntries.map(([columnName, columnDescription]) => [
|
|
614
|
+
columnName,
|
|
615
|
+
drizzleColumnToGraphQLType(
|
|
616
|
+
columnDescription,
|
|
617
|
+
columnName,
|
|
618
|
+
tableName,
|
|
619
|
+
false,
|
|
620
|
+
true,
|
|
621
|
+
true
|
|
622
|
+
)
|
|
623
|
+
])
|
|
624
|
+
);
|
|
625
|
+
const updateFields = Object.fromEntries(
|
|
626
|
+
columnEntries.map(([columnName, columnDescription]) => [
|
|
627
|
+
columnName,
|
|
628
|
+
drizzleColumnToGraphQLType(
|
|
629
|
+
columnDescription,
|
|
630
|
+
columnName,
|
|
631
|
+
tableName,
|
|
632
|
+
true,
|
|
633
|
+
false,
|
|
634
|
+
true
|
|
635
|
+
)
|
|
636
|
+
])
|
|
637
|
+
);
|
|
638
|
+
const insertInput = new GraphQLInputObjectType2({
|
|
639
|
+
name: `${stylizedName}InsertInput`,
|
|
640
|
+
fields: insertFields
|
|
641
|
+
});
|
|
642
|
+
const tableFieldsInterface = new GraphQLInterfaceType({
|
|
643
|
+
name: `${stylizedName}Fields`,
|
|
644
|
+
fields: tableFields
|
|
645
|
+
});
|
|
646
|
+
const selectSingleOutput = new GraphQLObjectType2({
|
|
647
|
+
name: `${stylizedName}SelectItem`,
|
|
648
|
+
fields: { ...tableFields, ...relationFields },
|
|
649
|
+
interfaces: [tableFieldsInterface]
|
|
650
|
+
});
|
|
651
|
+
const selectArrOutput = new GraphQLNonNull2(
|
|
652
|
+
new GraphQLList2(new GraphQLNonNull2(selectSingleOutput))
|
|
653
|
+
);
|
|
654
|
+
const singleTableItemOutput = withReturning ? new GraphQLObjectType2({
|
|
655
|
+
name: `${stylizedName}Item`,
|
|
656
|
+
fields: tableFields,
|
|
657
|
+
interfaces: [tableFieldsInterface]
|
|
658
|
+
}) : void 0;
|
|
659
|
+
const arrTableItemOutput = withReturning ? new GraphQLNonNull2(
|
|
660
|
+
new GraphQLList2(new GraphQLNonNull2(singleTableItemOutput))
|
|
661
|
+
) : void 0;
|
|
662
|
+
const updateInput = new GraphQLInputObjectType2({
|
|
663
|
+
name: `${stylizedName}UpdateInput`,
|
|
664
|
+
fields: updateFields
|
|
665
|
+
});
|
|
666
|
+
const inputs = {
|
|
667
|
+
insertInput,
|
|
668
|
+
updateInput,
|
|
669
|
+
tableOrder: order,
|
|
670
|
+
tableFilters: filters
|
|
671
|
+
};
|
|
672
|
+
const outputs = withReturning ? {
|
|
673
|
+
selectSingleOutput,
|
|
674
|
+
selectArrOutput,
|
|
675
|
+
singleTableItemOutput,
|
|
676
|
+
arrTableItemOutput,
|
|
677
|
+
tableFieldsInterface
|
|
678
|
+
} : {
|
|
679
|
+
selectSingleOutput,
|
|
680
|
+
selectArrOutput,
|
|
681
|
+
tableFieldsInterface
|
|
682
|
+
};
|
|
683
|
+
return {
|
|
684
|
+
inputs,
|
|
685
|
+
outputs
|
|
686
|
+
};
|
|
687
|
+
};
|
|
688
|
+
var extractOrderBy = (table, orderArgs) => {
|
|
689
|
+
const res = [];
|
|
690
|
+
for (const [column, config] of Object.entries(orderArgs).sort(
|
|
691
|
+
(a, b) => (b[1]?.priority ?? 0) - (a[1]?.priority ?? 0)
|
|
692
|
+
)) {
|
|
693
|
+
if (!config)
|
|
694
|
+
continue;
|
|
695
|
+
const { direction } = config;
|
|
696
|
+
res.push(
|
|
697
|
+
direction === "asc" ? asc(getTableColumns2(table)[column]) : desc(getTableColumns2(table)[column])
|
|
698
|
+
);
|
|
699
|
+
}
|
|
700
|
+
return res;
|
|
701
|
+
};
|
|
702
|
+
var extractFiltersColumn = (column, columnName, operators) => {
|
|
703
|
+
if (!operators.OR?.length)
|
|
704
|
+
delete operators.OR;
|
|
705
|
+
const entries = Object.entries(
|
|
706
|
+
operators
|
|
707
|
+
);
|
|
708
|
+
if (operators.OR) {
|
|
709
|
+
if (entries.length > 1) {
|
|
710
|
+
throw new GraphQLError2(
|
|
711
|
+
`WHERE ${columnName}: Cannot specify both fields and 'OR' in column operators!`
|
|
712
|
+
);
|
|
713
|
+
}
|
|
714
|
+
const variants2 = [];
|
|
715
|
+
for (const variant of operators.OR) {
|
|
716
|
+
const extracted = extractFiltersColumn(column, columnName, variant);
|
|
717
|
+
if (extracted)
|
|
718
|
+
variants2.push(extracted);
|
|
719
|
+
}
|
|
720
|
+
return variants2.length ? variants2.length > 1 ? or(...variants2) : variants2[0] : void 0;
|
|
721
|
+
}
|
|
722
|
+
const variants = [];
|
|
723
|
+
for (const [operatorName, operatorValue] of entries) {
|
|
724
|
+
if (operatorValue === null || operatorValue === false)
|
|
725
|
+
continue;
|
|
726
|
+
let operator;
|
|
727
|
+
switch (operatorName) {
|
|
728
|
+
case "eq":
|
|
729
|
+
operator = operator ?? eq;
|
|
730
|
+
case "ne":
|
|
731
|
+
operator = operator ?? ne;
|
|
732
|
+
case "gt":
|
|
733
|
+
operator = operator ?? gt;
|
|
734
|
+
case "gte":
|
|
735
|
+
operator = operator ?? gte;
|
|
736
|
+
case "lt":
|
|
737
|
+
operator = operator ?? lt;
|
|
738
|
+
case "lte":
|
|
739
|
+
operator = operator ?? lte;
|
|
740
|
+
const singleValue = remapFromGraphQLCore(
|
|
741
|
+
operatorValue,
|
|
742
|
+
column,
|
|
743
|
+
columnName
|
|
744
|
+
);
|
|
745
|
+
variants.push(operator(column, singleValue));
|
|
746
|
+
break;
|
|
747
|
+
case "like":
|
|
748
|
+
operator = operator ?? like;
|
|
749
|
+
case "notLike":
|
|
750
|
+
operator = operator ?? notLike;
|
|
751
|
+
case "ilike":
|
|
752
|
+
operator = operator ?? ilike;
|
|
753
|
+
case "notIlike":
|
|
754
|
+
operator = operator ?? notIlike;
|
|
755
|
+
variants.push(operator(column, operatorValue));
|
|
756
|
+
break;
|
|
757
|
+
case "inArray":
|
|
758
|
+
operator = operator ?? inArray;
|
|
759
|
+
case "notInArray":
|
|
760
|
+
operator = operator ?? notInArray;
|
|
761
|
+
if (!operatorValue.length) {
|
|
762
|
+
throw new GraphQLError2(
|
|
763
|
+
`WHERE ${columnName}: Unable to use operator ${operatorName} with an empty array!`
|
|
764
|
+
);
|
|
765
|
+
}
|
|
766
|
+
const arrayValue = operatorValue.map(
|
|
767
|
+
(val) => remapFromGraphQLCore(val, column, columnName)
|
|
768
|
+
);
|
|
769
|
+
variants.push(operator(column, arrayValue));
|
|
770
|
+
break;
|
|
771
|
+
case "isNull":
|
|
772
|
+
operator = operator ?? isNull;
|
|
773
|
+
case "isNotNull":
|
|
774
|
+
operator = operator ?? isNotNull;
|
|
775
|
+
variants.push(operator(column));
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
return variants.length ? variants.length > 1 ? and(...variants) : variants[0] : void 0;
|
|
779
|
+
};
|
|
780
|
+
var extractFilters = (table, tableName, filters) => {
|
|
781
|
+
if (!filters.OR?.length)
|
|
782
|
+
delete filters.OR;
|
|
783
|
+
const entries = Object.entries(filters);
|
|
784
|
+
if (!entries.length)
|
|
785
|
+
return;
|
|
786
|
+
if (filters.OR) {
|
|
787
|
+
if (entries.length > 1) {
|
|
788
|
+
throw new GraphQLError2(
|
|
789
|
+
`WHERE ${tableName}: Cannot specify both fields and 'OR' in table filters!`
|
|
790
|
+
);
|
|
791
|
+
}
|
|
792
|
+
const variants2 = [];
|
|
793
|
+
for (const variant of filters.OR) {
|
|
794
|
+
const extracted = extractFilters(table, tableName, variant);
|
|
795
|
+
if (extracted)
|
|
796
|
+
variants2.push(extracted);
|
|
797
|
+
}
|
|
798
|
+
return variants2.length ? variants2.length > 1 ? or(...variants2) : variants2[0] : void 0;
|
|
799
|
+
}
|
|
800
|
+
const variants = [];
|
|
801
|
+
for (const [columnName, operators] of entries) {
|
|
802
|
+
if (operators === null)
|
|
803
|
+
continue;
|
|
804
|
+
const column = getTableColumns2(table)[columnName];
|
|
805
|
+
variants.push(extractFiltersColumn(column, columnName, operators));
|
|
806
|
+
}
|
|
807
|
+
return variants.length ? variants.length > 1 ? and(...variants) : variants[0] : void 0;
|
|
808
|
+
};
|
|
809
|
+
var extractRelationsParamsInner = (relationMap, tables, tableName, typeName, originField, isInitial = false) => {
|
|
810
|
+
const relations = relationMap[tableName];
|
|
811
|
+
if (!relations)
|
|
812
|
+
return void 0;
|
|
813
|
+
const baseField = Object.entries(originField.fieldsByTypeName).find(
|
|
814
|
+
([key, value]) => key === typeName
|
|
815
|
+
)?.[1];
|
|
816
|
+
if (!baseField)
|
|
817
|
+
return void 0;
|
|
818
|
+
const args = {};
|
|
819
|
+
for (const [relName, { targetTableName, relation }] of Object.entries(
|
|
820
|
+
relations
|
|
821
|
+
)) {
|
|
822
|
+
const relTypeName = `${isInitial ? capitalize(tableName) : typeName}${capitalize(relName)}Relation`;
|
|
823
|
+
const relFieldSelection = Object.values(baseField).find(
|
|
824
|
+
(field) => field.name === relName
|
|
825
|
+
)?.fieldsByTypeName[relTypeName];
|
|
826
|
+
if (!relFieldSelection)
|
|
827
|
+
continue;
|
|
828
|
+
const columns = extractSelectedColumnsFromTree(
|
|
829
|
+
relFieldSelection,
|
|
830
|
+
tables[targetTableName]
|
|
831
|
+
);
|
|
832
|
+
const thisRecord = {};
|
|
833
|
+
thisRecord.columns = columns;
|
|
834
|
+
const relationField = Object.values(baseField).find(
|
|
835
|
+
(e) => e.name === relName
|
|
836
|
+
);
|
|
837
|
+
const relationArgs = relationField?.args;
|
|
838
|
+
const orderBy = relationArgs?.orderBy ? extractOrderBy(tables[targetTableName], relationArgs.orderBy) : void 0;
|
|
839
|
+
const where = relationArgs?.where ? extractFilters(tables[targetTableName], relName, relationArgs?.where) : void 0;
|
|
840
|
+
const offset = relationArgs?.offset ?? void 0;
|
|
841
|
+
const limit = relationArgs?.limit ?? void 0;
|
|
842
|
+
thisRecord.orderBy = orderBy;
|
|
843
|
+
thisRecord.where = where;
|
|
844
|
+
thisRecord.offset = offset;
|
|
845
|
+
thisRecord.limit = limit;
|
|
846
|
+
const relWith = relationField ? extractRelationsParamsInner(
|
|
847
|
+
relationMap,
|
|
848
|
+
tables,
|
|
849
|
+
targetTableName,
|
|
850
|
+
relTypeName,
|
|
851
|
+
relationField
|
|
852
|
+
) : void 0;
|
|
853
|
+
thisRecord.with = relWith;
|
|
854
|
+
args[relName] = thisRecord;
|
|
855
|
+
}
|
|
856
|
+
return args;
|
|
857
|
+
};
|
|
858
|
+
var extractRelationsParams = (relationMap, tables, tableName, info, typeName) => {
|
|
859
|
+
if (!info)
|
|
860
|
+
return void 0;
|
|
861
|
+
return extractRelationsParamsInner(
|
|
862
|
+
relationMap,
|
|
863
|
+
tables,
|
|
864
|
+
tableName,
|
|
865
|
+
typeName,
|
|
866
|
+
info,
|
|
867
|
+
true
|
|
868
|
+
);
|
|
869
|
+
};
|
|
870
|
+
|
|
871
|
+
// src/util/builders/mysql.ts
|
|
872
|
+
import { parseResolveInfo } from "graphql-parse-resolve-info";
|
|
873
|
+
var generateSelectArray = (db, tableName, tables, relationMap, orderArgs, filterArgs) => {
|
|
874
|
+
const queryName = `${uncapitalize(tableName)}`;
|
|
875
|
+
const queryBase = db.query[tableName];
|
|
876
|
+
if (!queryBase) {
|
|
877
|
+
throw new Error(
|
|
878
|
+
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
879
|
+
);
|
|
880
|
+
}
|
|
881
|
+
const queryArgs = {
|
|
882
|
+
offset: {
|
|
883
|
+
type: GraphQLInt3
|
|
884
|
+
},
|
|
885
|
+
limit: {
|
|
886
|
+
type: GraphQLInt3
|
|
887
|
+
},
|
|
888
|
+
orderBy: {
|
|
889
|
+
type: orderArgs
|
|
890
|
+
},
|
|
891
|
+
where: {
|
|
892
|
+
type: filterArgs
|
|
893
|
+
}
|
|
894
|
+
};
|
|
895
|
+
const typeName = `${capitalize(tableName)}SelectItem`;
|
|
896
|
+
const table = tables[tableName];
|
|
897
|
+
return {
|
|
898
|
+
name: queryName,
|
|
899
|
+
resolver: async (source, args, context, info) => {
|
|
900
|
+
try {
|
|
901
|
+
const { offset, limit, orderBy, where } = args;
|
|
902
|
+
const parsedInfo = parseResolveInfo(info, {
|
|
903
|
+
deep: true
|
|
904
|
+
});
|
|
905
|
+
const query = queryBase.findMany({
|
|
906
|
+
columns: extractSelectedColumnsFromTree(
|
|
907
|
+
parsedInfo.fieldsByTypeName[typeName],
|
|
908
|
+
table
|
|
909
|
+
),
|
|
910
|
+
offset,
|
|
911
|
+
limit,
|
|
912
|
+
orderBy: orderBy ? extractOrderBy(table, orderBy) : void 0,
|
|
913
|
+
where: where ? extractFilters(table, tableName, where) : void 0,
|
|
914
|
+
with: relationMap[tableName] ? extractRelationsParams(
|
|
915
|
+
relationMap,
|
|
916
|
+
tables,
|
|
917
|
+
tableName,
|
|
918
|
+
parsedInfo,
|
|
919
|
+
typeName
|
|
920
|
+
) : void 0
|
|
921
|
+
});
|
|
922
|
+
const result = await query;
|
|
923
|
+
return remapToGraphQLArrayOutput(result, tableName, table, relationMap);
|
|
924
|
+
} catch (e) {
|
|
925
|
+
if (typeof e === "object" && typeof e.message === "string") {
|
|
926
|
+
throw new GraphQLError3(e.message);
|
|
927
|
+
}
|
|
928
|
+
throw e;
|
|
929
|
+
}
|
|
930
|
+
},
|
|
931
|
+
args: queryArgs
|
|
932
|
+
};
|
|
933
|
+
};
|
|
934
|
+
var generateSelectSingle = (db, tableName, tables, relationMap, orderArgs, filterArgs) => {
|
|
935
|
+
const queryName = `${uncapitalize(tableName)}Single`;
|
|
936
|
+
const queryBase = db.query[tableName];
|
|
937
|
+
if (!queryBase) {
|
|
938
|
+
throw new Error(
|
|
939
|
+
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
940
|
+
);
|
|
941
|
+
}
|
|
942
|
+
const queryArgs = {
|
|
943
|
+
offset: {
|
|
944
|
+
type: GraphQLInt3
|
|
945
|
+
},
|
|
946
|
+
orderBy: {
|
|
947
|
+
type: orderArgs
|
|
948
|
+
},
|
|
949
|
+
where: {
|
|
950
|
+
type: filterArgs
|
|
951
|
+
}
|
|
952
|
+
};
|
|
953
|
+
const typeName = `${capitalize(tableName)}SelectItem`;
|
|
954
|
+
const table = tables[tableName];
|
|
955
|
+
return {
|
|
956
|
+
name: queryName,
|
|
957
|
+
resolver: async (source, args, context, info) => {
|
|
958
|
+
try {
|
|
959
|
+
const { offset, orderBy, where } = args;
|
|
960
|
+
const parsedInfo = parseResolveInfo(info, {
|
|
961
|
+
deep: true
|
|
962
|
+
});
|
|
963
|
+
const query = queryBase.findFirst({
|
|
964
|
+
columns: extractSelectedColumnsFromTree(
|
|
965
|
+
parsedInfo.fieldsByTypeName[typeName],
|
|
966
|
+
table
|
|
967
|
+
),
|
|
968
|
+
offset,
|
|
969
|
+
orderBy: orderBy ? extractOrderBy(table, orderBy) : void 0,
|
|
970
|
+
where: where ? extractFilters(table, tableName, where) : void 0,
|
|
971
|
+
with: relationMap[tableName] ? extractRelationsParams(
|
|
972
|
+
relationMap,
|
|
973
|
+
tables,
|
|
974
|
+
tableName,
|
|
975
|
+
parsedInfo,
|
|
976
|
+
typeName
|
|
977
|
+
) : void 0
|
|
978
|
+
});
|
|
979
|
+
const result = await query;
|
|
980
|
+
if (!result)
|
|
981
|
+
return void 0;
|
|
982
|
+
return remapToGraphQLSingleOutput(
|
|
983
|
+
result,
|
|
984
|
+
tableName,
|
|
985
|
+
table,
|
|
986
|
+
relationMap
|
|
987
|
+
);
|
|
988
|
+
} catch (e) {
|
|
989
|
+
if (typeof e === "object" && typeof e.message === "string") {
|
|
990
|
+
throw new GraphQLError3(e.message);
|
|
991
|
+
}
|
|
992
|
+
throw e;
|
|
993
|
+
}
|
|
994
|
+
},
|
|
995
|
+
args: queryArgs
|
|
996
|
+
};
|
|
997
|
+
};
|
|
998
|
+
var generateInsertArray = (db, tableName, table, baseType) => {
|
|
999
|
+
const queryName = `insertInto${capitalize(tableName)}`;
|
|
1000
|
+
const queryArgs = {
|
|
1001
|
+
values: {
|
|
1002
|
+
type: new GraphQLNonNull3(new GraphQLList3(new GraphQLNonNull3(baseType)))
|
|
1003
|
+
}
|
|
1004
|
+
};
|
|
1005
|
+
return {
|
|
1006
|
+
name: queryName,
|
|
1007
|
+
resolver: async (source, args, context, info) => {
|
|
1008
|
+
try {
|
|
1009
|
+
const input = remapFromGraphQLArrayInput(args.values, table);
|
|
1010
|
+
if (!input.length)
|
|
1011
|
+
throw new GraphQLError3("No values were provided!");
|
|
1012
|
+
await db.insert(table).values(input);
|
|
1013
|
+
return { isSuccess: true };
|
|
1014
|
+
} catch (e) {
|
|
1015
|
+
if (typeof e === "object" && typeof e.message === "string") {
|
|
1016
|
+
throw new GraphQLError3(e.message);
|
|
1017
|
+
}
|
|
1018
|
+
throw e;
|
|
1019
|
+
}
|
|
1020
|
+
},
|
|
1021
|
+
args: queryArgs
|
|
1022
|
+
};
|
|
1023
|
+
};
|
|
1024
|
+
var generateInsertSingle = (db, tableName, table, baseType) => {
|
|
1025
|
+
const queryName = `insertInto${capitalize(tableName)}Single`;
|
|
1026
|
+
const queryArgs = {
|
|
1027
|
+
values: {
|
|
1028
|
+
type: new GraphQLNonNull3(baseType)
|
|
1029
|
+
}
|
|
1030
|
+
};
|
|
1031
|
+
return {
|
|
1032
|
+
name: queryName,
|
|
1033
|
+
resolver: async (source, args, context, info) => {
|
|
1034
|
+
try {
|
|
1035
|
+
const input = remapFromGraphQLSingleInput(args.values, table);
|
|
1036
|
+
await db.insert(table).values(input);
|
|
1037
|
+
return { isSuccess: true };
|
|
1038
|
+
} catch (e) {
|
|
1039
|
+
if (typeof e === "object" && typeof e.message === "string") {
|
|
1040
|
+
throw new GraphQLError3(e.message);
|
|
1041
|
+
}
|
|
1042
|
+
throw e;
|
|
1043
|
+
}
|
|
1044
|
+
},
|
|
1045
|
+
args: queryArgs
|
|
1046
|
+
};
|
|
1047
|
+
};
|
|
1048
|
+
var generateUpdate = (db, tableName, table, setArgs, filterArgs) => {
|
|
1049
|
+
const queryName = `update${capitalize(tableName)}`;
|
|
1050
|
+
const queryArgs = {
|
|
1051
|
+
set: {
|
|
1052
|
+
type: new GraphQLNonNull3(setArgs)
|
|
1053
|
+
},
|
|
1054
|
+
where: {
|
|
1055
|
+
type: filterArgs
|
|
1056
|
+
}
|
|
1057
|
+
};
|
|
1058
|
+
return {
|
|
1059
|
+
name: queryName,
|
|
1060
|
+
resolver: async (source, args, context, info) => {
|
|
1061
|
+
try {
|
|
1062
|
+
const { where, set } = args;
|
|
1063
|
+
const input = remapFromGraphQLSingleInput(set, table);
|
|
1064
|
+
if (!Object.keys(input).length)
|
|
1065
|
+
throw new GraphQLError3("Unable to update with no values specified!");
|
|
1066
|
+
let query = db.update(table).set(input);
|
|
1067
|
+
if (where) {
|
|
1068
|
+
const filters = extractFilters(table, tableName, where);
|
|
1069
|
+
query = query.where(filters);
|
|
1070
|
+
}
|
|
1071
|
+
await query;
|
|
1072
|
+
return { isSuccess: true };
|
|
1073
|
+
} catch (e) {
|
|
1074
|
+
if (typeof e === "object" && typeof e.message === "string") {
|
|
1075
|
+
throw new GraphQLError3(e.message);
|
|
1076
|
+
}
|
|
1077
|
+
throw e;
|
|
1078
|
+
}
|
|
1079
|
+
},
|
|
1080
|
+
args: queryArgs
|
|
1081
|
+
};
|
|
1082
|
+
};
|
|
1083
|
+
var generateDelete = (db, tableName, table, filterArgs) => {
|
|
1084
|
+
const queryName = `deleteFrom${tableName}`;
|
|
1085
|
+
const queryArgs = {
|
|
1086
|
+
where: {
|
|
1087
|
+
type: filterArgs
|
|
1088
|
+
}
|
|
1089
|
+
};
|
|
1090
|
+
return {
|
|
1091
|
+
name: queryName,
|
|
1092
|
+
resolver: async (source, args, context, info) => {
|
|
1093
|
+
try {
|
|
1094
|
+
const { where } = args;
|
|
1095
|
+
let query = db.delete(table);
|
|
1096
|
+
if (where) {
|
|
1097
|
+
const filters = extractFilters(table, tableName, where);
|
|
1098
|
+
query = query.where(filters);
|
|
1099
|
+
}
|
|
1100
|
+
await query;
|
|
1101
|
+
return { isSuccess: true };
|
|
1102
|
+
} catch (e) {
|
|
1103
|
+
if (typeof e === "object" && typeof e.message === "string") {
|
|
1104
|
+
throw new GraphQLError3(e.message);
|
|
1105
|
+
}
|
|
1106
|
+
throw e;
|
|
1107
|
+
}
|
|
1108
|
+
},
|
|
1109
|
+
args: queryArgs
|
|
1110
|
+
};
|
|
1111
|
+
};
|
|
1112
|
+
var generateSchemaData = (db, schema, relationsDepthLimit) => {
|
|
1113
|
+
const rawSchema = schema;
|
|
1114
|
+
const schemaEntries = Object.entries(rawSchema);
|
|
1115
|
+
const tableEntries = schemaEntries.filter(
|
|
1116
|
+
([key, value]) => is3(value, MySqlTable)
|
|
1117
|
+
);
|
|
1118
|
+
const tables = Object.fromEntries(tableEntries);
|
|
1119
|
+
if (!tableEntries.length) {
|
|
1120
|
+
throw new Error(
|
|
1121
|
+
"Drizzle-GraphQL Error: No tables detected in Drizzle-ORM's database instance. Did you forget to pass schema to drizzle constructor?"
|
|
1122
|
+
);
|
|
1123
|
+
}
|
|
1124
|
+
const rawRelations = schemaEntries.filter(([key, value]) => is3(value, Relations)).map(([key, value]) => [
|
|
1125
|
+
tableEntries.find(
|
|
1126
|
+
([tableName, tableValue]) => tableValue === value.table
|
|
1127
|
+
)[0],
|
|
1128
|
+
value
|
|
1129
|
+
]).map(([tableName, relValue]) => [
|
|
1130
|
+
tableName,
|
|
1131
|
+
relValue.config(createTableRelationsHelpers(tables[tableName]))
|
|
1132
|
+
]);
|
|
1133
|
+
const namedRelations = Object.fromEntries(
|
|
1134
|
+
rawRelations.map(([relName, config]) => {
|
|
1135
|
+
const namedConfig = Object.fromEntries(
|
|
1136
|
+
Object.entries(config).map(([innerRelName, innerRelValue]) => [
|
|
1137
|
+
innerRelName,
|
|
1138
|
+
{
|
|
1139
|
+
relation: innerRelValue,
|
|
1140
|
+
targetTableName: tableEntries.find(
|
|
1141
|
+
([tableName, tableValue]) => tableValue === innerRelValue.referencedTable
|
|
1142
|
+
)[0]
|
|
1143
|
+
}
|
|
1144
|
+
])
|
|
1145
|
+
);
|
|
1146
|
+
return [relName, namedConfig];
|
|
1147
|
+
})
|
|
1148
|
+
);
|
|
1149
|
+
const queries = {};
|
|
1150
|
+
const mutations = {};
|
|
1151
|
+
const gqlSchemaTypes = Object.fromEntries(
|
|
1152
|
+
Object.entries(tables).map(([tableName, table]) => [
|
|
1153
|
+
tableName,
|
|
1154
|
+
generateTableTypes(
|
|
1155
|
+
tableName,
|
|
1156
|
+
tables,
|
|
1157
|
+
namedRelations,
|
|
1158
|
+
false,
|
|
1159
|
+
relationsDepthLimit
|
|
1160
|
+
)
|
|
1161
|
+
])
|
|
1162
|
+
);
|
|
1163
|
+
const mutationReturnType = new GraphQLObjectType3({
|
|
1164
|
+
name: `MutationReturn`,
|
|
1165
|
+
fields: {
|
|
1166
|
+
isSuccess: {
|
|
1167
|
+
type: new GraphQLNonNull3(GraphQLBoolean3)
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
});
|
|
1171
|
+
const inputs = {};
|
|
1172
|
+
const interfaces = {};
|
|
1173
|
+
const outputs = {
|
|
1174
|
+
MutationReturn: mutationReturnType
|
|
1175
|
+
};
|
|
1176
|
+
for (const [tableName, tableTypes] of Object.entries(gqlSchemaTypes)) {
|
|
1177
|
+
const { insertInput, updateInput, tableFilters, tableOrder } = tableTypes.inputs;
|
|
1178
|
+
const { selectSingleOutput, selectArrOutput, tableFieldsInterface } = tableTypes.outputs;
|
|
1179
|
+
const selectArrGenerated = generateSelectArray(
|
|
1180
|
+
db,
|
|
1181
|
+
tableName,
|
|
1182
|
+
tables,
|
|
1183
|
+
namedRelations,
|
|
1184
|
+
tableOrder,
|
|
1185
|
+
tableFilters
|
|
1186
|
+
);
|
|
1187
|
+
const selectSingleGenerated = generateSelectSingle(
|
|
1188
|
+
db,
|
|
1189
|
+
tableName,
|
|
1190
|
+
tables,
|
|
1191
|
+
namedRelations,
|
|
1192
|
+
tableOrder,
|
|
1193
|
+
tableFilters
|
|
1194
|
+
);
|
|
1195
|
+
const insertArrGenerated = generateInsertArray(
|
|
1196
|
+
db,
|
|
1197
|
+
tableName,
|
|
1198
|
+
schema[tableName],
|
|
1199
|
+
insertInput
|
|
1200
|
+
);
|
|
1201
|
+
const insertSingleGenerated = generateInsertSingle(
|
|
1202
|
+
db,
|
|
1203
|
+
tableName,
|
|
1204
|
+
schema[tableName],
|
|
1205
|
+
insertInput
|
|
1206
|
+
);
|
|
1207
|
+
const updateGenerated = generateUpdate(
|
|
1208
|
+
db,
|
|
1209
|
+
tableName,
|
|
1210
|
+
schema[tableName],
|
|
1211
|
+
updateInput,
|
|
1212
|
+
tableFilters
|
|
1213
|
+
);
|
|
1214
|
+
const deleteGenerated = generateDelete(
|
|
1215
|
+
db,
|
|
1216
|
+
tableName,
|
|
1217
|
+
schema[tableName],
|
|
1218
|
+
tableFilters
|
|
1219
|
+
);
|
|
1220
|
+
queries[selectArrGenerated.name] = {
|
|
1221
|
+
type: selectArrOutput,
|
|
1222
|
+
args: selectArrGenerated.args,
|
|
1223
|
+
resolve: selectArrGenerated.resolver
|
|
1224
|
+
};
|
|
1225
|
+
queries[selectSingleGenerated.name] = {
|
|
1226
|
+
type: selectSingleOutput,
|
|
1227
|
+
args: selectSingleGenerated.args,
|
|
1228
|
+
resolve: selectSingleGenerated.resolver
|
|
1229
|
+
};
|
|
1230
|
+
mutations[insertArrGenerated.name] = {
|
|
1231
|
+
type: mutationReturnType,
|
|
1232
|
+
args: insertArrGenerated.args,
|
|
1233
|
+
resolve: insertArrGenerated.resolver
|
|
1234
|
+
};
|
|
1235
|
+
mutations[insertSingleGenerated.name] = {
|
|
1236
|
+
type: mutationReturnType,
|
|
1237
|
+
args: insertSingleGenerated.args,
|
|
1238
|
+
resolve: insertSingleGenerated.resolver
|
|
1239
|
+
};
|
|
1240
|
+
mutations[updateGenerated.name] = {
|
|
1241
|
+
type: mutationReturnType,
|
|
1242
|
+
args: updateGenerated.args,
|
|
1243
|
+
resolve: updateGenerated.resolver
|
|
1244
|
+
};
|
|
1245
|
+
mutations[deleteGenerated.name] = {
|
|
1246
|
+
type: mutationReturnType,
|
|
1247
|
+
args: deleteGenerated.args,
|
|
1248
|
+
resolve: deleteGenerated.resolver
|
|
1249
|
+
};
|
|
1250
|
+
[insertInput, updateInput, tableFilters, tableOrder].forEach(
|
|
1251
|
+
(e) => inputs[e.name] = e
|
|
1252
|
+
);
|
|
1253
|
+
outputs[selectSingleOutput.name] = selectSingleOutput;
|
|
1254
|
+
interfaces[tableFieldsInterface.name] = tableFieldsInterface;
|
|
1255
|
+
}
|
|
1256
|
+
return { queries, mutations, inputs, interfaces, types: outputs };
|
|
1257
|
+
};
|
|
1258
|
+
|
|
1259
|
+
// src/util/builders/pg.ts
|
|
1260
|
+
import {
|
|
1261
|
+
createTableRelationsHelpers as createTableRelationsHelpers2,
|
|
1262
|
+
is as is4,
|
|
1263
|
+
Relations as Relations2
|
|
1264
|
+
} from "drizzle-orm";
|
|
1265
|
+
import { PgTable } from "drizzle-orm/pg-core";
|
|
1266
|
+
import {
|
|
1267
|
+
GraphQLError as GraphQLError4,
|
|
1268
|
+
GraphQLInt as GraphQLInt4,
|
|
1269
|
+
GraphQLList as GraphQLList4,
|
|
1270
|
+
GraphQLNonNull as GraphQLNonNull4
|
|
1271
|
+
} from "graphql";
|
|
1272
|
+
import { parseResolveInfo as parseResolveInfo2 } from "graphql-parse-resolve-info";
|
|
1273
|
+
var generateSelectArray2 = (db, tableName, tables, relationMap, orderArgs, filterArgs) => {
|
|
1274
|
+
const queryName = `${uncapitalize(tableName)}`;
|
|
1275
|
+
const queryBase = db.query[tableName];
|
|
1276
|
+
if (!queryBase) {
|
|
1277
|
+
throw new Error(
|
|
1278
|
+
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
1279
|
+
);
|
|
1280
|
+
}
|
|
1281
|
+
const queryArgs = {
|
|
1282
|
+
offset: {
|
|
1283
|
+
type: GraphQLInt4
|
|
1284
|
+
},
|
|
1285
|
+
limit: {
|
|
1286
|
+
type: GraphQLInt4
|
|
1287
|
+
},
|
|
1288
|
+
orderBy: {
|
|
1289
|
+
type: orderArgs
|
|
1290
|
+
},
|
|
1291
|
+
where: {
|
|
1292
|
+
type: filterArgs
|
|
1293
|
+
}
|
|
1294
|
+
};
|
|
1295
|
+
const typeName = `${capitalize(tableName)}SelectItem`;
|
|
1296
|
+
const table = tables[tableName];
|
|
1297
|
+
return {
|
|
1298
|
+
name: queryName,
|
|
1299
|
+
resolver: async (source, args, context, info) => {
|
|
1300
|
+
try {
|
|
1301
|
+
const { offset, limit, orderBy, where } = args;
|
|
1302
|
+
const parsedInfo = parseResolveInfo2(info, {
|
|
1303
|
+
deep: true
|
|
1304
|
+
});
|
|
1305
|
+
const query = queryBase.findMany({
|
|
1306
|
+
columns: extractSelectedColumnsFromTree(
|
|
1307
|
+
parsedInfo.fieldsByTypeName[typeName],
|
|
1308
|
+
table
|
|
1309
|
+
),
|
|
1310
|
+
offset,
|
|
1311
|
+
limit,
|
|
1312
|
+
orderBy: orderBy ? extractOrderBy(table, orderBy) : void 0,
|
|
1313
|
+
where: where ? extractFilters(table, tableName, where) : void 0,
|
|
1314
|
+
with: relationMap[tableName] ? extractRelationsParams(
|
|
1315
|
+
relationMap,
|
|
1316
|
+
tables,
|
|
1317
|
+
tableName,
|
|
1318
|
+
parsedInfo,
|
|
1319
|
+
typeName
|
|
1320
|
+
) : void 0
|
|
1321
|
+
});
|
|
1322
|
+
const result = await query;
|
|
1323
|
+
return remapToGraphQLArrayOutput(result, tableName, table, relationMap);
|
|
1324
|
+
} catch (e) {
|
|
1325
|
+
if (typeof e === "object" && typeof e.message === "string") {
|
|
1326
|
+
throw new GraphQLError4(e.message);
|
|
1327
|
+
}
|
|
1328
|
+
throw e;
|
|
1329
|
+
}
|
|
1330
|
+
},
|
|
1331
|
+
args: queryArgs
|
|
1332
|
+
};
|
|
1333
|
+
};
|
|
1334
|
+
var generateSelectSingle2 = (db, tableName, tables, relationMap, orderArgs, filterArgs) => {
|
|
1335
|
+
const queryName = `${uncapitalize(tableName)}Single`;
|
|
1336
|
+
const queryBase = db.query[tableName];
|
|
1337
|
+
if (!queryBase) {
|
|
1338
|
+
throw new Error(
|
|
1339
|
+
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
1340
|
+
);
|
|
1341
|
+
}
|
|
1342
|
+
const queryArgs = {
|
|
1343
|
+
offset: {
|
|
1344
|
+
type: GraphQLInt4
|
|
1345
|
+
},
|
|
1346
|
+
orderBy: {
|
|
1347
|
+
type: orderArgs
|
|
1348
|
+
},
|
|
1349
|
+
where: {
|
|
1350
|
+
type: filterArgs
|
|
1351
|
+
}
|
|
1352
|
+
};
|
|
1353
|
+
const typeName = `${capitalize(tableName)}SelectItem`;
|
|
1354
|
+
const table = tables[tableName];
|
|
1355
|
+
return {
|
|
1356
|
+
name: queryName,
|
|
1357
|
+
resolver: async (source, args, context, info) => {
|
|
1358
|
+
try {
|
|
1359
|
+
const { offset, orderBy, where } = args;
|
|
1360
|
+
const parsedInfo = parseResolveInfo2(info, {
|
|
1361
|
+
deep: true
|
|
1362
|
+
});
|
|
1363
|
+
const query = queryBase.findFirst({
|
|
1364
|
+
columns: extractSelectedColumnsFromTree(
|
|
1365
|
+
parsedInfo.fieldsByTypeName[typeName],
|
|
1366
|
+
table
|
|
1367
|
+
),
|
|
1368
|
+
offset,
|
|
1369
|
+
orderBy: orderBy ? extractOrderBy(table, orderBy) : void 0,
|
|
1370
|
+
where: where ? extractFilters(table, tableName, where) : void 0,
|
|
1371
|
+
with: relationMap[tableName] ? extractRelationsParams(
|
|
1372
|
+
relationMap,
|
|
1373
|
+
tables,
|
|
1374
|
+
tableName,
|
|
1375
|
+
parsedInfo,
|
|
1376
|
+
typeName
|
|
1377
|
+
) : void 0
|
|
1378
|
+
});
|
|
1379
|
+
const result = await query;
|
|
1380
|
+
if (!result)
|
|
1381
|
+
return void 0;
|
|
1382
|
+
return remapToGraphQLSingleOutput(
|
|
1383
|
+
result,
|
|
1384
|
+
tableName,
|
|
1385
|
+
table,
|
|
1386
|
+
relationMap
|
|
1387
|
+
);
|
|
1388
|
+
} catch (e) {
|
|
1389
|
+
if (typeof e === "object" && typeof e.message === "string") {
|
|
1390
|
+
throw new GraphQLError4(e.message);
|
|
1391
|
+
}
|
|
1392
|
+
throw e;
|
|
1393
|
+
}
|
|
1394
|
+
},
|
|
1395
|
+
args: queryArgs
|
|
1396
|
+
};
|
|
1397
|
+
};
|
|
1398
|
+
var generateInsertArray2 = (db, tableName, table, baseType) => {
|
|
1399
|
+
const queryName = `insertInto${capitalize(tableName)}`;
|
|
1400
|
+
const typeName = `${capitalize(tableName)}Item`;
|
|
1401
|
+
const queryArgs = {
|
|
1402
|
+
values: {
|
|
1403
|
+
type: new GraphQLNonNull4(new GraphQLList4(new GraphQLNonNull4(baseType)))
|
|
1404
|
+
}
|
|
1405
|
+
};
|
|
1406
|
+
return {
|
|
1407
|
+
name: queryName,
|
|
1408
|
+
resolver: async (source, args, context, info) => {
|
|
1409
|
+
try {
|
|
1410
|
+
const input = remapFromGraphQLArrayInput(args.values, table);
|
|
1411
|
+
if (!input.length)
|
|
1412
|
+
throw new GraphQLError4("No values were provided!");
|
|
1413
|
+
const parsedInfo = parseResolveInfo2(info, {
|
|
1414
|
+
deep: true
|
|
1415
|
+
});
|
|
1416
|
+
const columns = extractSelectedColumnsFromTreeSQLFormat(
|
|
1417
|
+
parsedInfo.fieldsByTypeName[typeName],
|
|
1418
|
+
table
|
|
1419
|
+
);
|
|
1420
|
+
const result = await db.insert(table).values(input).returning(columns).onConflictDoNothing();
|
|
1421
|
+
return remapToGraphQLArrayOutput(result, tableName, table);
|
|
1422
|
+
} catch (e) {
|
|
1423
|
+
if (typeof e === "object" && typeof e.message === "string") {
|
|
1424
|
+
throw new GraphQLError4(e.message);
|
|
1425
|
+
}
|
|
1426
|
+
throw e;
|
|
1427
|
+
}
|
|
1428
|
+
},
|
|
1429
|
+
args: queryArgs
|
|
1430
|
+
};
|
|
1431
|
+
};
|
|
1432
|
+
var generateInsertSingle2 = (db, tableName, table, baseType) => {
|
|
1433
|
+
const queryName = `insertInto${capitalize(tableName)}Single`;
|
|
1434
|
+
const typeName = `${capitalize(tableName)}Item`;
|
|
1435
|
+
const queryArgs = {
|
|
1436
|
+
values: {
|
|
1437
|
+
type: new GraphQLNonNull4(baseType)
|
|
1438
|
+
}
|
|
1439
|
+
};
|
|
1440
|
+
return {
|
|
1441
|
+
name: queryName,
|
|
1442
|
+
resolver: async (source, args, context, info) => {
|
|
1443
|
+
try {
|
|
1444
|
+
const input = remapFromGraphQLSingleInput(args.values, table);
|
|
1445
|
+
const parsedInfo = parseResolveInfo2(info, {
|
|
1446
|
+
deep: true
|
|
1447
|
+
});
|
|
1448
|
+
const columns = extractSelectedColumnsFromTreeSQLFormat(
|
|
1449
|
+
parsedInfo.fieldsByTypeName[typeName],
|
|
1450
|
+
table
|
|
1451
|
+
);
|
|
1452
|
+
const result = await db.insert(table).values(input).returning(columns).onConflictDoNothing();
|
|
1453
|
+
if (!result[0])
|
|
1454
|
+
return void 0;
|
|
1455
|
+
return remapToGraphQLSingleOutput(result[0], tableName, table);
|
|
1456
|
+
} catch (e) {
|
|
1457
|
+
if (typeof e === "object" && typeof e.message === "string") {
|
|
1458
|
+
throw new GraphQLError4(e.message);
|
|
1459
|
+
}
|
|
1460
|
+
throw e;
|
|
1461
|
+
}
|
|
1462
|
+
},
|
|
1463
|
+
args: queryArgs
|
|
1464
|
+
};
|
|
1465
|
+
};
|
|
1466
|
+
var generateUpdate2 = (db, tableName, table, setArgs, filterArgs) => {
|
|
1467
|
+
const queryName = `update${capitalize(tableName)}`;
|
|
1468
|
+
const typeName = `${capitalize(tableName)}Item`;
|
|
1469
|
+
const queryArgs = {
|
|
1470
|
+
set: {
|
|
1471
|
+
type: new GraphQLNonNull4(setArgs)
|
|
1472
|
+
},
|
|
1473
|
+
where: {
|
|
1474
|
+
type: filterArgs
|
|
1475
|
+
}
|
|
1476
|
+
};
|
|
1477
|
+
return {
|
|
1478
|
+
name: queryName,
|
|
1479
|
+
resolver: async (source, args, context, info) => {
|
|
1480
|
+
try {
|
|
1481
|
+
const { where, set } = args;
|
|
1482
|
+
const parsedInfo = parseResolveInfo2(info, {
|
|
1483
|
+
deep: true
|
|
1484
|
+
});
|
|
1485
|
+
const columns = extractSelectedColumnsFromTreeSQLFormat(
|
|
1486
|
+
parsedInfo.fieldsByTypeName[typeName],
|
|
1487
|
+
table
|
|
1488
|
+
);
|
|
1489
|
+
const input = remapFromGraphQLSingleInput(set, table);
|
|
1490
|
+
if (!Object.keys(input).length)
|
|
1491
|
+
throw new GraphQLError4("Unable to update with no values specified!");
|
|
1492
|
+
let query = db.update(table).set(input);
|
|
1493
|
+
if (where) {
|
|
1494
|
+
const filters = extractFilters(table, tableName, where);
|
|
1495
|
+
query = query.where(filters);
|
|
1496
|
+
}
|
|
1497
|
+
query = query.returning(columns);
|
|
1498
|
+
const result = await query;
|
|
1499
|
+
return remapToGraphQLArrayOutput(result, tableName, table);
|
|
1500
|
+
} catch (e) {
|
|
1501
|
+
if (typeof e === "object" && typeof e.message === "string") {
|
|
1502
|
+
throw new GraphQLError4(e.message);
|
|
1503
|
+
}
|
|
1504
|
+
throw e;
|
|
1505
|
+
}
|
|
1506
|
+
},
|
|
1507
|
+
args: queryArgs
|
|
1508
|
+
};
|
|
1509
|
+
};
|
|
1510
|
+
var generateDelete2 = (db, tableName, table, filterArgs) => {
|
|
1511
|
+
const queryName = `deleteFrom${capitalize(tableName)}`;
|
|
1512
|
+
const typeName = `${capitalize(tableName)}Item`;
|
|
1513
|
+
const queryArgs = {
|
|
1514
|
+
where: {
|
|
1515
|
+
type: filterArgs
|
|
1516
|
+
}
|
|
1517
|
+
};
|
|
1518
|
+
return {
|
|
1519
|
+
name: queryName,
|
|
1520
|
+
resolver: async (source, args, context, info) => {
|
|
1521
|
+
try {
|
|
1522
|
+
const { where } = args;
|
|
1523
|
+
const parsedInfo = parseResolveInfo2(info, {
|
|
1524
|
+
deep: true
|
|
1525
|
+
});
|
|
1526
|
+
const columns = extractSelectedColumnsFromTreeSQLFormat(
|
|
1527
|
+
parsedInfo.fieldsByTypeName[typeName],
|
|
1528
|
+
table
|
|
1529
|
+
);
|
|
1530
|
+
let query = db.delete(table);
|
|
1531
|
+
if (where) {
|
|
1532
|
+
const filters = extractFilters(table, tableName, where);
|
|
1533
|
+
query = query.where(filters);
|
|
1534
|
+
}
|
|
1535
|
+
query = query.returning(columns);
|
|
1536
|
+
const result = await query;
|
|
1537
|
+
return remapToGraphQLArrayOutput(result, tableName, table);
|
|
1538
|
+
} catch (e) {
|
|
1539
|
+
if (typeof e === "object" && typeof e.message === "string") {
|
|
1540
|
+
throw new GraphQLError4(e.message);
|
|
1541
|
+
}
|
|
1542
|
+
throw e;
|
|
1543
|
+
}
|
|
1544
|
+
},
|
|
1545
|
+
args: queryArgs
|
|
1546
|
+
};
|
|
1547
|
+
};
|
|
1548
|
+
var generateSchemaData2 = (db, schema, relationsDepthLimit) => {
|
|
1549
|
+
const rawSchema = schema;
|
|
1550
|
+
const schemaEntries = Object.entries(rawSchema);
|
|
1551
|
+
const tableEntries = schemaEntries.filter(
|
|
1552
|
+
([key, value]) => is4(value, PgTable)
|
|
1553
|
+
);
|
|
1554
|
+
const tables = Object.fromEntries(tableEntries);
|
|
1555
|
+
if (!tableEntries.length) {
|
|
1556
|
+
throw new Error(
|
|
1557
|
+
"Drizzle-GraphQL Error: No tables detected in Drizzle-ORM's database instance. Did you forget to pass schema to drizzle constructor?"
|
|
1558
|
+
);
|
|
1559
|
+
}
|
|
1560
|
+
const rawRelations = schemaEntries.filter(([key, value]) => is4(value, Relations2)).map(([key, value]) => [
|
|
1561
|
+
tableEntries.find(
|
|
1562
|
+
([tableName, tableValue]) => tableValue === value.table
|
|
1563
|
+
)[0],
|
|
1564
|
+
value
|
|
1565
|
+
]).map(([tableName, relValue]) => [
|
|
1566
|
+
tableName,
|
|
1567
|
+
relValue.config(createTableRelationsHelpers2(tables[tableName]))
|
|
1568
|
+
]);
|
|
1569
|
+
const namedRelations = Object.fromEntries(
|
|
1570
|
+
rawRelations.map(([relName, config]) => {
|
|
1571
|
+
const namedConfig = Object.fromEntries(
|
|
1572
|
+
Object.entries(config).map(([innerRelName, innerRelValue]) => [
|
|
1573
|
+
innerRelName,
|
|
1574
|
+
{
|
|
1575
|
+
relation: innerRelValue,
|
|
1576
|
+
targetTableName: tableEntries.find(
|
|
1577
|
+
([tableName, tableValue]) => tableValue === innerRelValue.referencedTable
|
|
1578
|
+
)[0]
|
|
1579
|
+
}
|
|
1580
|
+
])
|
|
1581
|
+
);
|
|
1582
|
+
return [relName, namedConfig];
|
|
1583
|
+
})
|
|
1584
|
+
);
|
|
1585
|
+
const queries = {};
|
|
1586
|
+
const mutations = {};
|
|
1587
|
+
const gqlSchemaTypes = Object.fromEntries(
|
|
1588
|
+
Object.entries(tables).map(([tableName, table]) => [
|
|
1589
|
+
tableName,
|
|
1590
|
+
generateTableTypes(
|
|
1591
|
+
tableName,
|
|
1592
|
+
tables,
|
|
1593
|
+
namedRelations,
|
|
1594
|
+
true,
|
|
1595
|
+
relationsDepthLimit
|
|
1596
|
+
)
|
|
1597
|
+
])
|
|
1598
|
+
);
|
|
1599
|
+
const inputs = {};
|
|
1600
|
+
const interfaces = {};
|
|
1601
|
+
const outputs = {};
|
|
1602
|
+
for (const [tableName, tableTypes] of Object.entries(gqlSchemaTypes)) {
|
|
1603
|
+
const { insertInput, updateInput, tableFilters, tableOrder } = tableTypes.inputs;
|
|
1604
|
+
const {
|
|
1605
|
+
selectSingleOutput,
|
|
1606
|
+
selectArrOutput,
|
|
1607
|
+
singleTableItemOutput,
|
|
1608
|
+
arrTableItemOutput,
|
|
1609
|
+
tableFieldsInterface
|
|
1610
|
+
} = tableTypes.outputs;
|
|
1611
|
+
const selectArrGenerated = generateSelectArray2(
|
|
1612
|
+
db,
|
|
1613
|
+
tableName,
|
|
1614
|
+
tables,
|
|
1615
|
+
namedRelations,
|
|
1616
|
+
tableOrder,
|
|
1617
|
+
tableFilters
|
|
1618
|
+
);
|
|
1619
|
+
const selectSingleGenerated = generateSelectSingle2(
|
|
1620
|
+
db,
|
|
1621
|
+
tableName,
|
|
1622
|
+
tables,
|
|
1623
|
+
namedRelations,
|
|
1624
|
+
tableOrder,
|
|
1625
|
+
tableFilters
|
|
1626
|
+
);
|
|
1627
|
+
const insertArrGenerated = generateInsertArray2(
|
|
1628
|
+
db,
|
|
1629
|
+
tableName,
|
|
1630
|
+
schema[tableName],
|
|
1631
|
+
insertInput
|
|
1632
|
+
);
|
|
1633
|
+
const insertSingleGenerated = generateInsertSingle2(
|
|
1634
|
+
db,
|
|
1635
|
+
tableName,
|
|
1636
|
+
schema[tableName],
|
|
1637
|
+
insertInput
|
|
1638
|
+
);
|
|
1639
|
+
const updateGenerated = generateUpdate2(
|
|
1640
|
+
db,
|
|
1641
|
+
tableName,
|
|
1642
|
+
schema[tableName],
|
|
1643
|
+
updateInput,
|
|
1644
|
+
tableFilters
|
|
1645
|
+
);
|
|
1646
|
+
const deleteGenerated = generateDelete2(
|
|
1647
|
+
db,
|
|
1648
|
+
tableName,
|
|
1649
|
+
schema[tableName],
|
|
1650
|
+
tableFilters
|
|
1651
|
+
);
|
|
1652
|
+
queries[selectArrGenerated.name] = {
|
|
1653
|
+
type: selectArrOutput,
|
|
1654
|
+
args: selectArrGenerated.args,
|
|
1655
|
+
resolve: selectArrGenerated.resolver
|
|
1656
|
+
};
|
|
1657
|
+
queries[selectSingleGenerated.name] = {
|
|
1658
|
+
type: selectSingleOutput,
|
|
1659
|
+
args: selectSingleGenerated.args,
|
|
1660
|
+
resolve: selectSingleGenerated.resolver
|
|
1661
|
+
};
|
|
1662
|
+
mutations[insertArrGenerated.name] = {
|
|
1663
|
+
type: arrTableItemOutput,
|
|
1664
|
+
args: insertArrGenerated.args,
|
|
1665
|
+
resolve: insertArrGenerated.resolver
|
|
1666
|
+
};
|
|
1667
|
+
mutations[insertSingleGenerated.name] = {
|
|
1668
|
+
type: singleTableItemOutput,
|
|
1669
|
+
args: insertSingleGenerated.args,
|
|
1670
|
+
resolve: insertSingleGenerated.resolver
|
|
1671
|
+
};
|
|
1672
|
+
mutations[updateGenerated.name] = {
|
|
1673
|
+
type: arrTableItemOutput,
|
|
1674
|
+
args: updateGenerated.args,
|
|
1675
|
+
resolve: updateGenerated.resolver
|
|
1676
|
+
};
|
|
1677
|
+
mutations[deleteGenerated.name] = {
|
|
1678
|
+
type: arrTableItemOutput,
|
|
1679
|
+
args: deleteGenerated.args,
|
|
1680
|
+
resolve: deleteGenerated.resolver
|
|
1681
|
+
};
|
|
1682
|
+
[insertInput, updateInput, tableFilters, tableOrder].forEach(
|
|
1683
|
+
(e) => inputs[e.name] = e
|
|
1684
|
+
);
|
|
1685
|
+
outputs[selectSingleOutput.name] = selectSingleOutput;
|
|
1686
|
+
outputs[singleTableItemOutput.name] = singleTableItemOutput;
|
|
1687
|
+
interfaces[tableFieldsInterface.name] = tableFieldsInterface;
|
|
1688
|
+
}
|
|
1689
|
+
return { queries, mutations, inputs, interfaces, types: outputs };
|
|
1690
|
+
};
|
|
1691
|
+
|
|
1692
|
+
// src/util/builders/sqlite.ts
|
|
1693
|
+
import {
|
|
1694
|
+
createTableRelationsHelpers as createTableRelationsHelpers3,
|
|
1695
|
+
is as is5,
|
|
1696
|
+
Relations as Relations3
|
|
1697
|
+
} from "drizzle-orm";
|
|
1698
|
+
import {
|
|
1699
|
+
SQLiteTable
|
|
1700
|
+
} from "drizzle-orm/sqlite-core";
|
|
1701
|
+
import {
|
|
1702
|
+
GraphQLError as GraphQLError5,
|
|
1703
|
+
GraphQLInt as GraphQLInt5,
|
|
1704
|
+
GraphQLList as GraphQLList5,
|
|
1705
|
+
GraphQLNonNull as GraphQLNonNull5
|
|
1706
|
+
} from "graphql";
|
|
1707
|
+
import { parseResolveInfo as parseResolveInfo3 } from "graphql-parse-resolve-info";
|
|
1708
|
+
var generateSelectArray3 = (db, tableName, tables, relationMap, orderArgs, filterArgs) => {
|
|
1709
|
+
const queryName = `${uncapitalize(tableName)}`;
|
|
1710
|
+
const queryBase = db.query[tableName];
|
|
1711
|
+
if (!queryBase) {
|
|
1712
|
+
throw new Error(
|
|
1713
|
+
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
1714
|
+
);
|
|
1715
|
+
}
|
|
1716
|
+
const queryArgs = {
|
|
1717
|
+
offset: {
|
|
1718
|
+
type: GraphQLInt5
|
|
1719
|
+
},
|
|
1720
|
+
limit: {
|
|
1721
|
+
type: GraphQLInt5
|
|
1722
|
+
},
|
|
1723
|
+
orderBy: {
|
|
1724
|
+
type: orderArgs
|
|
1725
|
+
},
|
|
1726
|
+
where: {
|
|
1727
|
+
type: filterArgs
|
|
1728
|
+
}
|
|
1729
|
+
};
|
|
1730
|
+
const typeName = `${capitalize(tableName)}SelectItem`;
|
|
1731
|
+
const table = tables[tableName];
|
|
1732
|
+
return {
|
|
1733
|
+
name: queryName,
|
|
1734
|
+
resolver: async (source, args, context, info) => {
|
|
1735
|
+
try {
|
|
1736
|
+
const { offset, limit, orderBy, where } = args;
|
|
1737
|
+
const parsedInfo = parseResolveInfo3(info, {
|
|
1738
|
+
deep: true
|
|
1739
|
+
});
|
|
1740
|
+
const query = queryBase.findMany({
|
|
1741
|
+
columns: extractSelectedColumnsFromTree(
|
|
1742
|
+
parsedInfo.fieldsByTypeName[typeName],
|
|
1743
|
+
table
|
|
1744
|
+
),
|
|
1745
|
+
offset,
|
|
1746
|
+
limit,
|
|
1747
|
+
orderBy: orderBy ? extractOrderBy(table, orderBy) : void 0,
|
|
1748
|
+
where: where ? extractFilters(table, tableName, where) : void 0,
|
|
1749
|
+
with: relationMap[tableName] ? extractRelationsParams(
|
|
1750
|
+
relationMap,
|
|
1751
|
+
tables,
|
|
1752
|
+
tableName,
|
|
1753
|
+
parsedInfo,
|
|
1754
|
+
typeName
|
|
1755
|
+
) : void 0
|
|
1756
|
+
});
|
|
1757
|
+
const result = await query;
|
|
1758
|
+
return remapToGraphQLArrayOutput(result, tableName, table, relationMap);
|
|
1759
|
+
} catch (e) {
|
|
1760
|
+
if (typeof e === "object" && typeof e.message === "string") {
|
|
1761
|
+
throw new GraphQLError5(e.message);
|
|
1762
|
+
}
|
|
1763
|
+
throw e;
|
|
1764
|
+
}
|
|
1765
|
+
},
|
|
1766
|
+
args: queryArgs
|
|
1767
|
+
};
|
|
1768
|
+
};
|
|
1769
|
+
var generateSelectSingle3 = (db, tableName, tables, relationMap, orderArgs, filterArgs) => {
|
|
1770
|
+
const queryName = `${uncapitalize(tableName)}Single`;
|
|
1771
|
+
const queryBase = db.query[tableName];
|
|
1772
|
+
if (!queryBase) {
|
|
1773
|
+
throw new Error(
|
|
1774
|
+
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
1775
|
+
);
|
|
1776
|
+
}
|
|
1777
|
+
const queryArgs = {
|
|
1778
|
+
offset: {
|
|
1779
|
+
type: GraphQLInt5
|
|
1780
|
+
},
|
|
1781
|
+
orderBy: {
|
|
1782
|
+
type: orderArgs
|
|
1783
|
+
},
|
|
1784
|
+
where: {
|
|
1785
|
+
type: filterArgs
|
|
1786
|
+
}
|
|
1787
|
+
};
|
|
1788
|
+
const typeName = `${capitalize(tableName)}SelectItem`;
|
|
1789
|
+
const table = tables[tableName];
|
|
1790
|
+
return {
|
|
1791
|
+
name: queryName,
|
|
1792
|
+
resolver: async (source, args, context, info) => {
|
|
1793
|
+
try {
|
|
1794
|
+
const { offset, orderBy, where } = args;
|
|
1795
|
+
const parsedInfo = parseResolveInfo3(info, {
|
|
1796
|
+
deep: true
|
|
1797
|
+
});
|
|
1798
|
+
const query = queryBase.findFirst({
|
|
1799
|
+
columns: extractSelectedColumnsFromTree(
|
|
1800
|
+
parsedInfo.fieldsByTypeName[typeName],
|
|
1801
|
+
table
|
|
1802
|
+
),
|
|
1803
|
+
offset,
|
|
1804
|
+
orderBy: orderBy ? extractOrderBy(table, orderBy) : void 0,
|
|
1805
|
+
where: where ? extractFilters(table, tableName, where) : void 0,
|
|
1806
|
+
with: relationMap[tableName] ? extractRelationsParams(
|
|
1807
|
+
relationMap,
|
|
1808
|
+
tables,
|
|
1809
|
+
tableName,
|
|
1810
|
+
parsedInfo,
|
|
1811
|
+
typeName
|
|
1812
|
+
) : void 0
|
|
1813
|
+
});
|
|
1814
|
+
const result = await query;
|
|
1815
|
+
if (!result)
|
|
1816
|
+
return void 0;
|
|
1817
|
+
return remapToGraphQLSingleOutput(
|
|
1818
|
+
result,
|
|
1819
|
+
tableName,
|
|
1820
|
+
table,
|
|
1821
|
+
relationMap
|
|
1822
|
+
);
|
|
1823
|
+
} catch (e) {
|
|
1824
|
+
if (typeof e === "object" && typeof e.message === "string") {
|
|
1825
|
+
throw new GraphQLError5(e.message);
|
|
1826
|
+
}
|
|
1827
|
+
throw e;
|
|
1828
|
+
}
|
|
1829
|
+
},
|
|
1830
|
+
args: queryArgs
|
|
1831
|
+
};
|
|
1832
|
+
};
|
|
1833
|
+
var generateInsertArray3 = (db, tableName, table, baseType) => {
|
|
1834
|
+
const queryName = `insertInto${capitalize(tableName)}`;
|
|
1835
|
+
const typeName = `${capitalize(tableName)}Item`;
|
|
1836
|
+
const queryArgs = {
|
|
1837
|
+
values: {
|
|
1838
|
+
type: new GraphQLNonNull5(new GraphQLList5(new GraphQLNonNull5(baseType)))
|
|
1839
|
+
}
|
|
1840
|
+
};
|
|
1841
|
+
return {
|
|
1842
|
+
name: queryName,
|
|
1843
|
+
resolver: async (source, args, context, info) => {
|
|
1844
|
+
try {
|
|
1845
|
+
const input = remapFromGraphQLArrayInput(args.values, table);
|
|
1846
|
+
if (!input.length)
|
|
1847
|
+
throw new GraphQLError5("No values were provided!");
|
|
1848
|
+
const parsedInfo = parseResolveInfo3(info, {
|
|
1849
|
+
deep: true
|
|
1850
|
+
});
|
|
1851
|
+
const columns = extractSelectedColumnsFromTreeSQLFormat(
|
|
1852
|
+
parsedInfo.fieldsByTypeName[typeName],
|
|
1853
|
+
table
|
|
1854
|
+
);
|
|
1855
|
+
const result = await db.insert(table).values(input).returning(columns).onConflictDoNothing();
|
|
1856
|
+
return remapToGraphQLArrayOutput(result, tableName, table);
|
|
1857
|
+
} catch (e) {
|
|
1858
|
+
if (typeof e === "object" && typeof e.message === "string") {
|
|
1859
|
+
throw new GraphQLError5(e.message);
|
|
1860
|
+
}
|
|
1861
|
+
throw e;
|
|
1862
|
+
}
|
|
1863
|
+
},
|
|
1864
|
+
args: queryArgs
|
|
1865
|
+
};
|
|
1866
|
+
};
|
|
1867
|
+
var generateInsertSingle3 = (db, tableName, table, baseType) => {
|
|
1868
|
+
const queryName = `insertInto${capitalize(tableName)}Single`;
|
|
1869
|
+
const typeName = `${capitalize(tableName)}Item`;
|
|
1870
|
+
const queryArgs = {
|
|
1871
|
+
values: {
|
|
1872
|
+
type: new GraphQLNonNull5(baseType)
|
|
1873
|
+
}
|
|
1874
|
+
};
|
|
1875
|
+
return {
|
|
1876
|
+
name: queryName,
|
|
1877
|
+
resolver: async (source, args, context, info) => {
|
|
1878
|
+
try {
|
|
1879
|
+
const input = remapFromGraphQLSingleInput(args.values, table);
|
|
1880
|
+
const parsedInfo = parseResolveInfo3(info, {
|
|
1881
|
+
deep: true
|
|
1882
|
+
});
|
|
1883
|
+
const columns = extractSelectedColumnsFromTreeSQLFormat(
|
|
1884
|
+
parsedInfo.fieldsByTypeName[typeName],
|
|
1885
|
+
table
|
|
1886
|
+
);
|
|
1887
|
+
const result = await db.insert(table).values(input).returning(columns).onConflictDoNothing();
|
|
1888
|
+
if (!result[0])
|
|
1889
|
+
return void 0;
|
|
1890
|
+
return remapToGraphQLSingleOutput(result[0], tableName, table);
|
|
1891
|
+
} catch (e) {
|
|
1892
|
+
if (typeof e === "object" && typeof e.message === "string") {
|
|
1893
|
+
throw new GraphQLError5(e.message);
|
|
1894
|
+
}
|
|
1895
|
+
throw e;
|
|
1896
|
+
}
|
|
1897
|
+
},
|
|
1898
|
+
args: queryArgs
|
|
1899
|
+
};
|
|
1900
|
+
};
|
|
1901
|
+
var generateUpdate3 = (db, tableName, table, setArgs, filterArgs) => {
|
|
1902
|
+
const queryName = `update${capitalize(tableName)}`;
|
|
1903
|
+
const typeName = `${capitalize(tableName)}Item`;
|
|
1904
|
+
const queryArgs = {
|
|
1905
|
+
set: {
|
|
1906
|
+
type: new GraphQLNonNull5(setArgs)
|
|
1907
|
+
},
|
|
1908
|
+
where: {
|
|
1909
|
+
type: filterArgs
|
|
1910
|
+
}
|
|
1911
|
+
};
|
|
1912
|
+
return {
|
|
1913
|
+
name: queryName,
|
|
1914
|
+
resolver: async (source, args, context, info) => {
|
|
1915
|
+
try {
|
|
1916
|
+
const { where, set } = args;
|
|
1917
|
+
const parsedInfo = parseResolveInfo3(info, {
|
|
1918
|
+
deep: true
|
|
1919
|
+
});
|
|
1920
|
+
const columns = extractSelectedColumnsFromTreeSQLFormat(
|
|
1921
|
+
parsedInfo.fieldsByTypeName[typeName],
|
|
1922
|
+
table
|
|
1923
|
+
);
|
|
1924
|
+
const input = remapFromGraphQLSingleInput(set, table);
|
|
1925
|
+
if (!Object.keys(input).length)
|
|
1926
|
+
throw new GraphQLError5("Unable to update with no values specified!");
|
|
1927
|
+
let query = db.update(table).set(input);
|
|
1928
|
+
if (where) {
|
|
1929
|
+
const filters = extractFilters(table, tableName, where);
|
|
1930
|
+
query = query.where(filters);
|
|
1931
|
+
}
|
|
1932
|
+
query = query.returning(columns);
|
|
1933
|
+
const result = await query;
|
|
1934
|
+
return remapToGraphQLArrayOutput(result, tableName, table);
|
|
1935
|
+
} catch (e) {
|
|
1936
|
+
if (typeof e === "object" && typeof e.message === "string") {
|
|
1937
|
+
throw new GraphQLError5(e.message);
|
|
1938
|
+
}
|
|
1939
|
+
throw e;
|
|
1940
|
+
}
|
|
1941
|
+
},
|
|
1942
|
+
args: queryArgs
|
|
1943
|
+
};
|
|
1944
|
+
};
|
|
1945
|
+
var generateDelete3 = (db, tableName, table, filterArgs) => {
|
|
1946
|
+
const queryName = `deleteFrom${capitalize(tableName)}`;
|
|
1947
|
+
const typeName = `${capitalize(tableName)}Item`;
|
|
1948
|
+
const queryArgs = {
|
|
1949
|
+
where: {
|
|
1950
|
+
type: filterArgs
|
|
1951
|
+
}
|
|
1952
|
+
};
|
|
1953
|
+
return {
|
|
1954
|
+
name: queryName,
|
|
1955
|
+
resolver: async (source, args, context, info) => {
|
|
1956
|
+
try {
|
|
1957
|
+
const { where } = args;
|
|
1958
|
+
const parsedInfo = parseResolveInfo3(info, {
|
|
1959
|
+
deep: true
|
|
1960
|
+
});
|
|
1961
|
+
const columns = extractSelectedColumnsFromTreeSQLFormat(
|
|
1962
|
+
parsedInfo.fieldsByTypeName[typeName],
|
|
1963
|
+
table
|
|
1964
|
+
);
|
|
1965
|
+
let query = db.delete(table);
|
|
1966
|
+
if (where) {
|
|
1967
|
+
const filters = extractFilters(table, tableName, where);
|
|
1968
|
+
query = query.where(filters);
|
|
1969
|
+
}
|
|
1970
|
+
query = query.returning(columns);
|
|
1971
|
+
const result = await query;
|
|
1972
|
+
return remapToGraphQLArrayOutput(result, tableName, table);
|
|
1973
|
+
} catch (e) {
|
|
1974
|
+
if (typeof e === "object" && typeof e.message === "string") {
|
|
1975
|
+
throw new GraphQLError5(e.message);
|
|
1976
|
+
}
|
|
1977
|
+
throw e;
|
|
1978
|
+
}
|
|
1979
|
+
},
|
|
1980
|
+
args: queryArgs
|
|
1981
|
+
};
|
|
1982
|
+
};
|
|
1983
|
+
var generateSchemaData3 = (db, schema, relationsDepthLimit) => {
|
|
1984
|
+
const rawSchema = schema;
|
|
1985
|
+
const schemaEntries = Object.entries(rawSchema);
|
|
1986
|
+
const tableEntries = schemaEntries.filter(
|
|
1987
|
+
([key, value]) => is5(value, SQLiteTable)
|
|
1988
|
+
);
|
|
1989
|
+
const tables = Object.fromEntries(tableEntries);
|
|
1990
|
+
if (!tableEntries.length) {
|
|
1991
|
+
throw new Error(
|
|
1992
|
+
"Drizzle-GraphQL Error: No tables detected in Drizzle-ORM's database instance. Did you forget to pass schema to drizzle constructor?"
|
|
1993
|
+
);
|
|
1994
|
+
}
|
|
1995
|
+
const rawRelations = schemaEntries.filter(([key, value]) => is5(value, Relations3)).map(([key, value]) => [
|
|
1996
|
+
tableEntries.find(
|
|
1997
|
+
([tableName, tableValue]) => tableValue === value.table
|
|
1998
|
+
)[0],
|
|
1999
|
+
value
|
|
2000
|
+
]).map(([tableName, relValue]) => [
|
|
2001
|
+
tableName,
|
|
2002
|
+
relValue.config(createTableRelationsHelpers3(tables[tableName]))
|
|
2003
|
+
]);
|
|
2004
|
+
const namedRelations = Object.fromEntries(
|
|
2005
|
+
rawRelations.map(([relName, config]) => {
|
|
2006
|
+
const namedConfig = Object.fromEntries(
|
|
2007
|
+
Object.entries(config).map(([innerRelName, innerRelValue]) => [
|
|
2008
|
+
innerRelName,
|
|
2009
|
+
{
|
|
2010
|
+
relation: innerRelValue,
|
|
2011
|
+
targetTableName: tableEntries.find(
|
|
2012
|
+
([tableName, tableValue]) => tableValue === innerRelValue.referencedTable
|
|
2013
|
+
)[0]
|
|
2014
|
+
}
|
|
2015
|
+
])
|
|
2016
|
+
);
|
|
2017
|
+
return [relName, namedConfig];
|
|
2018
|
+
})
|
|
2019
|
+
);
|
|
2020
|
+
const queries = {};
|
|
2021
|
+
const mutations = {};
|
|
2022
|
+
const gqlSchemaTypes = Object.fromEntries(
|
|
2023
|
+
Object.entries(tables).map(([tableName, table]) => [
|
|
2024
|
+
tableName,
|
|
2025
|
+
generateTableTypes(
|
|
2026
|
+
tableName,
|
|
2027
|
+
tables,
|
|
2028
|
+
namedRelations,
|
|
2029
|
+
true,
|
|
2030
|
+
relationsDepthLimit
|
|
2031
|
+
)
|
|
2032
|
+
])
|
|
2033
|
+
);
|
|
2034
|
+
const inputs = {};
|
|
2035
|
+
const interfaces = {};
|
|
2036
|
+
const outputs = {};
|
|
2037
|
+
for (const [tableName, tableTypes] of Object.entries(gqlSchemaTypes)) {
|
|
2038
|
+
const { insertInput, updateInput, tableFilters, tableOrder } = tableTypes.inputs;
|
|
2039
|
+
const {
|
|
2040
|
+
selectSingleOutput,
|
|
2041
|
+
selectArrOutput,
|
|
2042
|
+
singleTableItemOutput,
|
|
2043
|
+
arrTableItemOutput,
|
|
2044
|
+
tableFieldsInterface
|
|
2045
|
+
} = tableTypes.outputs;
|
|
2046
|
+
const selectArrGenerated = generateSelectArray3(
|
|
2047
|
+
db,
|
|
2048
|
+
tableName,
|
|
2049
|
+
tables,
|
|
2050
|
+
namedRelations,
|
|
2051
|
+
tableOrder,
|
|
2052
|
+
tableFilters
|
|
2053
|
+
);
|
|
2054
|
+
const selectSingleGenerated = generateSelectSingle3(
|
|
2055
|
+
db,
|
|
2056
|
+
tableName,
|
|
2057
|
+
tables,
|
|
2058
|
+
namedRelations,
|
|
2059
|
+
tableOrder,
|
|
2060
|
+
tableFilters
|
|
2061
|
+
);
|
|
2062
|
+
const insertArrGenerated = generateInsertArray3(
|
|
2063
|
+
db,
|
|
2064
|
+
tableName,
|
|
2065
|
+
schema[tableName],
|
|
2066
|
+
insertInput
|
|
2067
|
+
);
|
|
2068
|
+
const insertSingleGenerated = generateInsertSingle3(
|
|
2069
|
+
db,
|
|
2070
|
+
tableName,
|
|
2071
|
+
schema[tableName],
|
|
2072
|
+
insertInput
|
|
2073
|
+
);
|
|
2074
|
+
const updateGenerated = generateUpdate3(
|
|
2075
|
+
db,
|
|
2076
|
+
tableName,
|
|
2077
|
+
schema[tableName],
|
|
2078
|
+
updateInput,
|
|
2079
|
+
tableFilters
|
|
2080
|
+
);
|
|
2081
|
+
const deleteGenerated = generateDelete3(
|
|
2082
|
+
db,
|
|
2083
|
+
tableName,
|
|
2084
|
+
schema[tableName],
|
|
2085
|
+
tableFilters
|
|
2086
|
+
);
|
|
2087
|
+
queries[selectArrGenerated.name] = {
|
|
2088
|
+
type: selectArrOutput,
|
|
2089
|
+
args: selectArrGenerated.args,
|
|
2090
|
+
resolve: selectArrGenerated.resolver
|
|
2091
|
+
};
|
|
2092
|
+
queries[selectSingleGenerated.name] = {
|
|
2093
|
+
type: selectSingleOutput,
|
|
2094
|
+
args: selectSingleGenerated.args,
|
|
2095
|
+
resolve: selectSingleGenerated.resolver
|
|
2096
|
+
};
|
|
2097
|
+
mutations[insertArrGenerated.name] = {
|
|
2098
|
+
type: arrTableItemOutput,
|
|
2099
|
+
args: insertArrGenerated.args,
|
|
2100
|
+
resolve: insertArrGenerated.resolver
|
|
2101
|
+
};
|
|
2102
|
+
mutations[insertSingleGenerated.name] = {
|
|
2103
|
+
type: singleTableItemOutput,
|
|
2104
|
+
args: insertSingleGenerated.args,
|
|
2105
|
+
resolve: insertSingleGenerated.resolver
|
|
2106
|
+
};
|
|
2107
|
+
mutations[updateGenerated.name] = {
|
|
2108
|
+
type: arrTableItemOutput,
|
|
2109
|
+
args: updateGenerated.args,
|
|
2110
|
+
resolve: updateGenerated.resolver
|
|
2111
|
+
};
|
|
2112
|
+
mutations[deleteGenerated.name] = {
|
|
2113
|
+
type: arrTableItemOutput,
|
|
2114
|
+
args: deleteGenerated.args,
|
|
2115
|
+
resolve: deleteGenerated.resolver
|
|
2116
|
+
};
|
|
2117
|
+
[insertInput, updateInput, tableFilters, tableOrder].forEach(
|
|
2118
|
+
(e) => inputs[e.name] = e
|
|
2119
|
+
);
|
|
2120
|
+
outputs[selectSingleOutput.name] = selectSingleOutput;
|
|
2121
|
+
outputs[singleTableItemOutput.name] = singleTableItemOutput;
|
|
2122
|
+
interfaces[tableFieldsInterface.name] = tableFieldsInterface;
|
|
2123
|
+
}
|
|
2124
|
+
return { queries, mutations, inputs, interfaces, types: outputs };
|
|
2125
|
+
};
|
|
2126
|
+
|
|
2127
|
+
// src/index.ts
|
|
2128
|
+
var buildSchema = (db, config) => {
|
|
2129
|
+
const schema = db._.fullSchema;
|
|
2130
|
+
if (!schema) {
|
|
2131
|
+
throw new Error(
|
|
2132
|
+
"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!"
|
|
2133
|
+
);
|
|
2134
|
+
}
|
|
2135
|
+
if (typeof config?.relationsDepthLimit === "number") {
|
|
2136
|
+
if (config.relationsDepthLimit < 0) {
|
|
2137
|
+
throw new Error(
|
|
2138
|
+
"Drizzle-GraphQL Error: config.relationsDepthLimit is supposed to be nonnegative integer or undefined!"
|
|
2139
|
+
);
|
|
2140
|
+
}
|
|
2141
|
+
if (config.relationsDepthLimit !== ~~config.relationsDepthLimit) {
|
|
2142
|
+
throw new Error(
|
|
2143
|
+
"Drizzle-GraphQL Error: config.relationsDepthLimit is supposed to be nonnegative integer or undefined!"
|
|
2144
|
+
);
|
|
2145
|
+
}
|
|
2146
|
+
}
|
|
2147
|
+
let generatorOutput;
|
|
2148
|
+
if (is6(db, MySqlDatabase2)) {
|
|
2149
|
+
generatorOutput = generateSchemaData(db, schema, config?.relationsDepthLimit);
|
|
2150
|
+
} else if (is6(db, PgDatabase2)) {
|
|
2151
|
+
generatorOutput = generateSchemaData2(db, schema, config?.relationsDepthLimit);
|
|
2152
|
+
} else if (is6(db, BaseSQLiteDatabase2)) {
|
|
2153
|
+
generatorOutput = generateSchemaData3(db, schema, config?.relationsDepthLimit);
|
|
2154
|
+
} else
|
|
2155
|
+
throw new Error("Drizzle-GraphQL Error: Unknown database instance type");
|
|
2156
|
+
const { queries, mutations, inputs, types } = generatorOutput;
|
|
2157
|
+
const graphQLSchemaConfig = {
|
|
2158
|
+
types: [...Object.values(inputs), ...Object.values(types)],
|
|
2159
|
+
query: new GraphQLObjectType6({
|
|
2160
|
+
name: "Query",
|
|
2161
|
+
fields: queries
|
|
2162
|
+
})
|
|
2163
|
+
};
|
|
2164
|
+
if (config?.mutations !== false) {
|
|
2165
|
+
const mutation = new GraphQLObjectType6({
|
|
2166
|
+
name: "Mutation",
|
|
2167
|
+
fields: mutations
|
|
2168
|
+
});
|
|
2169
|
+
graphQLSchemaConfig.mutation = mutation;
|
|
2170
|
+
}
|
|
2171
|
+
const outputSchema = new GraphQLSchema(graphQLSchemaConfig);
|
|
2172
|
+
return { schema: outputSchema, entities: generatorOutput };
|
|
2173
|
+
};
|
|
2174
|
+
export {
|
|
2175
|
+
buildSchema
|
|
2176
|
+
};
|
|
2177
|
+
//# sourceMappingURL=index.js.map
|