@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/plan.ts
ADDED
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getNamedType,
|
|
3
|
+
isObjectType,
|
|
4
|
+
Kind,
|
|
5
|
+
type FieldNode,
|
|
6
|
+
type FragmentDefinitionNode,
|
|
7
|
+
type GraphQLField,
|
|
8
|
+
type GraphQLObjectType,
|
|
9
|
+
type GraphQLResolveInfo,
|
|
10
|
+
type GraphQLSchema,
|
|
11
|
+
type SelectionNode,
|
|
12
|
+
} from 'graphql';
|
|
13
|
+
import { getDefDirectives } from '@graphql-mesh/utils';
|
|
14
|
+
import { batchableEqualityColumn } from './batch.js';
|
|
15
|
+
import { parseSqlMapping } from './mapping.js';
|
|
16
|
+
import type { ParsedSqlMapping, SqlKind } from './types.js';
|
|
17
|
+
|
|
18
|
+
/** Parent objects stamped with GraphQL field names already satisfied by a JOIN. */
|
|
19
|
+
export const SQL_JOINED_FIELDS = Symbol.for('mesh.sql.joinedFields');
|
|
20
|
+
|
|
21
|
+
export interface BelongsToJoin {
|
|
22
|
+
fieldName: string;
|
|
23
|
+
table: string;
|
|
24
|
+
alias: string;
|
|
25
|
+
/** Alias of the FROM / join this join hangs off (`t` or a parent join alias). */
|
|
26
|
+
parentAlias: string;
|
|
27
|
+
/** Column on the joined table (left of `where` equality). */
|
|
28
|
+
joinColumn: string;
|
|
29
|
+
/** Column on the parent alias (from `$this.<field>` → parent selection). */
|
|
30
|
+
parentColumn: string;
|
|
31
|
+
selection: ParsedSqlMapping;
|
|
32
|
+
/** Nested belongs-to joins under this relation (multi-hop). */
|
|
33
|
+
joins: BelongsToJoin[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface HasManySubselect {
|
|
37
|
+
fieldName: string;
|
|
38
|
+
table: string;
|
|
39
|
+
/** Column on the child table for the FK equality. */
|
|
40
|
+
joinColumn: string;
|
|
41
|
+
/** GraphQL field on parent → SQL column used in correlation. */
|
|
42
|
+
parentGraphqlField: string;
|
|
43
|
+
parentColumn: string;
|
|
44
|
+
selection: ParsedSqlMapping;
|
|
45
|
+
limit?: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface SelectionPlan {
|
|
49
|
+
/** Root selection narrowed to requested scalar columns (+ FK keys for joins). */
|
|
50
|
+
selection: ParsedSqlMapping;
|
|
51
|
+
joins: BelongsToJoin[];
|
|
52
|
+
hasMany: HasManySubselect[];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Look ahead in `info` and build a Graphile-style selection plan:
|
|
57
|
+
* - belongs-to (`ROW` + `$this`) → LEFT JOIN chain (including multi-hop)
|
|
58
|
+
* - has-many (`ROWS` + `$this` + simple equality) → correlated JSON subselect
|
|
59
|
+
* (falls back to DataLoader when args/filters make folding unsafe)
|
|
60
|
+
*/
|
|
61
|
+
export function planSelection(
|
|
62
|
+
schema: GraphQLSchema,
|
|
63
|
+
info: GraphQLResolveInfo,
|
|
64
|
+
fullSelection: ParsedSqlMapping,
|
|
65
|
+
): SelectionPlan {
|
|
66
|
+
const parentType = resolveNodeObjectType(info.returnType);
|
|
67
|
+
if (!parentType) {
|
|
68
|
+
return { selection: fullSelection, joins: [], hasMany: [] };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const fieldNodes = collectObjectFieldNodes(info.fieldNodes, info.fragments, info.returnType);
|
|
72
|
+
|
|
73
|
+
const aliasCounter = { n: 0 };
|
|
74
|
+
const joins: BelongsToJoin[] = [];
|
|
75
|
+
const hasMany: HasManySubselect[] = [];
|
|
76
|
+
const neededRootKeys = new Set<string>();
|
|
77
|
+
|
|
78
|
+
for (const fieldNode of fieldNodes) {
|
|
79
|
+
if (fieldNode.name.value.startsWith('__')) continue;
|
|
80
|
+
const planned = planChildField(
|
|
81
|
+
schema,
|
|
82
|
+
parentType,
|
|
83
|
+
fieldNode,
|
|
84
|
+
info.fragments,
|
|
85
|
+
fullSelection,
|
|
86
|
+
't',
|
|
87
|
+
aliasCounter,
|
|
88
|
+
0,
|
|
89
|
+
);
|
|
90
|
+
if (!planned) continue;
|
|
91
|
+
if (planned.kind === 'belongsTo') {
|
|
92
|
+
joins.push(planned.join);
|
|
93
|
+
neededRootKeys.add(planned.parentGraphqlField);
|
|
94
|
+
} else {
|
|
95
|
+
hasMany.push(planned.subselect);
|
|
96
|
+
neededRootKeys.add(planned.parentGraphqlField);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const selection = narrowRootSelection(fullSelection, fieldNodes, info.fragments, neededRootKeys);
|
|
101
|
+
|
|
102
|
+
return { selection, joins, hasMany };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** @deprecated use planSelection */
|
|
106
|
+
export function collectBelongsToJoins(
|
|
107
|
+
schema: GraphQLSchema,
|
|
108
|
+
info: GraphQLResolveInfo,
|
|
109
|
+
parentSelection: ParsedSqlMapping,
|
|
110
|
+
): BelongsToJoin[] {
|
|
111
|
+
return planSelection(schema, info, parentSelection).joins;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
type PlannedChild =
|
|
115
|
+
| { kind: 'belongsTo'; join: BelongsToJoin; parentGraphqlField: string }
|
|
116
|
+
| { kind: 'hasMany'; subselect: HasManySubselect; parentGraphqlField: string };
|
|
117
|
+
|
|
118
|
+
function planChildField(
|
|
119
|
+
schema: GraphQLSchema,
|
|
120
|
+
parentType: GraphQLObjectType,
|
|
121
|
+
fieldNode: FieldNode,
|
|
122
|
+
fragments: Record<string, FragmentDefinitionNode>,
|
|
123
|
+
parentSelection: ParsedSqlMapping,
|
|
124
|
+
parentAlias: string,
|
|
125
|
+
aliasCounter: { n: number },
|
|
126
|
+
depth: number,
|
|
127
|
+
): PlannedChild | null {
|
|
128
|
+
if (depth > 8) return null;
|
|
129
|
+
const fieldName = fieldNode.name.value;
|
|
130
|
+
const field = parentType.getFields()[fieldName];
|
|
131
|
+
if (!field) return null;
|
|
132
|
+
|
|
133
|
+
const sqlArgs = getSqlArgs(schema, field);
|
|
134
|
+
if (!sqlArgs) return null;
|
|
135
|
+
const kind = (sqlArgs.kind || 'ROWS') as SqlKind;
|
|
136
|
+
const whereMapping = parseSqlMapping(sqlArgs.where as string | undefined);
|
|
137
|
+
const relSelection = parseSqlMapping(sqlArgs.selection as string | undefined);
|
|
138
|
+
|
|
139
|
+
const joinColumn = batchableEqualityColumn(whereMapping);
|
|
140
|
+
if (!joinColumn) return null;
|
|
141
|
+
const binder = whereMapping.entries.find(e => e.key === joinColumn);
|
|
142
|
+
if (!binder?.value || binder.value.kind !== 'this') return null;
|
|
143
|
+
|
|
144
|
+
const thisField = binder.value.path?.[0] || binder.value.value;
|
|
145
|
+
if (!thisField) return null;
|
|
146
|
+
|
|
147
|
+
const parentColEntry = parentSelection.entries.find(e => e.key === thisField);
|
|
148
|
+
const parentColumn =
|
|
149
|
+
parentColEntry?.value?.kind === 'column' ? parentColEntry.value.value : thisField;
|
|
150
|
+
|
|
151
|
+
const childType = unwrapToObjectType(field.type);
|
|
152
|
+
|
|
153
|
+
if (kind === 'ROW') {
|
|
154
|
+
const alias = `j${aliasCounter.n++}`;
|
|
155
|
+
const narrowed = narrowSelectionToFieldNodes(relSelection, fieldNode, fragments, childType);
|
|
156
|
+
const nestedJoins: BelongsToJoin[] = [];
|
|
157
|
+
const neededKeys = new Set<string>();
|
|
158
|
+
|
|
159
|
+
if (fieldNode.selectionSet && childType) {
|
|
160
|
+
const childFields = collectFieldNodesFromSelectionSet(fieldNode, fragments);
|
|
161
|
+
for (const childFn of childFields) {
|
|
162
|
+
if (childFn.name.value.startsWith('__')) continue;
|
|
163
|
+
const nested = planChildField(
|
|
164
|
+
schema,
|
|
165
|
+
childType,
|
|
166
|
+
childFn,
|
|
167
|
+
fragments,
|
|
168
|
+
relSelection,
|
|
169
|
+
alias,
|
|
170
|
+
aliasCounter,
|
|
171
|
+
depth + 1,
|
|
172
|
+
);
|
|
173
|
+
if (nested?.kind === 'belongsTo') {
|
|
174
|
+
nestedJoins.push(nested.join);
|
|
175
|
+
neededKeys.add(nested.parentGraphqlField);
|
|
176
|
+
}
|
|
177
|
+
// Nested has-many under a joined ROW stays on DataLoader (short-circuit only ROW).
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Keep FK columns required by nested joins even if the client didn't ask for them.
|
|
182
|
+
let selection = narrowed.entries.length ? narrowed : relSelection;
|
|
183
|
+
if (neededKeys.size) {
|
|
184
|
+
selection = ensureSelectionKeys(relSelection, selection, neededKeys);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return {
|
|
188
|
+
kind: 'belongsTo',
|
|
189
|
+
parentGraphqlField: thisField,
|
|
190
|
+
join: {
|
|
191
|
+
fieldName,
|
|
192
|
+
table: String(sqlArgs.table),
|
|
193
|
+
alias,
|
|
194
|
+
parentAlias,
|
|
195
|
+
joinColumn,
|
|
196
|
+
parentColumn,
|
|
197
|
+
selection,
|
|
198
|
+
joins: nestedJoins,
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (kind === 'ROWS') {
|
|
204
|
+
// Only fold when the only non-planner binder is `$this` equality and args are
|
|
205
|
+
// limited to limit/offset (no where/orderBy — those stay on DataLoader).
|
|
206
|
+
if (!isSimpleHasManyFoldable(whereMapping, fieldNode)) return null;
|
|
207
|
+
const limit = readLimitArg(fieldNode);
|
|
208
|
+
const narrowed = narrowSelectionToFieldNodes(relSelection, fieldNode, fragments, childType);
|
|
209
|
+
return {
|
|
210
|
+
kind: 'hasMany',
|
|
211
|
+
parentGraphqlField: thisField,
|
|
212
|
+
subselect: {
|
|
213
|
+
fieldName,
|
|
214
|
+
table: String(sqlArgs.table),
|
|
215
|
+
joinColumn,
|
|
216
|
+
parentGraphqlField: thisField,
|
|
217
|
+
parentColumn,
|
|
218
|
+
selection: narrowed.entries.length ? narrowed : relSelection,
|
|
219
|
+
limit,
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function getSqlArgs(schema: GraphQLSchema, field: GraphQLField<any, any>) {
|
|
228
|
+
const sqlDirs = getDefDirectives(schema, field).filter(d => d.name === 'sql');
|
|
229
|
+
return sqlDirs[0]?.args || null;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function isSimpleHasManyFoldable(_whereMapping: ParsedSqlMapping, fieldNode: FieldNode): boolean {
|
|
233
|
+
// Loader emits where/orderBy binders; fold only when the operation does not
|
|
234
|
+
// pass those args (limit/offset alone are fine).
|
|
235
|
+
if (fieldNode.arguments?.length) {
|
|
236
|
+
for (const arg of fieldNode.arguments) {
|
|
237
|
+
if (arg.name.value !== 'limit' && arg.name.value !== 'offset') return false;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return true;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function readLimitArg(fieldNode: FieldNode): number | undefined {
|
|
244
|
+
const arg = fieldNode.arguments?.find(a => a.name.value === 'limit');
|
|
245
|
+
if (!arg) return undefined;
|
|
246
|
+
if (arg.value.kind === Kind.INT) return Number(arg.value.value);
|
|
247
|
+
return undefined;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function ensureSelectionKeys(
|
|
251
|
+
full: ParsedSqlMapping,
|
|
252
|
+
narrowed: ParsedSqlMapping,
|
|
253
|
+
keys: Set<string>,
|
|
254
|
+
): ParsedSqlMapping {
|
|
255
|
+
const have = new Set(narrowed.entries.map(e => e.key));
|
|
256
|
+
const extra = full.entries.filter(e => keys.has(e.key) && !have.has(e.key));
|
|
257
|
+
if (!extra.length) return narrowed;
|
|
258
|
+
return { entries: [...narrowed.entries, ...extra] };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function narrowRootSelection(
|
|
262
|
+
full: ParsedSqlMapping,
|
|
263
|
+
fieldNodes: FieldNode[],
|
|
264
|
+
fragments: Record<string, FragmentDefinitionNode>,
|
|
265
|
+
neededKeys: Set<string>,
|
|
266
|
+
): ParsedSqlMapping {
|
|
267
|
+
const requested = new Set<string>(neededKeys);
|
|
268
|
+
for (const fn of fieldNodes) {
|
|
269
|
+
if (fn.name.value.startsWith('__')) continue;
|
|
270
|
+
// Scalar / enum fields on the object — relations are in neededKeys via FK.
|
|
271
|
+
if (!fn.selectionSet) {
|
|
272
|
+
requested.add(fn.name.value);
|
|
273
|
+
} else {
|
|
274
|
+
// Client asked for a relation field; FK may already be in neededKeys.
|
|
275
|
+
// Also keep the GraphQL field if it maps to a column (unlikely for relations).
|
|
276
|
+
requested.add(fn.name.value);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (!requested.size) return full;
|
|
280
|
+
const entries = full.entries.filter(e => requested.has(e.key) && e.value?.kind === 'column');
|
|
281
|
+
// If narrowing removed everything (only relations selected), keep needed FK columns.
|
|
282
|
+
if (!entries.length) {
|
|
283
|
+
return {
|
|
284
|
+
entries: full.entries.filter(e => neededKeys.has(e.key) && e.value?.kind === 'column'),
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
// Always keep needed FK keys
|
|
288
|
+
return ensureSelectionKeys(full, { entries }, neededKeys);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function unwrapToObjectType(type: any): GraphQLObjectType | null {
|
|
292
|
+
const named = getNamedType(type);
|
|
293
|
+
return isObjectType(named) ? named : null;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** Object type whose fields are planned (unwraps Relay Connection → node). */
|
|
297
|
+
function resolveNodeObjectType(returnType: any): GraphQLObjectType | null {
|
|
298
|
+
const named = getNamedType(returnType);
|
|
299
|
+
if (!isObjectType(named)) return null;
|
|
300
|
+
if (named.name.endsWith('Connection')) {
|
|
301
|
+
const nodes = named.getFields().nodes;
|
|
302
|
+
if (nodes) return unwrapToObjectType(nodes.type);
|
|
303
|
+
const edges = named.getFields().edges;
|
|
304
|
+
if (edges) {
|
|
305
|
+
const edgeType = unwrapToObjectType(edges.type);
|
|
306
|
+
const node = edgeType?.getFields().node;
|
|
307
|
+
if (node) return unwrapToObjectType(node.type);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return named;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function collectObjectFieldNodes(
|
|
314
|
+
fieldNodes: readonly FieldNode[],
|
|
315
|
+
fragments: Record<string, FragmentDefinitionNode>,
|
|
316
|
+
returnType: any,
|
|
317
|
+
): FieldNode[] {
|
|
318
|
+
const named = getNamedType(returnType);
|
|
319
|
+
if (!isObjectType(named)) return [];
|
|
320
|
+
|
|
321
|
+
if (named.name.endsWith('Connection') && (named.getFields().nodes || named.getFields().edges)) {
|
|
322
|
+
const out: FieldNode[] = [];
|
|
323
|
+
for (const fn of fieldNodes) {
|
|
324
|
+
if (!fn.selectionSet) continue;
|
|
325
|
+
for (const sel of fn.selectionSet.selections) {
|
|
326
|
+
collectConnectionNodeFields(sel, fragments, out);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return out;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const out: FieldNode[] = [];
|
|
333
|
+
for (const fn of fieldNodes) {
|
|
334
|
+
if (!fn.selectionSet) continue;
|
|
335
|
+
for (const sel of fn.selectionSet.selections) {
|
|
336
|
+
collectFieldNodes(sel, fragments, out);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return out;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function collectConnectionNodeFields(
|
|
343
|
+
sel: SelectionNode,
|
|
344
|
+
fragments: Record<string, FragmentDefinitionNode>,
|
|
345
|
+
out: FieldNode[],
|
|
346
|
+
): void {
|
|
347
|
+
if (sel.kind === Kind.FIELD) {
|
|
348
|
+
if (sel.name.value === 'nodes' && sel.selectionSet) {
|
|
349
|
+
for (const s of sel.selectionSet.selections) collectFieldNodes(s, fragments, out);
|
|
350
|
+
} else if (sel.name.value === 'edges' && sel.selectionSet) {
|
|
351
|
+
for (const edgeSel of sel.selectionSet.selections) {
|
|
352
|
+
if (edgeSel.kind === Kind.FIELD && edgeSel.name.value === 'node' && edgeSel.selectionSet) {
|
|
353
|
+
for (const s of edgeSel.selectionSet.selections) collectFieldNodes(s, fragments, out);
|
|
354
|
+
} else if (edgeSel.kind === Kind.FRAGMENT_SPREAD || edgeSel.kind === Kind.INLINE_FRAGMENT) {
|
|
355
|
+
collectConnectionNodeFields(edgeSel, fragments, out);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
} else if (sel.name.value === 'node' && sel.selectionSet) {
|
|
359
|
+
for (const s of sel.selectionSet.selections) collectFieldNodes(s, fragments, out);
|
|
360
|
+
}
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
if (sel.kind === Kind.INLINE_FRAGMENT && sel.selectionSet) {
|
|
364
|
+
for (const s of sel.selectionSet.selections) collectConnectionNodeFields(s, fragments, out);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
if (sel.kind === Kind.FRAGMENT_SPREAD) {
|
|
368
|
+
const frag = fragments[sel.name.value];
|
|
369
|
+
if (frag?.selectionSet) {
|
|
370
|
+
for (const s of frag.selectionSet.selections) collectConnectionNodeFields(s, fragments, out);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function collectFieldNodes(
|
|
376
|
+
sel: SelectionNode,
|
|
377
|
+
fragments: Record<string, FragmentDefinitionNode>,
|
|
378
|
+
out: FieldNode[],
|
|
379
|
+
): void {
|
|
380
|
+
if (sel.kind === Kind.FIELD) {
|
|
381
|
+
out.push(sel);
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
if (sel.kind === Kind.INLINE_FRAGMENT && sel.selectionSet) {
|
|
385
|
+
for (const s of sel.selectionSet.selections) collectFieldNodes(s, fragments, out);
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
if (sel.kind === Kind.FRAGMENT_SPREAD) {
|
|
389
|
+
const frag = fragments[sel.name.value];
|
|
390
|
+
if (frag?.selectionSet) {
|
|
391
|
+
for (const s of frag.selectionSet.selections) collectFieldNodes(s, fragments, out);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function collectFieldNodesFromSelectionSet(
|
|
397
|
+
fieldNode: FieldNode,
|
|
398
|
+
fragments: Record<string, FragmentDefinitionNode>,
|
|
399
|
+
): FieldNode[] {
|
|
400
|
+
const out: FieldNode[] = [];
|
|
401
|
+
if (!fieldNode.selectionSet) return out;
|
|
402
|
+
for (const sel of fieldNode.selectionSet.selections) {
|
|
403
|
+
collectFieldNodes(sel, fragments, out);
|
|
404
|
+
}
|
|
405
|
+
return out;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function narrowSelectionToFieldNodes(
|
|
409
|
+
full: ParsedSqlMapping,
|
|
410
|
+
fieldNode: FieldNode,
|
|
411
|
+
fragments: Record<string, FragmentDefinitionNode>,
|
|
412
|
+
objectType: GraphQLObjectType | null,
|
|
413
|
+
): ParsedSqlMapping {
|
|
414
|
+
if (!fieldNode.selectionSet || !objectType) return full;
|
|
415
|
+
const requested = new Set<string>();
|
|
416
|
+
const stack: SelectionNode[] = [...fieldNode.selectionSet.selections];
|
|
417
|
+
while (stack.length) {
|
|
418
|
+
const sel = stack.pop()!;
|
|
419
|
+
if (sel.kind === Kind.FIELD) {
|
|
420
|
+
if (!sel.name.value.startsWith('__')) requested.add(sel.name.value);
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
if (sel.kind === Kind.INLINE_FRAGMENT && sel.selectionSet) {
|
|
424
|
+
stack.push(...sel.selectionSet.selections);
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
if (sel.kind === Kind.FRAGMENT_SPREAD) {
|
|
428
|
+
const frag = fragments[sel.name.value];
|
|
429
|
+
if (frag?.selectionSet) stack.push(...frag.selectionSet.selections);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
if (!requested.size) return full;
|
|
433
|
+
return {
|
|
434
|
+
entries: full.entries.filter(e => requested.has(e.key) && e.value?.kind === 'column'),
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
export function flattenJoins(joins: BelongsToJoin[]): BelongsToJoin[] {
|
|
439
|
+
const out: BelongsToJoin[] = [];
|
|
440
|
+
const walk = (list: BelongsToJoin[]) => {
|
|
441
|
+
for (const j of list) {
|
|
442
|
+
out.push(j);
|
|
443
|
+
if (j.joins?.length) walk(j.joins);
|
|
444
|
+
}
|
|
445
|
+
};
|
|
446
|
+
walk(joins);
|
|
447
|
+
return out;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
export function stampJoinedFields<T extends Record<string, any>>(
|
|
451
|
+
obj: T,
|
|
452
|
+
joins: BelongsToJoin[],
|
|
453
|
+
): T {
|
|
454
|
+
const names = new Set(joins.map(j => j.fieldName));
|
|
455
|
+
Object.defineProperty(obj, SQL_JOINED_FIELDS, {
|
|
456
|
+
value: names,
|
|
457
|
+
enumerable: false,
|
|
458
|
+
configurable: true,
|
|
459
|
+
});
|
|
460
|
+
return obj;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
export function hasJoinedField(parent: any, fieldName: string): boolean {
|
|
464
|
+
const set = parent?.[SQL_JOINED_FIELDS] as Set<string> | undefined;
|
|
465
|
+
return Boolean(set?.has(fieldName));
|
|
466
|
+
}
|