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