@soda-gql/codegen 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.
@@ -2164,24 +2164,6 @@ const isEnumName = (schema, name) => schema.enums.has(name);
2164
2164
  const _isInputName = (schema, name) => schema.inputs.has(name);
2165
2165
  const isUnionName = (schema, name) => schema.unions.has(name);
2166
2166
  const isObjectName = (schema, name) => schema.objects.has(name);
2167
- const renderConstValue = (value) => {
2168
- switch (value.kind) {
2169
- case Kind.NULL: return "null";
2170
- case Kind.INT:
2171
- case Kind.FLOAT: return value.value;
2172
- case Kind.STRING:
2173
- case Kind.ENUM: return JSON.stringify(value.value);
2174
- case Kind.BOOLEAN: return value.value ? "true" : "false";
2175
- case Kind.LIST: return `[${value.values.map((item) => renderConstValue(item)).join(", ")}]`;
2176
- case Kind.OBJECT: {
2177
- if (value.fields.length === 0) {
2178
- return "{}";
2179
- }
2180
- const entries = value.fields.map((field) => `${field.name.value}: ${renderConstValue(field.value)}`);
2181
- return `{ ${entries.join(", ")} }`;
2182
- }
2183
- }
2184
- };
2185
2167
  /**
2186
2168
  * Maps type kind to deferred specifier prefix character.
2187
2169
  */
@@ -2197,10 +2179,8 @@ const renderInputRef = (schema, definition, excluded) => {
2197
2179
  const { name, modifier } = parseTypeReference(definition.type);
2198
2180
  const defaultValue = definition.defaultValue;
2199
2181
  if (excluded.has(name)) {
2200
- if (defaultValue) {
2201
- return `{ kind: "excluded", name: "${name}", modifier: "${modifier}", defaultValue: { default: ${renderConstValue(defaultValue)} } }`;
2202
- }
2203
- return `{ kind: "excluded", name: "${name}", modifier: "${modifier}" }`;
2182
+ const defaultSuffix$1 = defaultValue ? "|D" : "";
2183
+ return `"x|${name}|${modifier}${defaultSuffix$1}"`;
2204
2184
  }
2205
2185
  let kind;
2206
2186
  if (isScalarName(schema, name)) {
@@ -2260,7 +2240,7 @@ const renderOutputRef = (schema, type, args, excluded) => {
2260
2240
  const { name, modifier } = parseTypeReference(type);
2261
2241
  if (excluded.has(name)) {
2262
2242
  const argumentMap = renderArgumentMap(schema, args, excluded);
2263
- return `{ kind: "excluded", name: "${name}", modifier: "${modifier}", arguments: ${argumentMap} }`;
2243
+ return `{ spec: "x|${name}|${modifier}", arguments: ${argumentMap} }`;
2264
2244
  }
2265
2245
  let kind;
2266
2246
  if (isScalarName(schema, name)) {
@@ -2337,14 +2317,6 @@ const renderUnionVar = (schemaName, record, excluded) => {
2337
2317
  return `const union_${schemaName}_${record.name} = { name: "${record.name}", types: ${typesObj} } as const;`;
2338
2318
  };
2339
2319
  const collectObjectTypeNames = (schema) => Array.from(schema.objects.keys()).filter((name) => !name.startsWith("__")).sort((left, right) => left.localeCompare(right));
2340
- const renderFragmentBuildersType = (objectTypeNames, schemaName, adapterTypeName) => {
2341
- if (objectTypeNames.length === 0) {
2342
- return `type FragmentBuilders_${schemaName} = Record<string, never>;`;
2343
- }
2344
- const adapterPart = adapterTypeName ? `, ExtractMetadataAdapter<${adapterTypeName}>` : "";
2345
- const entries = objectTypeNames.map((name) => ` readonly ${name}: FragmentBuilderFor<Schema_${schemaName}, "${name}"${adapterPart}>`);
2346
- return `type FragmentBuilders_${schemaName} = {\n${entries.join(";\n")};\n};`;
2347
- };
2348
2320
  const collectInputTypeNames = (schema) => Array.from(schema.inputs.keys()).filter((name) => !name.startsWith("__")).sort((left, right) => left.localeCompare(right));
2349
2321
  const collectEnumTypeNames = (schema) => Array.from(schema.enums.keys()).filter((name) => !name.startsWith("__")).sort((left, right) => left.localeCompare(right));
2350
2322
  const collectUnionTypeNames = (schema) => Array.from(schema.unions.keys()).filter((name) => !name.startsWith("__")).sort((left, right) => left.localeCompare(right));
@@ -2522,7 +2494,7 @@ const multiRuntimeTemplate = ($$) => {
2522
2494
  }
2523
2495
  const extraImports = imports.length > 0 ? `${imports.join("\n")}\n` : "";
2524
2496
  const schemaBlocks = [];
2525
- const gqlEntries = [];
2497
+ const gqlExports = [];
2526
2498
  for (const [name, config] of Object.entries($$.schemas)) {
2527
2499
  const schemaVar = `${name}Schema`;
2528
2500
  const adapterVar = adapterAliases.get(name);
@@ -2530,7 +2502,6 @@ const multiRuntimeTemplate = ($$) => {
2530
2502
  if (adapterVar) {
2531
2503
  typeExports.push(`export type Adapter_${name} = typeof ${adapterVar} & { _?: never };`);
2532
2504
  }
2533
- typeExports.push(config.fragmentBuildersTypeBlock);
2534
2505
  const inputTypeMethodsVar = `inputTypeMethods_${name}`;
2535
2506
  const factoryVar = `createMethod_${name}`;
2536
2507
  const customDirectivesVar = `customDirectives_${name}`;
@@ -2591,10 +2562,10 @@ const ${customDirectivesVar} = { ...createStandardDirectives(), ...${config.dire
2591
2562
  ${typeExports.join("\n")}`);
2592
2563
  const gqlVarName = `gql_${name}`;
2593
2564
  if (adapterVar) {
2594
- const typeParams = `<Schema_${name}, FragmentBuilders_${name}, typeof ${customDirectivesVar}, Adapter_${name}>`;
2565
+ const typeParams = `<Schema_${name}, typeof ${customDirectivesVar}, Adapter_${name}>`;
2595
2566
  schemaBlocks.push(`const ${gqlVarName} = createGqlElementComposer${typeParams}(${schemaVar}, { adapter: ${adapterVar}, inputTypeMethods: ${inputTypeMethodsVar}, directiveMethods: ${customDirectivesVar} });`);
2596
2567
  } else {
2597
- const typeParams = `<Schema_${name}, FragmentBuilders_${name}, typeof ${customDirectivesVar}>`;
2568
+ const typeParams = `<Schema_${name}, typeof ${customDirectivesVar}>`;
2598
2569
  schemaBlocks.push(`const ${gqlVarName} = createGqlElementComposer${typeParams}(${schemaVar}, { inputTypeMethods: ${inputTypeMethodsVar}, directiveMethods: ${customDirectivesVar} });`);
2599
2570
  }
2600
2571
  schemaBlocks.push(`export type Context_${name} = Parameters<typeof ${gqlVarName}>[0] extends (ctx: infer C) => unknown ? C : never;`);
@@ -2607,13 +2578,11 @@ ${typeExports.join("\n")}`);
2607
2578
  prebuiltExports.push(`export { ${adapterVar} as __adapter_${name} }`);
2608
2579
  }
2609
2580
  schemaBlocks.push(`${prebuiltExports.join(";\n")};`);
2610
- gqlEntries.push(` ${name}: ${gqlVarName}`);
2581
+ gqlExports.push(`export { ${gqlVarName} as __gql_${name} }`);
2611
2582
  }
2612
2583
  const needsDefineEnum = false;
2613
2584
  return `\
2614
2585
  import {${needsDefineEnum ? "\n defineEnum," : ""}
2615
- type ExtractMetadataAdapter,
2616
- type FragmentBuilderFor,
2617
2586
  type AnyGraphqlSchema,
2618
2587
  createDirectiveMethod,
2619
2588
  createTypedDirectiveMethod,
@@ -2624,9 +2593,7 @@ import {${needsDefineEnum ? "\n defineEnum," : ""}
2624
2593
  ${extraImports}
2625
2594
  ${schemaBlocks.join("\n")}
2626
2595
 
2627
- export const gql = {
2628
- ${gqlEntries.join(",\n")}
2629
- };
2596
+ ${gqlExports.join(";\n")};
2630
2597
  `;
2631
2598
  };
2632
2599
  const generateMultiSchemaModule = (schemas, options) => {
@@ -2700,8 +2667,6 @@ const generateMultiSchemaModule = (schemas, options) => {
2700
2667
  const factoryVar = `createMethod_${name}`;
2701
2668
  const inputTypeMethodsBlock = renderInputTypeMethods(schema, factoryVar, excluded);
2702
2669
  const directiveMethodsBlock = renderDirectiveMethods(schema, excluded);
2703
- const adapterTypeName = options?.injection?.get(name)?.adapterImportPath ? `Adapter_${name}` : undefined;
2704
- const fragmentBuildersTypeBlock = renderFragmentBuildersType(objectTypeNames, name, adapterTypeName);
2705
2670
  const queryType = schema.operationTypes.query ?? "Query";
2706
2671
  const mutationType = schema.operationTypes.mutation ?? "Mutation";
2707
2672
  const subscriptionType = schema.operationTypes.subscription ?? "Subscription";
@@ -2721,7 +2686,6 @@ const generateMultiSchemaModule = (schemas, options) => {
2721
2686
  unionNames: unionTypeNames,
2722
2687
  inputTypeMethodsBlock,
2723
2688
  directiveMethodsBlock,
2724
- fragmentBuildersTypeBlock,
2725
2689
  defaultInputDepth: options?.defaultInputDepth?.get(name),
2726
2690
  inputDepthOverrides: options?.inputDepthOverrides?.get(name)
2727
2691
  };
@@ -2769,7 +2733,138 @@ const generateMultiSchemaModule = (schemas, options) => {
2769
2733
  stats: allStats
2770
2734
  };
2771
2735
  };
2736
+ /**
2737
+ * Generate a stub `types.prebuilt.ts` file with empty PrebuiltTypes registries.
2738
+ * This stub is only written when `types.prebuilt.ts` does not already exist.
2739
+ * Typegen will later overwrite it with the real type registry.
2740
+ */
2741
+ const generatePrebuiltStub = (schemaNames) => {
2742
+ const typeDeclarations = schemaNames.map((name) => `export type PrebuiltTypes_${name} = {
2743
+ readonly fragments: {};
2744
+ readonly operations: {};
2745
+ };`).join("\n\n");
2746
+ return `\
2747
+ /**
2748
+ * Prebuilt type registry stub.
2749
+ *
2750
+ * This file was generated by @soda-gql/codegen as an empty stub.
2751
+ * Run 'soda-gql typegen' to populate with real prebuilt types.
2752
+ *
2753
+ * @module
2754
+ * @generated
2755
+ */
2756
+
2757
+ ${typeDeclarations}
2758
+ `;
2759
+ };
2760
+ /**
2761
+ * Generate the `index.ts` module that re-exports from `_internal`
2762
+ * and constructs the `gql` object from individual `__gql_*` exports.
2763
+ *
2764
+ * The `gql` object preserves the original inferred types from schema inference.
2765
+ * PrebuiltContext types will be integrated once the type resolution strategy
2766
+ * is redesigned to match the tagged template runtime API.
2767
+ */
2768
+ const generateIndexModule = (schemaNames) => {
2769
+ const gqlImports = schemaNames.map((name) => `__gql_${name}`).join(", ");
2770
+ const prebuiltImports = schemaNames.map((name) => `PrebuiltTypes_${name}`).join(", ");
2771
+ const schemaTypeImports = schemaNames.map((name) => `Schema_${name}`).join(", ");
2772
+ const directiveImports = schemaNames.map((name) => `__directiveMethods_${name}`).join(", ");
2773
+ const perSchemaTypes = schemaNames.map((name) => `
2774
+ type ResolveFragmentAtBuilder_${name}<TKey extends string> =
2775
+ TKey extends keyof PrebuiltTypes_${name}["fragments"]
2776
+ ? Fragment<
2777
+ PrebuiltTypes_${name}["fragments"][TKey]["typename"],
2778
+ PrebuiltTypes_${name}["fragments"][TKey]["input"] extends void
2779
+ ? void
2780
+ : Partial<PrebuiltTypes_${name}["fragments"][TKey]["input"] & AnyConstAssignableInput>,
2781
+ Partial<AnyFields>,
2782
+ PrebuiltTypes_${name}["fragments"][TKey]["output"] & object
2783
+ >
2784
+ : Fragment<"(unknown)", PrebuiltEntryNotFound<TKey, "fragment">, Partial<AnyFields>, PrebuiltEntryNotFound<TKey, "fragment">>;
2785
+
2786
+ type ResolveOperationAtBuilder_${name}<TOperationType extends OperationType, TName extends string> =
2787
+ TName extends keyof PrebuiltTypes_${name}["operations"]
2788
+ ? Operation<
2789
+ TOperationType,
2790
+ TName,
2791
+ string[],
2792
+ PrebuiltTypes_${name}["operations"][TName]["input"] & AnyConstAssignableInput,
2793
+ Partial<AnyFields>,
2794
+ PrebuiltTypes_${name}["operations"][TName]["output"] & object
2795
+ >
2796
+ : Operation<
2797
+ TOperationType,
2798
+ TName,
2799
+ string[],
2800
+ PrebuiltEntryNotFound<TName, "operation">,
2801
+ Partial<AnyFields>,
2802
+ PrebuiltEntryNotFound<TName, "operation">
2803
+ >;
2804
+
2805
+ type PrebuiltCurriedFragment_${name} = <TKey extends string>(
2806
+ name: TKey,
2807
+ typeName: string,
2808
+ ) => (...args: unknown[]) => (...args: unknown[]) => ResolveFragmentAtBuilder_${name}<TKey>;
2809
+
2810
+ type PrebuiltCurriedOperation_${name}<TOperationType extends OperationType> = <TName extends string>(
2811
+ operationName: TName,
2812
+ ) => (...args: unknown[]) => (...args: unknown[]) => ResolveOperationAtBuilder_${name}<TOperationType, TName>;
2813
+
2814
+ type GenericFieldFactory_${name} = Record<string, (...args: unknown[]) => Record<string, unknown> & ((callback: (tools: GenericFieldsBuilderTools_${name}) => Record<string, unknown>) => Record<string, unknown>)>;
2815
+ type GenericFieldsBuilderTools_${name} = { readonly f: GenericFieldFactory_${name}; readonly $: Readonly<Record<string, never>> };
2816
+
2817
+ type PrebuiltCallbackOperation_${name}<TOperationType extends OperationType> = <TName extends string>(
2818
+ options: { name: TName; fields: (tools: GenericFieldsBuilderTools_${name}) => Record<string, unknown>; variables?: Record<string, unknown>; metadata?: (tools: { readonly $: Readonly<Record<string, never>>; readonly fragmentMetadata: unknown[] | undefined }) => Record<string, unknown> },
2819
+ ) => ResolveOperationAtBuilder_${name}<TOperationType, TName>;
2820
+
2821
+ export type PrebuiltContext_${name} = {
2822
+ readonly fragment: PrebuiltCurriedFragment_${name};
2823
+ readonly query: PrebuiltCurriedOperation_${name}<"query"> & {
2824
+ readonly operation: PrebuiltCallbackOperation_${name}<"query">;
2825
+ readonly compat: (operationName: string) => (strings: TemplateStringsArray, ...values: never[]) => GqlDefine<unknown>;
2826
+ };
2827
+ readonly mutation: PrebuiltCurriedOperation_${name}<"mutation"> & {
2828
+ readonly operation: PrebuiltCallbackOperation_${name}<"mutation">;
2829
+ readonly compat: (operationName: string) => (strings: TemplateStringsArray, ...values: never[]) => GqlDefine<unknown>;
2830
+ };
2831
+ readonly subscription: PrebuiltCurriedOperation_${name}<"subscription"> & {
2832
+ readonly operation: PrebuiltCallbackOperation_${name}<"subscription">;
2833
+ readonly compat: (operationName: string) => (strings: TemplateStringsArray, ...values: never[]) => GqlDefine<unknown>;
2834
+ };
2835
+ readonly define: <TValue>(factory: () => TValue | Promise<TValue>) => GqlDefine<TValue>;
2836
+ readonly extend: (...args: unknown[]) => AnyOperation;
2837
+ readonly $var: VarBuilder<Schema_${name}>;
2838
+ readonly $dir: typeof __directiveMethods_${name};
2839
+ readonly $colocate: <T extends Record<string, unknown>>(projections: T) => T;
2840
+ };
2841
+
2842
+ type GqlComposer_${name} = {
2843
+ <TResult>(composeElement: (context: PrebuiltContext_${name}) => TResult): TResult;
2844
+ readonly $schema: AnyGraphqlSchema;
2845
+ };`).join("\n");
2846
+ const gqlEntries = schemaNames.map((name) => ` ${name}: __gql_${name} as unknown as GqlComposer_${name}`).join(",\n");
2847
+ return `\
2848
+ /**
2849
+ * Generated by @soda-gql/codegen
2850
+ * @module
2851
+ * @generated
2852
+ */
2853
+
2854
+ export * from "./_internal";
2855
+ import { ${gqlImports} } from "./_internal";
2856
+ import type { ${schemaTypeImports} } from "./_internal";
2857
+ import type { ${directiveImports} } from "./_internal";
2858
+ import type { ${prebuiltImports} } from "./types.prebuilt";
2859
+ import type { Fragment, Operation, OperationType, PrebuiltEntryNotFound, AnyConstAssignableInput, AnyFields, AnyGraphqlSchema, AnyOperation, VarBuilder, GqlDefine } from "@soda-gql/core";
2860
+ ${perSchemaTypes}
2861
+
2862
+ export const gql = {
2863
+ ${gqlEntries}
2864
+ };
2865
+ `;
2866
+ };
2772
2867
 
2773
2868
  //#endregion
2774
- export { generateMultiSchemaModule as n, createSchemaIndex as t };
2775
- //# sourceMappingURL=generator-Be7iR3TC.mjs.map
2869
+ export { compileTypeFilter as a, generatePrebuiltStub as i, generateIndexModule as n, generateMultiSchemaModule as r, createSchemaIndex as t };
2870
+ //# sourceMappingURL=generator-76iJu4D4.mjs.map