@soda-gql/typegen 0.11.25 → 0.12.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/dist/index.cjs +505 -271
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -59
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +8 -59
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +482 -275
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -7
package/dist/index.cjs
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
1
28
|
let node_fs_promises = require("node:fs/promises");
|
|
2
29
|
let node_path = require("node:path");
|
|
3
30
|
let __soda_gql_builder = require("@soda-gql/builder");
|
|
@@ -5,7 +32,9 @@ let __soda_gql_core = require("@soda-gql/core");
|
|
|
5
32
|
let graphql = require("graphql");
|
|
6
33
|
let neverthrow = require("neverthrow");
|
|
7
34
|
let node_fs = require("node:fs");
|
|
8
|
-
let
|
|
35
|
+
let fast_glob = require("fast-glob");
|
|
36
|
+
fast_glob = __toESM(fast_glob);
|
|
37
|
+
let __swc_core = require("@swc/core");
|
|
9
38
|
|
|
10
39
|
//#region packages/typegen/src/emitter.ts
|
|
11
40
|
/**
|
|
@@ -214,7 +243,7 @@ const generateInputObjectTypeDefinitions = (schema, schemaName, inputNames) => {
|
|
|
214
243
|
depthOverrides,
|
|
215
244
|
formatters
|
|
216
245
|
});
|
|
217
|
-
lines.push(`type Input_${schemaName}_${inputName} = ${typeString};`);
|
|
246
|
+
lines.push(`export type Input_${schemaName}_${inputName} = ${typeString};`);
|
|
218
247
|
}
|
|
219
248
|
return lines;
|
|
220
249
|
};
|
|
@@ -252,8 +281,16 @@ const generateTypesCode = (grouped, schemas, injectsModulePath) => {
|
|
|
252
281
|
lines.push(...inputTypeLines);
|
|
253
282
|
lines.push("");
|
|
254
283
|
}
|
|
255
|
-
const
|
|
256
|
-
|
|
284
|
+
const deduplicatedFragments = new Map();
|
|
285
|
+
for (const f of fragments) {
|
|
286
|
+
deduplicatedFragments.set(f.key, f);
|
|
287
|
+
}
|
|
288
|
+
const fragmentEntries = Array.from(deduplicatedFragments.values()).sort((a, b) => a.key.localeCompare(b.key)).map((f) => ` readonly "${f.key}": { readonly typename: "${f.typename}"; readonly input: ${f.inputType}; readonly output: ${f.outputType} };`);
|
|
289
|
+
const deduplicatedOperations = new Map();
|
|
290
|
+
for (const o of operations) {
|
|
291
|
+
deduplicatedOperations.set(o.key, o);
|
|
292
|
+
}
|
|
293
|
+
const operationEntries = Array.from(deduplicatedOperations.values()).sort((a, b) => a.key.localeCompare(b.key)).map((o) => ` readonly "${o.key}": { readonly input: ${o.inputType}; readonly output: ${o.outputType} };`);
|
|
257
294
|
lines.push(`export type PrebuiltTypes_${schemaName} = {`);
|
|
258
295
|
lines.push(" readonly fragments: {");
|
|
259
296
|
if (fragmentEntries.length > 0) {
|
|
@@ -271,7 +308,7 @@ const generateTypesCode = (grouped, schemas, injectsModulePath) => {
|
|
|
271
308
|
return lines.join("\n");
|
|
272
309
|
};
|
|
273
310
|
/**
|
|
274
|
-
* Emit prebuilt types to the
|
|
311
|
+
* Emit prebuilt types to the types.prebuilt.ts file.
|
|
275
312
|
*
|
|
276
313
|
* This function uses a partial failure strategy: if type calculation fails for
|
|
277
314
|
* individual elements (e.g., due to invalid field selections or missing schema
|
|
@@ -340,18 +377,6 @@ const typegenErrors = {
|
|
|
340
377
|
code: "TYPEGEN_BUILD_FAILED",
|
|
341
378
|
message,
|
|
342
379
|
cause
|
|
343
|
-
}),
|
|
344
|
-
emitFailed: (path, message, cause) => ({
|
|
345
|
-
code: "TYPEGEN_EMIT_FAILED",
|
|
346
|
-
message,
|
|
347
|
-
path,
|
|
348
|
-
cause
|
|
349
|
-
}),
|
|
350
|
-
bundleFailed: (path, message, cause) => ({
|
|
351
|
-
code: "TYPEGEN_BUNDLE_FAILED",
|
|
352
|
-
message,
|
|
353
|
-
path,
|
|
354
|
-
cause
|
|
355
380
|
})
|
|
356
381
|
};
|
|
357
382
|
/**
|
|
@@ -368,10 +393,6 @@ const formatTypegenError = (error) => {
|
|
|
368
393
|
case "TYPEGEN_SCHEMA_LOAD_FAILED":
|
|
369
394
|
lines.push(` Schemas: ${error.schemaNames.join(", ")}`);
|
|
370
395
|
break;
|
|
371
|
-
case "TYPEGEN_EMIT_FAILED":
|
|
372
|
-
case "TYPEGEN_BUNDLE_FAILED":
|
|
373
|
-
lines.push(` Path: ${error.path}`);
|
|
374
|
-
break;
|
|
375
396
|
}
|
|
376
397
|
if ("cause" in error && error.cause) {
|
|
377
398
|
lines.push(` Caused by: ${error.cause}`);
|
|
@@ -380,192 +401,438 @@ const formatTypegenError = (error) => {
|
|
|
380
401
|
};
|
|
381
402
|
|
|
382
403
|
//#endregion
|
|
383
|
-
//#region packages/typegen/src/
|
|
404
|
+
//#region packages/typegen/src/template-extractor.ts
|
|
405
|
+
const OPERATION_KINDS = new Set([
|
|
406
|
+
"query",
|
|
407
|
+
"mutation",
|
|
408
|
+
"subscription",
|
|
409
|
+
"fragment"
|
|
410
|
+
]);
|
|
411
|
+
const isOperationKind = (value) => OPERATION_KINDS.has(value);
|
|
384
412
|
/**
|
|
385
|
-
*
|
|
386
|
-
*
|
|
387
|
-
* Generates index.prebuilt.ts with builder-level type resolution.
|
|
388
|
-
* Types are resolved at the fragment/operation builder level using TKey/TName,
|
|
389
|
-
* eliminating the need for ResolvePrebuiltElement at the composer level.
|
|
413
|
+
* Parse TypeScript source with SWC, returning null on failure.
|
|
390
414
|
*/
|
|
391
|
-
const
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
`__inputTypeMethods_${name}`,
|
|
404
|
-
`__directiveMethods_${name}`
|
|
405
|
-
]);
|
|
406
|
-
const genericTypes = `
|
|
415
|
+
const safeParseSync = (source, tsx) => {
|
|
416
|
+
try {
|
|
417
|
+
return (0, __swc_core.parseSync)(source, {
|
|
418
|
+
syntax: "typescript",
|
|
419
|
+
tsx,
|
|
420
|
+
decorators: false,
|
|
421
|
+
dynamicImport: true
|
|
422
|
+
});
|
|
423
|
+
} catch {
|
|
424
|
+
return null;
|
|
425
|
+
}
|
|
426
|
+
};
|
|
407
427
|
/**
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
428
|
+
* Collect gql identifiers from import declarations.
|
|
429
|
+
* Finds imports like `import { gql } from "./graphql-system"`.
|
|
430
|
+
*/
|
|
431
|
+
const collectGqlIdentifiers = (module$1, filePath, helper) => {
|
|
432
|
+
const identifiers = new Set();
|
|
433
|
+
for (const item of module$1.body) {
|
|
434
|
+
let declaration = null;
|
|
435
|
+
if (item.type === "ImportDeclaration") {
|
|
436
|
+
declaration = item;
|
|
437
|
+
} else if ("declaration" in item && item.declaration && item.declaration.type === "ImportDeclaration") {
|
|
438
|
+
declaration = item.declaration;
|
|
439
|
+
}
|
|
440
|
+
if (!declaration) {
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
if (!helper.isGraphqlSystemImportSpecifier({
|
|
444
|
+
filePath,
|
|
445
|
+
specifier: declaration.source.value
|
|
446
|
+
})) {
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
for (const specifier of declaration.specifiers ?? []) {
|
|
450
|
+
if (specifier.type === "ImportSpecifier") {
|
|
451
|
+
const imported = specifier.imported ? specifier.imported.value : specifier.local.value;
|
|
452
|
+
if (imported === "gql" && !specifier.imported) {
|
|
453
|
+
identifiers.add(specifier.local.value);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return identifiers;
|
|
459
|
+
};
|
|
416
460
|
/**
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
461
|
+
* Check if a call expression is a gql.{schemaName}(...) call.
|
|
462
|
+
* Returns the schema name if it is, null otherwise.
|
|
463
|
+
*/
|
|
464
|
+
const getGqlCallSchemaName = (identifiers, call) => {
|
|
465
|
+
const callee = call.callee;
|
|
466
|
+
if (callee.type !== "MemberExpression") {
|
|
467
|
+
return null;
|
|
468
|
+
}
|
|
469
|
+
const member = callee;
|
|
470
|
+
if (member.object.type !== "Identifier" || !identifiers.has(member.object.value)) {
|
|
471
|
+
return null;
|
|
472
|
+
}
|
|
473
|
+
if (member.property.type !== "Identifier") {
|
|
474
|
+
return null;
|
|
475
|
+
}
|
|
476
|
+
const firstArg = call.arguments[0];
|
|
477
|
+
if (!firstArg?.expression || firstArg.expression.type !== "ArrowFunctionExpression") {
|
|
478
|
+
return null;
|
|
479
|
+
}
|
|
480
|
+
return member.property.value;
|
|
423
481
|
};
|
|
424
|
-
`;
|
|
425
|
-
const contextTypes = schemaNames.map((name) => `
|
|
426
482
|
/**
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
483
|
+
* Extract templates from a gql callback's arrow function body.
|
|
484
|
+
* Handles both expression bodies and block bodies with return statements.
|
|
485
|
+
*/
|
|
486
|
+
const extractTemplatesFromCallback = (arrow, schemaName) => {
|
|
487
|
+
const templates = [];
|
|
488
|
+
const processExpression = (expr) => {
|
|
489
|
+
if (expr.type === "TaggedTemplateExpression") {
|
|
490
|
+
const tagged = expr;
|
|
491
|
+
extractFromTaggedTemplate(tagged, schemaName, templates);
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
if (expr.type === "CallExpression") {
|
|
495
|
+
const call = expr;
|
|
496
|
+
if (call.callee.type === "TaggedTemplateExpression") {
|
|
497
|
+
extractFromTaggedTemplate(call.callee, schemaName, templates);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
};
|
|
501
|
+
if (arrow.body.type !== "BlockStatement") {
|
|
502
|
+
processExpression(arrow.body);
|
|
503
|
+
return templates;
|
|
504
|
+
}
|
|
505
|
+
for (const stmt of arrow.body.stmts) {
|
|
506
|
+
if (stmt.type === "ReturnStatement" && stmt.argument) {
|
|
507
|
+
processExpression(stmt.argument);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return templates;
|
|
511
|
+
};
|
|
512
|
+
const extractFromTaggedTemplate = (tagged, schemaName, templates) => {
|
|
513
|
+
let kind;
|
|
514
|
+
let elementName;
|
|
515
|
+
let typeName;
|
|
516
|
+
if (tagged.tag.type === "Identifier") {
|
|
517
|
+
kind = tagged.tag.value;
|
|
518
|
+
} else if (tagged.tag.type === "CallExpression") {
|
|
519
|
+
const tagCall = tagged.tag;
|
|
520
|
+
if (tagCall.callee.type === "Identifier") {
|
|
521
|
+
kind = tagCall.callee.value;
|
|
522
|
+
} else {
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
const firstArg = tagCall.arguments[0]?.expression;
|
|
526
|
+
if (firstArg?.type === "StringLiteral") {
|
|
527
|
+
elementName = firstArg.value;
|
|
528
|
+
}
|
|
529
|
+
const secondArg = tagCall.arguments[1]?.expression;
|
|
530
|
+
if (secondArg?.type === "StringLiteral") {
|
|
531
|
+
typeName = secondArg.value;
|
|
532
|
+
}
|
|
533
|
+
} else {
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
if (!isOperationKind(kind)) {
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
const { quasis, expressions } = tagged.template;
|
|
540
|
+
if (tagged.tag.type === "Identifier" && expressions.length > 0) {
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
if (quasis.length === 0) {
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
let content;
|
|
547
|
+
if (expressions.length === 0) {
|
|
548
|
+
const quasi = quasis[0];
|
|
549
|
+
if (!quasi) return;
|
|
550
|
+
content = quasi.cooked ?? quasi.raw;
|
|
551
|
+
} else {
|
|
552
|
+
const parts = [];
|
|
553
|
+
for (let i = 0; i < quasis.length; i++) {
|
|
554
|
+
const quasi = quasis[i];
|
|
555
|
+
if (!quasi) continue;
|
|
556
|
+
parts.push(quasi.cooked ?? quasi.raw);
|
|
557
|
+
if (i < expressions.length) {
|
|
558
|
+
parts.push(`__FRAG_SPREAD_${i}__`);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
content = parts.join("");
|
|
562
|
+
}
|
|
563
|
+
templates.push({
|
|
564
|
+
schemaName,
|
|
565
|
+
kind,
|
|
566
|
+
content,
|
|
567
|
+
...elementName !== undefined ? { elementName } : {},
|
|
568
|
+
...typeName !== undefined ? { typeName } : {}
|
|
569
|
+
});
|
|
570
|
+
};
|
|
446
571
|
/**
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
TName,
|
|
464
|
-
string[],
|
|
465
|
-
PrebuiltEntryNotFound<TName, "operation">,
|
|
466
|
-
Partial<AnyFields>,
|
|
467
|
-
PrebuiltEntryNotFound<TName, "operation">
|
|
468
|
-
>;
|
|
469
|
-
|
|
572
|
+
* Find the innermost gql call, unwrapping method chains like .attach().
|
|
573
|
+
*/
|
|
574
|
+
const findGqlCall = (identifiers, node) => {
|
|
575
|
+
if (!node || node.type !== "CallExpression") {
|
|
576
|
+
return null;
|
|
577
|
+
}
|
|
578
|
+
const call = node;
|
|
579
|
+
if (getGqlCallSchemaName(identifiers, call) !== null) {
|
|
580
|
+
return call;
|
|
581
|
+
}
|
|
582
|
+
const callee = call.callee;
|
|
583
|
+
if (callee.type !== "MemberExpression") {
|
|
584
|
+
return null;
|
|
585
|
+
}
|
|
586
|
+
return findGqlCall(identifiers, callee.object);
|
|
587
|
+
};
|
|
470
588
|
/**
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
589
|
+
* Walk AST to find gql calls and extract templates.
|
|
590
|
+
*/
|
|
591
|
+
const walkAndExtract = (node, identifiers) => {
|
|
592
|
+
const templates = [];
|
|
593
|
+
const visit = (n) => {
|
|
594
|
+
if (!n || typeof n !== "object") {
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
if ("type" in n && n.type === "CallExpression") {
|
|
598
|
+
const gqlCall = findGqlCall(identifiers, n);
|
|
599
|
+
if (gqlCall) {
|
|
600
|
+
const schemaName = getGqlCallSchemaName(identifiers, gqlCall);
|
|
601
|
+
if (schemaName) {
|
|
602
|
+
const arrow = gqlCall.arguments[0]?.expression;
|
|
603
|
+
templates.push(...extractTemplatesFromCallback(arrow, schemaName));
|
|
604
|
+
}
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
if (Array.isArray(n)) {
|
|
609
|
+
for (const item of n) {
|
|
610
|
+
visit(item);
|
|
611
|
+
}
|
|
612
|
+
return;
|
|
613
|
+
}
|
|
614
|
+
for (const key of Object.keys(n)) {
|
|
615
|
+
if (key === "span" || key === "type") {
|
|
616
|
+
continue;
|
|
617
|
+
}
|
|
618
|
+
const value = n[key];
|
|
619
|
+
if (value && typeof value === "object") {
|
|
620
|
+
visit(value);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
};
|
|
624
|
+
visit(node);
|
|
625
|
+
return templates;
|
|
626
|
+
};
|
|
482
627
|
/**
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
628
|
+
* Extract all tagged templates from a TypeScript source file.
|
|
629
|
+
*
|
|
630
|
+
* @param filePath - Absolute path to the source file (used for import resolution)
|
|
631
|
+
* @param source - TypeScript source code
|
|
632
|
+
* @param helper - GraphQL system identifier for resolving gql imports
|
|
633
|
+
* @returns Extracted templates and any warnings
|
|
634
|
+
*/
|
|
635
|
+
const extractTemplatesFromSource = (filePath, source, helper) => {
|
|
636
|
+
const warnings = [];
|
|
637
|
+
const isTsx = filePath.endsWith(".tsx");
|
|
638
|
+
const program = safeParseSync(source, isTsx);
|
|
639
|
+
if (!program || program.type !== "Module") {
|
|
640
|
+
if (source.includes("gql")) {
|
|
641
|
+
warnings.push(`[typegen-extract] Failed to parse ${filePath}`);
|
|
642
|
+
}
|
|
643
|
+
return {
|
|
644
|
+
templates: [],
|
|
645
|
+
warnings
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
const gqlIdentifiers = collectGqlIdentifiers(program, filePath, helper);
|
|
649
|
+
if (gqlIdentifiers.size === 0) {
|
|
650
|
+
return {
|
|
651
|
+
templates: [],
|
|
652
|
+
warnings
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
return {
|
|
656
|
+
templates: walkAndExtract(program, gqlIdentifiers),
|
|
657
|
+
warnings
|
|
658
|
+
};
|
|
659
|
+
};
|
|
493
660
|
|
|
661
|
+
//#endregion
|
|
662
|
+
//#region packages/typegen/src/template-scanner.ts
|
|
494
663
|
/**
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
readonly $var: unknown;
|
|
503
|
-
readonly $dir: StandardDirectives;
|
|
504
|
-
readonly $colocate: unknown;
|
|
505
|
-
};`).join("\n");
|
|
506
|
-
const gqlEntries = schemaNames.map((name) => {
|
|
507
|
-
const config = injection.get(name);
|
|
508
|
-
const adapterLine = config?.hasAdapter ? `,\n adapter: adapter_${name}` : "";
|
|
509
|
-
return ` ${name}: createGqlElementComposer(
|
|
510
|
-
__schema_${name} as AnyGraphqlSchema,
|
|
511
|
-
{
|
|
512
|
-
inputTypeMethods: __inputTypeMethods_${name},
|
|
513
|
-
directiveMethods: __directiveMethods_${name}${adapterLine}
|
|
514
|
-
}
|
|
515
|
-
) as unknown as GqlComposer_${name}`;
|
|
516
|
-
});
|
|
517
|
-
const injectsImportSpecifiers = adapterImports.length > 0 ? adapterImports.join(", ") : "";
|
|
518
|
-
const injectsImportLine = injectsImportSpecifiers ? `import { ${injectsImportSpecifiers} } from "${options.injectsModulePath}";` : "";
|
|
519
|
-
const indexCode = `\
|
|
664
|
+
* Source file scanner for tagged template extraction.
|
|
665
|
+
*
|
|
666
|
+
* Discovers source files from config include/exclude patterns,
|
|
667
|
+
* reads them, and extracts tagged templates using the template extractor.
|
|
668
|
+
*
|
|
669
|
+
* @module
|
|
670
|
+
*/
|
|
520
671
|
/**
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
}
|
|
672
|
+
* Scan source files for tagged templates.
|
|
673
|
+
*
|
|
674
|
+
* Uses fast-glob to discover files matching include/exclude patterns,
|
|
675
|
+
* then extracts tagged templates from each file.
|
|
676
|
+
*/
|
|
677
|
+
const scanSourceFiles = (options) => {
|
|
678
|
+
const { include, exclude, baseDir, helper } = options;
|
|
679
|
+
const warnings = [];
|
|
680
|
+
const ignorePatterns = exclude.map((pattern) => pattern.startsWith("!") ? pattern.slice(1) : pattern);
|
|
681
|
+
const matchedFiles = fast_glob.default.sync(include, {
|
|
682
|
+
cwd: baseDir,
|
|
683
|
+
ignore: ignorePatterns,
|
|
684
|
+
onlyFiles: true,
|
|
685
|
+
absolute: true
|
|
686
|
+
});
|
|
687
|
+
const templates = new Map();
|
|
688
|
+
for (const filePath of matchedFiles) {
|
|
689
|
+
const normalizedPath = (0, node_path.normalize)((0, node_path.resolve)(filePath)).replace(/\\/g, "/");
|
|
690
|
+
try {
|
|
691
|
+
const source = (0, node_fs.readFileSync)(normalizedPath, "utf-8");
|
|
692
|
+
const { templates: extracted, warnings: extractionWarnings } = extractTemplatesFromSource(normalizedPath, source, helper);
|
|
693
|
+
warnings.push(...extractionWarnings);
|
|
694
|
+
if (extracted.length > 0) {
|
|
695
|
+
templates.set(normalizedPath, extracted);
|
|
696
|
+
}
|
|
697
|
+
} catch (error) {
|
|
698
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
699
|
+
warnings.push(`[typegen-scan] Failed to read ${normalizedPath}: ${message}`);
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
return {
|
|
703
|
+
templates,
|
|
704
|
+
warnings
|
|
705
|
+
};
|
|
706
|
+
};
|
|
556
707
|
|
|
708
|
+
//#endregion
|
|
709
|
+
//#region packages/typegen/src/template-to-selections.ts
|
|
710
|
+
/**
|
|
711
|
+
* Convert extracted templates into field selections for the emitter.
|
|
712
|
+
*
|
|
713
|
+
* @param templates - Templates extracted from source files, keyed by file path
|
|
714
|
+
* @param schemas - Loaded schema objects keyed by schema name
|
|
715
|
+
* @returns Map of canonical IDs to field selection data, plus any warnings
|
|
716
|
+
*/
|
|
717
|
+
const convertTemplatesToSelections = (templates, schemas) => {
|
|
718
|
+
const selections = new Map();
|
|
719
|
+
const warnings = [];
|
|
720
|
+
const schemaIndexes = new Map(Object.entries(schemas).map(([name, schema]) => [name, (0, __soda_gql_core.createSchemaIndexFromSchema)(schema)]));
|
|
721
|
+
for (const [filePath, fileTemplates] of templates) {
|
|
722
|
+
for (const template of fileTemplates) {
|
|
723
|
+
const schema = schemas[template.schemaName];
|
|
724
|
+
if (!schema) {
|
|
725
|
+
warnings.push(`[typegen-template] Unknown schema "${template.schemaName}" in ${filePath}`);
|
|
726
|
+
continue;
|
|
727
|
+
}
|
|
728
|
+
const schemaIndex = schemaIndexes.get(template.schemaName);
|
|
729
|
+
if (!schemaIndex) {
|
|
730
|
+
continue;
|
|
731
|
+
}
|
|
732
|
+
try {
|
|
733
|
+
if (template.kind === "fragment") {
|
|
734
|
+
const selection = convertFragmentTemplate(template, schema, filePath);
|
|
735
|
+
if (selection) {
|
|
736
|
+
selections.set(selection.id, selection.data);
|
|
737
|
+
}
|
|
738
|
+
} else {
|
|
739
|
+
const selection = convertOperationTemplate(template, schema, filePath);
|
|
740
|
+
if (selection) {
|
|
741
|
+
selections.set(selection.id, selection.data);
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
} catch (error) {
|
|
745
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
746
|
+
warnings.push(`[typegen-template] Failed to process ${template.kind} in ${filePath}: ${message}`);
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
return {
|
|
751
|
+
selections,
|
|
752
|
+
warnings
|
|
753
|
+
};
|
|
754
|
+
};
|
|
557
755
|
/**
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
${
|
|
756
|
+
* Reconstruct full GraphQL source from an extracted template.
|
|
757
|
+
* For curried syntax (new), prepends the definition header from tag call arguments.
|
|
758
|
+
* For old syntax, returns content as-is.
|
|
759
|
+
*/
|
|
760
|
+
const reconstructGraphql = (template) => {
|
|
761
|
+
if (template.elementName) {
|
|
762
|
+
if (template.kind === "fragment" && template.typeName) {
|
|
763
|
+
return `fragment ${template.elementName} on ${template.typeName} ${template.content}`;
|
|
764
|
+
}
|
|
765
|
+
return `${template.kind} ${template.elementName} ${template.content}`;
|
|
766
|
+
}
|
|
767
|
+
return template.content;
|
|
768
|
+
};
|
|
769
|
+
/**
|
|
770
|
+
* Convert a fragment template into FieldSelectionData.
|
|
771
|
+
*/
|
|
772
|
+
const convertFragmentTemplate = (template, schema, filePath) => {
|
|
773
|
+
const schemaIndex = (0, __soda_gql_core.createSchemaIndexFromSchema)(schema);
|
|
774
|
+
const graphqlSource = reconstructGraphql(template);
|
|
775
|
+
const variableDefinitions = (0, __soda_gql_core.extractFragmentVariables)(graphqlSource, schemaIndex);
|
|
776
|
+
const { preprocessed } = (0, __soda_gql_core.preprocessFragmentArgs)(graphqlSource);
|
|
777
|
+
const document = (0, graphql.parse)(preprocessed);
|
|
778
|
+
const fragDef = document.definitions.find((d) => d.kind === graphql.Kind.FRAGMENT_DEFINITION);
|
|
779
|
+
if (!fragDef || fragDef.kind !== graphql.Kind.FRAGMENT_DEFINITION) {
|
|
780
|
+
return null;
|
|
781
|
+
}
|
|
782
|
+
const fragmentName = fragDef.name.value;
|
|
783
|
+
const onType = fragDef.typeCondition.name.value;
|
|
784
|
+
const fields = (0, __soda_gql_core.buildFieldsFromSelectionSet)(fragDef.selectionSet, schema, onType);
|
|
785
|
+
const id = `${filePath}::${fragmentName}`;
|
|
786
|
+
return {
|
|
787
|
+
id,
|
|
788
|
+
data: {
|
|
789
|
+
type: "fragment",
|
|
790
|
+
schemaLabel: schema.label,
|
|
791
|
+
key: fragmentName,
|
|
792
|
+
typename: onType,
|
|
793
|
+
fields,
|
|
794
|
+
variableDefinitions
|
|
795
|
+
}
|
|
796
|
+
};
|
|
797
|
+
};
|
|
798
|
+
/**
|
|
799
|
+
* Convert an operation template into FieldSelectionData.
|
|
800
|
+
*/
|
|
801
|
+
const convertOperationTemplate = (template, schema, filePath) => {
|
|
802
|
+
const graphqlSource = reconstructGraphql(template);
|
|
803
|
+
const document = (0, graphql.parse)(graphqlSource);
|
|
804
|
+
const opDef = document.definitions.find((d) => d.kind === graphql.Kind.OPERATION_DEFINITION);
|
|
805
|
+
if (!opDef || opDef.kind !== graphql.Kind.OPERATION_DEFINITION) {
|
|
806
|
+
return null;
|
|
807
|
+
}
|
|
808
|
+
const operationName = opDef.name?.value ?? "Anonymous";
|
|
809
|
+
const operationType = opDef.operation;
|
|
810
|
+
const rootTypeName = getRootTypeName(schema, operationType);
|
|
811
|
+
const fields = (0, __soda_gql_core.buildFieldsFromSelectionSet)(opDef.selectionSet, schema, rootTypeName);
|
|
812
|
+
const variableDefinitions = opDef.variableDefinitions ?? [];
|
|
813
|
+
const id = `${filePath}::${operationName}`;
|
|
814
|
+
return {
|
|
815
|
+
id,
|
|
816
|
+
data: {
|
|
817
|
+
type: "operation",
|
|
818
|
+
schemaLabel: schema.label,
|
|
819
|
+
operationName,
|
|
820
|
+
operationType,
|
|
821
|
+
fields,
|
|
822
|
+
variableDefinitions: [...variableDefinitions]
|
|
823
|
+
}
|
|
824
|
+
};
|
|
566
825
|
};
|
|
567
|
-
|
|
568
|
-
|
|
826
|
+
/**
|
|
827
|
+
* Get the root type name for an operation type from the schema.
|
|
828
|
+
*/
|
|
829
|
+
const getRootTypeName = (schema, operationType) => {
|
|
830
|
+
switch (operationType) {
|
|
831
|
+
case "query": return schema.operations.query ?? "Query";
|
|
832
|
+
case "mutation": return schema.operations.mutation ?? "Mutation";
|
|
833
|
+
case "subscription": return schema.operations.subscription ?? "Subscription";
|
|
834
|
+
default: return "Query";
|
|
835
|
+
}
|
|
569
836
|
};
|
|
570
837
|
|
|
571
838
|
//#endregion
|
|
@@ -575,11 +842,10 @@ ${gqlEntries.join(",\n")}
|
|
|
575
842
|
*
|
|
576
843
|
* Orchestrates the prebuilt type generation process:
|
|
577
844
|
* 1. Load schemas from generated CJS bundle
|
|
578
|
-
* 2.
|
|
579
|
-
* 3.
|
|
580
|
-
* 4.
|
|
845
|
+
* 2. Build artifact to evaluate elements
|
|
846
|
+
* 3. Extract field selections from builder
|
|
847
|
+
* 4. Scan source files for tagged templates and merge selections
|
|
581
848
|
* 5. Emit types.prebuilt.ts
|
|
582
|
-
* 6. Bundle prebuilt module
|
|
583
849
|
*
|
|
584
850
|
* @module
|
|
585
851
|
*/
|
|
@@ -621,58 +887,14 @@ const toImportSpecifier = (fromPath, targetPath, options) => {
|
|
|
621
887
|
return `${withoutExt}${runtimeExt}`;
|
|
622
888
|
};
|
|
623
889
|
/**
|
|
624
|
-
* Bundle the prebuilt module to CJS format.
|
|
625
|
-
*/
|
|
626
|
-
const bundlePrebuiltModule = async (sourcePath) => {
|
|
627
|
-
const sourceExt = (0, node_path.extname)(sourcePath);
|
|
628
|
-
const baseName = sourcePath.slice(0, -sourceExt.length);
|
|
629
|
-
const cjsPath = `${baseName}.cjs`;
|
|
630
|
-
await (0, esbuild.build)({
|
|
631
|
-
entryPoints: [sourcePath],
|
|
632
|
-
outfile: cjsPath,
|
|
633
|
-
format: "cjs",
|
|
634
|
-
platform: "node",
|
|
635
|
-
bundle: true,
|
|
636
|
-
external: ["@soda-gql/core", "@soda-gql/runtime"],
|
|
637
|
-
sourcemap: false,
|
|
638
|
-
minify: false,
|
|
639
|
-
treeShaking: false
|
|
640
|
-
});
|
|
641
|
-
return { cjsPath };
|
|
642
|
-
};
|
|
643
|
-
/**
|
|
644
|
-
* Write a TypeScript module to disk.
|
|
645
|
-
*/
|
|
646
|
-
const writeModule = async (path, content) => {
|
|
647
|
-
await (0, node_fs_promises.mkdir)((0, node_path.dirname)(path), { recursive: true });
|
|
648
|
-
await (0, node_fs_promises.writeFile)(path, content, "utf-8");
|
|
649
|
-
};
|
|
650
|
-
/**
|
|
651
|
-
* Load GraphQL schema documents from schema paths.
|
|
652
|
-
* This is needed for generatePrebuiltModule which expects DocumentNode.
|
|
653
|
-
*/
|
|
654
|
-
const loadSchemaDocuments = (schemasConfig) => {
|
|
655
|
-
const documents = new Map();
|
|
656
|
-
for (const [name, schemaConfig] of Object.entries(schemasConfig)) {
|
|
657
|
-
const schemaPaths = Array.isArray(schemaConfig.schema) ? schemaConfig.schema : [schemaConfig.schema];
|
|
658
|
-
let combinedSource = "";
|
|
659
|
-
for (const schemaPath of schemaPaths) {
|
|
660
|
-
combinedSource += `${(0, node_fs.readFileSync)(schemaPath, "utf-8")}\n`;
|
|
661
|
-
}
|
|
662
|
-
documents.set(name, (0, graphql.parse)(combinedSource));
|
|
663
|
-
}
|
|
664
|
-
return documents;
|
|
665
|
-
};
|
|
666
|
-
/**
|
|
667
890
|
* Run the typegen process.
|
|
668
891
|
*
|
|
669
892
|
* This function:
|
|
670
893
|
* 1. Loads schemas from the generated CJS bundle
|
|
671
|
-
* 2.
|
|
672
|
-
* 3.
|
|
673
|
-
* 4.
|
|
894
|
+
* 2. Creates a BuilderService and builds the artifact
|
|
895
|
+
* 3. Extracts field selections from the artifact
|
|
896
|
+
* 4. Scans source files for tagged templates and merges selections
|
|
674
897
|
* 5. Emits types.prebuilt.ts using emitPrebuiltTypes
|
|
675
|
-
* 6. Bundles the prebuilt module
|
|
676
898
|
*
|
|
677
899
|
* @param options - Typegen options including config
|
|
678
900
|
* @returns Result containing success data or error
|
|
@@ -691,24 +913,8 @@ const runTypegen = async (options) => {
|
|
|
691
913
|
return (0, neverthrow.err)(typegenErrors.schemaLoadFailed(schemaNames, schemasResult.error));
|
|
692
914
|
}
|
|
693
915
|
const schemas = schemasResult.value;
|
|
694
|
-
const
|
|
695
|
-
const
|
|
696
|
-
const internalModulePath = toImportSpecifier(prebuiltIndexPath, (0, node_path.join)(outdir, "_internal.ts"), importSpecifierOptions);
|
|
697
|
-
const injectsModulePath = toImportSpecifier(prebuiltIndexPath, (0, node_path.join)(outdir, "_internal-injects.ts"), importSpecifierOptions);
|
|
698
|
-
const injection = new Map();
|
|
699
|
-
for (const [schemaName, schemaConfig] of Object.entries(config.schemas)) {
|
|
700
|
-
injection.set(schemaName, { hasAdapter: !!schemaConfig.inject.adapter });
|
|
701
|
-
}
|
|
702
|
-
const prebuilt = generatePrebuiltModule(schemaDocuments, {
|
|
703
|
-
internalModulePath,
|
|
704
|
-
injectsModulePath,
|
|
705
|
-
injection
|
|
706
|
-
});
|
|
707
|
-
try {
|
|
708
|
-
await writeModule(prebuiltIndexPath, prebuilt.indexCode);
|
|
709
|
-
} catch (error) {
|
|
710
|
-
return (0, neverthrow.err)(typegenErrors.emitFailed(prebuiltIndexPath, `Failed to write prebuilt index: ${error instanceof Error ? error.message : String(error)}`, error));
|
|
711
|
-
}
|
|
916
|
+
const prebuiltTypesPath = (0, node_path.join)(outdir, "types.prebuilt.ts");
|
|
917
|
+
const injectsModulePath = toImportSpecifier(prebuiltTypesPath, (0, node_path.join)(outdir, "_internal-injects.ts"), importSpecifierOptions);
|
|
712
918
|
const builderService = (0, __soda_gql_builder.createBuilderService)({ config });
|
|
713
919
|
const artifactResult = await builderService.buildAsync();
|
|
714
920
|
if (artifactResult.isErr()) {
|
|
@@ -719,7 +925,38 @@ const runTypegen = async (options) => {
|
|
|
719
925
|
return (0, neverthrow.err)(typegenErrors.buildFailed("No intermediate elements available after build", undefined));
|
|
720
926
|
}
|
|
721
927
|
const fieldSelectionsResult = (0, __soda_gql_builder.extractFieldSelections)(intermediateElements);
|
|
722
|
-
const { selections:
|
|
928
|
+
const { selections: builderSelections, warnings: extractWarnings } = fieldSelectionsResult;
|
|
929
|
+
const graphqlHelper = (0, __soda_gql_builder.createGraphqlSystemIdentifyHelper)(config);
|
|
930
|
+
const scanResult = scanSourceFiles({
|
|
931
|
+
include: [...config.include],
|
|
932
|
+
exclude: [...config.exclude],
|
|
933
|
+
baseDir: config.baseDir,
|
|
934
|
+
helper: graphqlHelper
|
|
935
|
+
});
|
|
936
|
+
const templateSelections = convertTemplatesToSelections(scanResult.templates, schemas);
|
|
937
|
+
const extractFilePart = (id) => {
|
|
938
|
+
const filePart = id.split("::")[0] ?? "";
|
|
939
|
+
if (filePart.startsWith("/")) {
|
|
940
|
+
return (0, node_path.relative)(config.baseDir, filePart);
|
|
941
|
+
}
|
|
942
|
+
return filePart;
|
|
943
|
+
};
|
|
944
|
+
const extractElementName = (data) => data.type === "fragment" ? data.key : data.operationName;
|
|
945
|
+
const templateElements = new Set();
|
|
946
|
+
for (const [id, data] of templateSelections.selections) {
|
|
947
|
+
const name = extractElementName(data);
|
|
948
|
+
if (name) templateElements.add(`${extractFilePart(id)}::${name}`);
|
|
949
|
+
}
|
|
950
|
+
const fieldSelections = new Map();
|
|
951
|
+
for (const [id, data] of builderSelections) {
|
|
952
|
+
const name = extractElementName(data);
|
|
953
|
+
if (name && templateElements.has(`${extractFilePart(id)}::${name}`)) continue;
|
|
954
|
+
fieldSelections.set(id, data);
|
|
955
|
+
}
|
|
956
|
+
for (const [id, data] of templateSelections.selections) {
|
|
957
|
+
fieldSelections.set(id, data);
|
|
958
|
+
}
|
|
959
|
+
const scanWarnings = [...scanResult.warnings, ...templateSelections.warnings];
|
|
723
960
|
const emitResult = await emitPrebuiltTypes({
|
|
724
961
|
schemas,
|
|
725
962
|
fieldSelections,
|
|
@@ -729,12 +966,7 @@ const runTypegen = async (options) => {
|
|
|
729
966
|
if (emitResult.isErr()) {
|
|
730
967
|
return (0, neverthrow.err)(emitResult.error);
|
|
731
968
|
}
|
|
732
|
-
const {
|
|
733
|
-
try {
|
|
734
|
-
await bundlePrebuiltModule(prebuiltIndexPath);
|
|
735
|
-
} catch (error) {
|
|
736
|
-
return (0, neverthrow.err)(typegenErrors.bundleFailed(prebuiltIndexPath, `Failed to bundle prebuilt module: ${error instanceof Error ? error.message : String(error)}`, error));
|
|
737
|
-
}
|
|
969
|
+
const { warnings: emitWarnings } = emitResult.value;
|
|
738
970
|
let fragmentCount = 0;
|
|
739
971
|
let operationCount = 0;
|
|
740
972
|
for (const selection of fieldSelections.values()) {
|
|
@@ -744,9 +976,12 @@ const runTypegen = async (options) => {
|
|
|
744
976
|
operationCount++;
|
|
745
977
|
}
|
|
746
978
|
}
|
|
747
|
-
const allWarnings = [
|
|
979
|
+
const allWarnings = [
|
|
980
|
+
...extractWarnings,
|
|
981
|
+
...scanWarnings,
|
|
982
|
+
...emitWarnings
|
|
983
|
+
];
|
|
748
984
|
return (0, neverthrow.ok)({
|
|
749
|
-
prebuiltIndexPath,
|
|
750
985
|
prebuiltTypesPath,
|
|
751
986
|
fragmentCount,
|
|
752
987
|
operationCount,
|
|
@@ -757,7 +992,6 @@ const runTypegen = async (options) => {
|
|
|
757
992
|
//#endregion
|
|
758
993
|
exports.emitPrebuiltTypes = emitPrebuiltTypes;
|
|
759
994
|
exports.formatTypegenError = formatTypegenError;
|
|
760
|
-
exports.generatePrebuiltModule = generatePrebuiltModule;
|
|
761
995
|
exports.runTypegen = runTypegen;
|
|
762
996
|
exports.typegenErrors = typegenErrors;
|
|
763
997
|
//# sourceMappingURL=index.cjs.map
|