@vladimirdev635/gql-codegen 0.0.97 → 0.0.98
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/ts/formatters/eslint/index.d.ts +1 -1
- package/actors/ts/formatters/index.d.ts +1 -0
- package/actors/ts/formatters/index.js +1 -0
- package/actors/ts/formatters/oxfmt/formatter.d.ts +3 -0
- package/actors/ts/formatters/oxfmt/formatter.js +20 -0
- package/actors/ts/formatters/oxfmt/index.d.ts +2 -0
- package/actors/ts/formatters/oxfmt/index.js +1 -0
- package/actors/ts/formatters/oxfmt/types.d.ts +20 -0
- package/actors/ts/formatters/oxfmt/types.js +1 -0
- package/actors/ts/gql-client/actor.js +1 -1
- package/actors/ts/gql-client/generators/main.js +25 -21
- package/actors/ts/gql-client/index.d.ts +1 -1
- package/actors/ts/gql-client/index.js +1 -1
- package/actors/ts/gql-client-react/actor.js +1 -1
- package/actors/ts/gql-client-react/generators/main.js +20 -14
- package/actors/ts/gql-client-react/index.d.ts +1 -1
- package/actors/ts/gql-client-react/index.js +1 -1
- package/actors/ts/graphql/actor.js +1 -1
- package/actors/ts/graphql/generators/client/fragments.js +53 -31
- package/actors/ts/graphql/generators/client/main.js +5 -5
- package/actors/ts/graphql/generators/client/operations.js +29 -13
- package/actors/ts/graphql/generators/main.js +2 -2
- package/actors/ts/graphql/generators/server/enums.js +1 -1
- package/actors/ts/graphql/generators/server/inputs.js +11 -8
- package/actors/ts/graphql/generators/server/main.js +13 -10
- package/actors/ts/graphql/generators/server/objects.js +15 -11
- package/actors/ts/graphql/generators/server/scalars/additional.js +23 -5
- package/actors/ts/graphql/generators/server/scalars/index.d.ts +1 -1
- package/actors/ts/graphql/generators/server/scalars/index.js +1 -1
- package/actors/ts/graphql/generators/server/scalars/mapping.js +1 -1
- package/actors/ts/graphql/generators/server/shared.js +6 -4
- package/actors/ts/graphql/generators/server/unions.js +5 -5
- package/actors/ts/graphql/generators/shared.js +6 -6
- package/actors/ts/graphql/index.d.ts +1 -1
- package/actors/ts/graphql/index.js +1 -1
- package/actors/ts/utils.js +3 -3
- package/config.d.ts +0 -2
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/package.json +23 -21
- package/schema/client/argument.d.ts +2 -2
- package/schema/client/argument.js +3 -3
- package/schema/client/directive.d.ts +2 -2
- package/schema/client/directive.js +1 -1
- package/schema/client/fragment.d.ts +5 -5
- package/schema/client/fragment.js +10 -10
- package/schema/client/operation.d.ts +3 -3
- package/schema/client/root.d.ts +6 -6
- package/schema/client/root.js +1 -1
- package/schema/server.d.ts +36 -36
- package/schema/server.js +13 -13
- package/schema/shared.d.ts +6 -6
- package/schema/shared.js +7 -7
- package/schema/test-client.json +52031 -1
- package/schema/test-server.json +38289 -1
- package/schema/utils.js +78 -67
- package/utils.js +1 -2
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export type { LintResult, ESLintClass, ESLintClassConstructor, LoadESLintFuncType } from './types.js';
|
|
1
|
+
export type { LintResult, ESLintClass, ESLintClassConstructor, LoadESLintFuncType, } from './types.js';
|
|
2
2
|
export { buildESLintFormatter } from './formatter.js';
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export async function buildOxfmtFormatter(formatFunc, options) {
|
|
2
|
+
return async (code) => {
|
|
3
|
+
const result = await formatFunc('[buffer].ts', code, options);
|
|
4
|
+
for (const error of result.errors) {
|
|
5
|
+
if (error.codeframe !== null) {
|
|
6
|
+
console.error(error.codeframe);
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
console.error(error.message);
|
|
10
|
+
if (error.helpMessage !== null) {
|
|
11
|
+
console.error(error.message);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
if (result.errors.length > 0) {
|
|
16
|
+
throw new Error('Oxfmt failed to format code');
|
|
17
|
+
}
|
|
18
|
+
return result.code;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { buildOxfmtFormatter } from './formatter.js';
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface ErrorLabel {
|
|
2
|
+
message: string | null;
|
|
3
|
+
start: number;
|
|
4
|
+
end: number;
|
|
5
|
+
}
|
|
6
|
+
export interface OxcError {
|
|
7
|
+
severity: Severity;
|
|
8
|
+
message: string;
|
|
9
|
+
labels: Array<ErrorLabel>;
|
|
10
|
+
helpMessage: string | null;
|
|
11
|
+
codeframe: string | null;
|
|
12
|
+
}
|
|
13
|
+
export type Severity = 'Error' | 'Warning' | 'Advice';
|
|
14
|
+
export interface FormatResult {
|
|
15
|
+
/** The formatted code. */
|
|
16
|
+
code: string;
|
|
17
|
+
/** Parse and format errors. */
|
|
18
|
+
errors: Array<OxcError>;
|
|
19
|
+
}
|
|
20
|
+
export type FormatFunc<T> = (filename: string, code: string, options: T) => Promise<FormatResult>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -3,38 +3,38 @@ function generateFunctionBlock(operationName, operationType, returnType) {
|
|
|
3
3
|
const callArgs = [
|
|
4
4
|
ts.factory.createIdentifier(operationName),
|
|
5
5
|
ts.factory.createIdentifier('variables'),
|
|
6
|
-
ts.factory.createIdentifier('requestContext')
|
|
6
|
+
ts.factory.createIdentifier('requestContext'),
|
|
7
7
|
];
|
|
8
8
|
if (operationType === 'SUBSCRIPTION') {
|
|
9
9
|
callArgs.push(ts.factory.createIdentifier('controller'));
|
|
10
10
|
}
|
|
11
|
-
const awaitExpression = ts.factory.createAwaitExpression(ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('executor'), operationType === 'SUBSCRIPTION'
|
|
12
|
-
'executeSubscription'
|
|
13
|
-
'executeSync'), undefined, callArgs));
|
|
11
|
+
const awaitExpression = ts.factory.createAwaitExpression(ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('executor'), operationType === 'SUBSCRIPTION'
|
|
12
|
+
? 'executeSubscription'
|
|
13
|
+
: 'executeSync'), undefined, callArgs));
|
|
14
14
|
if (returnType === 'ExecuteResult') {
|
|
15
|
-
return ts.factory.createBlock([
|
|
16
|
-
ts.factory.createReturnStatement(awaitExpression)
|
|
17
|
-
], true);
|
|
15
|
+
return ts.factory.createBlock([ts.factory.createReturnStatement(awaitExpression)], true);
|
|
18
16
|
}
|
|
19
17
|
return ts.factory.createBlock([
|
|
20
18
|
ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
21
19
|
ts.factory.createVariableDeclaration('executorResult', undefined, undefined, awaitExpression),
|
|
22
20
|
], ts.NodeFlags.Const)),
|
|
23
|
-
ts.factory.createReturnStatement(ts.factory.createPropertyAccessChain(ts.factory.createIdentifier('executorResult'), undefined, 'result'))
|
|
21
|
+
ts.factory.createReturnStatement(ts.factory.createPropertyAccessChain(ts.factory.createIdentifier('executorResult'), undefined, 'result')),
|
|
24
22
|
], true);
|
|
25
23
|
}
|
|
26
24
|
function createReturnTypeNode(resultName, returnType, operationType) {
|
|
27
25
|
const rOpType = ts.factory.createTypeReferenceNode(resultName);
|
|
28
|
-
const rType = operationType === 'SUBSCRIPTION'
|
|
29
|
-
ts.factory.createTypeReferenceNode('SubOpAsyncIterable', [
|
|
30
|
-
|
|
26
|
+
const rType = operationType === 'SUBSCRIPTION'
|
|
27
|
+
? ts.factory.createTypeReferenceNode('SubOpAsyncIterable', [
|
|
28
|
+
rOpType,
|
|
29
|
+
])
|
|
30
|
+
: rOpType;
|
|
31
31
|
if (returnType === 'ExecuteResult.result')
|
|
32
32
|
return rType;
|
|
33
33
|
return ts.factory.createTypeReferenceNode('ExecuteResult', [rType]);
|
|
34
34
|
}
|
|
35
35
|
function getReturnTypeFromConfig(config, operationName) {
|
|
36
|
-
return config.sdk.operationReturnTypeMapping[operationName] ||
|
|
37
|
-
config.sdk.defaultOperationReturnType;
|
|
36
|
+
return (config.sdk.operationReturnTypeMapping[operationName] ||
|
|
37
|
+
config.sdk.defaultOperationReturnType);
|
|
38
38
|
}
|
|
39
39
|
export function generateNodes(config, context) {
|
|
40
40
|
const graphqlImports = [];
|
|
@@ -58,7 +58,7 @@ export function generateNodes(config, context) {
|
|
|
58
58
|
funcArgs.push(ts.factory.createParameterDeclaration(undefined, undefined, 'controller', undefined, ts.factory.createTypeReferenceNode('AbortController')));
|
|
59
59
|
}
|
|
60
60
|
const propAssignment = ts.factory.createPropertyAssignment(operation.name, ts.factory.createArrowFunction(ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Async), undefined, funcArgs, ts.factory.createTypeReferenceNode('Promise', [
|
|
61
|
-
createReturnTypeNode(resultName, returnType, operation.type)
|
|
61
|
+
createReturnTypeNode(resultName, returnType, operation.type),
|
|
62
62
|
]), ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), generateFunctionBlock(operationName, operation.type, returnType)));
|
|
63
63
|
switch (operation.type) {
|
|
64
64
|
case 'QUERY': {
|
|
@@ -77,7 +77,7 @@ export function generateNodes(config, context) {
|
|
|
77
77
|
}
|
|
78
78
|
const gqlClientImports = [
|
|
79
79
|
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('IExecutor')),
|
|
80
|
-
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('RequestContext'))
|
|
80
|
+
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('RequestContext')),
|
|
81
81
|
];
|
|
82
82
|
if (shouldIncludeExecuteResultType) {
|
|
83
83
|
gqlClientImports.push(ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('ExecuteResult')));
|
|
@@ -97,12 +97,16 @@ export function generateNodes(config, context) {
|
|
|
97
97
|
ts.factory.createIdentifier('// @ts-nocheck'),
|
|
98
98
|
...config.importDeclarations,
|
|
99
99
|
ts.factory.createImportDeclaration([], ts.factory.createImportClause(true, undefined, ts.factory.createNamedImports(gqlClientImports)), ts.factory.createStringLiteral('@vladimirdev635/gql-client/types')),
|
|
100
|
-
ts.factory.createImportDeclaration(undefined, ts.factory.createImportClause(false, undefined, ts.factory.createNamedImports(graphqlImports.map(i => ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier(i))))), ts.factory.createStringLiteral(config.graphqlModulePath)),
|
|
100
|
+
ts.factory.createImportDeclaration(undefined, ts.factory.createImportClause(false, undefined, ts.factory.createNamedImports(graphqlImports.map((i) => ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier(i))))), ts.factory.createStringLiteral(config.graphqlModulePath)),
|
|
101
101
|
ts.factory.createIdentifier('\n'),
|
|
102
|
-
ts.factory.createFunctionDeclaration(ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Export), undefined, 'createSdk', [
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
ts.factory.
|
|
106
|
-
|
|
102
|
+
ts.factory.createFunctionDeclaration(ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Export), undefined, 'createSdk', [
|
|
103
|
+
ts.factory.createTypeParameterDeclaration(undefined, 'TRequestContext', ts.factory.createTypeReferenceNode('RequestContext')),
|
|
104
|
+
], [
|
|
105
|
+
ts.factory.createParameterDeclaration(undefined, undefined, 'executor', undefined, ts.factory.createTypeReferenceNode('IExecutor', [
|
|
106
|
+
ts.factory.createTypeReferenceNode('TRequestContext'),
|
|
107
|
+
])),
|
|
108
|
+
], undefined, ts.factory.createBlock([
|
|
109
|
+
ts.factory.createReturnStatement(ts.factory.createAsExpression(ts.factory.createObjectLiteralExpression(returnObjectNodes, true), ts.factory.createTypeReferenceNode('const'))),
|
|
110
|
+
], true)),
|
|
107
111
|
];
|
|
108
112
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { type OperationReturnType, type SDKConfig, type GQLClientActorConfig, buildGQLClientActor } from './actor.js';
|
|
1
|
+
export { type OperationReturnType, type SDKConfig, type GQLClientActorConfig, buildGQLClientActor, } from './actor.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { buildGQLClientActor } from './actor.js';
|
|
1
|
+
export { buildGQLClientActor, } from './actor.js';
|
|
@@ -7,5 +7,5 @@ async function gqlClientReactActor(config, context) {
|
|
|
7
7
|
writeFileSync(config.outPath, code);
|
|
8
8
|
}
|
|
9
9
|
export function buildGQLClientReactActor(config) {
|
|
10
|
-
return context => gqlClientReactActor(config, context);
|
|
10
|
+
return (context) => gqlClientReactActor(config, context);
|
|
11
11
|
}
|
|
@@ -6,19 +6,19 @@ function generateFunctionBlock(operationName, type) {
|
|
|
6
6
|
ts.factory.createIdentifier('executor'),
|
|
7
7
|
ts.factory.createIdentifier(operationName),
|
|
8
8
|
ts.factory.createIdentifier('variables'),
|
|
9
|
-
ts.factory.createIdentifier('requestContext')
|
|
9
|
+
ts.factory.createIdentifier('requestContext'),
|
|
10
10
|
]);
|
|
11
11
|
case 'LAZY':
|
|
12
12
|
return ts.factory.createCallExpression(ts.factory.createIdentifier('useLazyOperation'), undefined, [
|
|
13
13
|
ts.factory.createIdentifier('executor'),
|
|
14
|
-
ts.factory.createIdentifier(operationName)
|
|
14
|
+
ts.factory.createIdentifier(operationName),
|
|
15
15
|
]);
|
|
16
16
|
case 'SUBSCRIPTION':
|
|
17
17
|
return ts.factory.createCallExpression(ts.factory.createIdentifier('useSubscription'), undefined, [
|
|
18
18
|
ts.factory.createIdentifier('executor'),
|
|
19
19
|
ts.factory.createIdentifier(operationName),
|
|
20
20
|
ts.factory.createIdentifier('variables'),
|
|
21
|
-
ts.factory.createIdentifier('requestContext')
|
|
21
|
+
ts.factory.createIdentifier('requestContext'),
|
|
22
22
|
]);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
@@ -30,19 +30,21 @@ function generateArrowFunction(operationName, variablesName, resultName, type) {
|
|
|
30
30
|
resultType = ts.factory.createTypeReferenceNode('UseLazyOperationReturnType', [
|
|
31
31
|
ts.factory.createTypeReferenceNode(variablesName),
|
|
32
32
|
ts.factory.createTypeReferenceNode(resultName),
|
|
33
|
-
ts.factory.createTypeReferenceNode('TRequestContext')
|
|
33
|
+
ts.factory.createTypeReferenceNode('TRequestContext'),
|
|
34
34
|
]);
|
|
35
35
|
break;
|
|
36
36
|
case 'SYNC': {
|
|
37
37
|
resultType = ts.factory.createTypeReferenceNode('OperationState', [
|
|
38
|
-
ts.factory.createTypeReferenceNode(resultName)
|
|
38
|
+
ts.factory.createTypeReferenceNode(resultName),
|
|
39
39
|
]);
|
|
40
40
|
parameters.push(ts.factory.createParameterDeclaration(undefined, undefined, 'variables', undefined, ts.factory.createTypeReferenceNode(variablesName)), ts.factory.createParameterDeclaration(undefined, undefined, 'requestContext', undefined, ts.factory.createTypeReferenceNode('TRequestContext')));
|
|
41
41
|
break;
|
|
42
42
|
}
|
|
43
43
|
case 'SUBSCRIPTION': {
|
|
44
44
|
resultType = ts.factory.createTypeReferenceNode('OperationState', [
|
|
45
|
-
ts.factory.createTypeReferenceNode('SubOpAsyncIterable', [
|
|
45
|
+
ts.factory.createTypeReferenceNode('SubOpAsyncIterable', [
|
|
46
|
+
ts.factory.createTypeReferenceNode(resultName),
|
|
47
|
+
]),
|
|
46
48
|
]);
|
|
47
49
|
parameters.push(ts.factory.createParameterDeclaration(undefined, undefined, 'variables', undefined, ts.factory.createTypeReferenceNode(variablesName)), ts.factory.createParameterDeclaration(undefined, undefined, 'requestContext', undefined, ts.factory.createTypeReferenceNode('TRequestContext')));
|
|
48
50
|
break;
|
|
@@ -79,11 +81,11 @@ export function generateNodes(config, context) {
|
|
|
79
81
|
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('useOperation')),
|
|
80
82
|
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('useLazyOperation')),
|
|
81
83
|
ts.factory.createImportSpecifier(true, undefined, ts.factory.createIdentifier('OperationState')),
|
|
82
|
-
ts.factory.createImportSpecifier(true, undefined, ts.factory.createIdentifier('UseLazyOperationReturnType'))
|
|
84
|
+
ts.factory.createImportSpecifier(true, undefined, ts.factory.createIdentifier('UseLazyOperationReturnType')),
|
|
83
85
|
];
|
|
84
86
|
const gqlClientImports = [
|
|
85
87
|
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('IExecutor')),
|
|
86
|
-
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('RequestContext'))
|
|
88
|
+
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('RequestContext')),
|
|
87
89
|
];
|
|
88
90
|
if (subscriptionNodes.length !== 0) {
|
|
89
91
|
gqlClientReactImports.push(ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('useSubscription')));
|
|
@@ -104,12 +106,16 @@ export function generateNodes(config, context) {
|
|
|
104
106
|
...config.importDeclarations,
|
|
105
107
|
ts.factory.createImportDeclaration([], ts.factory.createImportClause(false, undefined, ts.factory.createNamedImports(gqlClientReactImports)), ts.factory.createStringLiteral('@vladimirdev635/gql-client-react')),
|
|
106
108
|
ts.factory.createImportDeclaration([], ts.factory.createImportClause(true, undefined, ts.factory.createNamedImports(gqlClientImports)), ts.factory.createStringLiteral('@vladimirdev635/gql-client/types')),
|
|
107
|
-
ts.factory.createImportDeclaration(undefined, ts.factory.createImportClause(false, undefined, ts.factory.createNamedImports(graphqlImports.map(i => ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier(i))))), ts.factory.createStringLiteral(config.graphqlModulePath)),
|
|
109
|
+
ts.factory.createImportDeclaration(undefined, ts.factory.createImportClause(false, undefined, ts.factory.createNamedImports(graphqlImports.map((i) => ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier(i))))), ts.factory.createStringLiteral(config.graphqlModulePath)),
|
|
108
110
|
ts.factory.createIdentifier('\n'),
|
|
109
|
-
ts.factory.createFunctionDeclaration(ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Export), undefined, 'createSdk', [
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
ts.factory.
|
|
113
|
-
|
|
111
|
+
ts.factory.createFunctionDeclaration(ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Export), undefined, 'createSdk', [
|
|
112
|
+
ts.factory.createTypeParameterDeclaration(undefined, 'TRequestContext', ts.factory.createTypeReferenceNode('RequestContext')),
|
|
113
|
+
], [
|
|
114
|
+
ts.factory.createParameterDeclaration(undefined, undefined, 'executor', undefined, ts.factory.createTypeReferenceNode('IExecutor', [
|
|
115
|
+
ts.factory.createTypeReferenceNode('TRequestContext'),
|
|
116
|
+
])),
|
|
117
|
+
], undefined, ts.factory.createBlock([
|
|
118
|
+
ts.factory.createReturnStatement(ts.factory.createObjectLiteralExpression(returnObjectNodes, true)),
|
|
119
|
+
], true)),
|
|
114
120
|
];
|
|
115
121
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { type GQLClientReactActorConfig, buildGQLClientReactActor } from './actor.js';
|
|
1
|
+
export { type GQLClientReactActorConfig, buildGQLClientReactActor, } from './actor.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { buildGQLClientReactActor } from './actor.js';
|
|
1
|
+
export { buildGQLClientReactActor, } from './actor.js';
|
|
@@ -1,32 +1,35 @@
|
|
|
1
1
|
/* eslint-disable max-lines */
|
|
2
2
|
import ts from 'typescript';
|
|
3
|
-
import { generateSchemaName, generateZodInferTypeAlias } from '../server/shared.js';
|
|
3
|
+
import { generateSchemaName, generateZodInferTypeAlias, } from '../server/shared.js';
|
|
4
4
|
import { generateZodObjectFieldSpec } from '../server/objects.js';
|
|
5
5
|
import assert from 'assert';
|
|
6
6
|
import { invokeMethod } from '../../../shared.js';
|
|
7
7
|
export function extractFragmentNamesInSpec(schema, fragmentSpec) {
|
|
8
8
|
if (fragmentSpec._type === 'UnionFragmentSpec') {
|
|
9
|
-
return fragmentSpec.selections
|
|
9
|
+
return fragmentSpec.selections
|
|
10
|
+
.map((s) => {
|
|
10
11
|
if (s._type === 'SpreadSelection') {
|
|
11
12
|
const fragment = schema.client.fragments[s.fragment];
|
|
12
13
|
return [
|
|
13
14
|
s.fragment,
|
|
14
|
-
...extractFragmentNamesInSpec(schema, fragment.spec)
|
|
15
|
+
...extractFragmentNamesInSpec(schema, fragment.spec),
|
|
15
16
|
];
|
|
16
17
|
}
|
|
17
18
|
if (s._type === 'ObjectConditionalSpreadSelection') {
|
|
18
19
|
return extractFragmentNamesInSpec(schema, s.spec);
|
|
19
20
|
}
|
|
20
21
|
return [];
|
|
21
|
-
})
|
|
22
|
+
})
|
|
23
|
+
.flat();
|
|
22
24
|
}
|
|
23
|
-
return fragmentSpec.selections
|
|
25
|
+
return fragmentSpec.selections
|
|
26
|
+
.map((s) => {
|
|
24
27
|
if (s._type === 'SpreadSelection') {
|
|
25
28
|
const fragment = schema.client.fragments[s.fragment];
|
|
26
29
|
assert(fragment.spec !== undefined);
|
|
27
30
|
return [
|
|
28
31
|
s.fragment,
|
|
29
|
-
...extractFragmentNamesInSpec(schema, fragment.spec)
|
|
32
|
+
...extractFragmentNamesInSpec(schema, fragment.spec),
|
|
30
33
|
];
|
|
31
34
|
}
|
|
32
35
|
if (s._type === 'FieldSelection' && s.selection != null) {
|
|
@@ -34,17 +37,19 @@ export function extractFragmentNamesInSpec(schema, fragmentSpec) {
|
|
|
34
37
|
return extractFragmentNamesInSpec(schema, s.selection);
|
|
35
38
|
}
|
|
36
39
|
return [];
|
|
37
|
-
})
|
|
40
|
+
})
|
|
41
|
+
.flat();
|
|
38
42
|
}
|
|
39
43
|
export function extractFragmentSourceTextsInSpec(schema, fragmentSpec) {
|
|
40
|
-
return Array.from(new Set(extractFragmentNamesInSpec(schema, fragmentSpec)))
|
|
41
|
-
.map(f => schema.client.fragments[f].sourceText);
|
|
44
|
+
return Array.from(new Set(extractFragmentNamesInSpec(schema, fragmentSpec))).map((f) => schema.client.fragments[f].sourceText);
|
|
42
45
|
}
|
|
43
46
|
function generateFragmentDocumentNode(schema, name, fragment) {
|
|
44
|
-
return ts.factory.createVariableStatement([ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
|
|
47
|
+
return ts.factory.createVariableStatement([ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
|
|
48
|
+
ts.factory.createVariableDeclaration(ts.factory.createIdentifier(name + 'FragmentDocument'), undefined, undefined, ts.factory.createStringLiteral([
|
|
45
49
|
fragment.sourceText,
|
|
46
|
-
...extractFragmentSourceTextsInSpec(schema, fragment.spec)
|
|
47
|
-
].join(' ')))
|
|
50
|
+
...extractFragmentSourceTextsInSpec(schema, fragment.spec),
|
|
51
|
+
].join(' '))),
|
|
52
|
+
], ts.NodeFlags.Const));
|
|
48
53
|
}
|
|
49
54
|
function generateZodObjectSelection(scalarsMapping, schema, objectType, selection, typenameConfig) {
|
|
50
55
|
switch (selection._type) {
|
|
@@ -65,13 +70,13 @@ function generateZodObjectSelection(scalarsMapping, schema, objectType, selectio
|
|
|
65
70
|
}
|
|
66
71
|
else {
|
|
67
72
|
const optional = fieldSpec.spec._type !== 'callable' ||
|
|
68
|
-
!selection.selection.selections.every(s => s._type === 'TypenameField') ||
|
|
73
|
+
!selection.selection.selections.every((s) => s._type === 'TypenameField') ||
|
|
69
74
|
!fieldSpec.nullable;
|
|
70
75
|
// eslint-disable-next-line no-use-before-define
|
|
71
76
|
expression = generateZodFragmentSpecCallExpression(scalarsMapping, schema, selection.selection, { ensurePresent: true, optional });
|
|
72
77
|
if (fieldSpec.spec._type === 'array' ||
|
|
73
|
-
fieldSpec.spec._type === 'callable' &&
|
|
74
|
-
fieldSpec.spec.returnType._type === 'array') {
|
|
78
|
+
(fieldSpec.spec._type === 'callable' &&
|
|
79
|
+
fieldSpec.spec.returnType._type === 'array')) {
|
|
75
80
|
expression = ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'array'), undefined, [expression]);
|
|
76
81
|
}
|
|
77
82
|
}
|
|
@@ -87,11 +92,13 @@ function generateZodObjectSelection(scalarsMapping, schema, objectType, selectio
|
|
|
87
92
|
}
|
|
88
93
|
function resolveSelections(specSelections, typenameConfig) {
|
|
89
94
|
const ignoreTypename = 'ignore' in typenameConfig;
|
|
90
|
-
const selections =
|
|
95
|
+
const selections = specSelections
|
|
96
|
+
.filter((s) => s._type !== 'TypenameField' || !ignoreTypename)
|
|
97
|
+
.toSorted((s1, s2) => s1._type.localeCompare(s2._type));
|
|
91
98
|
if (ignoreTypename)
|
|
92
99
|
return selections;
|
|
93
|
-
const hasTypename = specSelections.some(s => s._type === 'TypenameField');
|
|
94
|
-
const hasSpreadSelection = specSelections.some(s => s._type === 'SpreadSelection');
|
|
100
|
+
const hasTypename = specSelections.some((s) => s._type === 'TypenameField');
|
|
101
|
+
const hasSpreadSelection = specSelections.some((s) => s._type === 'SpreadSelection');
|
|
95
102
|
if (!hasTypename) {
|
|
96
103
|
if (hasSpreadSelection) {
|
|
97
104
|
selections.push({ _type: 'TypenameField', alias: null });
|
|
@@ -104,17 +111,24 @@ function resolveSelections(specSelections, typenameConfig) {
|
|
|
104
111
|
}
|
|
105
112
|
function generateZodObjectFragmentSpecCallExpression(scalarsMapping, schema, object, specSelections, insideLazy, typenameConfig) {
|
|
106
113
|
const selections = resolveSelections(specSelections, typenameConfig);
|
|
107
|
-
const expression = ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'object'), undefined, [
|
|
114
|
+
const expression = ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'object'), undefined, [
|
|
115
|
+
ts.factory.createObjectLiteralExpression(selections
|
|
116
|
+
.map((s) => generateZodObjectSelection(scalarsMapping, schema, object, s, typenameConfig))
|
|
117
|
+
.filter((s) => s !== null), true),
|
|
118
|
+
]);
|
|
108
119
|
if (insideLazy)
|
|
109
120
|
return expression;
|
|
110
|
-
const hasSpreadSelection = selections.some(s => s._type === 'SpreadSelection');
|
|
121
|
+
const hasSpreadSelection = selections.some((s) => s._type === 'SpreadSelection');
|
|
111
122
|
if (!hasSpreadSelection)
|
|
112
123
|
return expression;
|
|
113
|
-
return ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'lazy'), undefined, [
|
|
124
|
+
return ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'lazy'), undefined, [
|
|
125
|
+
ts.factory.createArrowFunction(undefined, undefined, [], undefined, ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), expression),
|
|
126
|
+
]);
|
|
114
127
|
}
|
|
115
128
|
function resolveUnionSelections(schema, specSelections) {
|
|
116
129
|
const typenameSelections = [];
|
|
117
|
-
const objectSelections = specSelections
|
|
130
|
+
const objectSelections = specSelections
|
|
131
|
+
.map((s) => {
|
|
118
132
|
assert(s._type !== 'UnionConditionalSpreadSelection');
|
|
119
133
|
if (s._type === 'TypenameField') {
|
|
120
134
|
typenameSelections.push(s);
|
|
@@ -127,26 +141,31 @@ function resolveUnionSelections(schema, specSelections) {
|
|
|
127
141
|
const [selections, tSelections] = resolveUnionSelections(schema, fragmentSpec.selections);
|
|
128
142
|
typenameSelections.push(...tSelections);
|
|
129
143
|
return selections;
|
|
130
|
-
})
|
|
144
|
+
})
|
|
145
|
+
.flat();
|
|
131
146
|
return [objectSelections, typenameSelections];
|
|
132
147
|
}
|
|
133
148
|
function generateZodUnionFragmentSpecCallExpression(scalarsMapping, schema, spec) {
|
|
134
149
|
const [objectSelections, typenameSelections] = resolveUnionSelections(schema, spec.selections);
|
|
135
150
|
for (const item of Object.keys(schema.server.unions[spec.name].items)) {
|
|
136
|
-
if (!objectSelections.some(s => s.object === item)) {
|
|
151
|
+
if (!objectSelections.some((s) => s.object === item)) {
|
|
137
152
|
objectSelections.push({
|
|
138
153
|
_type: 'ObjectConditionalSpreadSelection',
|
|
139
154
|
object: item,
|
|
140
155
|
spec: {
|
|
141
156
|
_type: 'ObjectFragmentSpec',
|
|
142
157
|
name: item,
|
|
143
|
-
selections: []
|
|
144
|
-
}
|
|
158
|
+
selections: [],
|
|
159
|
+
},
|
|
145
160
|
});
|
|
146
161
|
}
|
|
147
162
|
}
|
|
148
|
-
const expression = ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'union'), undefined, [
|
|
149
|
-
|
|
163
|
+
const expression = ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'union'), undefined, [
|
|
164
|
+
ts.factory.createArrayLiteralExpression(objectSelections.map((s) => generateZodObjectFragmentSpecCallExpression(scalarsMapping, schema, schema.server.objects[s.object], [...s.spec.selections, ...typenameSelections], true, { ensurePresent: true, optional: false })), true),
|
|
165
|
+
]);
|
|
166
|
+
return ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'lazy'), undefined, [
|
|
167
|
+
ts.factory.createArrowFunction(undefined, undefined, [], undefined, ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), expression),
|
|
168
|
+
]);
|
|
150
169
|
}
|
|
151
170
|
export function generateZodFragmentSpecCallExpression(scalarsMapping, schema, spec, typenameConfig) {
|
|
152
171
|
if (spec._type === 'ObjectFragmentSpec') {
|
|
@@ -155,19 +174,22 @@ export function generateZodFragmentSpecCallExpression(scalarsMapping, schema, sp
|
|
|
155
174
|
return generateZodUnionFragmentSpecCallExpression(scalarsMapping, schema, spec);
|
|
156
175
|
}
|
|
157
176
|
function generateZodFragmentSchema(scalarsMapping, schema, fragmentName, fragment) {
|
|
158
|
-
return ts.factory.createVariableStatement([ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
|
|
177
|
+
return ts.factory.createVariableStatement([ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
|
|
178
|
+
ts.factory.createVariableDeclaration(ts.factory.createIdentifier(generateSchemaName(fragmentName + 'Fragment')), undefined, undefined, generateZodFragmentSpecCallExpression(scalarsMapping, schema, fragment.spec)),
|
|
179
|
+
], ts.NodeFlags.Const));
|
|
159
180
|
}
|
|
160
181
|
function generateFragmentSpecDeclarations(scalarsMapping, schema, fragmentName, fragment) {
|
|
161
182
|
const fName = fragmentName + 'Fragment';
|
|
162
183
|
return [
|
|
163
184
|
generateFragmentDocumentNode(schema, fragmentName, fragment),
|
|
164
185
|
generateZodFragmentSchema(scalarsMapping, schema, fragmentName, fragment),
|
|
165
|
-
generateZodInferTypeAlias('output', fName, generateSchemaName(fName))
|
|
186
|
+
generateZodInferTypeAlias('output', fName, generateSchemaName(fName)),
|
|
166
187
|
];
|
|
167
188
|
}
|
|
168
189
|
export function generateFragmentTypes(scalarsMapping, schema) {
|
|
169
190
|
return Object.entries(schema.client.fragments)
|
|
170
191
|
.map(([name, fragment]) => {
|
|
171
192
|
return generateFragmentSpecDeclarations(scalarsMapping, schema, name, fragment);
|
|
172
|
-
})
|
|
193
|
+
})
|
|
194
|
+
.flat();
|
|
173
195
|
}
|
|
@@ -3,23 +3,23 @@ import { generateFragmentTypes } from './fragments.js';
|
|
|
3
3
|
import { generateOperationsNodes } from './operations.js';
|
|
4
4
|
const operationTypeNode = ts.factory.createInterfaceDeclaration(ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Export), 'Operation', [
|
|
5
5
|
ts.factory.createTypeParameterDeclaration(undefined, 'V'),
|
|
6
|
-
ts.factory.createTypeParameterDeclaration(undefined, 'R')
|
|
6
|
+
ts.factory.createTypeParameterDeclaration(undefined, 'R'),
|
|
7
7
|
], [], [
|
|
8
8
|
ts.factory.createPropertySignature(undefined, 'name', undefined, ts.factory.createTypeReferenceNode('string')),
|
|
9
|
-
ts.factory.createPropertySignature(undefined, 'type', undefined, ts.factory.createUnionTypeNode(['QUERY', 'MUTATION', 'SUBSCRIPTION'].map(v => ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(v))))),
|
|
9
|
+
ts.factory.createPropertySignature(undefined, 'type', undefined, ts.factory.createUnionTypeNode(['QUERY', 'MUTATION', 'SUBSCRIPTION'].map((v) => ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(v))))),
|
|
10
10
|
ts.factory.createPropertySignature(undefined, 'document', undefined, ts.factory.createTypeReferenceNode('string')),
|
|
11
11
|
ts.factory.createPropertySignature(undefined, 'variablesSchema', undefined, ts.factory.createTypeReferenceNode('z.ZodType', [
|
|
12
12
|
ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword),
|
|
13
|
-
ts.factory.createTypeReferenceNode('V')
|
|
13
|
+
ts.factory.createTypeReferenceNode('V'),
|
|
14
14
|
])),
|
|
15
15
|
ts.factory.createPropertySignature(undefined, 'resultSchema', undefined, ts.factory.createTypeReferenceNode('z.ZodType', [
|
|
16
|
-
ts.factory.createTypeReferenceNode('R')
|
|
16
|
+
ts.factory.createTypeReferenceNode('R'),
|
|
17
17
|
])),
|
|
18
18
|
]);
|
|
19
19
|
export function generateClientNodes(config, context) {
|
|
20
20
|
return [
|
|
21
21
|
operationTypeNode,
|
|
22
22
|
...generateFragmentTypes(config.scalarsMapping, context.schema),
|
|
23
|
-
...generateOperationsNodes(config.scalarsMapping, context.schema)
|
|
23
|
+
...generateOperationsNodes(config.scalarsMapping, context.schema),
|
|
24
24
|
];
|
|
25
25
|
}
|
|
@@ -1,38 +1,52 @@
|
|
|
1
1
|
/* eslint-disable max-lines */
|
|
2
2
|
import ts from 'typescript';
|
|
3
3
|
import { extractFragmentSourceTextsInSpec, generateZodFragmentSpecCallExpression, } from './fragments.js';
|
|
4
|
-
import { generateSchemaName, generateZodInferTypeAlias } from '../server/shared.js';
|
|
4
|
+
import { generateSchemaName, generateZodInferTypeAlias, } from '../server/shared.js';
|
|
5
5
|
import { generateInputTypeDefinitionFields } from '../server/inputs.js';
|
|
6
6
|
export function opTypeToName(type) {
|
|
7
7
|
switch (type) {
|
|
8
|
-
case 'QUERY':
|
|
9
|
-
|
|
10
|
-
case '
|
|
8
|
+
case 'QUERY':
|
|
9
|
+
return 'Query';
|
|
10
|
+
case 'MUTATION':
|
|
11
|
+
return 'Mutation';
|
|
12
|
+
case 'SUBSCRIPTION':
|
|
13
|
+
return 'Subscription';
|
|
11
14
|
}
|
|
12
15
|
}
|
|
13
16
|
function parametersToFields(parameters) {
|
|
14
|
-
return Object.fromEntries(Object.keys(parameters).map(name => [
|
|
17
|
+
return Object.fromEntries(Object.keys(parameters).map((name) => [
|
|
18
|
+
name.slice(1),
|
|
19
|
+
parameters[name],
|
|
20
|
+
]));
|
|
15
21
|
}
|
|
16
22
|
function generateOperationNode(schema, operation) {
|
|
17
|
-
return ts.factory.createVariableStatement(ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Export), ts.factory.createVariableDeclarationList([
|
|
23
|
+
return ts.factory.createVariableStatement(ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Export), ts.factory.createVariableDeclarationList([
|
|
24
|
+
ts.factory.createVariableDeclaration(ts.factory.createIdentifier(operation.name + 'Operation'), undefined, undefined, ts.factory.createSatisfiesExpression(ts.factory.createAsExpression(ts.factory.createObjectLiteralExpression([
|
|
18
25
|
ts.factory.createPropertyAssignment('name', ts.factory.createStringLiteral(operation.name)),
|
|
19
26
|
ts.factory.createPropertyAssignment('type', ts.factory.createStringLiteral(operation.type)),
|
|
20
27
|
ts.factory.createPropertyAssignment('document', ts.factory.createStringLiteral([
|
|
21
28
|
operation.sourceText,
|
|
22
|
-
...extractFragmentSourceTextsInSpec(schema, operation.fragmentSpec)
|
|
29
|
+
...extractFragmentSourceTextsInSpec(schema, operation.fragmentSpec),
|
|
23
30
|
].join(' '))),
|
|
24
31
|
ts.factory.createPropertyAssignment('variablesSchema', ts.factory.createIdentifier(generateSchemaName(operation.name + 'Variables'))),
|
|
25
32
|
ts.factory.createPropertyAssignment('resultSchema', ts.factory.createIdentifier(generateSchemaName(operation.name + 'Result'))),
|
|
26
33
|
], true), ts.factory.createTypeReferenceNode('const')), ts.factory.createTypeReferenceNode('Operation', [
|
|
27
34
|
ts.factory.createTypeReferenceNode(operation.name + 'Variables'),
|
|
28
35
|
ts.factory.createTypeReferenceNode(operation.name + 'Result'),
|
|
29
|
-
])))
|
|
36
|
+
]))),
|
|
37
|
+
], ts.NodeFlags.Const));
|
|
30
38
|
}
|
|
31
39
|
function generateOperationZodInputSchema(scalarsMapping, operation) {
|
|
32
|
-
return ts.factory.createVariableStatement([ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
|
|
40
|
+
return ts.factory.createVariableStatement([ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
|
|
41
|
+
ts.factory.createVariableDeclaration(ts.factory.createIdentifier(generateSchemaName(operation.name + 'Variables')), undefined, undefined, ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'object'), undefined, [
|
|
42
|
+
ts.factory.createObjectLiteralExpression(generateInputTypeDefinitionFields(scalarsMapping, parametersToFields(operation.parameters)), true),
|
|
43
|
+
])),
|
|
44
|
+
], ts.NodeFlags.Const));
|
|
33
45
|
}
|
|
34
46
|
function genearteOperationZodOutputSchema(scalarsMapping, schema, operation) {
|
|
35
|
-
return ts.factory.createVariableStatement([ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
|
|
47
|
+
return ts.factory.createVariableStatement([ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
|
|
48
|
+
ts.factory.createVariableDeclaration(ts.factory.createIdentifier(generateSchemaName(operation.name + 'Result')), undefined, undefined, generateZodFragmentSpecCallExpression(scalarsMapping, schema, operation.fragmentSpec)),
|
|
49
|
+
], ts.NodeFlags.Const));
|
|
36
50
|
}
|
|
37
51
|
function generateOperationNodes(scalarsMapping, schema, operation) {
|
|
38
52
|
return [
|
|
@@ -42,11 +56,13 @@ function generateOperationNodes(scalarsMapping, schema, operation) {
|
|
|
42
56
|
genearteOperationZodOutputSchema(scalarsMapping, schema, operation),
|
|
43
57
|
generateZodInferTypeAlias('output', operation.name + 'Result', generateSchemaName(operation.name + 'Result')),
|
|
44
58
|
generateOperationNode(schema, operation),
|
|
45
|
-
ts.factory.createIdentifier('\n')
|
|
59
|
+
ts.factory.createIdentifier('\n'),
|
|
46
60
|
];
|
|
47
61
|
}
|
|
48
62
|
export function generateOperationsNodes(scalarsMapping, schema) {
|
|
49
|
-
return Object.values(schema.client.operations)
|
|
63
|
+
return Object.values(schema.client.operations)
|
|
64
|
+
.map((operation) => {
|
|
50
65
|
return generateOperationNodes(scalarsMapping, schema, operation);
|
|
51
|
-
})
|
|
66
|
+
})
|
|
67
|
+
.flat();
|
|
52
68
|
}
|
|
@@ -4,11 +4,11 @@ import { generateClientNodes } from './client/main.js';
|
|
|
4
4
|
export function generateNodes(config, context) {
|
|
5
5
|
return [
|
|
6
6
|
ts.factory.createImportDeclaration([], ts.factory.createImportClause(false, undefined, ts.factory.createNamedImports([
|
|
7
|
-
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('z'))
|
|
7
|
+
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('z')),
|
|
8
8
|
])), ts.factory.createStringLiteral('zod/v4')),
|
|
9
9
|
...config.importDeclarations,
|
|
10
10
|
ts.factory.createIdentifier('\n'),
|
|
11
11
|
...generateServerNodes(config, context),
|
|
12
|
-
...generateClientNodes(config, context)
|
|
12
|
+
...generateClientNodes(config, context),
|
|
13
13
|
];
|
|
14
14
|
}
|