@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.
Files changed (100) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/dist/cjs/adapter.js +60 -0
  3. package/dist/cjs/batch.js +113 -0
  4. package/dist/cjs/client.js +205 -0
  5. package/dist/cjs/client.js.map +7 -0
  6. package/dist/cjs/dialect.js +60 -0
  7. package/dist/cjs/dialect.js.map +7 -0
  8. package/dist/cjs/entity.js +186 -0
  9. package/dist/cjs/execution.js +307 -0
  10. package/dist/cjs/execution.js.map +7 -0
  11. package/dist/cjs/index.js +23 -0
  12. package/dist/cjs/index.js.map +7 -0
  13. package/dist/cjs/mapping.js +260 -0
  14. package/dist/cjs/mapping.js.map +7 -0
  15. package/dist/cjs/package.json +1 -0
  16. package/dist/cjs/pg-executor.js +53 -0
  17. package/dist/cjs/pg-plans.js +103 -0
  18. package/dist/cjs/pg-registry.js +239 -0
  19. package/dist/cjs/plan.js +359 -0
  20. package/dist/cjs/sql-builder.js +653 -0
  21. package/dist/cjs/sql-builder.js.map +7 -0
  22. package/dist/cjs/types.js +0 -0
  23. package/dist/cjs/types.js.map +7 -0
  24. package/dist/cjs/uri.js +267 -0
  25. package/dist/esm/adapter.js +54 -0
  26. package/dist/esm/batch.js +107 -0
  27. package/dist/esm/client.js +197 -0
  28. package/dist/esm/client.js.map +7 -0
  29. package/dist/esm/dialect.js +54 -0
  30. package/dist/esm/dialect.js.map +7 -0
  31. package/dist/esm/entity.js +180 -0
  32. package/dist/esm/execution.js +303 -0
  33. package/dist/esm/execution.js.map +7 -0
  34. package/dist/esm/index.js +20 -0
  35. package/dist/esm/index.js.map +7 -0
  36. package/dist/esm/mapping.js +255 -0
  37. package/dist/esm/mapping.js.map +7 -0
  38. package/dist/esm/pg-executor.js +49 -0
  39. package/dist/esm/pg-plans.js +100 -0
  40. package/dist/esm/pg-registry.js +236 -0
  41. package/dist/esm/plan.js +351 -0
  42. package/dist/esm/sql-builder.js +636 -0
  43. package/dist/esm/sql-builder.js.map +7 -0
  44. package/dist/esm/types.js +0 -0
  45. package/dist/esm/types.js.map +7 -0
  46. package/dist/esm/uri.js +261 -0
  47. package/dist/package.json +57 -0
  48. package/dist/typings/adapter.d.cts +9 -0
  49. package/dist/typings/adapter.d.ts +9 -0
  50. package/dist/typings/batch.d.cts +21 -0
  51. package/dist/typings/batch.d.ts +21 -0
  52. package/dist/typings/client.d.cts +36 -0
  53. package/dist/typings/client.d.ts +36 -0
  54. package/dist/typings/dialect.d.cts +9 -0
  55. package/dist/typings/dialect.d.ts +9 -0
  56. package/dist/typings/entity.d.cts +24 -0
  57. package/dist/typings/entity.d.ts +24 -0
  58. package/dist/typings/execution.d.cts +9 -0
  59. package/dist/typings/execution.d.ts +9 -0
  60. package/dist/typings/index.d.cts +15 -0
  61. package/dist/typings/index.d.ts +15 -0
  62. package/dist/typings/mapping.d.cts +13 -0
  63. package/dist/typings/mapping.d.ts +13 -0
  64. package/dist/typings/pg-executor.d.cts +19 -0
  65. package/dist/typings/pg-executor.d.ts +19 -0
  66. package/dist/typings/pg-plans.d.cts +12 -0
  67. package/dist/typings/pg-plans.d.ts +12 -0
  68. package/dist/typings/pg-registry.d.cts +16 -0
  69. package/dist/typings/pg-registry.d.ts +16 -0
  70. package/dist/typings/plan.d.cts +47 -0
  71. package/dist/typings/plan.d.ts +47 -0
  72. package/dist/typings/sql-builder.d.cts +75 -0
  73. package/dist/typings/sql-builder.d.ts +75 -0
  74. package/dist/typings/types.d.cts +50 -0
  75. package/dist/typings/types.d.ts +50 -0
  76. package/dist/typings/uri.d.cts +42 -0
  77. package/dist/typings/uri.d.ts +42 -0
  78. package/package.json +67 -4
  79. package/src/adapter.ts +56 -0
  80. package/src/batch.ts +150 -0
  81. package/src/client.ts +233 -0
  82. package/src/entity.ts +235 -0
  83. package/src/execution.ts +411 -0
  84. package/src/index.ts +25 -0
  85. package/src/mapping.ts +291 -0
  86. package/src/pg-executor.ts +68 -0
  87. package/src/pg-plans.ts +118 -0
  88. package/src/pg-registry.ts +290 -0
  89. package/src/plan.ts +466 -0
  90. package/src/sql-builder.ts +809 -0
  91. package/src/types.ts +63 -0
  92. package/src/uri.ts +301 -0
  93. package/test/client-bun.test.ts +8 -0
  94. package/test/client.bun.test.ts +46 -0
  95. package/test/execution.test.ts +277 -0
  96. package/test/mapping.test.ts +79 -0
  97. package/test/pg-registry.test.ts +103 -0
  98. package/test/sql-builder.test.ts +131 -0
  99. package/test/uri.test.ts +123 -0
  100. package/README.md +0 -3
@@ -0,0 +1,290 @@
1
+ import { context, object, type Step } from 'grafast';
2
+ import {
3
+ getNamedType,
4
+ isNonNullType,
5
+ isObjectType,
6
+ isScalarType,
7
+ type GraphQLObjectType,
8
+ type GraphQLOutputType,
9
+ type GraphQLSchema,
10
+ } from 'graphql';
11
+ import { sql } from 'pg-sql2';
12
+ import {
13
+ makeRegistry,
14
+ makeRegistryBuilder,
15
+ PgExecutor,
16
+ pgResourceOptions,
17
+ recordCodec,
18
+ TYPES,
19
+ type PgCodec,
20
+ type PgCodecAttribute,
21
+ } from '@dataplan/pg';
22
+ import { getDefDirectives } from '@graphql-mesh/utils';
23
+ import { parseSqlMapping } from './mapping.js';
24
+ import type { ParsedSqlMapping, SqlKind } from './types.js';
25
+
26
+ export type MeshPgRegistry = ReturnType<typeof makeRegistry>;
27
+
28
+ interface TableBuildInfo {
29
+ table: string;
30
+ /** GraphQL field name → SQL column */
31
+ columns: Map<string, { sqlCol: string; notNull: boolean; codec: PgCodec }>;
32
+ /** GraphQL object type names that represent this table's rows */
33
+ typeNames: Set<string>;
34
+ }
35
+
36
+ /**
37
+ * Build a `@dataplan/pg` registry from `@sql` directives on the subgraph schema.
38
+ * Attribute names are GraphQL field names; SQL columns are selected via expression.
39
+ */
40
+ export function buildPgRegistryFromSqlSchema(schema: GraphQLSchema): {
41
+ registry: MeshPgRegistry;
42
+ executor: PgExecutor;
43
+ tableByType: Map<string, string>;
44
+ relationNames: Map<string, Map<string, { remoteTable: string; kind: 'ROW' | 'ROWS' }>>;
45
+ } {
46
+ const executor = new PgExecutor({
47
+ name: 'mesh-sql',
48
+ context: () => {
49
+ const $ctx = context();
50
+ return object({
51
+ pgSettings: $ctx.get('pgSettings'),
52
+ withPgClient: $ctx.get('withPgClient'),
53
+ }) as Step<any>;
54
+ },
55
+ });
56
+
57
+ const tables = new Map<string, TableBuildInfo>();
58
+ const tableByType = new Map<string, string>();
59
+ const relationNames = new Map<
60
+ string,
61
+ Map<string, { remoteTable: string; kind: 'ROW' | 'ROWS' }>
62
+ >();
63
+
64
+ const ensureTable = (table: string): TableBuildInfo => {
65
+ let info = tables.get(table);
66
+ if (!info) {
67
+ info = { table, columns: new Map(), typeNames: new Set() };
68
+ tables.set(table, info);
69
+ }
70
+ return info;
71
+ };
72
+
73
+ const absorbSelection = (
74
+ info: TableBuildInfo,
75
+ selection: ParsedSqlMapping,
76
+ objectType: GraphQLObjectType | null,
77
+ ) => {
78
+ for (const entry of selection.entries) {
79
+ if (!entry.value || entry.value.kind !== 'column') continue;
80
+ const sqlCol = entry.value.value;
81
+ const field = objectType?.getFields()[entry.key];
82
+ const codec = codecFromGraphqlType(field?.type);
83
+ const notNull = field ? isNonNullType(field.type as any) : false;
84
+ const prev = info.columns.get(entry.key);
85
+ if (!prev) {
86
+ info.columns.set(entry.key, { sqlCol, notNull, codec });
87
+ } else if (notNull && !prev.notNull) {
88
+ info.columns.set(entry.key, { sqlCol, notNull: true, codec: prev.codec });
89
+ }
90
+ }
91
+ };
92
+
93
+ for (const type of Object.values(schema.getTypeMap())) {
94
+ if (!isObjectType(type) || type.name.startsWith('__')) continue;
95
+
96
+ // Type-level @sql
97
+ for (const dir of getDefDirectives(schema, type).filter(d => d.name === 'sql')) {
98
+ const table = String(dir.args.table || '');
99
+ if (!table) continue;
100
+ const info = ensureTable(table);
101
+ info.typeNames.add(type.name);
102
+ tableByType.set(type.name, table);
103
+ absorbSelection(info, parseSqlMapping(dir.args.selection as string | undefined), type);
104
+ }
105
+
106
+ for (const field of Object.values(type.getFields())) {
107
+ for (const dir of getDefDirectives(schema, field).filter(d => d.name === 'sql')) {
108
+ const table = String(dir.args.table || '');
109
+ if (!table) continue;
110
+ const kind = (dir.args.kind || 'ROWS') as SqlKind;
111
+ const info = ensureTable(table);
112
+ const returnType = unwrapObjectType(field.type);
113
+ if (returnType && (kind === 'ROW' || kind === 'ROWS' || kind === 'CONNECTION')) {
114
+ // CONNECTION returns Connection type — dig nodes
115
+ const nodeType =
116
+ kind === 'CONNECTION' ? unwrapConnectionNodeType(returnType) : returnType;
117
+ if (nodeType) {
118
+ info.typeNames.add(nodeType.name);
119
+ tableByType.set(nodeType.name, table);
120
+ absorbSelection(
121
+ info,
122
+ parseSqlMapping(dir.args.selection as string | undefined),
123
+ nodeType,
124
+ );
125
+ }
126
+ } else {
127
+ absorbSelection(
128
+ info,
129
+ parseSqlMapping(dir.args.selection as string | undefined),
130
+ returnType,
131
+ );
132
+ }
133
+
134
+ // Record relations for plan wiring
135
+ if (
136
+ (kind === 'ROW' || kind === 'ROWS') &&
137
+ type.name !== 'Query' &&
138
+ type.name !== 'Mutation'
139
+ ) {
140
+ let rels = relationNames.get(type.name);
141
+ if (!rels) {
142
+ rels = new Map();
143
+ relationNames.set(type.name, rels);
144
+ }
145
+ rels.set(field.name, { remoteTable: table, kind });
146
+ }
147
+ }
148
+ }
149
+ }
150
+
151
+ // Ensure at least id columns exist
152
+ for (const info of tables.values()) {
153
+ if (!info.columns.has('id')) {
154
+ info.columns.set('id', {
155
+ sqlCol: 'id',
156
+ notNull: true,
157
+ codec: TYPES.int,
158
+ });
159
+ }
160
+ }
161
+
162
+ const builder = makeRegistryBuilder();
163
+ const resourceOptionsByTable = new Map<string, ReturnType<typeof pgResourceOptions>>();
164
+ const codecByTable = new Map<string, ReturnType<typeof recordCodec>>();
165
+
166
+ for (const info of tables.values()) {
167
+ const attributes: Record<string, PgCodecAttribute> = {};
168
+ for (const [gqlField, col] of info.columns) {
169
+ const sqlCol = col.sqlCol;
170
+ attributes[gqlField] = {
171
+ codec: col.codec,
172
+ notNull: col.notNull,
173
+ expression: alias => sql`${alias}.${sql.identifier(sqlCol)}`,
174
+ };
175
+ }
176
+
177
+ const codec = recordCodec({
178
+ executor,
179
+ name: `${info.table}_codec`,
180
+ identifier: sql`${sql.identifier(info.table)}`,
181
+ attributes,
182
+ });
183
+ codecByTable.set(info.table, codec);
184
+ builder.addCodec(codec);
185
+
186
+ const pkAttrs = info.columns.has('id') ? ['id'] : [info.columns.keys().next().value!];
187
+ const resourceOpts = pgResourceOptions({
188
+ executor,
189
+ codec,
190
+ from: sql`${sql.identifier(info.table)}`,
191
+ name: info.table,
192
+ uniques: [{ attributes: pkAttrs, isPrimary: true }],
193
+ });
194
+ resourceOptionsByTable.set(info.table, resourceOpts);
195
+ builder.addResource(resourceOpts);
196
+ }
197
+
198
+ // Relations from @sql $this binders
199
+ for (const type of Object.values(schema.getTypeMap())) {
200
+ if (!isObjectType(type) || type.name.startsWith('__')) continue;
201
+ const localTable = tableByType.get(type.name);
202
+ if (!localTable) continue;
203
+ const localCodec = codecByTable.get(localTable);
204
+ if (!localCodec) continue;
205
+
206
+ for (const field of Object.values(type.getFields())) {
207
+ for (const dir of getDefDirectives(schema, field).filter(d => d.name === 'sql')) {
208
+ const kind = (dir.args.kind || 'ROWS') as SqlKind;
209
+ if (kind !== 'ROW' && kind !== 'ROWS') continue;
210
+ const remoteTable = String(dir.args.table || '');
211
+ const remoteOpts = resourceOptionsByTable.get(remoteTable);
212
+ if (!remoteOpts) continue;
213
+
214
+ const where = parseSqlMapping(dir.args.where as string | undefined);
215
+ let localAttr: string | undefined;
216
+ let remoteAttr: string | undefined;
217
+ for (const entry of where.entries) {
218
+ if (entry.children) continue;
219
+ if (!entry.value || entry.value.kind !== 'this') continue;
220
+ remoteAttr = entry.key; // SQL column on remote — need GraphQL attr on remote codec
221
+ localAttr = entry.value.path?.[0] || entry.value.value;
222
+ break;
223
+ }
224
+ if (!localAttr || !remoteAttr) continue;
225
+
226
+ // Map remote SQL column → GraphQL attribute name on remote table
227
+ const remoteInfo = tables.get(remoteTable);
228
+ let remoteGqlAttr = remoteAttr;
229
+ if (remoteInfo) {
230
+ for (const [gql, col] of remoteInfo.columns) {
231
+ if (col.sqlCol === remoteAttr || gql === remoteAttr) {
232
+ remoteGqlAttr = gql;
233
+ break;
234
+ }
235
+ }
236
+ }
237
+
238
+ const relationName = field.name;
239
+ try {
240
+ builder.addRelation(localCodec, relationName, remoteOpts, {
241
+ localAttributes: [localAttr],
242
+ remoteAttributes: [remoteGqlAttr],
243
+ isUnique: kind === 'ROW',
244
+ isReferencee: kind === 'ROWS',
245
+ });
246
+ } catch {
247
+ // Duplicate / incompatible relations — skip
248
+ }
249
+ }
250
+ }
251
+ }
252
+
253
+ const registry = makeRegistry(builder.getRegistryConfig());
254
+ return { registry, executor, tableByType, relationNames };
255
+ }
256
+
257
+ function unwrapObjectType(type: GraphQLOutputType): GraphQLObjectType | null {
258
+ const named = getNamedType(type);
259
+ return isObjectType(named) ? named : null;
260
+ }
261
+
262
+ function unwrapConnectionNodeType(connectionType: GraphQLObjectType): GraphQLObjectType | null {
263
+ const nodes = connectionType.getFields().nodes;
264
+ if (nodes) return unwrapObjectType(nodes.type);
265
+ const edges = connectionType.getFields().edges;
266
+ if (edges) {
267
+ const edgeType = unwrapObjectType(edges.type);
268
+ const node = edgeType?.getFields().node;
269
+ if (node) return unwrapObjectType(node.type);
270
+ }
271
+ return null;
272
+ }
273
+
274
+ function codecFromGraphqlType(type: GraphQLOutputType | undefined): PgCodec {
275
+ if (!type) return TYPES.text;
276
+ const named = getNamedType(type);
277
+ if (!isScalarType(named)) return TYPES.json;
278
+ switch (named.name) {
279
+ case 'Int':
280
+ return TYPES.int;
281
+ case 'Float':
282
+ return TYPES.float;
283
+ case 'Boolean':
284
+ return TYPES.boolean;
285
+ case 'ID':
286
+ return TYPES.text;
287
+ default:
288
+ return TYPES.text;
289
+ }
290
+ }