@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
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { createPool } from 'mysql2/promise';
|
|
2
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
3
|
+
import pg from 'pg';
|
|
4
|
+
import { resolveSqlitePath } from './adapter.js';
|
|
5
|
+
import { parseSqlUri } from './uri.js';
|
|
6
|
+
function getBunSqlCtor() {
|
|
7
|
+
const bun = globalThis.Bun;
|
|
8
|
+
return typeof bun?.SQL === 'function' ? bun.SQL : undefined;
|
|
9
|
+
}
|
|
10
|
+
export function isBunSqlAvailable() {
|
|
11
|
+
return getBunSqlCtor() != null;
|
|
12
|
+
}
|
|
13
|
+
export function createSqlClient(opts) {
|
|
14
|
+
if (!opts.preferNode && isBunSqlAvailable()) {
|
|
15
|
+
return createBunSqlClient(opts);
|
|
16
|
+
}
|
|
17
|
+
switch (opts.adapter) {
|
|
18
|
+
case 'sqlite':
|
|
19
|
+
return createSqliteClient(opts.location, opts.cwd);
|
|
20
|
+
case 'mysql':
|
|
21
|
+
return createMysqlClient(opts);
|
|
22
|
+
case 'postgres':
|
|
23
|
+
return createPostgresClient(opts);
|
|
24
|
+
default:
|
|
25
|
+
throw new Error(`Unknown SQL adapter: ${opts.adapter}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Wrap Bun's native `SQL` client when running under Bun.
|
|
30
|
+
* Uses `sql.unsafe(query, params)` so our adapter placeholders (`$1` / `?`) work as-is.
|
|
31
|
+
*/
|
|
32
|
+
export function createBunSqlClient(opts) {
|
|
33
|
+
const BunSQL = getBunSqlCtor();
|
|
34
|
+
if (!BunSQL) {
|
|
35
|
+
throw new Error('Bun.SQL is not available in this runtime');
|
|
36
|
+
}
|
|
37
|
+
const bunOpts = {
|
|
38
|
+
adapter: opts.adapter === 'mysql' ? 'mysql' : opts.adapter,
|
|
39
|
+
};
|
|
40
|
+
if (opts.max != null)
|
|
41
|
+
bunOpts.max = opts.max;
|
|
42
|
+
if (opts.idleTimeout != null)
|
|
43
|
+
bunOpts.idleTimeout = opts.idleTimeout;
|
|
44
|
+
if (opts.connectionTimeout != null)
|
|
45
|
+
bunOpts.connectionTimeout = opts.connectionTimeout;
|
|
46
|
+
if (opts.tls != null)
|
|
47
|
+
bunOpts.tls = opts.tls === 'true' ? true : opts.tls;
|
|
48
|
+
const sql = new BunSQL(opts.location, bunOpts);
|
|
49
|
+
return {
|
|
50
|
+
adapter: opts.adapter,
|
|
51
|
+
async query(query, params = []) {
|
|
52
|
+
const result = await sql.unsafe(query, params);
|
|
53
|
+
const rows = normalizeBunRows(result);
|
|
54
|
+
const rowCount = typeof result?.count === 'number'
|
|
55
|
+
? result.count
|
|
56
|
+
: typeof result?.affectedRows === 'number'
|
|
57
|
+
? result.affectedRows
|
|
58
|
+
: rows.length;
|
|
59
|
+
return { rows, rowCount };
|
|
60
|
+
},
|
|
61
|
+
async dispose() {
|
|
62
|
+
if (typeof sql.close === 'function') {
|
|
63
|
+
await sql.close();
|
|
64
|
+
}
|
|
65
|
+
else if (typeof sql.end === 'function') {
|
|
66
|
+
await sql.end();
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function normalizeBunRows(result) {
|
|
72
|
+
if (result == null)
|
|
73
|
+
return [];
|
|
74
|
+
// Copy into a plain array — Bun results are array-like and may carry extra props (`count`).
|
|
75
|
+
if (typeof result.length === 'number') {
|
|
76
|
+
return Array.from({ length: result.length }, (_, i) => result[i]);
|
|
77
|
+
}
|
|
78
|
+
if (Array.isArray(result)) {
|
|
79
|
+
return [...result];
|
|
80
|
+
}
|
|
81
|
+
return [];
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Wrap an existing `pg.Pool` (or compatible) as SqlClient.
|
|
85
|
+
*/
|
|
86
|
+
export function wrapPgPoolAsSqlClient(pool) {
|
|
87
|
+
const client = {
|
|
88
|
+
adapter: 'postgres',
|
|
89
|
+
// Expose real pg.Pool when present for `@dataplan/pg`.
|
|
90
|
+
pgPool: 'connect' in pool ? pool : undefined,
|
|
91
|
+
async query(sql, params = []) {
|
|
92
|
+
const result = await pool.query(sql, params);
|
|
93
|
+
return {
|
|
94
|
+
rows: (result.rows || []),
|
|
95
|
+
rowCount: Number(result.rowCount ?? result.rows?.length ?? 0),
|
|
96
|
+
};
|
|
97
|
+
},
|
|
98
|
+
async dispose() {
|
|
99
|
+
if (typeof pool.end === 'function') {
|
|
100
|
+
await pool.end();
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
return client;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Wrap an existing `mysql` / `mysql2` pool (legacy handler `pool` config) as SqlClient.
|
|
108
|
+
*/
|
|
109
|
+
export function wrapMysqlPoolAsSqlClient(pool) {
|
|
110
|
+
// mysql (non-promise) exposes .promise(); mysql2/promise is already thenable
|
|
111
|
+
const p = typeof pool.promise === 'function' ? pool.promise() : pool;
|
|
112
|
+
return {
|
|
113
|
+
adapter: 'mysql',
|
|
114
|
+
async query(sql, params = []) {
|
|
115
|
+
const [result] = await p.query(sql, params);
|
|
116
|
+
if (Array.isArray(result)) {
|
|
117
|
+
return { rows: result, rowCount: result.length };
|
|
118
|
+
}
|
|
119
|
+
const header = result;
|
|
120
|
+
return { rows: [], rowCount: Number(header.affectedRows ?? 0) };
|
|
121
|
+
},
|
|
122
|
+
async dispose() {
|
|
123
|
+
if (typeof p.end === 'function') {
|
|
124
|
+
await p.end();
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
function createSqliteClient(location, cwd) {
|
|
130
|
+
const dbPath = resolveSqlitePath(location, cwd);
|
|
131
|
+
const db = new DatabaseSync(dbPath);
|
|
132
|
+
return {
|
|
133
|
+
adapter: 'sqlite',
|
|
134
|
+
async query(sql, params = []) {
|
|
135
|
+
const stmt = db.prepare(sql);
|
|
136
|
+
const bind = params;
|
|
137
|
+
const returnsRows = /^\s*(SELECT|PRAGMA|WITH)\b/i.test(sql) || /\bRETURNING\b/i.test(sql);
|
|
138
|
+
if (returnsRows) {
|
|
139
|
+
const rows = stmt.all(...bind);
|
|
140
|
+
return { rows, rowCount: rows.length };
|
|
141
|
+
}
|
|
142
|
+
const result = stmt.run(...bind);
|
|
143
|
+
return {
|
|
144
|
+
rows: [],
|
|
145
|
+
rowCount: Number(result.changes ?? 0),
|
|
146
|
+
};
|
|
147
|
+
},
|
|
148
|
+
dispose() {
|
|
149
|
+
db.close();
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function createMysqlClient(opts) {
|
|
154
|
+
const parsed = parseSqlUri(opts.location);
|
|
155
|
+
if (parsed.adapter !== 'mysql') {
|
|
156
|
+
throw new Error(`Expected mysql URI, got ${parsed.adapter}`);
|
|
157
|
+
}
|
|
158
|
+
const tls = resolveTlsFlag(opts.tls, parsed.ssl);
|
|
159
|
+
const pool = createPool({
|
|
160
|
+
host: parsed.host,
|
|
161
|
+
port: parsed.port,
|
|
162
|
+
user: parsed.user,
|
|
163
|
+
password: parsed.password,
|
|
164
|
+
database: parsed.database,
|
|
165
|
+
ssl: tls ? { rejectUnauthorized: false } : undefined,
|
|
166
|
+
supportBigNumbers: true,
|
|
167
|
+
bigNumberStrings: true,
|
|
168
|
+
connectionLimit: opts.max ?? 10,
|
|
169
|
+
idleTimeout: opts.idleTimeout != null ? opts.idleTimeout * 1000 : undefined,
|
|
170
|
+
connectTimeout: opts.connectionTimeout != null ? opts.connectionTimeout * 1000 : undefined,
|
|
171
|
+
});
|
|
172
|
+
return wrapMysqlPoolAsSqlClient(pool);
|
|
173
|
+
}
|
|
174
|
+
function createPostgresClient(opts) {
|
|
175
|
+
const parsed = parseSqlUri(opts.location);
|
|
176
|
+
if (parsed.adapter !== 'postgres') {
|
|
177
|
+
throw new Error(`Expected postgres URI, got ${parsed.adapter}`);
|
|
178
|
+
}
|
|
179
|
+
const tls = resolveTlsFlag(opts.tls, parsed.ssl);
|
|
180
|
+
const pool = new pg.Pool({
|
|
181
|
+
connectionString: opts.location,
|
|
182
|
+
max: opts.max ?? 10,
|
|
183
|
+
idleTimeoutMillis: opts.idleTimeout != null ? opts.idleTimeout * 1000 : undefined,
|
|
184
|
+
connectionTimeoutMillis: opts.connectionTimeout != null ? opts.connectionTimeout * 1000 : undefined,
|
|
185
|
+
ssl: tls ? { rejectUnauthorized: false } : undefined,
|
|
186
|
+
});
|
|
187
|
+
return wrapPgPoolAsSqlClient(pool);
|
|
188
|
+
}
|
|
189
|
+
function resolveTlsFlag(option, fromUri) {
|
|
190
|
+
if (option === false || option === 'disable')
|
|
191
|
+
return false;
|
|
192
|
+
if (option === true || option === 'true')
|
|
193
|
+
return true;
|
|
194
|
+
if (typeof option === 'string' && option.length > 0 && option !== 'false')
|
|
195
|
+
return true;
|
|
196
|
+
return Boolean(fromUri);
|
|
197
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/client.ts"],
|
|
4
|
+
"sourcesContent": ["import { DatabaseSync } from 'node:sqlite';\nimport { resolveSqlitePath } from './dialect.js';\nimport type { SqlClient, SqlDialect } from './types.js';\n\nexport interface CreateSqlClientOptions {\n dialect: SqlDialect;\n location: string;\n cwd?: string;\n}\n\nexport function createSqlClient(opts: CreateSqlClientOptions): SqlClient {\n switch (opts.dialect) {\n case 'sqlite':\n return createSqliteClient(opts.location, opts.cwd);\n case 'postgres':\n throw new Error(\n 'Postgres SQL transport dialect is not implemented yet in this PR (sqlite first).',\n );\n case 'mysql':\n throw new Error(\n 'MySQL SQL transport dialect is not implemented yet in this PR (sqlite first).',\n );\n default:\n throw new Error(`Unknown SQL dialect: ${opts.dialect as string}`);\n }\n}\n\nfunction createSqliteClient(location: string, cwd?: string): SqlClient {\n const dbPath = resolveSqlitePath(location, cwd);\n const db = new DatabaseSync(dbPath);\n return {\n dialect: 'sqlite',\n async query<T = Record<string, unknown>>(sql: string, params: unknown[] = []) {\n const stmt = db.prepare(sql);\n const returnsRows =\n /^\\s*(SELECT|PRAGMA|WITH)\\b/i.test(sql) || /\\bRETURNING\\b/i.test(sql);\n if (returnsRows) {\n const rows = stmt.all(...params) as T[];\n return { rows, rowCount: rows.length };\n }\n const result = stmt.run(...params) as { changes?: number };\n return {\n rows: [] as T[],\n rowCount: Number(result.changes ?? 0),\n };\n },\n dispose() {\n db.close();\n },\n };\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,oBAAoB;AAC7B,SAAS,yBAAyB;AAS3B,SAAS,gBAAgB,MAAyC;AACvE,UAAQ,KAAK,SAAS;AAAA,IACpB,KAAK;AACH,aAAO,mBAAmB,KAAK,UAAU,KAAK,GAAG;AAAA,IACnD,KAAK;AACH,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF,KAAK;AACH,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AACE,YAAM,IAAI,MAAM,wBAAwB,KAAK,OAAiB,EAAE;AAAA,EACpE;AACF;AAEA,SAAS,mBAAmB,UAAkB,KAAyB;AACrE,QAAM,SAAS,kBAAkB,UAAU,GAAG;AAC9C,QAAM,KAAK,IAAI,aAAa,MAAM;AAClC,SAAO;AAAA,IACL,SAAS;AAAA,IACT,MAAM,MAAmC,KAAa,SAAoB,CAAC,GAAG;AAC5E,YAAM,OAAO,GAAG,QAAQ,GAAG;AAC3B,YAAM,cACJ,8BAA8B,KAAK,GAAG,KAAK,iBAAiB,KAAK,GAAG;AACtE,UAAI,aAAa;AACf,cAAM,OAAO,KAAK,IAAI,GAAG,MAAM;AAC/B,eAAO,EAAE,MAAM,UAAU,KAAK,OAAO;AAAA,MACvC;AACA,YAAM,SAAS,KAAK,IAAI,GAAG,MAAM;AACjC,aAAO;AAAA,QACL,MAAM,CAAC;AAAA,QACP,UAAU,OAAO,OAAO,WAAW,CAAC;AAAA,MACtC;AAAA,IACF;AAAA,IACA,UAAU;AACR,SAAG,MAAM;AAAA,IACX;AAAA,EACF;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { isAbsolute as pathIsAbsolute, join as pathJoin } from 'node:path';
|
|
2
|
+
import { inferDialectFromConnectionString } from './uri.js';
|
|
3
|
+
/**
|
|
4
|
+
* Infer dialect from a transport `location` / connection URI.
|
|
5
|
+
* Prefer {@link inferDialectFromConnectionString} for full Bun-compatible rules.
|
|
6
|
+
*/
|
|
7
|
+
export function inferDialectFromLocation(location) {
|
|
8
|
+
if (!location)
|
|
9
|
+
return undefined;
|
|
10
|
+
try {
|
|
11
|
+
return inferDialectFromConnectionString(location);
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export function quoteIdent(dialect, name) {
|
|
18
|
+
switch (dialect) {
|
|
19
|
+
case 'mysql':
|
|
20
|
+
return `\`${name.replace(/`/g, '``')}\``;
|
|
21
|
+
case 'postgres':
|
|
22
|
+
case 'sqlite':
|
|
23
|
+
default:
|
|
24
|
+
return `"${name.replace(/"/g, '""')}"`;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export function placeholder(dialect, index) {
|
|
28
|
+
// index is 1-based for postgres
|
|
29
|
+
if (dialect === 'postgres') {
|
|
30
|
+
return `$${index}`;
|
|
31
|
+
}
|
|
32
|
+
return '?';
|
|
33
|
+
}
|
|
34
|
+
export function resolveSqlitePath(location, cwd) {
|
|
35
|
+
let path = location;
|
|
36
|
+
if (path.startsWith('file:')) {
|
|
37
|
+
path = path.slice('file:'.length);
|
|
38
|
+
}
|
|
39
|
+
else if (path.toLowerCase().startsWith('sqlite:')) {
|
|
40
|
+
path = path.slice(path.indexOf(':') + 1);
|
|
41
|
+
}
|
|
42
|
+
if (path.startsWith('//')) {
|
|
43
|
+
// file://absolute or file:///absolute
|
|
44
|
+
path = path.replace(/^\/\/+/, '/');
|
|
45
|
+
}
|
|
46
|
+
const qIdx = path.indexOf('?');
|
|
47
|
+
if (qIdx !== -1) {
|
|
48
|
+
path = path.slice(0, qIdx);
|
|
49
|
+
}
|
|
50
|
+
if (!pathIsAbsolute(path) && cwd && path !== ':memory:') {
|
|
51
|
+
return pathJoin(cwd, path);
|
|
52
|
+
}
|
|
53
|
+
return path;
|
|
54
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/dialect.ts"],
|
|
4
|
+
"sourcesContent": ["import { join as pathJoin, isAbsolute as pathIsAbsolute } from 'node:path';\nimport type { SqlDialect } from './types.js';\n\nexport function inferDialectFromLocation(location: string | undefined): SqlDialect | undefined {\n if (!location) return undefined;\n const lower = location.toLowerCase();\n if (lower.startsWith('postgres://') || lower.startsWith('postgresql://')) {\n return 'postgres';\n }\n if (lower.startsWith('mysql://') || lower.startsWith('mysqls://')) {\n return 'mysql';\n }\n if (\n lower.startsWith('file:') ||\n lower.startsWith('sqlite:') ||\n lower.endsWith('.db') ||\n lower.endsWith('.sqlite') ||\n lower.endsWith('.sqlite3')\n ) {\n return 'sqlite';\n }\n return undefined;\n}\n\nexport function quoteIdent(dialect: SqlDialect, name: string): string {\n switch (dialect) {\n case 'mysql':\n return `\\`${name.replace(/`/g, '``')}\\``;\n case 'postgres':\n case 'sqlite':\n default:\n return `\"${name.replace(/\"/g, '\"\"')}\"`;\n }\n}\n\nexport function placeholder(dialect: SqlDialect, index: number): string {\n // index is 1-based for postgres\n if (dialect === 'postgres') {\n return `$${index}`;\n }\n return '?';\n}\n\nexport function resolveSqlitePath(location: string, cwd?: string): string {\n let path = location;\n if (path.startsWith('file:')) {\n path = path.slice('file:'.length);\n } else if (path.startsWith('sqlite:')) {\n path = path.slice('sqlite:'.length);\n }\n if (path.startsWith('//')) {\n // file://absolute or file:///absolute\n path = path.replace(/^\\/\\/+/, '/');\n }\n if (!pathIsAbsolute(path) && cwd) {\n return pathJoin(cwd, path);\n }\n return path;\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,QAAQ,UAAU,cAAc,sBAAsB;AAGxD,SAAS,yBAAyB,UAAsD;AAC7F,MAAI,CAAC,SAAU,QAAO;AACtB,QAAM,QAAQ,SAAS,YAAY;AACnC,MAAI,MAAM,WAAW,aAAa,KAAK,MAAM,WAAW,eAAe,GAAG;AACxE,WAAO;AAAA,EACT;AACA,MAAI,MAAM,WAAW,UAAU,KAAK,MAAM,WAAW,WAAW,GAAG;AACjE,WAAO;AAAA,EACT;AACA,MACE,MAAM,WAAW,OAAO,KACxB,MAAM,WAAW,SAAS,KAC1B,MAAM,SAAS,KAAK,KACpB,MAAM,SAAS,SAAS,KACxB,MAAM,SAAS,UAAU,GACzB;AACA,WAAO;AAAA,EACT;AACA,SAAO;AACT;AAEO,SAAS,WAAW,SAAqB,MAAsB;AACpE,UAAQ,SAAS;AAAA,IACf,KAAK;AACH,aAAO,KAAK,KAAK,QAAQ,MAAM,IAAI,CAAC;AAAA,IACtC,KAAK;AAAA,IACL,KAAK;AAAA,IACL;AACE,aAAO,IAAI,KAAK,QAAQ,MAAM,IAAI,CAAC;AAAA,EACvC;AACF;AAEO,SAAS,YAAY,SAAqB,OAAuB;AAEtE,MAAI,YAAY,YAAY;AAC1B,WAAO,IAAI,KAAK;AAAA,EAClB;AACA,SAAO;AACT;AAEO,SAAS,kBAAkB,UAAkB,KAAsB;AACxE,MAAI,OAAO;AACX,MAAI,KAAK,WAAW,OAAO,GAAG;AAC5B,WAAO,KAAK,MAAM,QAAQ,MAAM;AAAA,EAClC,WAAW,KAAK,WAAW,SAAS,GAAG;AACrC,WAAO,KAAK,MAAM,UAAU,MAAM;AAAA,EACpC;AACA,MAAI,KAAK,WAAW,IAAI,GAAG;AAEzB,WAAO,KAAK,QAAQ,UAAU,GAAG;AAAA,EACnC;AACA,MAAI,CAAC,eAAe,IAAI,KAAK,KAAK;AAChC,WAAO,SAAS,KAAK,IAAI;AAAA,EAC3B;AACA,SAAO;AACT;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { GraphQLList, GraphQLNonNull, GraphQLScalarType, GraphQLUnionType, isObjectType, } from 'graphql';
|
|
2
|
+
import { entitiesResolver } from '@apollo/subgraph';
|
|
3
|
+
import { getDefDirectives } from '@graphql-mesh/utils';
|
|
4
|
+
import { fakePromise } from '@whatwg-node/promise-helpers';
|
|
5
|
+
/**
|
|
6
|
+
* Record `__resolveReference` the way `@apollo/subgraph` expects
|
|
7
|
+
* (`extensions.apollo.subgraph.resolveReference`) so we can reuse
|
|
8
|
+
* `entitiesResolver` instead of maintaining our own `_entities` dispatch.
|
|
9
|
+
* Also mirror on `type.resolveReference` for Hive stub hydration helpers / tests.
|
|
10
|
+
*/
|
|
11
|
+
export function setTypeResolveReference(type, resolveReference) {
|
|
12
|
+
const extensions = (type.extensions ??= {});
|
|
13
|
+
const apollo = (extensions.apollo ??= {});
|
|
14
|
+
const subgraph = (apollo.subgraph ??= {});
|
|
15
|
+
subgraph.resolveReference = resolveReference;
|
|
16
|
+
type.resolveReference = resolveReference;
|
|
17
|
+
}
|
|
18
|
+
export function getTypeResolveReference(type) {
|
|
19
|
+
const fromExtensions = type.extensions?.apollo?.subgraph?.resolveReference;
|
|
20
|
+
if (typeof fromExtensions === 'function') {
|
|
21
|
+
return fromExtensions;
|
|
22
|
+
}
|
|
23
|
+
const fromType = type.resolveReference;
|
|
24
|
+
return typeof fromType === 'function' ? fromType : undefined;
|
|
25
|
+
}
|
|
26
|
+
/** Raw `@sql` plus composed `@join__directive(name: "sql")` payloads. */
|
|
27
|
+
export function getSqlDirectiveArgs(schema, node) {
|
|
28
|
+
const fromDef = getDefDirectives(schema, node)
|
|
29
|
+
.filter(d => d.name === 'sql')
|
|
30
|
+
.map(d => d.args || {});
|
|
31
|
+
const joins = node.extensions?.directives?.join__directive;
|
|
32
|
+
const fromJoin = [];
|
|
33
|
+
if (Array.isArray(joins)) {
|
|
34
|
+
for (const join of joins) {
|
|
35
|
+
if (join?.name === 'sql' && join.args && typeof join.args === 'object') {
|
|
36
|
+
fromJoin.push(join.args);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const rootJoins = node.extensions?.join__directive;
|
|
41
|
+
if (Array.isArray(rootJoins)) {
|
|
42
|
+
for (const join of rootJoins) {
|
|
43
|
+
if (join?.name === 'sql' && join.args && typeof join.args === 'object') {
|
|
44
|
+
fromJoin.push(join.args);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return [...fromDef, ...fromJoin];
|
|
49
|
+
}
|
|
50
|
+
const MESH_ENTITY_FILL = Symbol.for('mesh.sql.entityFill');
|
|
51
|
+
/**
|
|
52
|
+
* For types with `resolveReference`, resolve missing fields by fetching the entity
|
|
53
|
+
* once per parent object (deduped). Lets Hive honor entity stubs the way Router does.
|
|
54
|
+
*/
|
|
55
|
+
function hydrateEntityStubFieldResolvers(schema) {
|
|
56
|
+
for (const type of Object.values(schema.getTypeMap())) {
|
|
57
|
+
if (!isObjectType(type) || type.name.startsWith('__')) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const resolveReference = getTypeResolveReference(type);
|
|
61
|
+
if (!resolveReference) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
const fields = type.getFields();
|
|
65
|
+
for (const fieldName of Object.keys(fields)) {
|
|
66
|
+
const field = fields[fieldName];
|
|
67
|
+
if (typeof field.resolve === 'function') {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
field.resolve = (parent, _args, context, info) => {
|
|
71
|
+
if (parent == null || typeof parent !== 'object') {
|
|
72
|
+
return parent?.[fieldName];
|
|
73
|
+
}
|
|
74
|
+
if (Object.prototype.hasOwnProperty.call(parent, fieldName)) {
|
|
75
|
+
return parent[fieldName];
|
|
76
|
+
}
|
|
77
|
+
let fill = parent[MESH_ENTITY_FILL];
|
|
78
|
+
if (!fill) {
|
|
79
|
+
fill = fakePromise(resolveReference(parent, context, info)).then((full) => {
|
|
80
|
+
if (full && typeof full === 'object') {
|
|
81
|
+
Object.assign(parent, full);
|
|
82
|
+
}
|
|
83
|
+
return parent;
|
|
84
|
+
});
|
|
85
|
+
parent[MESH_ENTITY_FILL] = fill;
|
|
86
|
+
}
|
|
87
|
+
return fill.then((full) => full?.[fieldName]);
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Add / repair Federation `Query._entities` (same contract as Connectors REST transport).
|
|
94
|
+
*/
|
|
95
|
+
function ensureFederationEntitiesField(schema) {
|
|
96
|
+
const queryType = schema.getQueryType();
|
|
97
|
+
if (!queryType) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const queryFields = queryType.getFields();
|
|
101
|
+
const entityTypes = Object.values(schema.getTypeMap()).filter((t) => isObjectType(t) && !t.name.startsWith('__') && !!getTypeResolveReference(t));
|
|
102
|
+
if (!entityTypes.length) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const typeMap = schema.getTypeMap();
|
|
106
|
+
let anyType = typeMap._Any;
|
|
107
|
+
if (!anyType) {
|
|
108
|
+
anyType = new GraphQLScalarType({
|
|
109
|
+
name: '_Any',
|
|
110
|
+
serialize: value => value,
|
|
111
|
+
parseValue: value => value,
|
|
112
|
+
});
|
|
113
|
+
typeMap._Any = anyType;
|
|
114
|
+
}
|
|
115
|
+
let entityUnion = typeMap._Entity;
|
|
116
|
+
if (!entityUnion) {
|
|
117
|
+
entityUnion = new GraphQLUnionType({
|
|
118
|
+
name: '_Entity',
|
|
119
|
+
types: entityTypes,
|
|
120
|
+
resolveType(obj) {
|
|
121
|
+
return obj?.__typename || undefined;
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
typeMap._Entity = entityUnion;
|
|
125
|
+
}
|
|
126
|
+
const entitiesResolve = (_root, args, context, info) => {
|
|
127
|
+
const representations = args?.representations;
|
|
128
|
+
if (!Array.isArray(representations)) {
|
|
129
|
+
return [];
|
|
130
|
+
}
|
|
131
|
+
return entitiesResolver({ representations, context, info });
|
|
132
|
+
};
|
|
133
|
+
if (queryFields._entities) {
|
|
134
|
+
queryFields._entities.resolve = entitiesResolve;
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
queryFields._entities = {
|
|
138
|
+
name: '_entities',
|
|
139
|
+
description: 'Federation entity resolver for SQL transport (Hive `_entities` path)',
|
|
140
|
+
type: new GraphQLNonNull(new GraphQLList(entityUnion)),
|
|
141
|
+
args: [
|
|
142
|
+
{
|
|
143
|
+
name: 'representations',
|
|
144
|
+
description: undefined,
|
|
145
|
+
type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(anyType))),
|
|
146
|
+
defaultValue: undefined,
|
|
147
|
+
deprecationReason: undefined,
|
|
148
|
+
extensions: {},
|
|
149
|
+
astNode: undefined,
|
|
150
|
+
},
|
|
151
|
+
],
|
|
152
|
+
resolve: entitiesResolve,
|
|
153
|
+
deprecationReason: undefined,
|
|
154
|
+
extensions: {},
|
|
155
|
+
astNode: undefined,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Wire Federation entity resolution from type-level `@sql` (Connectors type-level pattern).
|
|
160
|
+
* Query `entity: true` is no longer used — presence of OBJECT `@sql` is the signal.
|
|
161
|
+
*/
|
|
162
|
+
export function wireSqlEntityResolvers(schema, opts) {
|
|
163
|
+
for (const type of Object.values(schema.getTypeMap())) {
|
|
164
|
+
if (!isObjectType(type) || type.name.startsWith('__'))
|
|
165
|
+
continue;
|
|
166
|
+
if (getTypeResolveReference(type))
|
|
167
|
+
continue;
|
|
168
|
+
const sqlArgsList = getSqlDirectiveArgs(schema, type);
|
|
169
|
+
if (!sqlArgsList.length)
|
|
170
|
+
continue;
|
|
171
|
+
const sqlArgs = sqlArgsList[0];
|
|
172
|
+
const kind = sqlArgs.kind || 'ROW';
|
|
173
|
+
if (kind !== 'ROW')
|
|
174
|
+
continue;
|
|
175
|
+
setTypeResolveReference(type, (reference, context, info) => opts.executeTypeLevelRow(sqlArgs, reference, context, info));
|
|
176
|
+
}
|
|
177
|
+
ensureFederationEntitiesField(schema);
|
|
178
|
+
hydrateEntityStubFieldResolvers(schema);
|
|
179
|
+
return schema;
|
|
180
|
+
}
|