drizzle-graphql-plus 0.8.7 → 0.8.9
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/.github/workflows/release.yaml +74 -0
- package/LICENSE +201 -0
- package/dprint.json +31 -0
- package/drizzle.test-config.ts +29 -0
- package/package.json +17 -18
- package/scripts/build.ts +28 -0
- package/src/index.ts +74 -0
- package/src/types.ts +535 -0
- package/src/util/builders/common.ts +842 -0
- package/src/util/builders/index.ts +5 -0
- package/src/util/builders/mysql.ts +567 -0
- package/src/util/builders/pg.ts +616 -0
- package/src/util/builders/sqlite.ts +622 -0
- package/src/util/builders/types.ts +317 -0
- package/src/util/case-ops/index.ts +9 -0
- package/src/util/data-mappers/index.ts +162 -0
- package/src/util/type-converter/index.ts +148 -0
- package/src/util/type-converter/types.ts +54 -0
- package/tests/mysql-custom.test.ts +2009 -0
- package/tests/mysql.test.ts +4600 -0
- package/tests/pg-custom.test.ts +2054 -0
- package/tests/pg.test.ts +4285 -0
- package/tests/schema/mysql.ts +72 -0
- package/tests/schema/pg.ts +83 -0
- package/tests/schema/sqlite.ts +64 -0
- package/tests/sqlite-custom.test.ts +1956 -0
- package/tests/sqlite.test.ts +3749 -0
- package/tests/tsconfig.json +11 -0
- package/tests/util/query/index.ts +30 -0
- package/tsconfig.build.json +4 -0
- package/tsconfig.dts.json +13 -0
- package/tsconfig.json +48 -0
- package/vitest.config.ts +17 -0
- package/index.cjs +0 -1974
- package/index.cjs.map +0 -1
- package/index.d.cts +0 -247
- package/index.d.ts +0 -247
- package/index.js +0 -2012
- package/index.js.map +0 -1
|
@@ -0,0 +1,616 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createTableRelationsHelpers,
|
|
3
|
+
is,
|
|
4
|
+
Relation,
|
|
5
|
+
Relations,
|
|
6
|
+
Table,
|
|
7
|
+
} from "drizzle-orm";
|
|
8
|
+
import { PgColumn, PgDatabase, PgTable } from "drizzle-orm/pg-core";
|
|
9
|
+
import {
|
|
10
|
+
GraphQLError,
|
|
11
|
+
GraphQLInputObjectType,
|
|
12
|
+
GraphQLInt,
|
|
13
|
+
GraphQLInterfaceType,
|
|
14
|
+
GraphQLList,
|
|
15
|
+
GraphQLNonNull,
|
|
16
|
+
GraphQLObjectType,
|
|
17
|
+
} from "graphql";
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
extractFilters,
|
|
21
|
+
extractOrderBy,
|
|
22
|
+
extractRelationsParams,
|
|
23
|
+
extractSelectedColumnsFromTree,
|
|
24
|
+
extractSelectedColumnsFromTreeSQLFormat,
|
|
25
|
+
generateTableTypes,
|
|
26
|
+
} from "@/util/builders/common";
|
|
27
|
+
import { capitalize, uncapitalize } from "@/util/case-ops";
|
|
28
|
+
import {
|
|
29
|
+
remapFromGraphQLArrayInput,
|
|
30
|
+
remapFromGraphQLSingleInput,
|
|
31
|
+
remapToGraphQLArrayOutput,
|
|
32
|
+
remapToGraphQLSingleOutput,
|
|
33
|
+
} from "@/util/data-mappers";
|
|
34
|
+
import { parseResolveInfo } from "graphql-parse-resolve-info";
|
|
35
|
+
|
|
36
|
+
import type { GeneratedEntities } from "@/types";
|
|
37
|
+
import type { RelationalQueryBuilder } from "drizzle-orm/mysql-core/query-builders/query";
|
|
38
|
+
import type {
|
|
39
|
+
GraphQLFieldConfig,
|
|
40
|
+
GraphQLFieldConfigArgumentMap,
|
|
41
|
+
ThunkObjMap,
|
|
42
|
+
} from "graphql";
|
|
43
|
+
import type { ResolveTree } from "graphql-parse-resolve-info";
|
|
44
|
+
import type {
|
|
45
|
+
CreatedResolver,
|
|
46
|
+
Filters,
|
|
47
|
+
TableNamedRelations,
|
|
48
|
+
TableSelectArgs,
|
|
49
|
+
} from "./types";
|
|
50
|
+
|
|
51
|
+
const generateSelectArray = (
|
|
52
|
+
db: PgDatabase<any, any, any>,
|
|
53
|
+
tableName: string,
|
|
54
|
+
tables: Record<string, Table>,
|
|
55
|
+
relationMap: Record<string, Record<string, TableNamedRelations>>,
|
|
56
|
+
orderArgs: GraphQLInputObjectType,
|
|
57
|
+
filterArgs: GraphQLInputObjectType
|
|
58
|
+
): CreatedResolver => {
|
|
59
|
+
const queryName = `${uncapitalize(tableName)}`;
|
|
60
|
+
const queryBase = db.query[tableName as keyof typeof db.query] as unknown as
|
|
61
|
+
| RelationalQueryBuilder<any, any, any>
|
|
62
|
+
| undefined;
|
|
63
|
+
if (!queryBase) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const queryArgs = {
|
|
70
|
+
offset: {
|
|
71
|
+
type: GraphQLInt,
|
|
72
|
+
},
|
|
73
|
+
limit: {
|
|
74
|
+
type: GraphQLInt,
|
|
75
|
+
},
|
|
76
|
+
orderBy: {
|
|
77
|
+
type: orderArgs,
|
|
78
|
+
},
|
|
79
|
+
where: {
|
|
80
|
+
type: filterArgs,
|
|
81
|
+
},
|
|
82
|
+
} as GraphQLFieldConfigArgumentMap;
|
|
83
|
+
|
|
84
|
+
const typeName = `${capitalize(tableName)}SelectItem`;
|
|
85
|
+
const table = tables[tableName]!;
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
name: queryName,
|
|
89
|
+
resolver: async (source, args: Partial<TableSelectArgs>, context, info) => {
|
|
90
|
+
try {
|
|
91
|
+
const { offset, limit, orderBy, where } = args;
|
|
92
|
+
|
|
93
|
+
const parsedInfo = parseResolveInfo(info, {
|
|
94
|
+
deep: true,
|
|
95
|
+
}) as ResolveTree;
|
|
96
|
+
|
|
97
|
+
const query = queryBase.findMany({
|
|
98
|
+
columns: extractSelectedColumnsFromTree(
|
|
99
|
+
parsedInfo.fieldsByTypeName[typeName]!,
|
|
100
|
+
table
|
|
101
|
+
) /*extractSelectedColumnsFromNode(tableSelection, info.fragments, table) */,
|
|
102
|
+
offset,
|
|
103
|
+
limit,
|
|
104
|
+
orderBy: orderBy ? extractOrderBy(table, orderBy) : undefined,
|
|
105
|
+
where: where ? extractFilters(table, tableName, where) : undefined,
|
|
106
|
+
with: relationMap[tableName]
|
|
107
|
+
? extractRelationsParams(
|
|
108
|
+
relationMap,
|
|
109
|
+
tables,
|
|
110
|
+
tableName,
|
|
111
|
+
parsedInfo,
|
|
112
|
+
typeName
|
|
113
|
+
)
|
|
114
|
+
: undefined,
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
const result = await query;
|
|
118
|
+
|
|
119
|
+
return remapToGraphQLArrayOutput(result, tableName, table, relationMap);
|
|
120
|
+
} catch (e) {
|
|
121
|
+
if (typeof e === "object" && typeof (<any>e).message === "string") {
|
|
122
|
+
throw new GraphQLError((<any>e).message);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
throw e;
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
args: queryArgs,
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const generateSelectSingle = (
|
|
133
|
+
db: PgDatabase<any, any, any>,
|
|
134
|
+
tableName: string,
|
|
135
|
+
tables: Record<string, Table>,
|
|
136
|
+
relationMap: Record<string, Record<string, TableNamedRelations>>,
|
|
137
|
+
orderArgs: GraphQLInputObjectType,
|
|
138
|
+
filterArgs: GraphQLInputObjectType
|
|
139
|
+
): CreatedResolver => {
|
|
140
|
+
const queryName = `${uncapitalize(tableName)}Single`;
|
|
141
|
+
const queryBase = db.query[tableName as keyof typeof db.query] as unknown as
|
|
142
|
+
| RelationalQueryBuilder<any, any, any>
|
|
143
|
+
| undefined;
|
|
144
|
+
if (!queryBase) {
|
|
145
|
+
throw new Error(
|
|
146
|
+
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const queryArgs = {
|
|
151
|
+
offset: {
|
|
152
|
+
type: GraphQLInt,
|
|
153
|
+
},
|
|
154
|
+
orderBy: {
|
|
155
|
+
type: orderArgs,
|
|
156
|
+
},
|
|
157
|
+
where: {
|
|
158
|
+
type: filterArgs,
|
|
159
|
+
},
|
|
160
|
+
} as GraphQLFieldConfigArgumentMap;
|
|
161
|
+
|
|
162
|
+
const typeName = `${capitalize(tableName)}SelectItem`;
|
|
163
|
+
const table = tables[tableName]!;
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
name: queryName,
|
|
167
|
+
resolver: async (source, args: Partial<TableSelectArgs>, context, info) => {
|
|
168
|
+
try {
|
|
169
|
+
const { offset, orderBy, where } = args;
|
|
170
|
+
|
|
171
|
+
const parsedInfo = parseResolveInfo(info, {
|
|
172
|
+
deep: true,
|
|
173
|
+
}) as ResolveTree;
|
|
174
|
+
|
|
175
|
+
const query = queryBase.findFirst({
|
|
176
|
+
columns: extractSelectedColumnsFromTree(
|
|
177
|
+
parsedInfo.fieldsByTypeName[typeName]!,
|
|
178
|
+
table
|
|
179
|
+
),
|
|
180
|
+
offset,
|
|
181
|
+
orderBy: orderBy ? extractOrderBy(table, orderBy) : undefined,
|
|
182
|
+
where: where ? extractFilters(table, tableName, where) : undefined,
|
|
183
|
+
with: relationMap[tableName]
|
|
184
|
+
? extractRelationsParams(
|
|
185
|
+
relationMap,
|
|
186
|
+
tables,
|
|
187
|
+
tableName,
|
|
188
|
+
parsedInfo,
|
|
189
|
+
typeName
|
|
190
|
+
)
|
|
191
|
+
: undefined,
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
const result = await query;
|
|
195
|
+
if (!result) return undefined;
|
|
196
|
+
|
|
197
|
+
return remapToGraphQLSingleOutput(
|
|
198
|
+
result,
|
|
199
|
+
tableName,
|
|
200
|
+
table,
|
|
201
|
+
relationMap
|
|
202
|
+
);
|
|
203
|
+
} catch (e) {
|
|
204
|
+
if (typeof e === "object" && typeof (<any>e).message === "string") {
|
|
205
|
+
throw new GraphQLError((<any>e).message);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
throw e;
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
args: queryArgs,
|
|
212
|
+
};
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const generateInsertArray = (
|
|
216
|
+
db: PgDatabase<any, any, any>,
|
|
217
|
+
tableName: string,
|
|
218
|
+
table: PgTable,
|
|
219
|
+
baseType: GraphQLInputObjectType
|
|
220
|
+
): CreatedResolver => {
|
|
221
|
+
const queryName = `insertInto${capitalize(tableName)}`;
|
|
222
|
+
const typeName = `${capitalize(tableName)}Item`;
|
|
223
|
+
|
|
224
|
+
const queryArgs: GraphQLFieldConfigArgumentMap = {
|
|
225
|
+
values: {
|
|
226
|
+
type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(baseType))),
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
name: queryName,
|
|
232
|
+
resolver: async (
|
|
233
|
+
source,
|
|
234
|
+
args: { values: Record<string, any>[] },
|
|
235
|
+
context,
|
|
236
|
+
info
|
|
237
|
+
) => {
|
|
238
|
+
try {
|
|
239
|
+
const input = remapFromGraphQLArrayInput(args.values, table);
|
|
240
|
+
if (!input.length) throw new GraphQLError("No values were provided!");
|
|
241
|
+
|
|
242
|
+
const parsedInfo = parseResolveInfo(info, {
|
|
243
|
+
deep: true,
|
|
244
|
+
}) as ResolveTree;
|
|
245
|
+
|
|
246
|
+
const columns = extractSelectedColumnsFromTreeSQLFormat<PgColumn>(
|
|
247
|
+
parsedInfo.fieldsByTypeName[typeName]!,
|
|
248
|
+
table
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
const result = await db
|
|
252
|
+
.insert(table)
|
|
253
|
+
.values(input)
|
|
254
|
+
.returning(columns)
|
|
255
|
+
.onConflictDoNothing();
|
|
256
|
+
|
|
257
|
+
return remapToGraphQLArrayOutput(result, tableName, table);
|
|
258
|
+
} catch (e) {
|
|
259
|
+
if (typeof e === "object" && typeof (<any>e).message === "string") {
|
|
260
|
+
throw new GraphQLError((<any>e).message);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
throw e;
|
|
264
|
+
}
|
|
265
|
+
},
|
|
266
|
+
args: queryArgs,
|
|
267
|
+
};
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
const generateInsertSingle = (
|
|
271
|
+
db: PgDatabase<any, any, any>,
|
|
272
|
+
tableName: string,
|
|
273
|
+
table: PgTable,
|
|
274
|
+
baseType: GraphQLInputObjectType
|
|
275
|
+
): CreatedResolver => {
|
|
276
|
+
const queryName = `insertInto${capitalize(tableName)}Single`;
|
|
277
|
+
const typeName = `${capitalize(tableName)}Item`;
|
|
278
|
+
|
|
279
|
+
const queryArgs: GraphQLFieldConfigArgumentMap = {
|
|
280
|
+
values: {
|
|
281
|
+
type: new GraphQLNonNull(baseType),
|
|
282
|
+
},
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
return {
|
|
286
|
+
name: queryName,
|
|
287
|
+
resolver: async (
|
|
288
|
+
source,
|
|
289
|
+
args: { values: Record<string, any> },
|
|
290
|
+
context,
|
|
291
|
+
info
|
|
292
|
+
) => {
|
|
293
|
+
try {
|
|
294
|
+
const input = remapFromGraphQLSingleInput(args.values, table);
|
|
295
|
+
|
|
296
|
+
const parsedInfo = parseResolveInfo(info, {
|
|
297
|
+
deep: true,
|
|
298
|
+
}) as ResolveTree;
|
|
299
|
+
|
|
300
|
+
const columns = extractSelectedColumnsFromTreeSQLFormat<PgColumn>(
|
|
301
|
+
parsedInfo.fieldsByTypeName[typeName]!,
|
|
302
|
+
table
|
|
303
|
+
);
|
|
304
|
+
|
|
305
|
+
const result = await db
|
|
306
|
+
.insert(table)
|
|
307
|
+
.values(input)
|
|
308
|
+
.returning(columns)
|
|
309
|
+
.onConflictDoNothing();
|
|
310
|
+
|
|
311
|
+
if (!result[0]) return undefined;
|
|
312
|
+
|
|
313
|
+
return remapToGraphQLSingleOutput(result[0], tableName, table);
|
|
314
|
+
} catch (e) {
|
|
315
|
+
if (typeof e === "object" && typeof (<any>e).message === "string") {
|
|
316
|
+
throw new GraphQLError((<any>e).message);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
throw e;
|
|
320
|
+
}
|
|
321
|
+
},
|
|
322
|
+
args: queryArgs,
|
|
323
|
+
};
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
const generateUpdate = (
|
|
327
|
+
db: PgDatabase<any, any, any>,
|
|
328
|
+
tableName: string,
|
|
329
|
+
table: PgTable,
|
|
330
|
+
setArgs: GraphQLInputObjectType,
|
|
331
|
+
filterArgs: GraphQLInputObjectType
|
|
332
|
+
): CreatedResolver => {
|
|
333
|
+
const queryName = `update${capitalize(tableName)}`;
|
|
334
|
+
const typeName = `${capitalize(tableName)}Item`;
|
|
335
|
+
|
|
336
|
+
const queryArgs = {
|
|
337
|
+
set: {
|
|
338
|
+
type: new GraphQLNonNull(setArgs),
|
|
339
|
+
},
|
|
340
|
+
where: {
|
|
341
|
+
type: filterArgs,
|
|
342
|
+
},
|
|
343
|
+
} as const satisfies GraphQLFieldConfigArgumentMap;
|
|
344
|
+
|
|
345
|
+
return {
|
|
346
|
+
name: queryName,
|
|
347
|
+
resolver: async (
|
|
348
|
+
source,
|
|
349
|
+
args: { where?: Filters<Table>; set: Record<string, any> },
|
|
350
|
+
context,
|
|
351
|
+
info
|
|
352
|
+
) => {
|
|
353
|
+
try {
|
|
354
|
+
const { where, set } = args;
|
|
355
|
+
|
|
356
|
+
const parsedInfo = parseResolveInfo(info, {
|
|
357
|
+
deep: true,
|
|
358
|
+
}) as ResolveTree;
|
|
359
|
+
|
|
360
|
+
const columns = extractSelectedColumnsFromTreeSQLFormat<PgColumn>(
|
|
361
|
+
parsedInfo.fieldsByTypeName[typeName]!,
|
|
362
|
+
table
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
const input = remapFromGraphQLSingleInput(set, table);
|
|
366
|
+
if (!Object.keys(input).length)
|
|
367
|
+
throw new GraphQLError("Unable to update with no values specified!");
|
|
368
|
+
|
|
369
|
+
let query = db.update(table).set(input);
|
|
370
|
+
if (where) {
|
|
371
|
+
const filters = extractFilters(table, tableName, where);
|
|
372
|
+
query = query.where(filters) as any;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
query = query.returning(columns) as any;
|
|
376
|
+
|
|
377
|
+
const result = await query;
|
|
378
|
+
|
|
379
|
+
return remapToGraphQLArrayOutput(result, tableName, table);
|
|
380
|
+
} catch (e) {
|
|
381
|
+
if (typeof e === "object" && typeof (<any>e).message === "string") {
|
|
382
|
+
throw new GraphQLError((<any>e).message);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
throw e;
|
|
386
|
+
}
|
|
387
|
+
},
|
|
388
|
+
args: queryArgs,
|
|
389
|
+
};
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
const generateDelete = (
|
|
393
|
+
db: PgDatabase<any, any, any>,
|
|
394
|
+
tableName: string,
|
|
395
|
+
table: PgTable,
|
|
396
|
+
filterArgs: GraphQLInputObjectType
|
|
397
|
+
): CreatedResolver => {
|
|
398
|
+
const queryName = `deleteFrom${capitalize(tableName)}`;
|
|
399
|
+
const typeName = `${capitalize(tableName)}Item`;
|
|
400
|
+
|
|
401
|
+
const queryArgs = {
|
|
402
|
+
where: {
|
|
403
|
+
type: filterArgs,
|
|
404
|
+
},
|
|
405
|
+
} as const satisfies GraphQLFieldConfigArgumentMap;
|
|
406
|
+
|
|
407
|
+
return {
|
|
408
|
+
name: queryName,
|
|
409
|
+
resolver: async (
|
|
410
|
+
source,
|
|
411
|
+
args: { where?: Filters<Table> },
|
|
412
|
+
context,
|
|
413
|
+
info
|
|
414
|
+
) => {
|
|
415
|
+
try {
|
|
416
|
+
const { where } = args;
|
|
417
|
+
|
|
418
|
+
const parsedInfo = parseResolveInfo(info, {
|
|
419
|
+
deep: true,
|
|
420
|
+
}) as ResolveTree;
|
|
421
|
+
|
|
422
|
+
const columns = extractSelectedColumnsFromTreeSQLFormat<PgColumn>(
|
|
423
|
+
parsedInfo.fieldsByTypeName[typeName]!,
|
|
424
|
+
table
|
|
425
|
+
);
|
|
426
|
+
|
|
427
|
+
let query = db.delete(table);
|
|
428
|
+
if (where) {
|
|
429
|
+
const filters = extractFilters(table, tableName, where);
|
|
430
|
+
query = query.where(filters) as any;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
query = query.returning(columns) as any;
|
|
434
|
+
|
|
435
|
+
const result = await query;
|
|
436
|
+
|
|
437
|
+
return remapToGraphQLArrayOutput(result, tableName, table);
|
|
438
|
+
} catch (e) {
|
|
439
|
+
if (typeof e === "object" && typeof (<any>e).message === "string") {
|
|
440
|
+
throw new GraphQLError((<any>e).message);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
throw e;
|
|
444
|
+
}
|
|
445
|
+
},
|
|
446
|
+
args: queryArgs,
|
|
447
|
+
};
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
export const generateSchemaData = <
|
|
451
|
+
TDrizzleInstance extends PgDatabase<any, any, any>,
|
|
452
|
+
TSchema extends Record<string, Table | unknown>
|
|
453
|
+
>(
|
|
454
|
+
db: TDrizzleInstance,
|
|
455
|
+
schema: TSchema,
|
|
456
|
+
relationsDepthLimit: number | undefined
|
|
457
|
+
): GeneratedEntities<TDrizzleInstance, TSchema> => {
|
|
458
|
+
const rawSchema = schema;
|
|
459
|
+
const schemaEntries = Object.entries(rawSchema);
|
|
460
|
+
|
|
461
|
+
const tableEntries = schemaEntries.filter(([key, value]) =>
|
|
462
|
+
is(value, PgTable)
|
|
463
|
+
) as [string, PgTable][];
|
|
464
|
+
const tables = Object.fromEntries(tableEntries) as Record<string, PgTable>;
|
|
465
|
+
|
|
466
|
+
if (!tableEntries.length) {
|
|
467
|
+
throw new Error(
|
|
468
|
+
"Drizzle-GraphQL Error: No tables detected in Drizzle-ORM's database instance. Did you forget to pass schema to drizzle constructor?"
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const rawRelations = schemaEntries
|
|
473
|
+
.filter(([key, value]) => is(value, Relations))
|
|
474
|
+
.map<[string, Relations]>(([key, value]) => [
|
|
475
|
+
tableEntries.find(
|
|
476
|
+
([tableName, tableValue]) => tableValue === (value as Relations).table
|
|
477
|
+
)![0] as string,
|
|
478
|
+
value as Relations,
|
|
479
|
+
])
|
|
480
|
+
.map<[string, Record<string, Relation>]>(([tableName, relValue]) => [
|
|
481
|
+
tableName,
|
|
482
|
+
relValue.config(createTableRelationsHelpers(tables[tableName]!)),
|
|
483
|
+
]);
|
|
484
|
+
|
|
485
|
+
const namedRelations = Object.fromEntries(
|
|
486
|
+
rawRelations.map(([relName, config]) => {
|
|
487
|
+
const namedConfig: Record<string, TableNamedRelations> =
|
|
488
|
+
Object.fromEntries(
|
|
489
|
+
Object.entries(config).map(([innerRelName, innerRelValue]) => [
|
|
490
|
+
innerRelName,
|
|
491
|
+
{
|
|
492
|
+
relation: innerRelValue,
|
|
493
|
+
targetTableName: tableEntries.find(
|
|
494
|
+
([tableName, tableValue]) =>
|
|
495
|
+
tableValue === innerRelValue.referencedTable
|
|
496
|
+
)![0],
|
|
497
|
+
},
|
|
498
|
+
])
|
|
499
|
+
);
|
|
500
|
+
|
|
501
|
+
return [relName, namedConfig];
|
|
502
|
+
})
|
|
503
|
+
);
|
|
504
|
+
|
|
505
|
+
const queries: ThunkObjMap<GraphQLFieldConfig<any, any>> = {};
|
|
506
|
+
const mutations: ThunkObjMap<GraphQLFieldConfig<any, any>> = {};
|
|
507
|
+
const gqlSchemaTypes = Object.fromEntries(
|
|
508
|
+
Object.entries(tables).map(([tableName, table]) => [
|
|
509
|
+
tableName,
|
|
510
|
+
generateTableTypes(
|
|
511
|
+
tableName,
|
|
512
|
+
tables,
|
|
513
|
+
namedRelations,
|
|
514
|
+
true,
|
|
515
|
+
relationsDepthLimit
|
|
516
|
+
),
|
|
517
|
+
])
|
|
518
|
+
);
|
|
519
|
+
|
|
520
|
+
const inputs: Record<string, GraphQLInputObjectType> = {};
|
|
521
|
+
const interfaces: Record<string, GraphQLInterfaceType> = {};
|
|
522
|
+
const outputs: Record<string, GraphQLObjectType> = {};
|
|
523
|
+
|
|
524
|
+
for (const [tableName, tableTypes] of Object.entries(gqlSchemaTypes)) {
|
|
525
|
+
const { insertInput, updateInput, tableFilters, tableOrder } =
|
|
526
|
+
tableTypes.inputs;
|
|
527
|
+
const {
|
|
528
|
+
selectSingleOutput,
|
|
529
|
+
selectArrOutput,
|
|
530
|
+
singleTableItemOutput,
|
|
531
|
+
arrTableItemOutput,
|
|
532
|
+
tableFieldsInterface,
|
|
533
|
+
} = tableTypes.outputs;
|
|
534
|
+
|
|
535
|
+
const selectArrGenerated = generateSelectArray(
|
|
536
|
+
db,
|
|
537
|
+
tableName,
|
|
538
|
+
tables,
|
|
539
|
+
namedRelations,
|
|
540
|
+
tableOrder,
|
|
541
|
+
tableFilters
|
|
542
|
+
);
|
|
543
|
+
const selectSingleGenerated = generateSelectSingle(
|
|
544
|
+
db,
|
|
545
|
+
tableName,
|
|
546
|
+
tables,
|
|
547
|
+
namedRelations,
|
|
548
|
+
tableOrder,
|
|
549
|
+
tableFilters
|
|
550
|
+
);
|
|
551
|
+
const insertArrGenerated = generateInsertArray(
|
|
552
|
+
db,
|
|
553
|
+
tableName,
|
|
554
|
+
schema[tableName] as PgTable,
|
|
555
|
+
insertInput
|
|
556
|
+
);
|
|
557
|
+
const insertSingleGenerated = generateInsertSingle(
|
|
558
|
+
db,
|
|
559
|
+
tableName,
|
|
560
|
+
schema[tableName] as PgTable,
|
|
561
|
+
insertInput
|
|
562
|
+
);
|
|
563
|
+
const updateGenerated = generateUpdate(
|
|
564
|
+
db,
|
|
565
|
+
tableName,
|
|
566
|
+
schema[tableName] as PgTable,
|
|
567
|
+
updateInput,
|
|
568
|
+
tableFilters
|
|
569
|
+
);
|
|
570
|
+
const deleteGenerated = generateDelete(
|
|
571
|
+
db,
|
|
572
|
+
tableName,
|
|
573
|
+
schema[tableName] as PgTable,
|
|
574
|
+
tableFilters
|
|
575
|
+
);
|
|
576
|
+
|
|
577
|
+
queries[selectArrGenerated.name] = {
|
|
578
|
+
type: selectArrOutput,
|
|
579
|
+
args: selectArrGenerated.args,
|
|
580
|
+
resolve: selectArrGenerated.resolver,
|
|
581
|
+
};
|
|
582
|
+
queries[selectSingleGenerated.name] = {
|
|
583
|
+
type: selectSingleOutput,
|
|
584
|
+
args: selectSingleGenerated.args,
|
|
585
|
+
resolve: selectSingleGenerated.resolver,
|
|
586
|
+
};
|
|
587
|
+
mutations[insertArrGenerated.name] = {
|
|
588
|
+
type: arrTableItemOutput,
|
|
589
|
+
args: insertArrGenerated.args,
|
|
590
|
+
resolve: insertArrGenerated.resolver,
|
|
591
|
+
};
|
|
592
|
+
mutations[insertSingleGenerated.name] = {
|
|
593
|
+
type: singleTableItemOutput,
|
|
594
|
+
args: insertSingleGenerated.args,
|
|
595
|
+
resolve: insertSingleGenerated.resolver,
|
|
596
|
+
};
|
|
597
|
+
mutations[updateGenerated.name] = {
|
|
598
|
+
type: arrTableItemOutput,
|
|
599
|
+
args: updateGenerated.args,
|
|
600
|
+
resolve: updateGenerated.resolver,
|
|
601
|
+
};
|
|
602
|
+
mutations[deleteGenerated.name] = {
|
|
603
|
+
type: arrTableItemOutput,
|
|
604
|
+
args: deleteGenerated.args,
|
|
605
|
+
resolve: deleteGenerated.resolver,
|
|
606
|
+
};
|
|
607
|
+
[insertInput, updateInput, tableFilters, tableOrder].forEach(
|
|
608
|
+
(e) => (inputs[e.name] = e)
|
|
609
|
+
);
|
|
610
|
+
outputs[selectSingleOutput.name] = selectSingleOutput;
|
|
611
|
+
outputs[singleTableItemOutput.name] = singleTableItemOutput;
|
|
612
|
+
interfaces[tableFieldsInterface.name] = tableFieldsInterface;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
return { queries, mutations, inputs, interfaces, types: outputs } as any;
|
|
616
|
+
};
|