@vladimirdev635/gql-codegen 0.0.110 → 0.0.112
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/actors/text-diff.d.ts +2 -0
- package/actors/text-diff.js +113 -0
- package/actors/ts/gql-client/actor.d.ts +2 -2
- package/actors/ts/gql-client/actor.js +4 -4
- package/actors/ts/gql-client-framework/actor.js +4 -4
- package/actors/ts/gql-client-react/actor.d.ts +3 -2
- package/actors/ts/gql-client-vue/actor.d.ts +3 -2
- package/actors/ts/gql-client-vue/actor.js +1 -1
- package/actors/ts/graphql/actor.d.ts +1 -1
- package/actors/ts/graphql/actor.js +4 -4
- package/actors/ts/graphql/generators/client/fragments/document.d.ts +5 -0
- package/actors/ts/graphql/generators/client/fragments/document.js +10 -0
- package/actors/ts/graphql/generators/client/fragments/index.d.ts +4 -0
- package/actors/ts/graphql/generators/client/fragments/index.js +24 -0
- package/actors/ts/graphql/generators/client/fragments/schema.d.ts +6 -0
- package/actors/ts/graphql/generators/client/fragments/schema.js +12 -0
- package/actors/ts/graphql/generators/client/fragments/spec/index.d.ts +5 -0
- package/actors/ts/graphql/generators/client/fragments/spec/index.js +8 -0
- package/actors/ts/graphql/generators/client/fragments/spec/object/field_selection.d.ts +7 -0
- package/actors/ts/graphql/generators/client/fragments/spec/object/field_selection.js +27 -0
- package/actors/ts/graphql/generators/client/fragments/spec/object/index.d.ts +8 -0
- package/actors/ts/graphql/generators/client/fragments/spec/object/index.js +23 -0
- package/actors/ts/graphql/generators/client/fragments/spec/object/selection.d.ts +8 -0
- package/actors/ts/graphql/generators/client/fragments/spec/object/selection.js +25 -0
- package/actors/ts/graphql/generators/client/fragments/spec/object/spread_selection.d.ts +2 -0
- package/actors/ts/graphql/generators/client/fragments/spec/object/spread_selection.js +13 -0
- package/actors/ts/graphql/generators/client/fragments/spec/object/typename_field.d.ts +5 -0
- package/actors/ts/graphql/generators/client/fragments/spec/object/typename_field.js +13 -0
- package/actors/ts/graphql/generators/client/{fragments.d.ts → fragments/spec/shared.d.ts} +5 -10
- package/actors/ts/graphql/generators/client/fragments/spec/shared.js +59 -0
- package/actors/ts/graphql/generators/client/fragments/spec/union/index.d.ts +6 -0
- package/actors/ts/graphql/generators/client/fragments/spec/union/index.js +28 -0
- package/actors/ts/graphql/generators/client/fragments/spec/union/selections.d.ts +7 -0
- package/actors/ts/graphql/generators/client/fragments/spec/union/selections.js +21 -0
- package/actors/ts/graphql/generators/client/main.js +5 -4
- package/actors/ts/graphql/generators/client/operations/index.d.ts +8 -0
- package/actors/ts/graphql/generators/client/operations/index.js +33 -0
- package/actors/ts/graphql/generators/client/operations/input-schema.d.ts +5 -0
- package/actors/ts/graphql/generators/client/operations/input-schema.js +16 -0
- package/actors/ts/graphql/generators/client/operations/node.d.ts +6 -0
- package/actors/ts/graphql/generators/client/operations/node.js +20 -0
- package/actors/ts/graphql/generators/client/operations/output-schema.d.ts +6 -0
- package/actors/ts/graphql/generators/client/operations/output-schema.js +8 -0
- package/actors/utils.d.ts +3 -0
- package/actors/utils.js +22 -0
- package/config.d.ts +6 -1
- package/config.js +18 -1
- package/index.d.ts +1 -1
- package/index.js +1 -0
- package/main.d.ts +2 -2
- package/main.js +2 -2
- package/package.json +5 -1
- package/actors/ts/graphql/generators/client/fragments.js +0 -198
- package/actors/ts/graphql/generators/client/operations.d.ts +0 -8
- package/actors/ts/graphql/generators/client/operations.js +0 -66
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import pc from 'picocolors';
|
|
2
|
+
var LineType;
|
|
3
|
+
(function (LineType) {
|
|
4
|
+
LineType[LineType["Added"] = 0] = "Added";
|
|
5
|
+
LineType[LineType["Removed"] = 1] = "Removed";
|
|
6
|
+
LineType[LineType["Unchanged"] = 2] = "Unchanged";
|
|
7
|
+
})(LineType || (LineType = {}));
|
|
8
|
+
function buildLineNumbersString(newLineNumber, oldLineNumber, lineType) {
|
|
9
|
+
switch (lineType) {
|
|
10
|
+
case LineType.Added: {
|
|
11
|
+
const newStr = String(newLineNumber);
|
|
12
|
+
const oldStr = ' '.repeat(newStr.length);
|
|
13
|
+
return pc.green('+ ' + oldStr + ' ' + newStr);
|
|
14
|
+
}
|
|
15
|
+
case LineType.Removed: {
|
|
16
|
+
const oldStr = String(oldLineNumber);
|
|
17
|
+
const newStr = ' '.repeat(oldStr.length);
|
|
18
|
+
return pc.red('- ' + oldStr + ' ' + newStr);
|
|
19
|
+
}
|
|
20
|
+
case LineType.Unchanged: {
|
|
21
|
+
const oldStr = String(oldLineNumber);
|
|
22
|
+
const newStr = String(newLineNumber);
|
|
23
|
+
return pc.dim(' ' + oldStr + ' ' + newStr);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function getColorFunc(lineType) {
|
|
28
|
+
switch (lineType) {
|
|
29
|
+
case LineType.Added:
|
|
30
|
+
return pc.green;
|
|
31
|
+
case LineType.Removed:
|
|
32
|
+
return pc.red;
|
|
33
|
+
case LineType.Unchanged:
|
|
34
|
+
return pc.white;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function formatPrefix(newLineNumber, oldLineNumber, lineType) {
|
|
38
|
+
const colorFunc = getColorFunc(lineType);
|
|
39
|
+
return (buildLineNumbersString(newLineNumber, oldLineNumber, lineType) +
|
|
40
|
+
' ' +
|
|
41
|
+
colorFunc('|'));
|
|
42
|
+
}
|
|
43
|
+
function formatLine(newLineNumber, oldLineNumber, lineType, line) {
|
|
44
|
+
const colorFunc = getColorFunc(lineType);
|
|
45
|
+
return (formatPrefix(newLineNumber, oldLineNumber, lineType) +
|
|
46
|
+
colorFunc(' ' + line));
|
|
47
|
+
}
|
|
48
|
+
function buildSeparatorLine(newLineNumber, oldLineNumber) {
|
|
49
|
+
return pc
|
|
50
|
+
.blue('-')
|
|
51
|
+
.repeat(2 +
|
|
52
|
+
String(newLineNumber).length +
|
|
53
|
+
1 +
|
|
54
|
+
String(oldLineNumber).length +
|
|
55
|
+
2);
|
|
56
|
+
}
|
|
57
|
+
const n = 10;
|
|
58
|
+
function printLine(state, part, line) {
|
|
59
|
+
if (part.added) {
|
|
60
|
+
if (state.beforeBufferList.length !== 0) {
|
|
61
|
+
process.stdout.write(buildSeparatorLine(state.newLineNumber, state.oldLineNumber) +
|
|
62
|
+
'\n');
|
|
63
|
+
process.stdout.write(state.beforeBufferList.join('\n') + '\n');
|
|
64
|
+
state.beforeBufferList = [];
|
|
65
|
+
}
|
|
66
|
+
state.nextN = n;
|
|
67
|
+
process.stdout.write(formatLine(state.newLineNumber, state.oldLineNumber, LineType.Added, line) + '\n');
|
|
68
|
+
state.newLineNumber++;
|
|
69
|
+
}
|
|
70
|
+
else if (part.removed) {
|
|
71
|
+
if (state.beforeBufferList.length !== 0) {
|
|
72
|
+
process.stdout.write(buildSeparatorLine(state.newLineNumber, state.oldLineNumber) +
|
|
73
|
+
'\n');
|
|
74
|
+
process.stdout.write(state.beforeBufferList.join('\n') + '\n');
|
|
75
|
+
state.beforeBufferList = [];
|
|
76
|
+
}
|
|
77
|
+
state.nextN = n;
|
|
78
|
+
process.stdout.write(formatLine(state.newLineNumber, state.oldLineNumber, LineType.Removed, line) + '\n');
|
|
79
|
+
state.oldLineNumber++;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
const formattedLine = formatLine(state.newLineNumber, state.oldLineNumber, LineType.Unchanged, line);
|
|
83
|
+
if (state.nextN !== 0) {
|
|
84
|
+
process.stdout.write(formattedLine + '\n');
|
|
85
|
+
state.nextN -= 1;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
if (state.beforeBufferList.length < n) {
|
|
89
|
+
state.beforeBufferList.push(formattedLine);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
state.beforeBufferList.shift();
|
|
93
|
+
state.beforeBufferList.push(formattedLine);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
state.oldLineNumber++;
|
|
97
|
+
state.newLineNumber++;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
export function printChanges(changes) {
|
|
101
|
+
let state = {
|
|
102
|
+
oldLineNumber: 1,
|
|
103
|
+
newLineNumber: 1,
|
|
104
|
+
beforeBufferList: [],
|
|
105
|
+
nextN: 0,
|
|
106
|
+
};
|
|
107
|
+
for (const part of changes) {
|
|
108
|
+
const lines = part.value.replace(/\n$/, '').split('\n');
|
|
109
|
+
for (const line of lines) {
|
|
110
|
+
printLine(state, part, line);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import { Actor, ActorContext } from '../../../config.js';
|
|
2
|
+
import { OperationType } from '../../../schema/client/operation.js';
|
|
1
3
|
import { PathOrFileDescriptor } from 'fs';
|
|
2
4
|
import ts from 'typescript';
|
|
3
|
-
import { Actor, ActorContext } from '../../../config.js';
|
|
4
5
|
import { ClientTypeNameBuilders, TSActorConfig } from '../shared.js';
|
|
5
|
-
import { OperationType } from '../../../schema/client/operation.js';
|
|
6
6
|
export type OperationReturnType = 'ExecuteResult' | 'ExecuteResult.result';
|
|
7
7
|
export interface SDKConfig {
|
|
8
8
|
defaultOperationReturnType: OperationReturnType;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { executeRunAction } from '../../utils.js';
|
|
2
2
|
import { renderNodes, } from '../shared.js';
|
|
3
3
|
import { generateNodes } from './generators/main.js';
|
|
4
|
-
async function actor(config, context) {
|
|
4
|
+
async function actor(config, context, action) {
|
|
5
5
|
const nodes = generateNodes(config, context);
|
|
6
6
|
const code = await renderNodes(config, nodes);
|
|
7
|
-
|
|
7
|
+
executeRunAction(config.outPath, action, code);
|
|
8
8
|
}
|
|
9
9
|
export function build(config) {
|
|
10
|
-
return (context) => actor(config, context);
|
|
10
|
+
return (context, action) => actor(config, context, action);
|
|
11
11
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { executeRunAction } from '../../utils.js';
|
|
2
2
|
import { renderNodes, } from '../shared.js';
|
|
3
3
|
import { generateNodes } from './generators/main.js';
|
|
4
|
-
async function actor(config, context) {
|
|
4
|
+
async function actor(config, context, action) {
|
|
5
5
|
const nodes = generateNodes(config, context);
|
|
6
6
|
const code = await renderNodes(config, nodes);
|
|
7
|
-
|
|
7
|
+
executeRunAction(config.outPath, action, code);
|
|
8
8
|
}
|
|
9
9
|
export function build(config) {
|
|
10
|
-
return (context) => actor(config, context);
|
|
10
|
+
return (context, action) => actor(config, context, action);
|
|
11
11
|
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Actor, ActorContext } from '../../../config.js';
|
|
2
2
|
import { Config as FrameworkConfig, SDKConfig as FrameworkSDKConfig } from '../gql-client-framework/index.js';
|
|
3
|
-
|
|
3
|
+
import { TSActorConfig } from '../shared.js';
|
|
4
|
+
export interface SDKConfig extends Pick<FrameworkSDKConfig, 'typeName' | 'queriesKey' | 'mutationsKey' | 'subscriptionsKey' | 'clientTypeNameBuilders' | 'hookNameBuilders' | 'operationHooksTypeNameBuilder' | 'lazyHookBuilderName' | 'lazyHookTypeName' | 'syncHookBuilderName' | 'syncHookTypeName' | 'subscriptionHookBuilderName' | 'subscriptionHookTypeName'> {
|
|
4
5
|
}
|
|
5
|
-
export interface Config extends
|
|
6
|
+
export interface Config extends Pick<FrameworkConfig, 'outPath' | 'importDeclarations' | 'graphqlModulePath' | keyof TSActorConfig> {
|
|
6
7
|
sdk: SDKConfig;
|
|
7
8
|
}
|
|
8
9
|
export declare function build(config: Config): Actor<ActorContext>;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Actor, ActorContext } from '../../../config.js';
|
|
2
2
|
import { Config as FrameworkConfig, SDKConfig as FrameworkSDKConfig } from '../gql-client-framework/index.js';
|
|
3
|
-
|
|
3
|
+
import { TSActorConfig } from '../shared.js';
|
|
4
|
+
export interface SDKConfig extends Pick<FrameworkSDKConfig, 'typeName' | 'queriesKey' | 'mutationsKey' | 'subscriptionsKey' | 'clientTypeNameBuilders' | 'hookNameBuilders' | 'operationHooksTypeNameBuilder' | 'lazyHookBuilderName' | 'lazyHookTypeName' | 'syncHookBuilderName' | 'syncHookTypeName' | 'subscriptionHookBuilderName' | 'subscriptionHookTypeName'> {
|
|
4
5
|
}
|
|
5
|
-
export interface Config extends
|
|
6
|
+
export interface Config extends Pick<FrameworkConfig, 'outPath' | 'importDeclarations' | 'graphqlModulePath' | keyof TSActorConfig> {
|
|
6
7
|
sdk: SDKConfig;
|
|
7
8
|
}
|
|
8
9
|
export declare function build(config: Config): Actor<ActorContext>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Actor, ActorContext } from '../../../config.js';
|
|
2
1
|
import { PathOrFileDescriptor } from 'fs';
|
|
3
2
|
import ts from 'typescript';
|
|
3
|
+
import { Actor, ActorContext } from '../../../config.js';
|
|
4
4
|
import { ClientTypeNameBuilders, TSActorConfig } from '../shared.js';
|
|
5
5
|
import { ScalarsMapping } from './generators/server/scalars/index.js';
|
|
6
6
|
export interface Config extends TSActorConfig {
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { writeFileSync } from 'fs';
|
|
2
1
|
import { renderNodes, } from '../shared.js';
|
|
2
|
+
import { executeRunAction } from '../../utils.js';
|
|
3
3
|
import { generateNodes } from './generators/main.js';
|
|
4
|
-
async function actor(config, context) {
|
|
4
|
+
async function actor(config, context, action) {
|
|
5
5
|
const nodes = generateNodes(config, context);
|
|
6
6
|
const code = await renderNodes(config, nodes);
|
|
7
|
-
|
|
7
|
+
executeRunAction(config.outPath, action, code);
|
|
8
8
|
}
|
|
9
9
|
export function build(config) {
|
|
10
|
-
return (context) => actor(config, context);
|
|
10
|
+
return (context, action) => actor(config, context, action);
|
|
11
11
|
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { z } from 'zod/v4';
|
|
2
|
+
import { RootSchema } from '../../../../../../schema/root.js';
|
|
3
|
+
import { fragmentSchema } from '../../../../../../schema/client/fragment.js';
|
|
4
|
+
import ts from 'typescript';
|
|
5
|
+
export declare function generateFragmentDocumentNode(schema: RootSchema, name: string, fragment: z.infer<typeof fragmentSchema>): ts.VariableStatement;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { extractFragmentSourceTextsInSpec } from './spec/shared.js';
|
|
3
|
+
export function generateFragmentDocumentNode(schema, name, fragment) {
|
|
4
|
+
return ts.factory.createVariableStatement([ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
|
|
5
|
+
ts.factory.createVariableDeclaration(ts.factory.createIdentifier(name + 'FragmentDocument'), undefined, undefined, ts.factory.createStringLiteral([
|
|
6
|
+
fragment.sourceText,
|
|
7
|
+
...extractFragmentSourceTextsInSpec(schema, fragment.spec),
|
|
8
|
+
].join(' '))),
|
|
9
|
+
], ts.NodeFlags.Const));
|
|
10
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { RootSchema } from '../../../../../../schema/root.js';
|
|
2
|
+
import ts from 'typescript';
|
|
3
|
+
import { ScalarsMapping } from '../../server/scalars/mapping.js';
|
|
4
|
+
export declare function generateFragmentTypes(scalarsMapping: ScalarsMapping, schema: RootSchema, lazyFragmentsSet: Set<string>): ts.Node[];
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { generateSchemaName, generateZodInferInterfaceType, generateZodInferTypeAlias, } from '../../server/shared.js';
|
|
2
|
+
import { generateFragmentDocumentNode } from './document.js';
|
|
3
|
+
import { generateZodFragmentSchema } from './schema.js';
|
|
4
|
+
function generateFragmentDeclarations(scalarsMapping, schema, lazyFragmentsSet, fragmentName, fragment) {
|
|
5
|
+
const fName = fragmentName + 'Fragment';
|
|
6
|
+
const [fragmentSchemaNode, isLazy] = generateZodFragmentSchema(scalarsMapping, schema, lazyFragmentsSet, fragmentName, fragment);
|
|
7
|
+
if (isLazy) {
|
|
8
|
+
lazyFragmentsSet.add(fragmentName);
|
|
9
|
+
}
|
|
10
|
+
return [
|
|
11
|
+
generateFragmentDocumentNode(schema, fragmentName, fragment),
|
|
12
|
+
fragmentSchemaNode,
|
|
13
|
+
fragment.spec._type === 'ObjectFragmentSpec'
|
|
14
|
+
? generateZodInferInterfaceType('output', fName, generateSchemaName(fName))
|
|
15
|
+
: generateZodInferTypeAlias('output', fName, generateSchemaName(fName)),
|
|
16
|
+
];
|
|
17
|
+
}
|
|
18
|
+
export function generateFragmentTypes(scalarsMapping, schema, lazyFragmentsSet) {
|
|
19
|
+
return Object.entries(schema.client.fragments)
|
|
20
|
+
.map(([name, fragment]) => {
|
|
21
|
+
return generateFragmentDeclarations(scalarsMapping, schema, lazyFragmentsSet, name, fragment);
|
|
22
|
+
})
|
|
23
|
+
.flat();
|
|
24
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { fragmentSchema } from '../../../../../../schema/client/fragment.js';
|
|
2
|
+
import { RootSchema } from '../../../../../../schema/root.js';
|
|
3
|
+
import ts from 'typescript';
|
|
4
|
+
import { z } from 'zod/v4';
|
|
5
|
+
import { ScalarsMapping } from '../../server/scalars/mapping.js';
|
|
6
|
+
export declare function generateZodFragmentSchema(scalarsMapping: ScalarsMapping, schema: RootSchema, lazyFragmentsSet: Set<string>, fragmentName: string, fragment: z.infer<typeof fragmentSchema>): [ts.VariableStatement, boolean];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { generateSchemaName } from '../../server/shared.js';
|
|
3
|
+
import { generateZodFragmentSpecCallExpression } from './spec/index.js';
|
|
4
|
+
export function generateZodFragmentSchema(scalarsMapping, schema, lazyFragmentsSet, fragmentName, fragment) {
|
|
5
|
+
const [expression, isLazy] = generateZodFragmentSpecCallExpression(scalarsMapping, schema, lazyFragmentsSet, fragment.spec);
|
|
6
|
+
return [
|
|
7
|
+
ts.factory.createVariableStatement([ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
|
|
8
|
+
ts.factory.createVariableDeclaration(ts.factory.createIdentifier(generateSchemaName(fragmentName + 'Fragment')), undefined, undefined, expression),
|
|
9
|
+
], ts.NodeFlags.Const)),
|
|
10
|
+
isLazy,
|
|
11
|
+
];
|
|
12
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { RootSchema } from '../../../../../../../schema/root.js';
|
|
2
|
+
import { ScalarsMapping } from '../../../server/scalars/mapping.js';
|
|
3
|
+
import { FragmentSpecSchemaType } from '../../../../../../../schema/client/fragment.js';
|
|
4
|
+
import { resolveSelections } from './shared.js';
|
|
5
|
+
export declare function generateZodFragmentSpecCallExpression(scalarsMapping: ScalarsMapping, schema: RootSchema, lazyFragmentsSet: Set<string>, spec: FragmentSpecSchemaType, typenameConfig?: Parameters<typeof resolveSelections>[1], insideLazy?: boolean): [import("typescript").CallExpression, boolean];
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { generateZodObjectFragmentSpecCallExpression } from './object/index.js';
|
|
2
|
+
import { generateZodUnionFragmentSpecCallExpression } from './union/index.js';
|
|
3
|
+
export function generateZodFragmentSpecCallExpression(scalarsMapping, schema, lazyFragmentsSet, spec, typenameConfig, insideLazy = false) {
|
|
4
|
+
if (spec._type === 'ObjectFragmentSpec') {
|
|
5
|
+
return generateZodObjectFragmentSpecCallExpression(scalarsMapping, schema, lazyFragmentsSet, schema.server.objects[spec.name], spec.selections, insideLazy, typenameConfig || { ensurePresent: true, optional: true });
|
|
6
|
+
}
|
|
7
|
+
return generateZodUnionFragmentSpecCallExpression(scalarsMapping, schema, lazyFragmentsSet, spec, insideLazy);
|
|
8
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { fieldSelection } from '../../../../../../../../schema/client/fragment.js';
|
|
2
|
+
import { RootSchema } from '../../../../../../../../schema/root.js';
|
|
3
|
+
import { objectSchema } from '../../../../../../../../schema/server.js';
|
|
4
|
+
import ts from 'typescript';
|
|
5
|
+
import { z } from 'zod/v4';
|
|
6
|
+
import { ScalarsMapping } from '../../../../server/scalars/mapping.js';
|
|
7
|
+
export declare function generateFieldSelection(scalarsMapping: ScalarsMapping, schema: RootSchema, lazyFragmentsSet: Set<string>, insideLazy: boolean, objectType: z.infer<typeof objectSchema>, selection: z.infer<typeof fieldSelection>): ts.PropertyAssignment;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { invokeMethod } from '../../../../../../shared.js';
|
|
2
|
+
import ts from 'typescript';
|
|
3
|
+
import { generateZodObjectFieldSpec } from '../../../../server/objects.js';
|
|
4
|
+
import { generateZodFragmentSpecCallExpression } from '../index.js';
|
|
5
|
+
export function generateFieldSelection(scalarsMapping, schema, lazyFragmentsSet, insideLazy, objectType, selection) {
|
|
6
|
+
const fieldSpec = objectType.fields[selection.name];
|
|
7
|
+
let expression;
|
|
8
|
+
if (selection.selection == null) {
|
|
9
|
+
expression = generateZodObjectFieldSpec(scalarsMapping, fieldSpec);
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
const optional = fieldSpec.spec._type !== 'callable' ||
|
|
13
|
+
!selection.selection.selections.every((s) => s._type === 'TypenameField') ||
|
|
14
|
+
!fieldSpec.nullable;
|
|
15
|
+
// eslint-disable-next-line no-use-before-define
|
|
16
|
+
[expression] = generateZodFragmentSpecCallExpression(scalarsMapping, schema, lazyFragmentsSet, selection.selection, { ensurePresent: true, optional }, insideLazy);
|
|
17
|
+
if (fieldSpec.spec._type === 'array' ||
|
|
18
|
+
(fieldSpec.spec._type === 'callable' &&
|
|
19
|
+
fieldSpec.spec.returnType._type === 'array')) {
|
|
20
|
+
expression = ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'array'), undefined, [expression]);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (fieldSpec.nullable) {
|
|
24
|
+
expression = invokeMethod(invokeMethod(expression, 'nullable', []), 'optional', []);
|
|
25
|
+
}
|
|
26
|
+
return ts.factory.createPropertyAssignment(selection.alias, expression);
|
|
27
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { objectSelection } from '../../../../../../../../schema/client/fragment.js';
|
|
2
|
+
import { RootSchema } from '../../../../../../../../schema/root.js';
|
|
3
|
+
import { objectSchema } from '../../../../../../../../schema/server.js';
|
|
4
|
+
import ts from 'typescript';
|
|
5
|
+
import { z } from 'zod/v4';
|
|
6
|
+
import { ScalarsMapping } from '../../../../server/scalars/mapping.js';
|
|
7
|
+
import { resolveSelections } from '../shared.js';
|
|
8
|
+
export declare function generateZodObjectFragmentSpecCallExpression(scalarsMapping: ScalarsMapping, schema: RootSchema, lazyFragmentsSet: Set<string>, object: z.infer<typeof objectSchema>, specSelections: z.infer<typeof objectSelection>[], insideLazy: boolean, typenameConfig: Parameters<typeof resolveSelections>[1]): [ts.CallExpression, boolean];
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { resolveSelections } from '../shared.js';
|
|
3
|
+
import { generateZodObjectSelection } from './selection.js';
|
|
4
|
+
export function generateZodObjectFragmentSpecCallExpression(scalarsMapping, schema, lazyFragmentsSet, object, specSelections, insideLazy, typenameConfig) {
|
|
5
|
+
const selections = resolveSelections(specSelections, typenameConfig);
|
|
6
|
+
let needsLazy = false;
|
|
7
|
+
const properties = selections
|
|
8
|
+
.map((s) => {
|
|
9
|
+
const [property, pNeedsLazy] = generateZodObjectSelection(scalarsMapping, schema, lazyFragmentsSet, object, s, insideLazy, typenameConfig);
|
|
10
|
+
if (needsLazy === false && pNeedsLazy) {
|
|
11
|
+
needsLazy = true;
|
|
12
|
+
}
|
|
13
|
+
return property;
|
|
14
|
+
})
|
|
15
|
+
.filter((s) => s !== null);
|
|
16
|
+
let expression = ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'object'), undefined, [ts.factory.createObjectLiteralExpression(properties, true)]);
|
|
17
|
+
if (needsLazy) {
|
|
18
|
+
expression = ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'lazy'), undefined, [
|
|
19
|
+
ts.factory.createArrowFunction(undefined, undefined, [], undefined, ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), expression),
|
|
20
|
+
]);
|
|
21
|
+
}
|
|
22
|
+
return [expression, needsLazy];
|
|
23
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { objectSelection } from '../../../../../../../../schema/client/fragment.js';
|
|
2
|
+
import { RootSchema } from '../../../../../../../../schema/root.js';
|
|
3
|
+
import { objectSchema } from '../../../../../../../../schema/server.js';
|
|
4
|
+
import ts from 'typescript';
|
|
5
|
+
import { z } from 'zod/v4';
|
|
6
|
+
import { ScalarsMapping } from '../../../../server/scalars/mapping.js';
|
|
7
|
+
import { resolveSelections } from '../shared.js';
|
|
8
|
+
export declare function generateZodObjectSelection(scalarsMapping: ScalarsMapping, schema: RootSchema, lazyFragmentsSet: Set<string>, objectType: z.infer<typeof objectSchema>, selection: z.infer<typeof objectSelection>, insideLazy: boolean, typenameConfig: Parameters<typeof resolveSelections>[1]): [ts.PropertyAssignment | ts.SpreadAssignment | null, boolean];
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { generateFieldSelection } from './field_selection.js';
|
|
2
|
+
import { generateSpreadSelection } from './spread_selection.js';
|
|
3
|
+
import { generateTypenameFieldSelection } from './typename_field.js';
|
|
4
|
+
export function generateZodObjectSelection(scalarsMapping, schema, lazyFragmentsSet, objectType, selection, insideLazy, typenameConfig) {
|
|
5
|
+
switch (selection._type) {
|
|
6
|
+
case 'TypenameField': {
|
|
7
|
+
return [
|
|
8
|
+
generateTypenameFieldSelection(typenameConfig, objectType.name, selection),
|
|
9
|
+
false,
|
|
10
|
+
];
|
|
11
|
+
}
|
|
12
|
+
case 'FieldSelection': {
|
|
13
|
+
return [
|
|
14
|
+
generateFieldSelection(scalarsMapping, schema, lazyFragmentsSet, insideLazy, objectType, selection),
|
|
15
|
+
false,
|
|
16
|
+
];
|
|
17
|
+
}
|
|
18
|
+
case 'SpreadSelection': {
|
|
19
|
+
return [
|
|
20
|
+
generateSpreadSelection(selection.fragment, lazyFragmentsSet.has(selection.fragment)),
|
|
21
|
+
true,
|
|
22
|
+
];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { invokeMethod } from '../../../../../../shared.js';
|
|
3
|
+
import { generateSchemaName } from '../../../../server/shared.js';
|
|
4
|
+
function constructObjectWithShape(fragmentName, isLazy) {
|
|
5
|
+
const schemaName = generateSchemaName(fragmentName + 'Fragment');
|
|
6
|
+
const identifier = ts.factory.createIdentifier(schemaName);
|
|
7
|
+
if (!isLazy)
|
|
8
|
+
return identifier;
|
|
9
|
+
return invokeMethod(ts.factory.createPropertyAccessExpression(identifier, 'def'), 'getter', []);
|
|
10
|
+
}
|
|
11
|
+
export function generateSpreadSelection(fragmentName, isLazy) {
|
|
12
|
+
return ts.factory.createSpreadAssignment(ts.factory.createPropertyAccessExpression(constructObjectWithShape(fragmentName, isLazy), ts.factory.createIdentifier('shape')));
|
|
13
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { z } from 'zod/v4';
|
|
2
|
+
import { resolveSelections } from '../shared.js';
|
|
3
|
+
import ts from 'typescript';
|
|
4
|
+
import { typenameSelection } from '../../../../../../../../schema/client/fragment.js';
|
|
5
|
+
export declare function generateTypenameFieldSelection(typenameConfig: Parameters<typeof resolveSelections>[1], objectTypeName: string, selection: z.infer<typeof typenameSelection>): ts.PropertyAssignment | null;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { invokeMethod } from '../../../../../../shared.js';
|
|
2
|
+
import ts from 'typescript';
|
|
3
|
+
export function generateTypenameFieldSelection(typenameConfig, objectTypeName, selection) {
|
|
4
|
+
if ('ignore' in typenameConfig)
|
|
5
|
+
return null;
|
|
6
|
+
let expression = invokeMethod(ts.factory.createIdentifier('z'), 'literal', [
|
|
7
|
+
ts.factory.createStringLiteral(objectTypeName),
|
|
8
|
+
]);
|
|
9
|
+
if (selection.alias === null && typenameConfig.optional) {
|
|
10
|
+
expression = invokeMethod(invokeMethod(expression, 'nullable', []), 'optional', []);
|
|
11
|
+
}
|
|
12
|
+
return ts.factory.createPropertyAssignment(selection.alias || '__typename', expression);
|
|
13
|
+
}
|
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
import { FragmentSpecSchemaType, objectSelection } from '../../../../../schema/client/fragment.js';
|
|
2
|
-
import { RootSchema } from '../../../../../schema/root.js';
|
|
3
|
-
import ts from 'typescript';
|
|
4
1
|
import { z } from 'zod/v4';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
export declare function
|
|
8
|
-
declare function resolveSelections(specSelections: z.infer<typeof objectSelection>[], typenameConfig: {
|
|
2
|
+
import { FragmentSpecSchemaType, objectSelection } from '../../../../../../../schema/client/fragment.js';
|
|
3
|
+
import { RootSchema } from '../../../../../../../schema/root.js';
|
|
4
|
+
export declare function resolveSelections(specSelections: z.infer<typeof objectSelection>[], typenameConfig: {
|
|
9
5
|
ensurePresent: boolean;
|
|
10
6
|
optional: boolean;
|
|
11
7
|
} | {
|
|
@@ -90,6 +86,5 @@ declare function resolveSelections(specSelections: z.infer<typeof objectSelectio
|
|
|
90
86
|
})[];
|
|
91
87
|
} | null;
|
|
92
88
|
})[];
|
|
93
|
-
export declare function
|
|
94
|
-
export declare function
|
|
95
|
-
export {};
|
|
89
|
+
export declare function extractFragmentNamesInSpec(schema: RootSchema, fragmentSpec: FragmentSpecSchemaType): string[];
|
|
90
|
+
export declare function extractFragmentSourceTextsInSpec(schema: RootSchema, fragmentSpec: FragmentSpecSchemaType): string[];
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import assert from 'assert';
|
|
2
|
+
export function resolveSelections(specSelections, typenameConfig) {
|
|
3
|
+
const ignoreTypename = 'ignore' in typenameConfig;
|
|
4
|
+
const selections = specSelections
|
|
5
|
+
.filter((s) => s._type !== 'TypenameField' || !ignoreTypename)
|
|
6
|
+
.toSorted((s1, s2) => s1._type.localeCompare(s2._type));
|
|
7
|
+
if (ignoreTypename)
|
|
8
|
+
return selections;
|
|
9
|
+
const hasTypename = specSelections.some((s) => s._type === 'TypenameField');
|
|
10
|
+
const hasSpreadSelection = specSelections.some((s) => s._type === 'SpreadSelection');
|
|
11
|
+
if (!hasTypename) {
|
|
12
|
+
if (hasSpreadSelection) {
|
|
13
|
+
selections.push({ _type: 'TypenameField', alias: null });
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
selections.unshift({ _type: 'TypenameField', alias: null });
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return selections;
|
|
20
|
+
}
|
|
21
|
+
export function extractFragmentNamesInSpec(schema, fragmentSpec) {
|
|
22
|
+
if (fragmentSpec._type === 'UnionFragmentSpec') {
|
|
23
|
+
return fragmentSpec.selections
|
|
24
|
+
.map((s) => {
|
|
25
|
+
if (s._type === 'SpreadSelection') {
|
|
26
|
+
const fragment = schema.client.fragments[s.fragment];
|
|
27
|
+
return [
|
|
28
|
+
s.fragment,
|
|
29
|
+
...extractFragmentNamesInSpec(schema, fragment.spec),
|
|
30
|
+
];
|
|
31
|
+
}
|
|
32
|
+
if (s._type === 'ObjectConditionalSpreadSelection') {
|
|
33
|
+
return extractFragmentNamesInSpec(schema, s.spec);
|
|
34
|
+
}
|
|
35
|
+
return [];
|
|
36
|
+
})
|
|
37
|
+
.flat();
|
|
38
|
+
}
|
|
39
|
+
return fragmentSpec.selections
|
|
40
|
+
.map((s) => {
|
|
41
|
+
if (s._type === 'SpreadSelection') {
|
|
42
|
+
const fragment = schema.client.fragments[s.fragment];
|
|
43
|
+
assert(fragment.spec !== undefined);
|
|
44
|
+
return [
|
|
45
|
+
s.fragment,
|
|
46
|
+
...extractFragmentNamesInSpec(schema, fragment.spec),
|
|
47
|
+
];
|
|
48
|
+
}
|
|
49
|
+
if (s._type === 'FieldSelection' && s.selection != null) {
|
|
50
|
+
assert(s.selection !== undefined);
|
|
51
|
+
return extractFragmentNamesInSpec(schema, s.selection);
|
|
52
|
+
}
|
|
53
|
+
return [];
|
|
54
|
+
})
|
|
55
|
+
.flat();
|
|
56
|
+
}
|
|
57
|
+
export function extractFragmentSourceTextsInSpec(schema, fragmentSpec) {
|
|
58
|
+
return Array.from(new Set(extractFragmentNamesInSpec(schema, fragmentSpec))).map((f) => schema.client.fragments[f].sourceText);
|
|
59
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { unionFragmentSpec } from '../../../../../../../../schema/client/fragment.js';
|
|
2
|
+
import { RootSchema } from '../../../../../../../../schema/root.js';
|
|
3
|
+
import ts from 'typescript';
|
|
4
|
+
import { z } from 'zod/v4';
|
|
5
|
+
import { ScalarsMapping } from '../../../../server/scalars/mapping.js';
|
|
6
|
+
export declare function generateZodUnionFragmentSpecCallExpression(scalarsMapping: ScalarsMapping, schema: RootSchema, lazyFragmentsSet: Set<string>, spec: z.infer<typeof unionFragmentSpec>, insideLazy: boolean): [ts.CallExpression, boolean];
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { generateZodObjectFragmentSpecCallExpression } from '../object/index.js';
|
|
3
|
+
import { resolveUnionSelections } from './selections.js';
|
|
4
|
+
export function generateZodUnionFragmentSpecCallExpression(scalarsMapping, schema, lazyFragmentsSet, spec, insideLazy) {
|
|
5
|
+
const [objectSelections, typenameSelections] = resolveUnionSelections(schema, spec.selections);
|
|
6
|
+
for (const item of Object.keys(schema.server.unions[spec.name].items)) {
|
|
7
|
+
if (!objectSelections.some((s) => s.object === item)) {
|
|
8
|
+
objectSelections.push({
|
|
9
|
+
_type: 'ObjectConditionalSpreadSelection',
|
|
10
|
+
object: item,
|
|
11
|
+
spec: {
|
|
12
|
+
_type: 'ObjectFragmentSpec',
|
|
13
|
+
name: item,
|
|
14
|
+
selections: [],
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
let expression = ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'union'), undefined, [
|
|
20
|
+
ts.factory.createArrayLiteralExpression(objectSelections.map((s) => generateZodObjectFragmentSpecCallExpression(scalarsMapping, schema, lazyFragmentsSet, schema.server.objects[s.object], [...s.spec.selections, ...typenameSelections], true, { ensurePresent: true, optional: false })[0]), true),
|
|
21
|
+
]);
|
|
22
|
+
if (!insideLazy) {
|
|
23
|
+
expression = ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'lazy'), undefined, [
|
|
24
|
+
ts.factory.createArrowFunction(undefined, undefined, [], undefined, ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), expression),
|
|
25
|
+
]);
|
|
26
|
+
}
|
|
27
|
+
return [expression, !insideLazy];
|
|
28
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { z } from 'zod/v4';
|
|
2
|
+
import { objectConditionalSpreadSelection, typenameSelection, UnionSelection } from '../../../../../../../../schema/client/fragment.js';
|
|
3
|
+
import { RootSchema } from '../../../../../../../../schema/root.js';
|
|
4
|
+
export declare function resolveUnionSelections(schema: RootSchema, specSelections: UnionSelection[]): [
|
|
5
|
+
z.infer<typeof objectConditionalSpreadSelection>[],
|
|
6
|
+
z.infer<typeof typenameSelection>[]
|
|
7
|
+
];
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import assert from 'assert';
|
|
2
|
+
export function resolveUnionSelections(schema, specSelections) {
|
|
3
|
+
const typenameSelections = [];
|
|
4
|
+
const objectSelections = specSelections
|
|
5
|
+
.map((s) => {
|
|
6
|
+
assert(s._type !== 'UnionConditionalSpreadSelection');
|
|
7
|
+
if (s._type === 'TypenameField') {
|
|
8
|
+
typenameSelections.push(s);
|
|
9
|
+
return [];
|
|
10
|
+
}
|
|
11
|
+
if (s._type === 'ObjectConditionalSpreadSelection')
|
|
12
|
+
return [s];
|
|
13
|
+
const fragmentSpec = schema.client.fragments[s.fragment].spec;
|
|
14
|
+
assert(fragmentSpec._type === 'UnionFragmentSpec');
|
|
15
|
+
const [selections, tSelections] = resolveUnionSelections(schema, fragmentSpec.selections);
|
|
16
|
+
typenameSelections.push(...tSelections);
|
|
17
|
+
return selections;
|
|
18
|
+
})
|
|
19
|
+
.flat();
|
|
20
|
+
return [objectSelections, typenameSelections];
|
|
21
|
+
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { generateFragmentTypes } from './fragments.js';
|
|
2
|
-
import { generateOperationsNodes } from './operations.js';
|
|
1
|
+
import { generateFragmentTypes } from './fragments/index.js';
|
|
2
|
+
import { generateOperationsNodes } from './operations/index.js';
|
|
3
3
|
export function generateClientNodes(config, context) {
|
|
4
|
+
const lazyFragmentsSet = new Set();
|
|
4
5
|
return [
|
|
5
|
-
...generateFragmentTypes(config.scalarsMapping, context.schema),
|
|
6
|
-
...generateOperationsNodes(config.clientTypeNameBuilders, config.scalarsMapping, context.schema),
|
|
6
|
+
...generateFragmentTypes(config.scalarsMapping, context.schema, lazyFragmentsSet),
|
|
7
|
+
...generateOperationsNodes(config.clientTypeNameBuilders, config.scalarsMapping, context.schema, lazyFragmentsSet),
|
|
7
8
|
];
|
|
8
9
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ClientTypeNameBuilders } from '../../../../../../actors/ts/shared.js';
|
|
2
|
+
import { operationSchema } from '../../../../../../schema/client/operation.js';
|
|
3
|
+
import { RootSchema } from '../../../../../../schema/root.js';
|
|
4
|
+
import ts from 'typescript';
|
|
5
|
+
import { z } from 'zod/v4';
|
|
6
|
+
import { ScalarsMapping } from '../../server/scalars/mapping.js';
|
|
7
|
+
export declare function opTypeToName(type: z.infer<typeof operationSchema>['type']): string;
|
|
8
|
+
export declare function generateOperationsNodes(clientTypeNameBuilders: ClientTypeNameBuilders, scalarsMapping: ScalarsMapping, schema: RootSchema, lazyFragmentsSet: Set<string>): ts.Node[];
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { generateSchemaName, generateZodInferInterfaceType, } from '../../server/shared.js';
|
|
3
|
+
import { generateOperationZodInputSchema } from './input-schema.js';
|
|
4
|
+
import { generateOperationNode } from './node.js';
|
|
5
|
+
import { generateOperationZodOutputSchema } from './output-schema.js';
|
|
6
|
+
export function opTypeToName(type) {
|
|
7
|
+
switch (type) {
|
|
8
|
+
case 'QUERY':
|
|
9
|
+
return 'Query';
|
|
10
|
+
case 'MUTATION':
|
|
11
|
+
return 'Mutation';
|
|
12
|
+
case 'SUBSCRIPTION':
|
|
13
|
+
return 'Subscription';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function generateOperationNodes(clientTypeNameBuilders, scalarsMapping, lazyFragmentsSet, schema, operation) {
|
|
17
|
+
const variablesName = clientTypeNameBuilders.variablesTypeName(operation.name);
|
|
18
|
+
const resultName = clientTypeNameBuilders.resultTypeName(operation.name);
|
|
19
|
+
return [
|
|
20
|
+
generateOperationZodInputSchema(scalarsMapping, operation, variablesName),
|
|
21
|
+
generateZodInferInterfaceType('input', variablesName, generateSchemaName(variablesName)),
|
|
22
|
+
ts.factory.createIdentifier('\n'),
|
|
23
|
+
generateOperationZodOutputSchema(scalarsMapping, schema, lazyFragmentsSet, operation, resultName),
|
|
24
|
+
generateZodInferInterfaceType('output', resultName, generateSchemaName(resultName)),
|
|
25
|
+
generateOperationNode(clientTypeNameBuilders, schema, operation),
|
|
26
|
+
ts.factory.createIdentifier('\n'),
|
|
27
|
+
];
|
|
28
|
+
}
|
|
29
|
+
export function generateOperationsNodes(clientTypeNameBuilders, scalarsMapping, schema, lazyFragmentsSet) {
|
|
30
|
+
return Object.values(schema.client.operations)
|
|
31
|
+
.map((operation) => generateOperationNodes(clientTypeNameBuilders, scalarsMapping, lazyFragmentsSet, schema, operation))
|
|
32
|
+
.flat();
|
|
33
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { operationSchema } from '../../../../../../schema/client/operation.js';
|
|
2
|
+
import ts from 'typescript';
|
|
3
|
+
import { z } from 'zod/v4';
|
|
4
|
+
import { ScalarsMapping } from '../../server/scalars/mapping.js';
|
|
5
|
+
export declare function generateOperationZodInputSchema(scalarsMapping: ScalarsMapping, operation: z.infer<typeof operationSchema>, variablesName: string): ts.VariableStatement;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { generateInputTypeDefinitionFields } from '../../server/inputs.js';
|
|
3
|
+
import { generateSchemaName } from '../../server/shared.js';
|
|
4
|
+
function parametersToFields(parameters) {
|
|
5
|
+
return Object.fromEntries(Object.keys(parameters).map((name) => [
|
|
6
|
+
name.slice(1),
|
|
7
|
+
parameters[name],
|
|
8
|
+
]));
|
|
9
|
+
}
|
|
10
|
+
export function generateOperationZodInputSchema(scalarsMapping, operation, variablesName) {
|
|
11
|
+
return ts.factory.createVariableStatement([ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
|
|
12
|
+
ts.factory.createVariableDeclaration(ts.factory.createIdentifier(generateSchemaName(variablesName)), undefined, undefined, ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'object'), undefined, [
|
|
13
|
+
ts.factory.createObjectLiteralExpression(generateInputTypeDefinitionFields(scalarsMapping, parametersToFields(operation.parameters), true), true),
|
|
14
|
+
])),
|
|
15
|
+
], ts.NodeFlags.Const));
|
|
16
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ClientTypeNameBuilders } from '../../../../../../actors/ts/shared.js';
|
|
2
|
+
import { operationSchema } from '../../../../../../schema/client/operation.js';
|
|
3
|
+
import { RootSchema } from '../../../../../../schema/root.js';
|
|
4
|
+
import ts from 'typescript';
|
|
5
|
+
import { z } from 'zod/v4';
|
|
6
|
+
export declare function generateOperationNode(clientTypeNameBuilders: ClientTypeNameBuilders, schema: RootSchema, operation: z.infer<typeof operationSchema>): ts.VariableStatement;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { generateSchemaName } from '../../server/shared.js';
|
|
3
|
+
import { extractFragmentSourceTextsInSpec } from '../fragments/spec/shared.js';
|
|
4
|
+
function generateProperties(clientTypeNameBuilders, schema, operation) {
|
|
5
|
+
return [
|
|
6
|
+
ts.factory.createPropertyAssignment('name', ts.factory.createStringLiteral(operation.name)),
|
|
7
|
+
ts.factory.createPropertyAssignment('type', ts.factory.createStringLiteral(operation.type)),
|
|
8
|
+
ts.factory.createPropertyAssignment('document', ts.factory.createStringLiteral([
|
|
9
|
+
operation.sourceText,
|
|
10
|
+
...extractFragmentSourceTextsInSpec(schema, operation.fragmentSpec),
|
|
11
|
+
].join(' '))),
|
|
12
|
+
ts.factory.createPropertyAssignment('variablesSchema', ts.factory.createIdentifier(generateSchemaName(clientTypeNameBuilders.variablesTypeName(operation.name)))),
|
|
13
|
+
ts.factory.createPropertyAssignment('resultSchema', ts.factory.createIdentifier(generateSchemaName(clientTypeNameBuilders.resultTypeName(operation.name)))),
|
|
14
|
+
];
|
|
15
|
+
}
|
|
16
|
+
export function generateOperationNode(clientTypeNameBuilders, schema, operation) {
|
|
17
|
+
return ts.factory.createVariableStatement(ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Export), ts.factory.createVariableDeclarationList([
|
|
18
|
+
ts.factory.createVariableDeclaration(ts.factory.createIdentifier(clientTypeNameBuilders.operationTypeName(operation.name)), undefined, undefined, ts.factory.createAsExpression(ts.factory.createObjectLiteralExpression(generateProperties(clientTypeNameBuilders, schema, operation), true), ts.factory.createTypeReferenceNode('const'))),
|
|
19
|
+
], ts.NodeFlags.Const));
|
|
20
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { operationSchema } from '../../../../../../schema/client/operation.js';
|
|
2
|
+
import { RootSchema } from '../../../../../../schema/root.js';
|
|
3
|
+
import ts from 'typescript';
|
|
4
|
+
import { z } from 'zod/v4';
|
|
5
|
+
import { ScalarsMapping } from '../../server/scalars/mapping.js';
|
|
6
|
+
export declare function generateOperationZodOutputSchema(scalarsMapping: ScalarsMapping, schema: RootSchema, lazyFragmentsSet: Set<string>, operation: z.infer<typeof operationSchema>, resultName: string): ts.VariableStatement;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { generateSchemaName } from '../../server/shared.js';
|
|
3
|
+
import { generateZodFragmentSpecCallExpression } from '../fragments/spec/index.js';
|
|
4
|
+
export function generateOperationZodOutputSchema(scalarsMapping, schema, lazyFragmentsSet, operation, resultName) {
|
|
5
|
+
return ts.factory.createVariableStatement([ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
|
|
6
|
+
ts.factory.createVariableDeclaration(ts.factory.createIdentifier(generateSchemaName(resultName)), undefined, undefined, generateZodFragmentSpecCallExpression(scalarsMapping, schema, lazyFragmentsSet, operation.fragmentSpec, undefined, true)[0]),
|
|
7
|
+
], ts.NodeFlags.Const));
|
|
8
|
+
}
|
package/actors/utils.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import pc from 'picocolors';
|
|
4
|
+
import { RunAction } from '../config.js';
|
|
5
|
+
import { diffLines } from 'diff';
|
|
6
|
+
import { printChanges } from './text-diff.js';
|
|
7
|
+
export function executeRunAction(outPath, action, code) {
|
|
8
|
+
switch (action) {
|
|
9
|
+
case RunAction.Generate: {
|
|
10
|
+
fs.writeFileSync(outPath, code);
|
|
11
|
+
break;
|
|
12
|
+
}
|
|
13
|
+
case RunAction.Validate: {
|
|
14
|
+
const fileCode = fs.readFileSync(outPath).toString();
|
|
15
|
+
if (fileCode !== code) {
|
|
16
|
+
const changes = diffLines(fileCode, code);
|
|
17
|
+
process.stdout.write(pc.blue(`${path.relative(process.cwd(), outPath.toString())}:\n`));
|
|
18
|
+
printChanges(changes);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
package/config.d.ts
CHANGED
|
@@ -2,7 +2,12 @@ import { RootSchema } from './schema/root.js';
|
|
|
2
2
|
export interface ActorContext {
|
|
3
3
|
readonly schema: RootSchema;
|
|
4
4
|
}
|
|
5
|
-
export
|
|
5
|
+
export declare enum RunAction {
|
|
6
|
+
Generate = 0,
|
|
7
|
+
Validate = 1
|
|
8
|
+
}
|
|
9
|
+
export declare function runActionFromArgv(argv: string[]): RunAction;
|
|
10
|
+
export type Actor<T extends ActorContext> = (c: T, action: RunAction) => Promise<void> | void;
|
|
6
11
|
export interface Config<T extends ActorContext> {
|
|
7
12
|
context: T;
|
|
8
13
|
actors: Actor<T>[];
|
package/config.js
CHANGED
|
@@ -1 +1,18 @@
|
|
|
1
|
-
export
|
|
1
|
+
export var RunAction;
|
|
2
|
+
(function (RunAction) {
|
|
3
|
+
RunAction[RunAction["Generate"] = 0] = "Generate";
|
|
4
|
+
RunAction[RunAction["Validate"] = 1] = "Validate";
|
|
5
|
+
})(RunAction || (RunAction = {}));
|
|
6
|
+
export function runActionFromArgv(argv) {
|
|
7
|
+
if (argv.length < 3) {
|
|
8
|
+
throw new Error('argv length is less than 3, action is not provided, valid actions are: "generate" and "validate"');
|
|
9
|
+
}
|
|
10
|
+
switch (argv[2]) {
|
|
11
|
+
case 'validate':
|
|
12
|
+
return RunAction.Validate;
|
|
13
|
+
case 'generate':
|
|
14
|
+
return RunAction.Generate;
|
|
15
|
+
default:
|
|
16
|
+
throw new Error(`Unknown action: ${argv[2]}`);
|
|
17
|
+
}
|
|
18
|
+
}
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { Actor, ActorContext, Config } from './config.js';
|
|
1
|
+
export { type Actor, type ActorContext, type Config, type RunAction, runActionFromArgv, } from './config.js';
|
|
2
2
|
export { run } from './main.js';
|
|
3
3
|
export { loadClientSchemaFromFile, loadServerSchemaFromFile, loadRootSchemaFromGQLSubprocess, } from './schema/utils.js';
|
|
4
4
|
export * as actors from './actors/index.js';
|
package/index.js
CHANGED
package/main.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { ActorContext, Config } from './config.js';
|
|
2
|
-
export declare function run<TContext extends ActorContext>(config: Config<TContext
|
|
1
|
+
import { ActorContext, Config, RunAction } from './config.js';
|
|
2
|
+
export declare function run<TContext extends ActorContext>(config: Config<TContext>, action: RunAction): Promise<void>;
|
package/main.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vladimirdev635/gql-codegen",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.112",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"homepage": "https://github.com/Voldemat/graphql-plus-plus#readme",
|
|
@@ -25,6 +25,10 @@
|
|
|
25
25
|
"test": "vitest",
|
|
26
26
|
"build": "tsc && tsc-alias && cp package.json ./dist/package.json"
|
|
27
27
|
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"diff": "^9.0.0",
|
|
30
|
+
"picocolors": "^1.1.1"
|
|
31
|
+
},
|
|
28
32
|
"devDependencies": {
|
|
29
33
|
"@types/node": "^24.0.1",
|
|
30
34
|
"assert": "^2.1.0",
|
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
import assert from 'assert';
|
|
2
|
-
import ts from 'typescript';
|
|
3
|
-
import { invokeMethod } from '../../../shared.js';
|
|
4
|
-
import { generateZodObjectFieldSpec } from '../server/objects.js';
|
|
5
|
-
import { generateSchemaName, generateZodInferInterfaceType, generateZodInferTypeAlias, } from '../server/shared.js';
|
|
6
|
-
export function extractFragmentNamesInSpec(schema, fragmentSpec) {
|
|
7
|
-
if (fragmentSpec._type === 'UnionFragmentSpec') {
|
|
8
|
-
return fragmentSpec.selections
|
|
9
|
-
.map((s) => {
|
|
10
|
-
if (s._type === 'SpreadSelection') {
|
|
11
|
-
const fragment = schema.client.fragments[s.fragment];
|
|
12
|
-
return [
|
|
13
|
-
s.fragment,
|
|
14
|
-
...extractFragmentNamesInSpec(schema, fragment.spec),
|
|
15
|
-
];
|
|
16
|
-
}
|
|
17
|
-
if (s._type === 'ObjectConditionalSpreadSelection') {
|
|
18
|
-
return extractFragmentNamesInSpec(schema, s.spec);
|
|
19
|
-
}
|
|
20
|
-
return [];
|
|
21
|
-
})
|
|
22
|
-
.flat();
|
|
23
|
-
}
|
|
24
|
-
return fragmentSpec.selections
|
|
25
|
-
.map((s) => {
|
|
26
|
-
if (s._type === 'SpreadSelection') {
|
|
27
|
-
const fragment = schema.client.fragments[s.fragment];
|
|
28
|
-
assert(fragment.spec !== undefined);
|
|
29
|
-
return [
|
|
30
|
-
s.fragment,
|
|
31
|
-
...extractFragmentNamesInSpec(schema, fragment.spec),
|
|
32
|
-
];
|
|
33
|
-
}
|
|
34
|
-
if (s._type === 'FieldSelection' && s.selection != null) {
|
|
35
|
-
assert(s.selection !== undefined);
|
|
36
|
-
return extractFragmentNamesInSpec(schema, s.selection);
|
|
37
|
-
}
|
|
38
|
-
return [];
|
|
39
|
-
})
|
|
40
|
-
.flat();
|
|
41
|
-
}
|
|
42
|
-
export function extractFragmentSourceTextsInSpec(schema, fragmentSpec) {
|
|
43
|
-
return Array.from(new Set(extractFragmentNamesInSpec(schema, fragmentSpec))).map((f) => schema.client.fragments[f].sourceText);
|
|
44
|
-
}
|
|
45
|
-
function generateFragmentDocumentNode(schema, name, fragment) {
|
|
46
|
-
return ts.factory.createVariableStatement([ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
|
|
47
|
-
ts.factory.createVariableDeclaration(ts.factory.createIdentifier(name + 'FragmentDocument'), undefined, undefined, ts.factory.createStringLiteral([
|
|
48
|
-
fragment.sourceText,
|
|
49
|
-
...extractFragmentSourceTextsInSpec(schema, fragment.spec),
|
|
50
|
-
].join(' '))),
|
|
51
|
-
], ts.NodeFlags.Const));
|
|
52
|
-
}
|
|
53
|
-
function generateZodObjectSelection(scalarsMapping, schema, objectType, selection, insideLazy, typenameConfig) {
|
|
54
|
-
switch (selection._type) {
|
|
55
|
-
case 'TypenameField': {
|
|
56
|
-
if ('ignore' in typenameConfig)
|
|
57
|
-
return null;
|
|
58
|
-
let expression = invokeMethod(ts.factory.createIdentifier('z'), 'literal', [ts.factory.createStringLiteral(objectType.name)]);
|
|
59
|
-
if (selection.alias === null && typenameConfig.optional) {
|
|
60
|
-
expression = invokeMethod(invokeMethod(expression, 'nullable', []), 'optional', []);
|
|
61
|
-
}
|
|
62
|
-
return ts.factory.createPropertyAssignment(selection.alias || '__typename', expression);
|
|
63
|
-
}
|
|
64
|
-
case 'FieldSelection': {
|
|
65
|
-
const fieldSpec = objectType.fields[selection.name];
|
|
66
|
-
let expression;
|
|
67
|
-
if (selection.selection == null) {
|
|
68
|
-
expression = generateZodObjectFieldSpec(scalarsMapping, fieldSpec);
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
const optional = fieldSpec.spec._type !== 'callable' ||
|
|
72
|
-
!selection.selection.selections.every((s) => s._type === 'TypenameField') ||
|
|
73
|
-
!fieldSpec.nullable;
|
|
74
|
-
// eslint-disable-next-line no-use-before-define
|
|
75
|
-
expression = generateZodFragmentSpecCallExpression(scalarsMapping, schema, selection.selection, { ensurePresent: true, optional }, insideLazy);
|
|
76
|
-
if (fieldSpec.spec._type === 'array' ||
|
|
77
|
-
(fieldSpec.spec._type === 'callable' &&
|
|
78
|
-
fieldSpec.spec.returnType._type === 'array')) {
|
|
79
|
-
expression = ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'array'), undefined, [expression]);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
if (fieldSpec.nullable) {
|
|
83
|
-
expression = invokeMethod(invokeMethod(expression, 'nullable', []), 'optional', []);
|
|
84
|
-
}
|
|
85
|
-
return ts.factory.createPropertyAssignment(selection.alias, expression);
|
|
86
|
-
}
|
|
87
|
-
case 'SpreadSelection': {
|
|
88
|
-
const schemaName = generateSchemaName(selection.fragment + 'Fragment');
|
|
89
|
-
const expressionForShape = insideLazy
|
|
90
|
-
? ts.factory.createIdentifier(schemaName)
|
|
91
|
-
: ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createPropertyAccessExpression(ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), ts.factory.createIdentifier('lazy')), undefined, [
|
|
92
|
-
ts.factory.createArrowFunction(undefined, undefined, [], undefined, ts.factory.createToken(ts.SyntaxKind
|
|
93
|
-
.EqualsGreaterThanToken), ts.factory.createIdentifier(schemaName)),
|
|
94
|
-
]), ts.factory.createIdentifier('def')), ts.factory.createIdentifier('getter')), undefined, []);
|
|
95
|
-
return ts.factory.createSpreadAssignment(ts.factory.createPropertyAccessExpression(expressionForShape, ts.factory.createIdentifier('shape')));
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
function resolveSelections(specSelections, typenameConfig) {
|
|
100
|
-
const ignoreTypename = 'ignore' in typenameConfig;
|
|
101
|
-
const selections = specSelections
|
|
102
|
-
.filter((s) => s._type !== 'TypenameField' || !ignoreTypename)
|
|
103
|
-
.toSorted((s1, s2) => s1._type.localeCompare(s2._type));
|
|
104
|
-
if (ignoreTypename)
|
|
105
|
-
return selections;
|
|
106
|
-
const hasTypename = specSelections.some((s) => s._type === 'TypenameField');
|
|
107
|
-
const hasSpreadSelection = specSelections.some((s) => s._type === 'SpreadSelection');
|
|
108
|
-
if (!hasTypename) {
|
|
109
|
-
if (hasSpreadSelection) {
|
|
110
|
-
selections.push({ _type: 'TypenameField', alias: null });
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
selections.unshift({ _type: 'TypenameField', alias: null });
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
return selections;
|
|
117
|
-
}
|
|
118
|
-
function generateZodObjectFragmentSpecCallExpression(scalarsMapping, schema, object, specSelections, insideLazy, typenameConfig) {
|
|
119
|
-
const selections = resolveSelections(specSelections, typenameConfig);
|
|
120
|
-
const expression = ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'object'), undefined, [
|
|
121
|
-
ts.factory.createObjectLiteralExpression(selections
|
|
122
|
-
.map((s) => generateZodObjectSelection(scalarsMapping, schema, object, s, insideLazy, typenameConfig))
|
|
123
|
-
.filter((s) => s !== null), true),
|
|
124
|
-
]);
|
|
125
|
-
return expression;
|
|
126
|
-
}
|
|
127
|
-
function resolveUnionSelections(schema, specSelections) {
|
|
128
|
-
const typenameSelections = [];
|
|
129
|
-
const objectSelections = specSelections
|
|
130
|
-
.map((s) => {
|
|
131
|
-
assert(s._type !== 'UnionConditionalSpreadSelection');
|
|
132
|
-
if (s._type === 'TypenameField') {
|
|
133
|
-
typenameSelections.push(s);
|
|
134
|
-
return [];
|
|
135
|
-
}
|
|
136
|
-
if (s._type === 'ObjectConditionalSpreadSelection')
|
|
137
|
-
return [s];
|
|
138
|
-
const fragmentSpec = schema.client.fragments[s.fragment].spec;
|
|
139
|
-
assert(fragmentSpec._type === 'UnionFragmentSpec');
|
|
140
|
-
const [selections, tSelections] = resolveUnionSelections(schema, fragmentSpec.selections);
|
|
141
|
-
typenameSelections.push(...tSelections);
|
|
142
|
-
return selections;
|
|
143
|
-
})
|
|
144
|
-
.flat();
|
|
145
|
-
return [objectSelections, typenameSelections];
|
|
146
|
-
}
|
|
147
|
-
function generateZodUnionFragmentSpecCallExpression(scalarsMapping, schema, spec, insideLazy) {
|
|
148
|
-
const [objectSelections, typenameSelections] = resolveUnionSelections(schema, spec.selections);
|
|
149
|
-
for (const item of Object.keys(schema.server.unions[spec.name].items)) {
|
|
150
|
-
if (!objectSelections.some((s) => s.object === item)) {
|
|
151
|
-
objectSelections.push({
|
|
152
|
-
_type: 'ObjectConditionalSpreadSelection',
|
|
153
|
-
object: item,
|
|
154
|
-
spec: {
|
|
155
|
-
_type: 'ObjectFragmentSpec',
|
|
156
|
-
name: item,
|
|
157
|
-
selections: [],
|
|
158
|
-
},
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
const expression = ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'union'), undefined, [
|
|
163
|
-
ts.factory.createArrayLiteralExpression(objectSelections.map((s) => generateZodObjectFragmentSpecCallExpression(scalarsMapping, schema, schema.server.objects[s.object], [...s.spec.selections, ...typenameSelections], true, { ensurePresent: true, optional: false })), true),
|
|
164
|
-
]);
|
|
165
|
-
if (insideLazy)
|
|
166
|
-
return expression;
|
|
167
|
-
return ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'lazy'), undefined, [
|
|
168
|
-
ts.factory.createArrowFunction(undefined, undefined, [], undefined, ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), expression),
|
|
169
|
-
]);
|
|
170
|
-
}
|
|
171
|
-
export function generateZodFragmentSpecCallExpression(scalarsMapping, schema, spec, typenameConfig, insideLazy = false) {
|
|
172
|
-
if (spec._type === 'ObjectFragmentSpec') {
|
|
173
|
-
return generateZodObjectFragmentSpecCallExpression(scalarsMapping, schema, schema.server.objects[spec.name], spec.selections, insideLazy, typenameConfig || { ensurePresent: true, optional: true });
|
|
174
|
-
}
|
|
175
|
-
return generateZodUnionFragmentSpecCallExpression(scalarsMapping, schema, spec, insideLazy);
|
|
176
|
-
}
|
|
177
|
-
function generateZodFragmentSchema(scalarsMapping, schema, fragmentName, fragment) {
|
|
178
|
-
return ts.factory.createVariableStatement([ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
|
|
179
|
-
ts.factory.createVariableDeclaration(ts.factory.createIdentifier(generateSchemaName(fragmentName + 'Fragment')), undefined, undefined, generateZodFragmentSpecCallExpression(scalarsMapping, schema, fragment.spec)),
|
|
180
|
-
], ts.NodeFlags.Const));
|
|
181
|
-
}
|
|
182
|
-
function generateFragmentSpecDeclarations(scalarsMapping, schema, fragmentName, fragment) {
|
|
183
|
-
const fName = fragmentName + 'Fragment';
|
|
184
|
-
return [
|
|
185
|
-
generateFragmentDocumentNode(schema, fragmentName, fragment),
|
|
186
|
-
generateZodFragmentSchema(scalarsMapping, schema, fragmentName, fragment),
|
|
187
|
-
fragment.spec._type === 'ObjectFragmentSpec'
|
|
188
|
-
? generateZodInferInterfaceType('output', fName, generateSchemaName(fName))
|
|
189
|
-
: generateZodInferTypeAlias('output', fName, generateSchemaName(fName)),
|
|
190
|
-
];
|
|
191
|
-
}
|
|
192
|
-
export function generateFragmentTypes(scalarsMapping, schema) {
|
|
193
|
-
return Object.entries(schema.client.fragments)
|
|
194
|
-
.map(([name, fragment]) => {
|
|
195
|
-
return generateFragmentSpecDeclarations(scalarsMapping, schema, name, fragment);
|
|
196
|
-
})
|
|
197
|
-
.flat();
|
|
198
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { ClientTypeNameBuilders } from '../../../../../actors/ts/shared.js';
|
|
2
|
-
import { operationSchema } from '../../../../../schema/client/operation.js';
|
|
3
|
-
import { RootSchema } from '../../../../../schema/root.js';
|
|
4
|
-
import ts from 'typescript';
|
|
5
|
-
import { z } from 'zod/v4';
|
|
6
|
-
import { ScalarsMapping } from '../server/scalars/mapping.js';
|
|
7
|
-
export declare function opTypeToName(type: z.infer<typeof operationSchema>['type']): string;
|
|
8
|
-
export declare function generateOperationsNodes(clientTypeNameBuilders: ClientTypeNameBuilders, scalarsMapping: ScalarsMapping, schema: RootSchema): ts.Node[];
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import ts from 'typescript';
|
|
2
|
-
import { generateInputTypeDefinitionFields } from '../server/inputs.js';
|
|
3
|
-
import { generateSchemaName, generateZodInferInterfaceType, } from '../server/shared.js';
|
|
4
|
-
import { extractFragmentSourceTextsInSpec, generateZodFragmentSpecCallExpression, } from './fragments.js';
|
|
5
|
-
export function opTypeToName(type) {
|
|
6
|
-
switch (type) {
|
|
7
|
-
case 'QUERY':
|
|
8
|
-
return 'Query';
|
|
9
|
-
case 'MUTATION':
|
|
10
|
-
return 'Mutation';
|
|
11
|
-
case 'SUBSCRIPTION':
|
|
12
|
-
return 'Subscription';
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
function parametersToFields(parameters) {
|
|
16
|
-
return Object.fromEntries(Object.keys(parameters).map((name) => [
|
|
17
|
-
name.slice(1),
|
|
18
|
-
parameters[name],
|
|
19
|
-
]));
|
|
20
|
-
}
|
|
21
|
-
function generateOperationNode(clientTypeNameBuilders, schema, operation) {
|
|
22
|
-
return ts.factory.createVariableStatement(ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Export), ts.factory.createVariableDeclarationList([
|
|
23
|
-
ts.factory.createVariableDeclaration(ts.factory.createIdentifier(clientTypeNameBuilders.operationTypeName(operation.name)), undefined, undefined, ts.factory.createAsExpression(ts.factory.createObjectLiteralExpression([
|
|
24
|
-
ts.factory.createPropertyAssignment('name', ts.factory.createStringLiteral(operation.name)),
|
|
25
|
-
ts.factory.createPropertyAssignment('type', ts.factory.createStringLiteral(operation.type)),
|
|
26
|
-
ts.factory.createPropertyAssignment('document', ts.factory.createStringLiteral([
|
|
27
|
-
operation.sourceText,
|
|
28
|
-
...extractFragmentSourceTextsInSpec(schema, operation.fragmentSpec),
|
|
29
|
-
].join(' '))),
|
|
30
|
-
ts.factory.createPropertyAssignment('variablesSchema', ts.factory.createIdentifier(generateSchemaName(clientTypeNameBuilders.variablesTypeName(operation.name)))),
|
|
31
|
-
ts.factory.createPropertyAssignment('resultSchema', ts.factory.createIdentifier(generateSchemaName(clientTypeNameBuilders.resultTypeName(operation.name)))),
|
|
32
|
-
], true), ts.factory.createTypeReferenceNode('const'))),
|
|
33
|
-
], ts.NodeFlags.Const));
|
|
34
|
-
}
|
|
35
|
-
function generateOperationZodInputSchema(scalarsMapping, operation, variablesName) {
|
|
36
|
-
return ts.factory.createVariableStatement([ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
|
|
37
|
-
ts.factory.createVariableDeclaration(ts.factory.createIdentifier(generateSchemaName(variablesName)), undefined, undefined, ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'object'), undefined, [
|
|
38
|
-
ts.factory.createObjectLiteralExpression(generateInputTypeDefinitionFields(scalarsMapping, parametersToFields(operation.parameters), true), true),
|
|
39
|
-
])),
|
|
40
|
-
], ts.NodeFlags.Const));
|
|
41
|
-
}
|
|
42
|
-
function genearteOperationZodOutputSchema(scalarsMapping, schema, operation, resultName) {
|
|
43
|
-
return ts.factory.createVariableStatement([ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
|
|
44
|
-
ts.factory.createVariableDeclaration(ts.factory.createIdentifier(generateSchemaName(resultName)), undefined, undefined, generateZodFragmentSpecCallExpression(scalarsMapping, schema, operation.fragmentSpec, undefined, true)),
|
|
45
|
-
], ts.NodeFlags.Const));
|
|
46
|
-
}
|
|
47
|
-
function generateOperationNodes(clientTypeNameBuilders, scalarsMapping, schema, operation) {
|
|
48
|
-
const variablesName = clientTypeNameBuilders.variablesTypeName(operation.name);
|
|
49
|
-
const resultName = clientTypeNameBuilders.resultTypeName(operation.name);
|
|
50
|
-
return [
|
|
51
|
-
generateOperationZodInputSchema(scalarsMapping, operation, variablesName),
|
|
52
|
-
generateZodInferInterfaceType('input', variablesName, generateSchemaName(variablesName)),
|
|
53
|
-
ts.factory.createIdentifier('\n'),
|
|
54
|
-
genearteOperationZodOutputSchema(scalarsMapping, schema, operation, resultName),
|
|
55
|
-
generateZodInferInterfaceType('output', resultName, generateSchemaName(resultName)),
|
|
56
|
-
generateOperationNode(clientTypeNameBuilders, schema, operation),
|
|
57
|
-
ts.factory.createIdentifier('\n'),
|
|
58
|
-
];
|
|
59
|
-
}
|
|
60
|
-
export function generateOperationsNodes(clientTypeNameBuilders, scalarsMapping, schema) {
|
|
61
|
-
return Object.values(schema.client.operations)
|
|
62
|
-
.map((operation) => {
|
|
63
|
-
return generateOperationNodes(clientTypeNameBuilders, scalarsMapping, schema, operation);
|
|
64
|
-
})
|
|
65
|
-
.flat();
|
|
66
|
-
}
|