@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,49 @@
|
|
|
1
|
+
import { execute as grafastExecute } from 'grafast';
|
|
2
|
+
import { makePgAdaptorWithPgClient } from '@dataplan/pg/adaptors/pg';
|
|
3
|
+
import { DisposableSymbols } from '@whatwg-node/disposablestack';
|
|
4
|
+
import { wirePostgresSqlPlans } from './pg-plans.js';
|
|
5
|
+
import { buildPgRegistryFromSqlSchema } from './pg-registry.js';
|
|
6
|
+
/**
|
|
7
|
+
* Postgres fast path: Grafast + `@dataplan/pg` plans for `@sql` fields.
|
|
8
|
+
* Requires a Node `pg.Pool` on `sqlClient.pgPool` (Bun.SQL falls back to the
|
|
9
|
+
* generic GraphQL.js planner).
|
|
10
|
+
*/
|
|
11
|
+
export function createPostgresGrafastExecutor(opts) {
|
|
12
|
+
const pool = opts.sqlClient.pgPool;
|
|
13
|
+
if (!pool)
|
|
14
|
+
return null;
|
|
15
|
+
const { registry, relationNames } = buildPgRegistryFromSqlSchema(opts.subgraph);
|
|
16
|
+
const schema = wirePostgresSqlPlans(opts.subgraph, registry, { relationNames });
|
|
17
|
+
const withPgClient = makePgAdaptorWithPgClient(pool);
|
|
18
|
+
const executor = async function postgresGrafastExecutor(executionRequest) {
|
|
19
|
+
const baseContext = executionRequest.context && typeof executionRequest.context === 'object'
|
|
20
|
+
? executionRequest.context
|
|
21
|
+
: {};
|
|
22
|
+
const contextValue = {
|
|
23
|
+
...baseContext,
|
|
24
|
+
pgSettings: baseContext.pgSettings ?? {},
|
|
25
|
+
withPgClient,
|
|
26
|
+
pgSubscriber: baseContext.pgSubscriber ?? null,
|
|
27
|
+
};
|
|
28
|
+
return grafastExecute({
|
|
29
|
+
schema,
|
|
30
|
+
document: executionRequest.document,
|
|
31
|
+
variableValues: executionRequest.variables,
|
|
32
|
+
rootValue: executionRequest.rootValue,
|
|
33
|
+
contextValue,
|
|
34
|
+
operationName: executionRequest.operationName,
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
executor[DisposableSymbols.dispose] = () => {
|
|
38
|
+
void withPgClient.release?.();
|
|
39
|
+
void opts.disposeClient();
|
|
40
|
+
};
|
|
41
|
+
executor[DisposableSymbols.asyncDispose] = async () => {
|
|
42
|
+
await withPgClient.release?.();
|
|
43
|
+
await opts.disposeClient();
|
|
44
|
+
};
|
|
45
|
+
return executor;
|
|
46
|
+
}
|
|
47
|
+
export function hasPgPool(client) {
|
|
48
|
+
return Boolean(client.pgPool);
|
|
49
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { connection } from 'grafast';
|
|
2
|
+
import { getDefDirectives } from '@graphql-mesh/utils';
|
|
3
|
+
import { MapperKind, mapSchema } from '@graphql-tools/utils';
|
|
4
|
+
import { batchableEqualityColumn } from './batch.js';
|
|
5
|
+
import { parseSqlMapping } from './mapping.js';
|
|
6
|
+
/**
|
|
7
|
+
* Attach Grafast / `@dataplan/pg` plan resolvers for `@sql` query fields.
|
|
8
|
+
* Mutations keep traditional resolvers for this slice.
|
|
9
|
+
*/
|
|
10
|
+
export function wirePostgresSqlPlans(schema, registry, meta) {
|
|
11
|
+
return mapSchema(schema, {
|
|
12
|
+
[MapperKind.OBJECT_FIELD](fieldConfig, fieldName, typeName) {
|
|
13
|
+
const sqlDir = getDefDirectives(schema, fieldConfig).find(d => d.name === 'sql');
|
|
14
|
+
if (!sqlDir)
|
|
15
|
+
return fieldConfig;
|
|
16
|
+
const table = String(sqlDir.args.table || '');
|
|
17
|
+
const kind = (sqlDir.args.kind || 'ROWS');
|
|
18
|
+
const resource = registry.pgResources[table];
|
|
19
|
+
if (!resource)
|
|
20
|
+
return fieldConfig;
|
|
21
|
+
if (kind === 'INSERT' || kind === 'UPDATE' || kind === 'DELETE') {
|
|
22
|
+
return fieldConfig;
|
|
23
|
+
}
|
|
24
|
+
const where = parseSqlMapping(sqlDir.args.where);
|
|
25
|
+
const extensions = (fieldConfig.extensions ??= {});
|
|
26
|
+
const grafast = (extensions.grafast ??= {});
|
|
27
|
+
const isRoot = typeName === 'Query' || typeName === 'Mutation';
|
|
28
|
+
const relMeta = meta.relationNames.get(typeName)?.get(fieldName);
|
|
29
|
+
if (!isRoot && relMeta && (kind === 'ROW' || kind === 'ROWS')) {
|
|
30
|
+
grafast.plan = makeRelationPlan(kind, fieldName);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
grafast.plan = makeRootPlan(resource, kind, where);
|
|
34
|
+
}
|
|
35
|
+
delete fieldConfig.resolve;
|
|
36
|
+
return fieldConfig;
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
function makeRelationPlan(kind, relationName) {
|
|
41
|
+
return function sqlRelationPlan($parent) {
|
|
42
|
+
if (kind === 'ROW' && typeof $parent?.singleRelation === 'function') {
|
|
43
|
+
return $parent.singleRelation(relationName);
|
|
44
|
+
}
|
|
45
|
+
if (kind === 'ROWS' && typeof $parent?.manyRelation === 'function') {
|
|
46
|
+
const $rows = $parent.manyRelation(relationName);
|
|
47
|
+
return $rows;
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function makeRootPlan(resource, kind, where) {
|
|
53
|
+
return function sqlRootPlan(_$parent, fieldArgs) {
|
|
54
|
+
if (kind === 'ROW') {
|
|
55
|
+
const eqCol = batchableEqualityColumn(where);
|
|
56
|
+
const binder = eqCol ? where.entries.find(e => e.key === eqCol) : undefined;
|
|
57
|
+
if (binder?.value?.kind === 'args') {
|
|
58
|
+
const argName = binder.value.path?.[0] || binder.value.value;
|
|
59
|
+
const $arg = fieldArgs.get(argName);
|
|
60
|
+
const attr = pkAttribute(resource);
|
|
61
|
+
return resource.get({ [attr]: $arg });
|
|
62
|
+
}
|
|
63
|
+
const $rows = resource.find();
|
|
64
|
+
return typeof $rows.single === 'function' ? $rows.single() : $rows;
|
|
65
|
+
}
|
|
66
|
+
if (kind === 'CONNECTION') {
|
|
67
|
+
const $rows = resource.find();
|
|
68
|
+
if (typeof $rows.setFirst === 'function') {
|
|
69
|
+
const $first = fieldArgs.getRaw('first');
|
|
70
|
+
$rows.setFirst($first);
|
|
71
|
+
}
|
|
72
|
+
const $connection = connection($rows);
|
|
73
|
+
if (typeof $connection.setFirst === 'function') {
|
|
74
|
+
$connection.setFirst(fieldArgs.getRaw('first'));
|
|
75
|
+
}
|
|
76
|
+
return $connection;
|
|
77
|
+
}
|
|
78
|
+
if (kind === 'COUNT') {
|
|
79
|
+
const $rows = resource.find();
|
|
80
|
+
// Dataplan aggregate COUNT can be layered later; approximate via find for now.
|
|
81
|
+
return $rows;
|
|
82
|
+
}
|
|
83
|
+
// ROWS
|
|
84
|
+
const $rows = resource.find();
|
|
85
|
+
if (typeof $rows.setFirst === 'function') {
|
|
86
|
+
$rows.setFirst(fieldArgs.getRaw('limit'));
|
|
87
|
+
}
|
|
88
|
+
if (typeof $rows.setOffset === 'function') {
|
|
89
|
+
$rows.setOffset(fieldArgs.getRaw('offset'));
|
|
90
|
+
}
|
|
91
|
+
return $rows;
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function pkAttribute(resource) {
|
|
95
|
+
const attrs = resource?.codec?.attributes;
|
|
96
|
+
if (attrs && typeof attrs === 'object' && 'id' in attrs)
|
|
97
|
+
return 'id';
|
|
98
|
+
const keys = attrs ? Object.keys(attrs) : [];
|
|
99
|
+
return String(keys[0] ?? 'id');
|
|
100
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { context, object } from 'grafast';
|
|
2
|
+
import { getNamedType, isNonNullType, isObjectType, isScalarType, } from 'graphql';
|
|
3
|
+
import { sql } from 'pg-sql2';
|
|
4
|
+
import { makeRegistry, makeRegistryBuilder, PgExecutor, pgResourceOptions, recordCodec, TYPES, } from '@dataplan/pg';
|
|
5
|
+
import { getDefDirectives } from '@graphql-mesh/utils';
|
|
6
|
+
import { parseSqlMapping } from './mapping.js';
|
|
7
|
+
/**
|
|
8
|
+
* Build a `@dataplan/pg` registry from `@sql` directives on the subgraph schema.
|
|
9
|
+
* Attribute names are GraphQL field names; SQL columns are selected via expression.
|
|
10
|
+
*/
|
|
11
|
+
export function buildPgRegistryFromSqlSchema(schema) {
|
|
12
|
+
const executor = new PgExecutor({
|
|
13
|
+
name: 'mesh-sql',
|
|
14
|
+
context: () => {
|
|
15
|
+
const $ctx = context();
|
|
16
|
+
return object({
|
|
17
|
+
pgSettings: $ctx.get('pgSettings'),
|
|
18
|
+
withPgClient: $ctx.get('withPgClient'),
|
|
19
|
+
});
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
const tables = new Map();
|
|
23
|
+
const tableByType = new Map();
|
|
24
|
+
const relationNames = new Map();
|
|
25
|
+
const ensureTable = (table) => {
|
|
26
|
+
let info = tables.get(table);
|
|
27
|
+
if (!info) {
|
|
28
|
+
info = { table, columns: new Map(), typeNames: new Set() };
|
|
29
|
+
tables.set(table, info);
|
|
30
|
+
}
|
|
31
|
+
return info;
|
|
32
|
+
};
|
|
33
|
+
const absorbSelection = (info, selection, objectType) => {
|
|
34
|
+
for (const entry of selection.entries) {
|
|
35
|
+
if (!entry.value || entry.value.kind !== 'column')
|
|
36
|
+
continue;
|
|
37
|
+
const sqlCol = entry.value.value;
|
|
38
|
+
const field = objectType?.getFields()[entry.key];
|
|
39
|
+
const codec = codecFromGraphqlType(field?.type);
|
|
40
|
+
const notNull = field ? isNonNullType(field.type) : false;
|
|
41
|
+
const prev = info.columns.get(entry.key);
|
|
42
|
+
if (!prev) {
|
|
43
|
+
info.columns.set(entry.key, { sqlCol, notNull, codec });
|
|
44
|
+
}
|
|
45
|
+
else if (notNull && !prev.notNull) {
|
|
46
|
+
info.columns.set(entry.key, { sqlCol, notNull: true, codec: prev.codec });
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
for (const type of Object.values(schema.getTypeMap())) {
|
|
51
|
+
if (!isObjectType(type) || type.name.startsWith('__'))
|
|
52
|
+
continue;
|
|
53
|
+
// Type-level @sql
|
|
54
|
+
for (const dir of getDefDirectives(schema, type).filter(d => d.name === 'sql')) {
|
|
55
|
+
const table = String(dir.args.table || '');
|
|
56
|
+
if (!table)
|
|
57
|
+
continue;
|
|
58
|
+
const info = ensureTable(table);
|
|
59
|
+
info.typeNames.add(type.name);
|
|
60
|
+
tableByType.set(type.name, table);
|
|
61
|
+
absorbSelection(info, parseSqlMapping(dir.args.selection), type);
|
|
62
|
+
}
|
|
63
|
+
for (const field of Object.values(type.getFields())) {
|
|
64
|
+
for (const dir of getDefDirectives(schema, field).filter(d => d.name === 'sql')) {
|
|
65
|
+
const table = String(dir.args.table || '');
|
|
66
|
+
if (!table)
|
|
67
|
+
continue;
|
|
68
|
+
const kind = (dir.args.kind || 'ROWS');
|
|
69
|
+
const info = ensureTable(table);
|
|
70
|
+
const returnType = unwrapObjectType(field.type);
|
|
71
|
+
if (returnType && (kind === 'ROW' || kind === 'ROWS' || kind === 'CONNECTION')) {
|
|
72
|
+
// CONNECTION returns Connection type — dig nodes
|
|
73
|
+
const nodeType = kind === 'CONNECTION' ? unwrapConnectionNodeType(returnType) : returnType;
|
|
74
|
+
if (nodeType) {
|
|
75
|
+
info.typeNames.add(nodeType.name);
|
|
76
|
+
tableByType.set(nodeType.name, table);
|
|
77
|
+
absorbSelection(info, parseSqlMapping(dir.args.selection), nodeType);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
absorbSelection(info, parseSqlMapping(dir.args.selection), returnType);
|
|
82
|
+
}
|
|
83
|
+
// Record relations for plan wiring
|
|
84
|
+
if ((kind === 'ROW' || kind === 'ROWS') &&
|
|
85
|
+
type.name !== 'Query' &&
|
|
86
|
+
type.name !== 'Mutation') {
|
|
87
|
+
let rels = relationNames.get(type.name);
|
|
88
|
+
if (!rels) {
|
|
89
|
+
rels = new Map();
|
|
90
|
+
relationNames.set(type.name, rels);
|
|
91
|
+
}
|
|
92
|
+
rels.set(field.name, { remoteTable: table, kind });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// Ensure at least id columns exist
|
|
98
|
+
for (const info of tables.values()) {
|
|
99
|
+
if (!info.columns.has('id')) {
|
|
100
|
+
info.columns.set('id', {
|
|
101
|
+
sqlCol: 'id',
|
|
102
|
+
notNull: true,
|
|
103
|
+
codec: TYPES.int,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const builder = makeRegistryBuilder();
|
|
108
|
+
const resourceOptionsByTable = new Map();
|
|
109
|
+
const codecByTable = new Map();
|
|
110
|
+
for (const info of tables.values()) {
|
|
111
|
+
const attributes = {};
|
|
112
|
+
for (const [gqlField, col] of info.columns) {
|
|
113
|
+
const sqlCol = col.sqlCol;
|
|
114
|
+
attributes[gqlField] = {
|
|
115
|
+
codec: col.codec,
|
|
116
|
+
notNull: col.notNull,
|
|
117
|
+
expression: alias => sql `${alias}.${sql.identifier(sqlCol)}`,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
const codec = recordCodec({
|
|
121
|
+
executor,
|
|
122
|
+
name: `${info.table}_codec`,
|
|
123
|
+
identifier: sql `${sql.identifier(info.table)}`,
|
|
124
|
+
attributes,
|
|
125
|
+
});
|
|
126
|
+
codecByTable.set(info.table, codec);
|
|
127
|
+
builder.addCodec(codec);
|
|
128
|
+
const pkAttrs = info.columns.has('id') ? ['id'] : [info.columns.keys().next().value];
|
|
129
|
+
const resourceOpts = pgResourceOptions({
|
|
130
|
+
executor,
|
|
131
|
+
codec,
|
|
132
|
+
from: sql `${sql.identifier(info.table)}`,
|
|
133
|
+
name: info.table,
|
|
134
|
+
uniques: [{ attributes: pkAttrs, isPrimary: true }],
|
|
135
|
+
});
|
|
136
|
+
resourceOptionsByTable.set(info.table, resourceOpts);
|
|
137
|
+
builder.addResource(resourceOpts);
|
|
138
|
+
}
|
|
139
|
+
// Relations from @sql $this binders
|
|
140
|
+
for (const type of Object.values(schema.getTypeMap())) {
|
|
141
|
+
if (!isObjectType(type) || type.name.startsWith('__'))
|
|
142
|
+
continue;
|
|
143
|
+
const localTable = tableByType.get(type.name);
|
|
144
|
+
if (!localTable)
|
|
145
|
+
continue;
|
|
146
|
+
const localCodec = codecByTable.get(localTable);
|
|
147
|
+
if (!localCodec)
|
|
148
|
+
continue;
|
|
149
|
+
for (const field of Object.values(type.getFields())) {
|
|
150
|
+
for (const dir of getDefDirectives(schema, field).filter(d => d.name === 'sql')) {
|
|
151
|
+
const kind = (dir.args.kind || 'ROWS');
|
|
152
|
+
if (kind !== 'ROW' && kind !== 'ROWS')
|
|
153
|
+
continue;
|
|
154
|
+
const remoteTable = String(dir.args.table || '');
|
|
155
|
+
const remoteOpts = resourceOptionsByTable.get(remoteTable);
|
|
156
|
+
if (!remoteOpts)
|
|
157
|
+
continue;
|
|
158
|
+
const where = parseSqlMapping(dir.args.where);
|
|
159
|
+
let localAttr;
|
|
160
|
+
let remoteAttr;
|
|
161
|
+
for (const entry of where.entries) {
|
|
162
|
+
if (entry.children)
|
|
163
|
+
continue;
|
|
164
|
+
if (!entry.value || entry.value.kind !== 'this')
|
|
165
|
+
continue;
|
|
166
|
+
remoteAttr = entry.key; // SQL column on remote — need GraphQL attr on remote codec
|
|
167
|
+
localAttr = entry.value.path?.[0] || entry.value.value;
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
if (!localAttr || !remoteAttr)
|
|
171
|
+
continue;
|
|
172
|
+
// Map remote SQL column → GraphQL attribute name on remote table
|
|
173
|
+
const remoteInfo = tables.get(remoteTable);
|
|
174
|
+
let remoteGqlAttr = remoteAttr;
|
|
175
|
+
if (remoteInfo) {
|
|
176
|
+
for (const [gql, col] of remoteInfo.columns) {
|
|
177
|
+
if (col.sqlCol === remoteAttr || gql === remoteAttr) {
|
|
178
|
+
remoteGqlAttr = gql;
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
const relationName = field.name;
|
|
184
|
+
try {
|
|
185
|
+
builder.addRelation(localCodec, relationName, remoteOpts, {
|
|
186
|
+
localAttributes: [localAttr],
|
|
187
|
+
remoteAttributes: [remoteGqlAttr],
|
|
188
|
+
isUnique: kind === 'ROW',
|
|
189
|
+
isReferencee: kind === 'ROWS',
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
catch {
|
|
193
|
+
// Duplicate / incompatible relations — skip
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const registry = makeRegistry(builder.getRegistryConfig());
|
|
199
|
+
return { registry, executor, tableByType, relationNames };
|
|
200
|
+
}
|
|
201
|
+
function unwrapObjectType(type) {
|
|
202
|
+
const named = getNamedType(type);
|
|
203
|
+
return isObjectType(named) ? named : null;
|
|
204
|
+
}
|
|
205
|
+
function unwrapConnectionNodeType(connectionType) {
|
|
206
|
+
const nodes = connectionType.getFields().nodes;
|
|
207
|
+
if (nodes)
|
|
208
|
+
return unwrapObjectType(nodes.type);
|
|
209
|
+
const edges = connectionType.getFields().edges;
|
|
210
|
+
if (edges) {
|
|
211
|
+
const edgeType = unwrapObjectType(edges.type);
|
|
212
|
+
const node = edgeType?.getFields().node;
|
|
213
|
+
if (node)
|
|
214
|
+
return unwrapObjectType(node.type);
|
|
215
|
+
}
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
function codecFromGraphqlType(type) {
|
|
219
|
+
if (!type)
|
|
220
|
+
return TYPES.text;
|
|
221
|
+
const named = getNamedType(type);
|
|
222
|
+
if (!isScalarType(named))
|
|
223
|
+
return TYPES.json;
|
|
224
|
+
switch (named.name) {
|
|
225
|
+
case 'Int':
|
|
226
|
+
return TYPES.int;
|
|
227
|
+
case 'Float':
|
|
228
|
+
return TYPES.float;
|
|
229
|
+
case 'Boolean':
|
|
230
|
+
return TYPES.boolean;
|
|
231
|
+
case 'ID':
|
|
232
|
+
return TYPES.text;
|
|
233
|
+
default:
|
|
234
|
+
return TYPES.text;
|
|
235
|
+
}
|
|
236
|
+
}
|