@graphql-mesh/transport-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 +9 -0
- package/dist/cjs/adapter.js +60 -0
- package/dist/cjs/batch.js +113 -0
- package/dist/cjs/client.js +205 -0
- package/dist/cjs/client.js.map +7 -0
- package/dist/cjs/dialect.js +60 -0
- package/dist/cjs/dialect.js.map +7 -0
- package/dist/cjs/entity.js +186 -0
- package/dist/cjs/execution.js +307 -0
- package/dist/cjs/execution.js.map +7 -0
- package/dist/cjs/index.js +23 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/mapping.js +260 -0
- package/dist/cjs/mapping.js.map +7 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/pg-executor.js +53 -0
- package/dist/cjs/pg-plans.js +103 -0
- package/dist/cjs/pg-registry.js +239 -0
- package/dist/cjs/plan.js +359 -0
- package/dist/cjs/sql-builder.js +653 -0
- package/dist/cjs/sql-builder.js.map +7 -0
- package/dist/cjs/types.js +0 -0
- package/dist/cjs/types.js.map +7 -0
- package/dist/cjs/uri.js +267 -0
- package/dist/esm/adapter.js +54 -0
- package/dist/esm/batch.js +107 -0
- package/dist/esm/client.js +197 -0
- package/dist/esm/client.js.map +7 -0
- package/dist/esm/dialect.js +54 -0
- package/dist/esm/dialect.js.map +7 -0
- package/dist/esm/entity.js +180 -0
- package/dist/esm/execution.js +303 -0
- package/dist/esm/execution.js.map +7 -0
- package/dist/esm/index.js +20 -0
- package/dist/esm/index.js.map +7 -0
- package/dist/esm/mapping.js +255 -0
- package/dist/esm/mapping.js.map +7 -0
- package/dist/esm/pg-executor.js +49 -0
- package/dist/esm/pg-plans.js +100 -0
- package/dist/esm/pg-registry.js +236 -0
- package/dist/esm/plan.js +351 -0
- package/dist/esm/sql-builder.js +636 -0
- package/dist/esm/sql-builder.js.map +7 -0
- package/dist/esm/types.js +0 -0
- package/dist/esm/types.js.map +7 -0
- package/dist/esm/uri.js +261 -0
- package/dist/package.json +57 -0
- package/dist/typings/adapter.d.cts +9 -0
- package/dist/typings/adapter.d.ts +9 -0
- package/dist/typings/batch.d.cts +21 -0
- package/dist/typings/batch.d.ts +21 -0
- package/dist/typings/client.d.cts +36 -0
- package/dist/typings/client.d.ts +36 -0
- package/dist/typings/dialect.d.cts +9 -0
- package/dist/typings/dialect.d.ts +9 -0
- package/dist/typings/entity.d.cts +24 -0
- package/dist/typings/entity.d.ts +24 -0
- package/dist/typings/execution.d.cts +9 -0
- package/dist/typings/execution.d.ts +9 -0
- package/dist/typings/index.d.cts +15 -0
- package/dist/typings/index.d.ts +15 -0
- package/dist/typings/mapping.d.cts +13 -0
- package/dist/typings/mapping.d.ts +13 -0
- package/dist/typings/pg-executor.d.cts +19 -0
- package/dist/typings/pg-executor.d.ts +19 -0
- package/dist/typings/pg-plans.d.cts +12 -0
- package/dist/typings/pg-plans.d.ts +12 -0
- package/dist/typings/pg-registry.d.cts +16 -0
- package/dist/typings/pg-registry.d.ts +16 -0
- package/dist/typings/plan.d.cts +47 -0
- package/dist/typings/plan.d.ts +47 -0
- package/dist/typings/sql-builder.d.cts +75 -0
- package/dist/typings/sql-builder.d.ts +75 -0
- package/dist/typings/types.d.cts +50 -0
- package/dist/typings/types.d.ts +50 -0
- package/dist/typings/uri.d.cts +42 -0
- package/dist/typings/uri.d.ts +42 -0
- package/package.json +67 -4
- package/src/adapter.ts +56 -0
- package/src/batch.ts +150 -0
- package/src/client.ts +233 -0
- package/src/entity.ts +235 -0
- package/src/execution.ts +411 -0
- package/src/index.ts +25 -0
- package/src/mapping.ts +291 -0
- package/src/pg-executor.ts +68 -0
- package/src/pg-plans.ts +118 -0
- package/src/pg-registry.ts +290 -0
- package/src/plan.ts +466 -0
- package/src/sql-builder.ts +809 -0
- package/src/types.ts +63 -0
- package/src/uri.ts +301 -0
- package/test/client-bun.test.ts +8 -0
- package/test/client.bun.test.ts +46 -0
- package/test/execution.test.ts +277 -0
- package/test/mapping.test.ts +79 -0
- package/test/pg-registry.test.ts +103 -0
- package/test/sql-builder.test.ts +131 -0
- package/test/uri.test.ts +123 -0
- package/README.md +0 -3
package/src/execution.ts
ADDED
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
import type { GraphQLResolveInfo, GraphQLSchema } from 'graphql';
|
|
2
|
+
import { createDefaultExecutor, type DisposableExecutor } from '@graphql-mesh/transport-common';
|
|
3
|
+
import { getDefDirectives } from '@graphql-mesh/utils';
|
|
4
|
+
import { getDirective, MapperKind, mapSchema } from '@graphql-tools/utils';
|
|
5
|
+
import { DisposableSymbols } from '@whatwg-node/disposablestack';
|
|
6
|
+
import { inferAdapterFromLocation } from './adapter.js';
|
|
7
|
+
import { batchableEqualityColumn, loadBatchedByColumn, resolveEqualityKey } from './batch.js';
|
|
8
|
+
import { createSqlClient } from './client.js';
|
|
9
|
+
import { wireSqlEntityResolvers } from './entity.js';
|
|
10
|
+
import { resolveMappingValue } from './mapping.js';
|
|
11
|
+
import { hasJoinedField, planSelection } from './plan.js';
|
|
12
|
+
import {
|
|
13
|
+
buildDelete,
|
|
14
|
+
buildInsert,
|
|
15
|
+
buildSelect,
|
|
16
|
+
buildUpdate,
|
|
17
|
+
collectReturningColumns,
|
|
18
|
+
decodeCursor,
|
|
19
|
+
encodeCursor,
|
|
20
|
+
mapRowToGraphql,
|
|
21
|
+
mapRowToGraphqlWithJoins,
|
|
22
|
+
nestedSelection,
|
|
23
|
+
parseSqlDirectiveMappings,
|
|
24
|
+
type SqlPlanners,
|
|
25
|
+
} from './sql-builder.js';
|
|
26
|
+
import type {
|
|
27
|
+
ParsedSqlMapping,
|
|
28
|
+
SqlAdapter,
|
|
29
|
+
SqlClient,
|
|
30
|
+
SqlKind,
|
|
31
|
+
SqlMutationKind,
|
|
32
|
+
SqlQueryKind,
|
|
33
|
+
SqlTransportOptions,
|
|
34
|
+
} from './types.js';
|
|
35
|
+
import { resolveConnectionStringFromEnv } from './uri.js';
|
|
36
|
+
|
|
37
|
+
export interface GetSqlExecutorOpts {
|
|
38
|
+
subgraph: GraphQLSchema;
|
|
39
|
+
cwd?: string;
|
|
40
|
+
client?: SqlClient;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const MUTATION_KINDS = new Set<SqlKind>(['INSERT', 'UPDATE', 'DELETE']);
|
|
44
|
+
|
|
45
|
+
function connectionNodeSelection(selection: ParsedSqlMapping): ParsedSqlMapping {
|
|
46
|
+
const nodes = nestedSelection(selection, 'nodes');
|
|
47
|
+
if (nodes.entries.length) return nodes;
|
|
48
|
+
const edgeNode = nestedSelection(nestedSelection(selection, 'edges'), 'node');
|
|
49
|
+
if (edgeNode.entries.length) return edgeNode;
|
|
50
|
+
return selection;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function mapSelectRow(
|
|
54
|
+
row: Record<string, unknown>,
|
|
55
|
+
selection: ParsedSqlMapping,
|
|
56
|
+
joins: ReturnType<typeof planSelection>['joins'],
|
|
57
|
+
hasMany: ReturnType<typeof planSelection>['hasMany'] = [],
|
|
58
|
+
) {
|
|
59
|
+
return joins.length || hasMany.length
|
|
60
|
+
? mapRowToGraphqlWithJoins(row, selection, joins, hasMany)
|
|
61
|
+
: mapRowToGraphql(row, selection);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function rowWhereFullyUnresolved(
|
|
65
|
+
whereMapping: ParsedSqlMapping,
|
|
66
|
+
graphqlArgs: Record<string, any>,
|
|
67
|
+
parent: any,
|
|
68
|
+
): boolean {
|
|
69
|
+
let required = 0;
|
|
70
|
+
let resolved = 0;
|
|
71
|
+
for (const entry of whereMapping.entries) {
|
|
72
|
+
if (entry.children || entry.spread || entry.key.startsWith('$')) continue;
|
|
73
|
+
if (
|
|
74
|
+
entry.key === 'filter' ||
|
|
75
|
+
entry.key === 'where' ||
|
|
76
|
+
entry.key === 'orderBy' ||
|
|
77
|
+
entry.key === 'limit' ||
|
|
78
|
+
entry.key === 'offset' ||
|
|
79
|
+
entry.key === 'first' ||
|
|
80
|
+
entry.key === 'after' ||
|
|
81
|
+
entry.key === 'last' ||
|
|
82
|
+
entry.key === 'before'
|
|
83
|
+
) {
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
required++;
|
|
87
|
+
const val = resolveMappingValue(entry.value, { args: graphqlArgs, parent });
|
|
88
|
+
if (val !== undefined) resolved++;
|
|
89
|
+
}
|
|
90
|
+
return required > 0 && resolved === 0;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function runSqlSelect(
|
|
94
|
+
sqlClient: SqlClient,
|
|
95
|
+
opts: {
|
|
96
|
+
schema?: GraphQLSchema;
|
|
97
|
+
info?: GraphQLResolveInfo;
|
|
98
|
+
table: string;
|
|
99
|
+
kind: SqlQueryKind;
|
|
100
|
+
selection: ParsedSqlMapping;
|
|
101
|
+
whereMapping: ParsedSqlMapping;
|
|
102
|
+
planners?: SqlPlanners;
|
|
103
|
+
graphqlArgs: Record<string, any>;
|
|
104
|
+
parent: any;
|
|
105
|
+
context: any;
|
|
106
|
+
},
|
|
107
|
+
) {
|
|
108
|
+
const { table, kind, selection, whereMapping, planners, graphqlArgs, parent, context } = opts;
|
|
109
|
+
const fullSelection = kind === 'CONNECTION' ? connectionNodeSelection(selection) : selection;
|
|
110
|
+
|
|
111
|
+
// Never run a ROW with unresolved equality binders (would pick an arbitrary row).
|
|
112
|
+
if (kind === 'ROW' && rowWhereFullyUnresolved(whereMapping, graphqlArgs, parent)) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const planned =
|
|
117
|
+
opts.schema && opts.info && kind !== 'COUNT'
|
|
118
|
+
? planSelection(opts.schema, opts.info, fullSelection)
|
|
119
|
+
: { selection: fullSelection, joins: [], hasMany: [] };
|
|
120
|
+
|
|
121
|
+
const selectSelection = planned.selection.entries.length ? planned.selection : fullSelection;
|
|
122
|
+
const { joins, hasMany } = planned;
|
|
123
|
+
|
|
124
|
+
// Batch ROW / simple ROWS when there is exactly one equality binder (FK / PK)
|
|
125
|
+
// and we are not folding joins / has-many into this query.
|
|
126
|
+
if (!joins.length && !hasMany.length && (kind === 'ROW' || kind === 'ROWS')) {
|
|
127
|
+
const column = batchableEqualityColumn(whereMapping);
|
|
128
|
+
if (column) {
|
|
129
|
+
const key = resolveEqualityKey(whereMapping, column, graphqlArgs, parent);
|
|
130
|
+
if (key !== undefined) {
|
|
131
|
+
return loadBatchedByColumn(
|
|
132
|
+
context,
|
|
133
|
+
{
|
|
134
|
+
client: sqlClient,
|
|
135
|
+
table,
|
|
136
|
+
kind,
|
|
137
|
+
selection: selectSelection,
|
|
138
|
+
whereMapping,
|
|
139
|
+
planners,
|
|
140
|
+
column,
|
|
141
|
+
graphqlArgs,
|
|
142
|
+
},
|
|
143
|
+
key,
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const query = buildSelect(sqlClient.adapter, {
|
|
150
|
+
table,
|
|
151
|
+
kind,
|
|
152
|
+
selection: selectSelection,
|
|
153
|
+
whereMapping,
|
|
154
|
+
planners,
|
|
155
|
+
graphqlArgs,
|
|
156
|
+
parent,
|
|
157
|
+
joins,
|
|
158
|
+
hasMany,
|
|
159
|
+
});
|
|
160
|
+
const result = await sqlClient.query(query.sql, query.params);
|
|
161
|
+
|
|
162
|
+
if (kind === 'COUNT') {
|
|
163
|
+
const row = result.rows[0] as Record<string, unknown> | undefined;
|
|
164
|
+
const count = row?.count ?? row?.COUNT ?? Object.values(row || {})[0] ?? 0;
|
|
165
|
+
return Number(count);
|
|
166
|
+
}
|
|
167
|
+
if (kind === 'ROW') {
|
|
168
|
+
const row = result.rows[0] as Record<string, unknown> | undefined;
|
|
169
|
+
return row ? mapSelectRow(row, selectSelection, joins, hasMany) : null;
|
|
170
|
+
}
|
|
171
|
+
if (kind === 'CONNECTION') {
|
|
172
|
+
const first = Number(graphqlArgs.first ?? 10);
|
|
173
|
+
const after = typeof graphqlArgs.after === 'string' ? graphqlArgs.after : '';
|
|
174
|
+
const offset = after ? decodeCursor(after) : 0;
|
|
175
|
+
const rows = result.rows as Record<string, unknown>[];
|
|
176
|
+
const hasNextPage = rows.length > first;
|
|
177
|
+
const pageRows = hasNextPage ? rows.slice(0, first) : rows;
|
|
178
|
+
const nodes = pageRows.map(r => mapSelectRow(r, selectSelection, joins, hasMany));
|
|
179
|
+
const edges = nodes.map((node, i) => ({
|
|
180
|
+
cursor: encodeCursor(offset + i),
|
|
181
|
+
node,
|
|
182
|
+
}));
|
|
183
|
+
const endCursor = edges.length ? edges[edges.length - 1].cursor : null;
|
|
184
|
+
const startCursor = edges.length ? edges[0].cursor : null;
|
|
185
|
+
|
|
186
|
+
let totalCount = offset + pageRows.length + (hasNextPage ? 1 : 0);
|
|
187
|
+
if (selection.entries.some(e => e.key === 'totalCount')) {
|
|
188
|
+
const countQ = buildSelect(sqlClient.adapter, {
|
|
189
|
+
table,
|
|
190
|
+
kind: 'COUNT',
|
|
191
|
+
selection: selectSelection,
|
|
192
|
+
whereMapping,
|
|
193
|
+
planners,
|
|
194
|
+
graphqlArgs,
|
|
195
|
+
parent,
|
|
196
|
+
});
|
|
197
|
+
const countRes = await sqlClient.query(countQ.sql, countQ.params);
|
|
198
|
+
const crow = countRes.rows[0] as Record<string, unknown> | undefined;
|
|
199
|
+
totalCount = Number(crow?.count ?? crow?.COUNT ?? Object.values(crow || {})[0] ?? 0);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return {
|
|
203
|
+
nodes,
|
|
204
|
+
edges,
|
|
205
|
+
pageInfo: {
|
|
206
|
+
hasNextPage,
|
|
207
|
+
hasPreviousPage: offset > 0,
|
|
208
|
+
startCursor,
|
|
209
|
+
endCursor,
|
|
210
|
+
},
|
|
211
|
+
totalCount,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
return (result.rows as Record<string, unknown>[]).map(r =>
|
|
215
|
+
mapSelectRow(r, selectSelection, joins, hasMany),
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export async function getSqlExecutor({
|
|
220
|
+
subgraph,
|
|
221
|
+
cwd,
|
|
222
|
+
client,
|
|
223
|
+
}: GetSqlExecutorOpts): Promise<DisposableExecutor> {
|
|
224
|
+
const transportDirectives = getDirective(subgraph, subgraph, 'transport');
|
|
225
|
+
if (!transportDirectives?.length) {
|
|
226
|
+
throw new Error('No @transport directive found on SQL subgraph schema');
|
|
227
|
+
}
|
|
228
|
+
const transport = transportDirectives[0] as {
|
|
229
|
+
location?: string;
|
|
230
|
+
options?: SqlTransportOptions;
|
|
231
|
+
};
|
|
232
|
+
const location =
|
|
233
|
+
transport.location ||
|
|
234
|
+
resolveConnectionStringFromEnv(undefined, { adapter: transport.options?.adapter });
|
|
235
|
+
const adapter: SqlAdapter =
|
|
236
|
+
transport.options?.adapter || inferAdapterFromLocation(location) || 'sqlite';
|
|
237
|
+
if (!location) {
|
|
238
|
+
throw new Error(
|
|
239
|
+
'@transport(kind: "sql") requires location (connection URI / file path) or a Bun-compatible env URL (DATABASE_URL, POSTGRES_URL, MYSQL_URL, SQLITE_URL, …)',
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const sqlClient =
|
|
244
|
+
client ||
|
|
245
|
+
createSqlClient({
|
|
246
|
+
adapter,
|
|
247
|
+
location,
|
|
248
|
+
cwd,
|
|
249
|
+
max: transport.options?.max,
|
|
250
|
+
idleTimeout: transport.options?.idleTimeout,
|
|
251
|
+
connectionTimeout: transport.options?.connectionTimeout,
|
|
252
|
+
tls: transport.options?.tls,
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
subgraph = mapSchema(subgraph, {
|
|
256
|
+
[MapperKind.OBJECT_FIELD](fieldConfig) {
|
|
257
|
+
const directives = getDefDirectives(subgraph, fieldConfig);
|
|
258
|
+
for (const directive of directives) {
|
|
259
|
+
switch (directive.name) {
|
|
260
|
+
case 'sql': {
|
|
261
|
+
const table = directive.args.table as string;
|
|
262
|
+
const kind = (directive.args.kind || 'ROWS') as SqlKind;
|
|
263
|
+
const { whereMapping, setMapping, selection, planners } = parseSqlDirectiveMappings({
|
|
264
|
+
where: directive.args.where as string | undefined,
|
|
265
|
+
set: directive.args.set as string | undefined,
|
|
266
|
+
selection: directive.args.selection as string | undefined,
|
|
267
|
+
// Legacy SDL may still carry a separate filter binder.
|
|
268
|
+
filter: directive.args.filter as string | undefined,
|
|
269
|
+
orderBy: directive.args.orderBy as string | undefined,
|
|
270
|
+
limit: directive.args.limit as string | undefined,
|
|
271
|
+
offset: directive.args.offset as string | undefined,
|
|
272
|
+
first: directive.args.first as string | undefined,
|
|
273
|
+
after: directive.args.after as string | undefined,
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
if (MUTATION_KINDS.has(kind)) {
|
|
277
|
+
const mutationKind = kind as SqlMutationKind;
|
|
278
|
+
fieldConfig.resolve = async function sqlMutationResolver(_root, args) {
|
|
279
|
+
if (mutationKind === 'DELETE') {
|
|
280
|
+
const query = buildDelete(sqlClient.adapter, table, whereMapping, args);
|
|
281
|
+
const result = await sqlClient.query(query.sql, query.params);
|
|
282
|
+
const affected = result.rowCount;
|
|
283
|
+
return {
|
|
284
|
+
ok: affected > 0,
|
|
285
|
+
affectedRows: affected,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const returningCols = collectReturningColumns(selection);
|
|
290
|
+
const query =
|
|
291
|
+
mutationKind === 'INSERT'
|
|
292
|
+
? buildInsert(sqlClient.adapter, table, setMapping, args, returningCols)
|
|
293
|
+
: buildUpdate(
|
|
294
|
+
sqlClient.adapter,
|
|
295
|
+
table,
|
|
296
|
+
setMapping,
|
|
297
|
+
whereMapping,
|
|
298
|
+
args,
|
|
299
|
+
returningCols,
|
|
300
|
+
);
|
|
301
|
+
|
|
302
|
+
let row: Record<string, unknown> | undefined;
|
|
303
|
+
if (returningCols.length && (adapter === 'sqlite' || adapter === 'postgres')) {
|
|
304
|
+
const result = await sqlClient.query(query.sql, query.params);
|
|
305
|
+
row = result.rows[0] as Record<string, unknown> | undefined;
|
|
306
|
+
} else {
|
|
307
|
+
await sqlClient.query(query.sql, query.params);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const payloadKey = selection.entries.find(e => e.children)?.key;
|
|
311
|
+
if (payloadKey) {
|
|
312
|
+
const nested = nestedSelection(selection, payloadKey);
|
|
313
|
+
return {
|
|
314
|
+
[payloadKey]: row ? mapRowToGraphql(row, nested) : null,
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
return row ? mapRowToGraphql(row, selection) : null;
|
|
318
|
+
};
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const queryKind = kind as SqlQueryKind;
|
|
323
|
+
fieldConfig.resolve = async function sqlQueryResolver(
|
|
324
|
+
root,
|
|
325
|
+
args,
|
|
326
|
+
context,
|
|
327
|
+
info: GraphQLResolveInfo,
|
|
328
|
+
) {
|
|
329
|
+
// Parent already folded this belongs-to via JOIN lookahead.
|
|
330
|
+
if (hasJoinedField(root, info.fieldName)) {
|
|
331
|
+
return root[info.fieldName];
|
|
332
|
+
}
|
|
333
|
+
return runSqlSelect(sqlClient, {
|
|
334
|
+
schema: subgraph,
|
|
335
|
+
info,
|
|
336
|
+
table,
|
|
337
|
+
kind: queryKind,
|
|
338
|
+
selection,
|
|
339
|
+
whereMapping,
|
|
340
|
+
planners,
|
|
341
|
+
graphqlArgs: args,
|
|
342
|
+
parent: root,
|
|
343
|
+
context,
|
|
344
|
+
});
|
|
345
|
+
};
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
return fieldConfig;
|
|
351
|
+
},
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
// Type-level `@sql` → resolveReference + Query._entities (Connectors-style).
|
|
355
|
+
subgraph = wireSqlEntityResolvers(subgraph, {
|
|
356
|
+
executeTypeLevelRow(sqlArgs, reference, context) {
|
|
357
|
+
const table = sqlArgs.table as string;
|
|
358
|
+
const { whereMapping, selection, planners } = parseSqlDirectiveMappings({
|
|
359
|
+
where: sqlArgs.where as string | undefined,
|
|
360
|
+
selection: sqlArgs.selection as string | undefined,
|
|
361
|
+
filter: sqlArgs.filter as string | undefined,
|
|
362
|
+
orderBy: sqlArgs.orderBy as string | undefined,
|
|
363
|
+
limit: sqlArgs.limit as string | undefined,
|
|
364
|
+
offset: sqlArgs.offset as string | undefined,
|
|
365
|
+
first: sqlArgs.first as string | undefined,
|
|
366
|
+
after: sqlArgs.after as string | undefined,
|
|
367
|
+
});
|
|
368
|
+
return runSqlSelect(sqlClient, {
|
|
369
|
+
table,
|
|
370
|
+
kind: 'ROW',
|
|
371
|
+
selection,
|
|
372
|
+
whereMapping,
|
|
373
|
+
planners,
|
|
374
|
+
graphqlArgs: {},
|
|
375
|
+
parent: reference,
|
|
376
|
+
context,
|
|
377
|
+
});
|
|
378
|
+
},
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
// Postgres + Node pg.Pool → Grafast / `@dataplan/pg` fast path.
|
|
382
|
+
// MySQL / SQLite / Bun.SQL keep the GraphQL.js + JOIN planner path.
|
|
383
|
+
// Dynamic import keeps grafast off the static graph (Jest --detectLeaks).
|
|
384
|
+
if (adapter === 'postgres' && sqlClient.pgPool) {
|
|
385
|
+
const { createPostgresGrafastExecutor } = await import('./pg-executor.js');
|
|
386
|
+
const grafastExecutor = createPostgresGrafastExecutor({
|
|
387
|
+
subgraph,
|
|
388
|
+
sqlClient,
|
|
389
|
+
disposeClient: () => sqlClient.dispose(),
|
|
390
|
+
});
|
|
391
|
+
if (grafastExecutor) {
|
|
392
|
+
return grafastExecutor;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const defaultExecutor = createDefaultExecutor(subgraph);
|
|
397
|
+
const executor: DisposableExecutor = async function sqlExecutor(executionRequest) {
|
|
398
|
+
// Ensure a shared mutable context so DataLoaders coalesce within one operation.
|
|
399
|
+
const context =
|
|
400
|
+
executionRequest.context && typeof executionRequest.context === 'object'
|
|
401
|
+
? executionRequest.context
|
|
402
|
+
: {};
|
|
403
|
+
return defaultExecutor({
|
|
404
|
+
...executionRequest,
|
|
405
|
+
context,
|
|
406
|
+
});
|
|
407
|
+
};
|
|
408
|
+
executor[DisposableSymbols.dispose] = () => sqlClient.dispose();
|
|
409
|
+
executor[DisposableSymbols.asyncDispose] = async () => sqlClient.dispose();
|
|
410
|
+
return executor;
|
|
411
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Transport } from '@graphql-mesh/transport-common';
|
|
2
|
+
import { getSqlExecutor } from './execution.js';
|
|
3
|
+
import type { SqlTransportOptions } from './types.js';
|
|
4
|
+
|
|
5
|
+
export * from './adapter.js';
|
|
6
|
+
export * from './batch.js';
|
|
7
|
+
export * from './client.js';
|
|
8
|
+
export * from './entity.js';
|
|
9
|
+
export * from './execution.js';
|
|
10
|
+
export * from './mapping.js';
|
|
11
|
+
export * from './plan.js';
|
|
12
|
+
export * from './sql-builder.js';
|
|
13
|
+
export * from './uri.js';
|
|
14
|
+
export type * from './types.js';
|
|
15
|
+
// pg-executor / pg-registry / pg-plans stay internal: they pull grafast +
|
|
16
|
+
// @dataplan/pg, which register process-lifetime state that fails Jest --detectLeaks.
|
|
17
|
+
|
|
18
|
+
export default {
|
|
19
|
+
getSubgraphExecutor({ subgraph, cwd }) {
|
|
20
|
+
return getSqlExecutor({
|
|
21
|
+
subgraph,
|
|
22
|
+
cwd,
|
|
23
|
+
});
|
|
24
|
+
},
|
|
25
|
+
} satisfies Transport<SqlTransportOptions>;
|