@omnigraph/sql 0.0.0-stage → 0.1.0
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/CHANGELOG.md +7 -0
- package/dist/cjs/directives.js +59 -0
- package/dist/cjs/directives.js.map +7 -0
- package/dist/cjs/index.js +22 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/introspect/mysql.js +93 -0
- package/dist/cjs/introspect/postgres.js +106 -0
- package/dist/cjs/introspect/sqlite.js +60 -0
- package/dist/cjs/introspect/sqlite.js.map +7 -0
- package/dist/cjs/introspect/types.js +0 -0
- package/dist/cjs/introspect/types.js.map +7 -0
- package/dist/cjs/naming.js +54 -0
- package/dist/cjs/naming.js.map +7 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/schema.js +712 -0
- package/dist/cjs/schema.js.map +7 -0
- package/dist/cjs/type-map.js +40 -0
- package/dist/cjs/type-map.js.map +7 -0
- package/dist/esm/directives.js +55 -0
- package/dist/esm/directives.js.map +7 -0
- package/dist/esm/index.js +15 -0
- package/dist/esm/index.js.map +7 -0
- package/dist/esm/introspect/mysql.js +90 -0
- package/dist/esm/introspect/postgres.js +102 -0
- package/dist/esm/introspect/sqlite.js +57 -0
- package/dist/esm/introspect/sqlite.js.map +7 -0
- package/dist/esm/introspect/types.js +0 -0
- package/dist/esm/introspect/types.js.map +7 -0
- package/dist/esm/naming.js +50 -0
- package/dist/esm/naming.js.map +7 -0
- package/dist/esm/schema.js +707 -0
- package/dist/esm/schema.js.map +7 -0
- package/dist/esm/type-map.js +37 -0
- package/dist/esm/type-map.js.map +7 -0
- package/dist/package.json +54 -0
- package/dist/typings/directives.d.cts +17 -0
- package/dist/typings/directives.d.ts +17 -0
- package/dist/typings/index.d.cts +10 -0
- package/dist/typings/index.d.ts +10 -0
- package/dist/typings/introspect/mysql.d.cts +7 -0
- package/dist/typings/introspect/mysql.d.ts +7 -0
- package/dist/typings/introspect/postgres.d.cts +9 -0
- package/dist/typings/introspect/postgres.d.ts +9 -0
- package/dist/typings/introspect/sqlite.d.cts +8 -0
- package/dist/typings/introspect/sqlite.d.ts +8 -0
- package/dist/typings/introspect/types.d.cts +26 -0
- package/dist/typings/introspect/types.d.ts +26 -0
- package/dist/typings/naming.d.cts +17 -0
- package/dist/typings/naming.d.ts +17 -0
- package/dist/typings/schema.d.cts +39 -0
- package/dist/typings/schema.d.ts +39 -0
- package/dist/typings/type-map.d.cts +4 -0
- package/dist/typings/type-map.d.ts +4 -0
- package/package.json +61 -4
- package/src/directives.ts +67 -0
- package/src/index.ts +20 -0
- package/src/introspect/mysql.ts +110 -0
- package/src/introspect/postgres.ts +142 -0
- package/src/introspect/sqlite.ts +90 -0
- package/src/introspect/types.ts +31 -0
- package/src/naming.ts +67 -0
- package/src/schema.ts +834 -0
- package/src/type-map.ts +50 -0
- package/test/__snapshots__/sqlite-chinook.spec.ts.snap +328 -0
- package/test/sqlite-chinook.spec.ts +33 -0
- package/README.md +0 -3
|
@@ -0,0 +1,712 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadGraphQLSchemaFromSql = loadGraphQLSchemaFromSql;
|
|
4
|
+
exports.resolveOptsEndpoint = resolveOptsEndpoint;
|
|
5
|
+
exports.resolveAdapter = resolveAdapter;
|
|
6
|
+
const graphql_1 = require("graphql");
|
|
7
|
+
const graphql_compose_1 = require("graphql-compose");
|
|
8
|
+
const graphql_scalars_1 = require("graphql-scalars");
|
|
9
|
+
const cross_helpers_1 = require("@graphql-mesh/cross-helpers");
|
|
10
|
+
const transport_sql_1 = require("@graphql-mesh/transport-sql");
|
|
11
|
+
const directives_js_1 = require("./directives.js");
|
|
12
|
+
const mysql_js_1 = require("./introspect/mysql.js");
|
|
13
|
+
const postgres_js_1 = require("./introspect/postgres.js");
|
|
14
|
+
const sqlite_js_1 = require("./introspect/sqlite.js");
|
|
15
|
+
const naming_js_1 = require("./naming.js");
|
|
16
|
+
const type_map_js_1 = require("./type-map.js");
|
|
17
|
+
const FEDERATION_LINK_URL = 'https://specs.apollo.dev/federation/v2.6';
|
|
18
|
+
async function loadGraphQLSchemaFromSql(subgraphName, opts) {
|
|
19
|
+
const naming = { ...(0, naming_js_1.createDefaultNaming)(), ...opts.naming };
|
|
20
|
+
const withEndpoint = resolveOptsEndpoint(opts);
|
|
21
|
+
const adapter = resolveAdapter(withEndpoint);
|
|
22
|
+
const resolved = {
|
|
23
|
+
...withEndpoint,
|
|
24
|
+
adapter,
|
|
25
|
+
};
|
|
26
|
+
const meta = applyTableFieldsFilter(await introspect(resolved), opts.tableFields);
|
|
27
|
+
const location = resolveLocation(resolved);
|
|
28
|
+
const schemaComposer = new graphql_compose_1.SchemaComposer();
|
|
29
|
+
schemaComposer.add(graphql_scalars_1.GraphQLBigInt);
|
|
30
|
+
schemaComposer.add(graphql_scalars_1.GraphQLJSON);
|
|
31
|
+
schemaComposer.add(graphql_scalars_1.GraphQLDate);
|
|
32
|
+
schemaComposer.add(graphql_scalars_1.GraphQLDateTime);
|
|
33
|
+
schemaComposer.addDirective(directives_js_1.TransportDirective);
|
|
34
|
+
schemaComposer.addDirective(directives_js_1.SqlDirective);
|
|
35
|
+
schemaComposer.add(directives_js_1.SqlKindEnum);
|
|
36
|
+
ensureSharedFilterInputs(schemaComposer);
|
|
37
|
+
ensurePageInfo(schemaComposer);
|
|
38
|
+
const tableByName = new Map(meta.tables.map(t => [t.name, t]));
|
|
39
|
+
const typeNameByTable = new Map(meta.tables.map(t => [t.name, naming.typeName(t.name)]));
|
|
40
|
+
// Pass 1: object types + column fields
|
|
41
|
+
for (const table of meta.tables) {
|
|
42
|
+
const typeName = typeNameByTable.get(table.name);
|
|
43
|
+
const pkCols = table.columns.filter(c => c.primaryKey);
|
|
44
|
+
const solePk = pkCols.length === 1 ? pkCols[0] : undefined;
|
|
45
|
+
const otc = schemaComposer.createObjectTC({
|
|
46
|
+
name: typeName,
|
|
47
|
+
fields: {},
|
|
48
|
+
});
|
|
49
|
+
const typeDirectives = [
|
|
50
|
+
{
|
|
51
|
+
name: 'key',
|
|
52
|
+
args: {
|
|
53
|
+
fields: solePk
|
|
54
|
+
? naming.pkFieldName(solePk.name, true)
|
|
55
|
+
: pkCols.map(c => naming.fieldName(c.name)).join(' '),
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
];
|
|
59
|
+
// Type-level `@sql` = Federation entity resolver (Connectors-style `$this` keys).
|
|
60
|
+
if (pkCols.length) {
|
|
61
|
+
const keyArgs = pkCols
|
|
62
|
+
.map(col => {
|
|
63
|
+
const field = solePk && col.name === solePk.name
|
|
64
|
+
? naming.pkFieldName(col.name, true)
|
|
65
|
+
: naming.fieldName(col.name);
|
|
66
|
+
return `${col.name}: $this.${field}`;
|
|
67
|
+
})
|
|
68
|
+
.join('\n');
|
|
69
|
+
typeDirectives.push({
|
|
70
|
+
name: 'sql',
|
|
71
|
+
args: {
|
|
72
|
+
subgraph: subgraphName,
|
|
73
|
+
table: table.name,
|
|
74
|
+
kind: 'ROW',
|
|
75
|
+
where: blockArg(keyArgs),
|
|
76
|
+
selection: selectionBlock(naming, table),
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
otc.setDirectives(typeDirectives);
|
|
81
|
+
for (const col of table.columns) {
|
|
82
|
+
const isSolePkCol = Boolean(solePk && col.name === solePk.name);
|
|
83
|
+
const fieldName = isSolePkCol
|
|
84
|
+
? naming.pkFieldName(col.name, true)
|
|
85
|
+
: naming.fieldName(col.name);
|
|
86
|
+
const gqlType = (0, type_map_js_1.mapSqlTypeToGraphQL)(col.dataType, { asId: isSolePkCol });
|
|
87
|
+
otc.setField(fieldName, {
|
|
88
|
+
type: col.notNull || isSolePkCol ? new graphql_1.GraphQLNonNull(gqlType) : gqlType,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
createFilterAndOrder(schemaComposer, naming, typeName, table, solePk);
|
|
92
|
+
createConnectionTypes(schemaComposer, typeName);
|
|
93
|
+
createMutationInputs(schemaComposer, naming, typeName, table, solePk);
|
|
94
|
+
}
|
|
95
|
+
// Pass 2: relations
|
|
96
|
+
for (const table of meta.tables) {
|
|
97
|
+
const typeName = typeNameByTable.get(table.name);
|
|
98
|
+
const otc = schemaComposer.getOTC(typeName);
|
|
99
|
+
const solePk = table.columns.filter(c => c.primaryKey);
|
|
100
|
+
const solePkCol = solePk.length === 1 ? solePk[0] : undefined;
|
|
101
|
+
for (const fk of table.foreignKeys) {
|
|
102
|
+
if (fk.columns.length !== 1 || fk.refColumns.length !== 1)
|
|
103
|
+
continue;
|
|
104
|
+
const refTable = tableByName.get(fk.refTable);
|
|
105
|
+
if (!refTable)
|
|
106
|
+
continue;
|
|
107
|
+
const refTypeName = typeNameByTable.get(fk.refTable);
|
|
108
|
+
const localCol = fk.columns[0];
|
|
109
|
+
const refCol = fk.refColumns[0];
|
|
110
|
+
const localField = solePkCol && localCol === solePkCol.name
|
|
111
|
+
? naming.pkFieldName(localCol, true)
|
|
112
|
+
: naming.fieldName(localCol);
|
|
113
|
+
const refSolePk = refTable.columns.filter(c => c.primaryKey);
|
|
114
|
+
const refField = refSolePk.length === 1 && refCol === refSolePk[0].name
|
|
115
|
+
? naming.pkFieldName(refCol, true)
|
|
116
|
+
: naming.fieldName(refCol);
|
|
117
|
+
// belongsTo on local table
|
|
118
|
+
const belongsToName = naming.singularField(refTypeName);
|
|
119
|
+
if (!otc.hasField(belongsToName)) {
|
|
120
|
+
otc.setField(belongsToName, {
|
|
121
|
+
type: refTypeName,
|
|
122
|
+
directives: [
|
|
123
|
+
{
|
|
124
|
+
name: 'sql',
|
|
125
|
+
args: {
|
|
126
|
+
subgraph: subgraphName,
|
|
127
|
+
table: fk.refTable,
|
|
128
|
+
kind: 'ROW',
|
|
129
|
+
where: blockArg(`${refCol}: $this.${localField}`),
|
|
130
|
+
selection: selectionBlock(naming, refTable),
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
],
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
// hasMany on referenced table
|
|
137
|
+
const refOtc = schemaComposer.getOTC(refTypeName);
|
|
138
|
+
const hasManyName = naming.pluralField(typeName);
|
|
139
|
+
if (!refOtc.hasField(hasManyName)) {
|
|
140
|
+
refOtc.setField(hasManyName, {
|
|
141
|
+
type: `[${typeName}!]!`,
|
|
142
|
+
args: {
|
|
143
|
+
filter: naming.filterInput(typeName),
|
|
144
|
+
orderBy: `[${naming.orderByEnum(typeName)}!]`,
|
|
145
|
+
limit: 'Int',
|
|
146
|
+
offset: 'Int',
|
|
147
|
+
},
|
|
148
|
+
directives: [
|
|
149
|
+
{
|
|
150
|
+
name: 'sql',
|
|
151
|
+
args: {
|
|
152
|
+
subgraph: subgraphName,
|
|
153
|
+
table: table.name,
|
|
154
|
+
kind: 'ROWS',
|
|
155
|
+
where: blockArg(`${localCol}: $this.${refField}`),
|
|
156
|
+
filter: '$args.filter',
|
|
157
|
+
orderBy: '$args.orderBy',
|
|
158
|
+
limit: '$args.limit',
|
|
159
|
+
offset: '$args.offset',
|
|
160
|
+
selection: selectionBlock(naming, table),
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
],
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
// Pass 3: Query + Mutation roots
|
|
169
|
+
const queryTC = schemaComposer.Query;
|
|
170
|
+
const mutationTC = schemaComposer.Mutation;
|
|
171
|
+
for (const table of meta.tables) {
|
|
172
|
+
const typeName = typeNameByTable.get(table.name);
|
|
173
|
+
const pkCols = table.columns.filter(c => c.primaryKey);
|
|
174
|
+
const solePk = pkCols.length === 1 ? pkCols[0] : undefined;
|
|
175
|
+
const selectionRaw = selectionLines(naming, table);
|
|
176
|
+
const selection = blockArg(selectionRaw);
|
|
177
|
+
const plural = naming.pluralField(typeName);
|
|
178
|
+
const singular = naming.singularField(typeName);
|
|
179
|
+
if (solePk) {
|
|
180
|
+
const pkField = naming.pkFieldName(solePk.name, true);
|
|
181
|
+
queryTC.setField(singular, {
|
|
182
|
+
type: typeName,
|
|
183
|
+
args: {
|
|
184
|
+
[pkField]: 'ID!',
|
|
185
|
+
},
|
|
186
|
+
directives: [
|
|
187
|
+
{
|
|
188
|
+
name: 'sql',
|
|
189
|
+
args: {
|
|
190
|
+
subgraph: subgraphName,
|
|
191
|
+
table: table.name,
|
|
192
|
+
kind: 'ROW',
|
|
193
|
+
where: blockArg(`${solePk.name}: $args.${pkField}`),
|
|
194
|
+
selection,
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
],
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
queryTC.setField(plural, {
|
|
201
|
+
type: `[${typeName}!]!`,
|
|
202
|
+
args: {
|
|
203
|
+
filter: naming.filterInput(typeName),
|
|
204
|
+
orderBy: `[${naming.orderByEnum(typeName)}!]`,
|
|
205
|
+
limit: { type: graphql_1.GraphQLInt, defaultValue: 10 },
|
|
206
|
+
offset: { type: graphql_1.GraphQLInt, defaultValue: 0 },
|
|
207
|
+
},
|
|
208
|
+
directives: [
|
|
209
|
+
{
|
|
210
|
+
name: 'sql',
|
|
211
|
+
args: {
|
|
212
|
+
subgraph: subgraphName,
|
|
213
|
+
table: table.name,
|
|
214
|
+
kind: 'ROWS',
|
|
215
|
+
filter: '$args.filter',
|
|
216
|
+
orderBy: '$args.orderBy',
|
|
217
|
+
limit: '$args.limit',
|
|
218
|
+
offset: '$args.offset',
|
|
219
|
+
selection,
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
],
|
|
223
|
+
});
|
|
224
|
+
queryTC.setField(naming.connectionField(typeName), {
|
|
225
|
+
type: `${typeName}Connection!`,
|
|
226
|
+
args: {
|
|
227
|
+
filter: naming.filterInput(typeName),
|
|
228
|
+
orderBy: `[${naming.orderByEnum(typeName)}!]`,
|
|
229
|
+
first: graphql_1.GraphQLInt,
|
|
230
|
+
after: graphql_1.GraphQLString,
|
|
231
|
+
},
|
|
232
|
+
directives: [
|
|
233
|
+
{
|
|
234
|
+
name: 'sql',
|
|
235
|
+
args: {
|
|
236
|
+
subgraph: subgraphName,
|
|
237
|
+
table: table.name,
|
|
238
|
+
kind: 'CONNECTION',
|
|
239
|
+
filter: '$args.filter',
|
|
240
|
+
orderBy: '$args.orderBy',
|
|
241
|
+
first: '$args.first',
|
|
242
|
+
after: '$args.after',
|
|
243
|
+
selection: blockArg([
|
|
244
|
+
`nodes {`,
|
|
245
|
+
indent(selectionRaw),
|
|
246
|
+
`}`,
|
|
247
|
+
`edges {`,
|
|
248
|
+
` cursor: $cursor`,
|
|
249
|
+
` node {`,
|
|
250
|
+
indent(selectionRaw, 2),
|
|
251
|
+
` }`,
|
|
252
|
+
`}`,
|
|
253
|
+
`pageInfo {`,
|
|
254
|
+
` hasNextPage: $hasNextPage`,
|
|
255
|
+
` hasPreviousPage: $hasPreviousPage`,
|
|
256
|
+
` startCursor: $startCursor`,
|
|
257
|
+
` endCursor: $endCursor`,
|
|
258
|
+
`}`,
|
|
259
|
+
`totalCount: $totalCount`,
|
|
260
|
+
].join('\n')),
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
],
|
|
264
|
+
});
|
|
265
|
+
queryTC.setField(naming.countField(typeName), {
|
|
266
|
+
type: 'Int!',
|
|
267
|
+
args: {
|
|
268
|
+
filter: naming.filterInput(typeName),
|
|
269
|
+
},
|
|
270
|
+
directives: [
|
|
271
|
+
{
|
|
272
|
+
name: 'sql',
|
|
273
|
+
args: {
|
|
274
|
+
subgraph: subgraphName,
|
|
275
|
+
table: table.name,
|
|
276
|
+
kind: 'COUNT',
|
|
277
|
+
filter: '$args.filter',
|
|
278
|
+
selection: blockArg(`$`),
|
|
279
|
+
},
|
|
280
|
+
},
|
|
281
|
+
],
|
|
282
|
+
});
|
|
283
|
+
// Mutations
|
|
284
|
+
const payloadKey = singular;
|
|
285
|
+
mutationTC.setField(`create${typeName}`, {
|
|
286
|
+
type: `${naming.payloadType(typeName)}!`,
|
|
287
|
+
args: {
|
|
288
|
+
input: `${naming.createInput(typeName)}!`,
|
|
289
|
+
},
|
|
290
|
+
directives: [
|
|
291
|
+
{
|
|
292
|
+
name: 'sql',
|
|
293
|
+
args: {
|
|
294
|
+
subgraph: subgraphName,
|
|
295
|
+
table: table.name,
|
|
296
|
+
kind: 'INSERT',
|
|
297
|
+
set: insertSetBlock(naming, table, solePk),
|
|
298
|
+
selection: blockArg(`${payloadKey} {\n${indent(selectionRaw)}\n}`),
|
|
299
|
+
},
|
|
300
|
+
},
|
|
301
|
+
],
|
|
302
|
+
});
|
|
303
|
+
if (solePk) {
|
|
304
|
+
const pkField = naming.pkFieldName(solePk.name, true);
|
|
305
|
+
mutationTC.setField(`update${typeName}`, {
|
|
306
|
+
type: `${naming.payloadType(typeName)}!`,
|
|
307
|
+
args: {
|
|
308
|
+
[pkField]: 'ID!',
|
|
309
|
+
input: `${naming.updateInput(typeName)}!`,
|
|
310
|
+
},
|
|
311
|
+
directives: [
|
|
312
|
+
{
|
|
313
|
+
name: 'sql',
|
|
314
|
+
args: {
|
|
315
|
+
subgraph: subgraphName,
|
|
316
|
+
table: table.name,
|
|
317
|
+
kind: 'UPDATE',
|
|
318
|
+
set: blockArg(updateSetBlock(naming, table, solePk)),
|
|
319
|
+
where: blockArg(`${solePk.name}: $args.${pkField}`),
|
|
320
|
+
selection: blockArg(`${payloadKey} {\n${indent(selectionRaw)}\n}`),
|
|
321
|
+
},
|
|
322
|
+
},
|
|
323
|
+
],
|
|
324
|
+
});
|
|
325
|
+
mutationTC.setField(`delete${typeName}`, {
|
|
326
|
+
type: `${naming.deletePayloadType(typeName)}!`,
|
|
327
|
+
args: {
|
|
328
|
+
[pkField]: 'ID!',
|
|
329
|
+
},
|
|
330
|
+
directives: [
|
|
331
|
+
{
|
|
332
|
+
name: 'sql',
|
|
333
|
+
args: {
|
|
334
|
+
subgraph: subgraphName,
|
|
335
|
+
table: table.name,
|
|
336
|
+
kind: 'DELETE',
|
|
337
|
+
where: blockArg(`${solePk.name}: $args.${pkField}`),
|
|
338
|
+
selection: blockArg([`ok: $affectedRows`, `affectedRows: $affectedRows`].join('\n')),
|
|
339
|
+
},
|
|
340
|
+
},
|
|
341
|
+
],
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
const schema = schemaComposer.buildSchema();
|
|
346
|
+
const extensions = (schema.extensions ||= {});
|
|
347
|
+
extensions.directives ||= {};
|
|
348
|
+
extensions.directives.transport = {
|
|
349
|
+
subgraph: subgraphName,
|
|
350
|
+
kind: 'sql',
|
|
351
|
+
location,
|
|
352
|
+
options: buildTransportOptions(resolved, adapter),
|
|
353
|
+
};
|
|
354
|
+
extensions.directives.link = [
|
|
355
|
+
{
|
|
356
|
+
url: FEDERATION_LINK_URL,
|
|
357
|
+
import: ['@key'],
|
|
358
|
+
},
|
|
359
|
+
];
|
|
360
|
+
// graphql-compose emits StringValue without `block: true`, so printers escape
|
|
361
|
+
// newlines as `\n`. Mark @sql args/selection as block strings for readable """ SDL.
|
|
362
|
+
markSqlDirectiveStringsAsBlocks(schema);
|
|
363
|
+
return schema;
|
|
364
|
+
}
|
|
365
|
+
async function introspect(opts) {
|
|
366
|
+
switch (opts.adapter) {
|
|
367
|
+
case 'sqlite': {
|
|
368
|
+
if (!opts.endpoint) {
|
|
369
|
+
throw new Error(`loadSqlSubgraph sqlite requires endpoint (file:… / *.db)`);
|
|
370
|
+
}
|
|
371
|
+
return (0, sqlite_js_1.introspectSqlite)({
|
|
372
|
+
db: opts.endpoint,
|
|
373
|
+
cwd: opts.cwd,
|
|
374
|
+
includeTables: opts.includeTables,
|
|
375
|
+
excludeTables: opts.excludeTables,
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
case 'mysql': {
|
|
379
|
+
if (!opts.endpoint) {
|
|
380
|
+
throw new Error(`loadSqlSubgraph mysql requires endpoint (mysql://…)`);
|
|
381
|
+
}
|
|
382
|
+
return (0, mysql_js_1.introspectMysql)({
|
|
383
|
+
connectionString: opts.endpoint,
|
|
384
|
+
includeTables: opts.includeTables,
|
|
385
|
+
excludeTables: opts.excludeTables,
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
case 'postgres': {
|
|
389
|
+
if (!opts.endpoint) {
|
|
390
|
+
throw new Error(`loadSqlSubgraph postgres requires endpoint (postgres://…)`);
|
|
391
|
+
}
|
|
392
|
+
return (0, postgres_js_1.introspectPostgres)({
|
|
393
|
+
connectionString: opts.endpoint,
|
|
394
|
+
includeTables: opts.includeTables,
|
|
395
|
+
excludeTables: opts.excludeTables,
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
default:
|
|
399
|
+
throw new Error(`Unknown SQL adapter: ${opts.adapter}`);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Fill `endpoint` from Bun-compatible env vars when omitted.
|
|
404
|
+
*/
|
|
405
|
+
function resolveOptsEndpoint(opts) {
|
|
406
|
+
if (opts.endpoint)
|
|
407
|
+
return opts;
|
|
408
|
+
const fromEnv = (0, transport_sql_1.resolveConnectionStringFromEnv)(undefined, { adapter: opts.adapter });
|
|
409
|
+
if (!fromEnv) {
|
|
410
|
+
throw new Error('loadSqlSubgraph requires endpoint or a Bun-compatible env URL (DATABASE_URL, POSTGRES_URL, MYSQL_URL, SQLITE_URL, …)');
|
|
411
|
+
}
|
|
412
|
+
return { ...opts, endpoint: fromEnv };
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Resolve adapter: explicit `adapter` wins; otherwise Bun-style protocol detection
|
|
416
|
+
* from `endpoint` (or env-resolved endpoint).
|
|
417
|
+
*/
|
|
418
|
+
function resolveAdapter(opts) {
|
|
419
|
+
if (opts.adapter)
|
|
420
|
+
return opts.adapter;
|
|
421
|
+
const endpoint = opts.endpoint || (0, transport_sql_1.resolveConnectionStringFromEnv)();
|
|
422
|
+
if (!endpoint) {
|
|
423
|
+
throw new Error('loadSqlSubgraph requires adapter or endpoint (or DATABASE_URL / …)');
|
|
424
|
+
}
|
|
425
|
+
return (0, transport_sql_1.inferAdapterFromConnectionString)(endpoint);
|
|
426
|
+
}
|
|
427
|
+
function buildTransportOptions(opts, adapter) {
|
|
428
|
+
const options = { adapter };
|
|
429
|
+
if (opts.max != null)
|
|
430
|
+
options.max = opts.max;
|
|
431
|
+
if (opts.idleTimeout != null)
|
|
432
|
+
options.idleTimeout = opts.idleTimeout;
|
|
433
|
+
if (opts.connectionTimeout != null)
|
|
434
|
+
options.connectionTimeout = opts.connectionTimeout;
|
|
435
|
+
if (opts.tls != null)
|
|
436
|
+
options.tls = opts.tls;
|
|
437
|
+
return options;
|
|
438
|
+
}
|
|
439
|
+
function resolveLocation(opts) {
|
|
440
|
+
const raw = opts.endpoint;
|
|
441
|
+
if (!raw) {
|
|
442
|
+
throw new Error('SQL loader requires endpoint');
|
|
443
|
+
}
|
|
444
|
+
// Normalize bare sqlite paths to absolute file: URIs for @transport(location).
|
|
445
|
+
try {
|
|
446
|
+
if ((0, transport_sql_1.inferAdapterFromConnectionString)(raw) === 'sqlite') {
|
|
447
|
+
if (raw === ':memory:' || raw.toLowerCase().startsWith('sqlite:')) {
|
|
448
|
+
return raw;
|
|
449
|
+
}
|
|
450
|
+
if (raw.toLowerCase().startsWith('file:')) {
|
|
451
|
+
const cwd = opts.cwd || process.cwd();
|
|
452
|
+
const rest = raw.slice('file:'.length).replace(/^\/\/+/, '/');
|
|
453
|
+
if (cross_helpers_1.path.isAbsolute(rest) || rest === ':memory:')
|
|
454
|
+
return raw;
|
|
455
|
+
return `file:${cross_helpers_1.path.join(cwd, rest)}`;
|
|
456
|
+
}
|
|
457
|
+
const cwd = opts.cwd || process.cwd();
|
|
458
|
+
const abs = cross_helpers_1.path.isAbsolute(raw) ? raw : cross_helpers_1.path.join(cwd, raw);
|
|
459
|
+
return `file:${abs}`;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
catch {
|
|
463
|
+
// non-sqlite / unknown — return as given
|
|
464
|
+
}
|
|
465
|
+
return raw;
|
|
466
|
+
}
|
|
467
|
+
function applyTableFieldsFilter(meta, tableFields) {
|
|
468
|
+
if (!tableFields?.length)
|
|
469
|
+
return meta;
|
|
470
|
+
const allow = new Map(tableFields.map(tf => [tf.table, new Set(tf.fields)]));
|
|
471
|
+
return {
|
|
472
|
+
...meta,
|
|
473
|
+
tables: meta.tables.map(table => {
|
|
474
|
+
const fields = allow.get(table.name);
|
|
475
|
+
if (!fields)
|
|
476
|
+
return table;
|
|
477
|
+
return {
|
|
478
|
+
...table,
|
|
479
|
+
columns: table.columns.filter(c => fields.has(c.name) || c.primaryKey),
|
|
480
|
+
};
|
|
481
|
+
}),
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
function ensureSharedFilterInputs(schemaComposer) {
|
|
485
|
+
if (!schemaComposer.has('StringFilter')) {
|
|
486
|
+
schemaComposer.createInputTC({
|
|
487
|
+
name: 'StringFilter',
|
|
488
|
+
fields: {
|
|
489
|
+
eq: 'String',
|
|
490
|
+
ne: 'String',
|
|
491
|
+
in: '[String!]',
|
|
492
|
+
nin: '[String!]',
|
|
493
|
+
lt: 'String',
|
|
494
|
+
lte: 'String',
|
|
495
|
+
gt: 'String',
|
|
496
|
+
gte: 'String',
|
|
497
|
+
includes: 'String',
|
|
498
|
+
startsWith: 'String',
|
|
499
|
+
endsWith: 'String',
|
|
500
|
+
isNull: 'Boolean',
|
|
501
|
+
},
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
if (!schemaComposer.has('IntFilter')) {
|
|
505
|
+
schemaComposer.createInputTC({
|
|
506
|
+
name: 'IntFilter',
|
|
507
|
+
fields: {
|
|
508
|
+
eq: 'Int',
|
|
509
|
+
ne: 'Int',
|
|
510
|
+
in: '[Int!]',
|
|
511
|
+
nin: '[Int!]',
|
|
512
|
+
lt: 'Int',
|
|
513
|
+
lte: 'Int',
|
|
514
|
+
gt: 'Int',
|
|
515
|
+
gte: 'Int',
|
|
516
|
+
isNull: 'Boolean',
|
|
517
|
+
},
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
function ensurePageInfo(schemaComposer) {
|
|
522
|
+
if (!schemaComposer.has('PageInfo')) {
|
|
523
|
+
schemaComposer.createObjectTC({
|
|
524
|
+
name: 'PageInfo',
|
|
525
|
+
fields: {
|
|
526
|
+
hasNextPage: 'Boolean!',
|
|
527
|
+
hasPreviousPage: 'Boolean!',
|
|
528
|
+
startCursor: 'String',
|
|
529
|
+
endCursor: 'String',
|
|
530
|
+
},
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
function createFilterAndOrder(schemaComposer, naming, typeName, table, solePk) {
|
|
535
|
+
const filterFields = {
|
|
536
|
+
and: `[${naming.filterInput(typeName)}!]`,
|
|
537
|
+
or: `[${naming.filterInput(typeName)}!]`,
|
|
538
|
+
not: naming.filterInput(typeName),
|
|
539
|
+
};
|
|
540
|
+
const orderValues = {};
|
|
541
|
+
for (const col of table.columns) {
|
|
542
|
+
const isSolePkCol = Boolean(solePk && col.name === solePk.name);
|
|
543
|
+
const fieldName = isSolePkCol ? naming.pkFieldName(col.name, true) : naming.fieldName(col.name);
|
|
544
|
+
const gql = (0, type_map_js_1.mapSqlTypeToGraphQL)(col.dataType, { asId: isSolePkCol });
|
|
545
|
+
const filterType = gql === graphql_1.GraphQLInt || gql.name === 'Int' || gql.name === 'BigInt'
|
|
546
|
+
? 'IntFilter'
|
|
547
|
+
: gql === graphql_1.GraphQLBoolean
|
|
548
|
+
? 'IntFilter'
|
|
549
|
+
: 'StringFilter';
|
|
550
|
+
filterFields[fieldName] = filterType;
|
|
551
|
+
orderValues[naming.orderValue(fieldName, 'ASC')] = {
|
|
552
|
+
value: naming.orderValue(fieldName, 'ASC'),
|
|
553
|
+
};
|
|
554
|
+
orderValues[naming.orderValue(fieldName, 'DESC')] = {
|
|
555
|
+
value: naming.orderValue(fieldName, 'DESC'),
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
schemaComposer.createInputTC({
|
|
559
|
+
name: naming.filterInput(typeName),
|
|
560
|
+
fields: filterFields,
|
|
561
|
+
});
|
|
562
|
+
schemaComposer.createEnumTC({
|
|
563
|
+
name: naming.orderByEnum(typeName),
|
|
564
|
+
values: orderValues,
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
function createConnectionTypes(schemaComposer, typeName) {
|
|
568
|
+
if (!schemaComposer.has(`${typeName}Edge`)) {
|
|
569
|
+
schemaComposer.createObjectTC({
|
|
570
|
+
name: `${typeName}Edge`,
|
|
571
|
+
fields: {
|
|
572
|
+
cursor: 'String!',
|
|
573
|
+
node: `${typeName}!`,
|
|
574
|
+
},
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
if (!schemaComposer.has(`${typeName}Connection`)) {
|
|
578
|
+
schemaComposer.createObjectTC({
|
|
579
|
+
name: `${typeName}Connection`,
|
|
580
|
+
fields: {
|
|
581
|
+
edges: `[${typeName}Edge!]!`,
|
|
582
|
+
pageInfo: 'PageInfo!',
|
|
583
|
+
nodes: `[${typeName}!]!`,
|
|
584
|
+
totalCount: 'Int!',
|
|
585
|
+
},
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
function createMutationInputs(schemaComposer, naming, typeName, table, solePk) {
|
|
590
|
+
const createFields = {};
|
|
591
|
+
const updateFields = {};
|
|
592
|
+
for (const col of table.columns) {
|
|
593
|
+
const isSolePkCol = Boolean(solePk && col.name === solePk.name);
|
|
594
|
+
if (isSolePkCol && col.autoIncrement)
|
|
595
|
+
continue;
|
|
596
|
+
const fieldName = isSolePkCol ? naming.pkFieldName(col.name, true) : naming.fieldName(col.name);
|
|
597
|
+
const gqlType = (0, type_map_js_1.mapSqlTypeToGraphQL)(col.dataType, { asId: isSolePkCol });
|
|
598
|
+
createFields[fieldName] = {
|
|
599
|
+
type: col.notNull && !col.autoIncrement ? new graphql_1.GraphQLNonNull(gqlType) : gqlType,
|
|
600
|
+
};
|
|
601
|
+
updateFields[fieldName] = { type: gqlType };
|
|
602
|
+
}
|
|
603
|
+
schemaComposer.createInputTC({
|
|
604
|
+
name: naming.createInput(typeName),
|
|
605
|
+
fields: createFields,
|
|
606
|
+
});
|
|
607
|
+
schemaComposer.createInputTC({
|
|
608
|
+
name: naming.updateInput(typeName),
|
|
609
|
+
fields: updateFields,
|
|
610
|
+
});
|
|
611
|
+
const singular = naming.singularField(typeName);
|
|
612
|
+
schemaComposer.createObjectTC({
|
|
613
|
+
name: naming.payloadType(typeName),
|
|
614
|
+
fields: {
|
|
615
|
+
[singular]: typeName,
|
|
616
|
+
},
|
|
617
|
+
});
|
|
618
|
+
schemaComposer.createObjectTC({
|
|
619
|
+
name: naming.deletePayloadType(typeName),
|
|
620
|
+
fields: {
|
|
621
|
+
ok: 'Boolean!',
|
|
622
|
+
affectedRows: 'Int!',
|
|
623
|
+
},
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
function markSqlDirectiveStringsAsBlocks(schema) {
|
|
627
|
+
const markNode = (astNode) => {
|
|
628
|
+
const directives = astNode?.directives;
|
|
629
|
+
if (!directives)
|
|
630
|
+
return;
|
|
631
|
+
for (const directive of directives) {
|
|
632
|
+
if (directive.name.value !== 'sql' || !directive.arguments)
|
|
633
|
+
continue;
|
|
634
|
+
for (const arg of directive.arguments) {
|
|
635
|
+
if (arg.value.kind !== 'StringValue')
|
|
636
|
+
continue;
|
|
637
|
+
if (arg.name.value === 'where' ||
|
|
638
|
+
arg.name.value === 'set' ||
|
|
639
|
+
arg.name.value === 'selection') {
|
|
640
|
+
arg.value.block = true;
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
};
|
|
645
|
+
for (const type of Object.values(schema.getTypeMap())) {
|
|
646
|
+
if (!(type instanceof graphql_1.GraphQLObjectType))
|
|
647
|
+
continue;
|
|
648
|
+
markNode(type.astNode);
|
|
649
|
+
for (const field of Object.values(type.getFields())) {
|
|
650
|
+
markNode(field.astNode);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
/**
|
|
655
|
+
* Format `@sql` where/set/selection for Connectors-style `"""` block strings.
|
|
656
|
+
* Indent body lines; for single-line values prefix a newline so the printer
|
|
657
|
+
* emits a multiline `"""` block (not `"""value"""`) without a trailing blank.
|
|
658
|
+
*/
|
|
659
|
+
function blockArg(body) {
|
|
660
|
+
const trimmed = body.replace(/^\n+|\n+$/g, '');
|
|
661
|
+
const indented = trimmed
|
|
662
|
+
.split('\n')
|
|
663
|
+
.map(line => (line.length ? ` ${line}` : line))
|
|
664
|
+
.join('\n');
|
|
665
|
+
return indented.includes('\n') ? indented : `\n${indented}`;
|
|
666
|
+
}
|
|
667
|
+
/** Raw column mapping lines (no block wrapping / indent). */
|
|
668
|
+
function selectionLines(naming, table) {
|
|
669
|
+
const pkCols = table.columns.filter(c => c.primaryKey);
|
|
670
|
+
const solePk = pkCols.length === 1 ? pkCols[0] : undefined;
|
|
671
|
+
return table.columns
|
|
672
|
+
.map(col => {
|
|
673
|
+
const isSolePkCol = Boolean(solePk && col.name === solePk.name);
|
|
674
|
+
const fieldName = isSolePkCol
|
|
675
|
+
? naming.pkFieldName(col.name, true)
|
|
676
|
+
: naming.fieldName(col.name);
|
|
677
|
+
return `${fieldName}: ${col.name}`;
|
|
678
|
+
})
|
|
679
|
+
.join('\n');
|
|
680
|
+
}
|
|
681
|
+
function selectionBlock(naming, table) {
|
|
682
|
+
return blockArg(selectionLines(naming, table));
|
|
683
|
+
}
|
|
684
|
+
function insertSetBlock(naming, table, solePk) {
|
|
685
|
+
const lines = [];
|
|
686
|
+
for (const col of table.columns) {
|
|
687
|
+
const isSolePkCol = Boolean(solePk && col.name === solePk.name);
|
|
688
|
+
if (isSolePkCol && col.autoIncrement)
|
|
689
|
+
continue;
|
|
690
|
+
const fieldName = isSolePkCol ? naming.pkFieldName(col.name, true) : naming.fieldName(col.name);
|
|
691
|
+
lines.push(`${col.name}: $args.input.${fieldName}`);
|
|
692
|
+
}
|
|
693
|
+
return blockArg(lines.join('\n'));
|
|
694
|
+
}
|
|
695
|
+
function updateSetBlock(naming, table, solePk) {
|
|
696
|
+
const lines = [];
|
|
697
|
+
for (const col of table.columns) {
|
|
698
|
+
const isSolePkCol = Boolean(solePk && col.name === solePk.name);
|
|
699
|
+
if (isSolePkCol)
|
|
700
|
+
continue;
|
|
701
|
+
const fieldName = naming.fieldName(col.name);
|
|
702
|
+
lines.push(`${col.name}: $args.input.${fieldName}`);
|
|
703
|
+
}
|
|
704
|
+
return lines.join('\n');
|
|
705
|
+
}
|
|
706
|
+
function indent(text, spaces = 1) {
|
|
707
|
+
const pad = ' '.repeat(spaces);
|
|
708
|
+
return text
|
|
709
|
+
.split('\n')
|
|
710
|
+
.map(l => (l ? pad + l : l))
|
|
711
|
+
.join('\n');
|
|
712
|
+
}
|