@vladimirdev635/gql-codegen 0.0.28 → 0.0.29
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/gql-client/actor.d.ts +1 -1
- package/actors/ts/gql-client/generators/main.js +31 -17
- package/actors/ts/gql-client-react/generators/main.js +19 -16
- package/actors/ts/graphql/generators/server/shared.d.ts +1 -1
- package/actors/ts/graphql/generators/shared.d.ts +1 -1
- package/package.json +1 -1
|
@@ -2,7 +2,7 @@ import { PathOrFileDescriptor } from 'fs';
|
|
|
2
2
|
import ts from 'typescript';
|
|
3
3
|
import { Actor, ActorContext } from '@/config.js';
|
|
4
4
|
import { TSActorConfig } from '../shared.js';
|
|
5
|
-
export type OperationReturnType = '
|
|
5
|
+
export type OperationReturnType = 'ExecuteResult' | 'ExecuteResult.result';
|
|
6
6
|
export interface SDKConfig {
|
|
7
7
|
defaultOperationReturnType: OperationReturnType;
|
|
8
8
|
operationReturnTypeMapping: Record<string, OperationReturnType>;
|
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
|
-
function generateVariablesTypeNode(operationName) {
|
|
3
|
-
return ts.factory.createTypeReferenceNode('z.infer', [
|
|
4
|
-
ts.factory.createIndexedAccessTypeNode(ts.factory.createParenthesizedType(ts.factory.createTypeQueryNode(ts.factory.createIdentifier(operationName))), ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral('variablesSchema')))
|
|
5
|
-
]);
|
|
6
|
-
}
|
|
7
2
|
function generateFunctionBlock(operationName, returnType) {
|
|
8
3
|
const awaitExpression = ts.factory.createAwaitExpression(ts.factory.createCallExpression(ts.factory.createIdentifier('executor'), undefined, [
|
|
9
4
|
ts.factory.createIdentifier(operationName),
|
|
10
5
|
ts.factory.createIdentifier('variables'),
|
|
11
6
|
ts.factory.createIdentifier('requestContext')
|
|
12
7
|
]));
|
|
13
|
-
if (returnType === '
|
|
8
|
+
if (returnType === 'ExecuteResult') {
|
|
14
9
|
return ts.factory.createBlock([
|
|
15
10
|
ts.factory.createReturnStatement(awaitExpression)
|
|
16
11
|
], true);
|
|
@@ -22,27 +17,46 @@ function generateFunctionBlock(operationName, returnType) {
|
|
|
22
17
|
ts.factory.createReturnStatement(ts.factory.createPropertyAccessChain(ts.factory.createIdentifier('executorResult'), undefined, 'result'))
|
|
23
18
|
], true);
|
|
24
19
|
}
|
|
20
|
+
function createReturnTypeNode(resultName, returnType) {
|
|
21
|
+
const rType = ts.factory.createTypeReferenceNode(resultName);
|
|
22
|
+
if (returnType === 'ExecuteResult.result')
|
|
23
|
+
return rType;
|
|
24
|
+
return ts.factory.createTypeReferenceNode('ExecuteResult', [rType]);
|
|
25
|
+
}
|
|
26
|
+
function getReturnTypeFromConfig(config, operationName) {
|
|
27
|
+
return config.sdk.operationReturnTypeMapping[operationName] ||
|
|
28
|
+
config.sdk.defaultOperationReturnType;
|
|
29
|
+
}
|
|
25
30
|
export function generateNodes(config, context) {
|
|
26
31
|
const graphqlImports = [];
|
|
32
|
+
let shouldIncludeExecuteResultType = false;
|
|
27
33
|
const nodes = Object.values(context.schema.client.operations)
|
|
28
34
|
.map(operation => {
|
|
29
35
|
const operationName = operation.name + 'Operation';
|
|
30
|
-
|
|
36
|
+
const variablesName = operation.name + 'Variables';
|
|
37
|
+
const resultName = operation.name + 'Result';
|
|
38
|
+
graphqlImports.push(operationName, variablesName, resultName);
|
|
39
|
+
const returnType = getReturnTypeFromConfig(config, operation.name);
|
|
40
|
+
if (returnType === 'ExecuteResult')
|
|
41
|
+
shouldIncludeExecuteResultType = true;
|
|
31
42
|
return ts.factory.createPropertyAssignment(operation.name, ts.factory.createArrowFunction(ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Async), undefined, [
|
|
32
|
-
ts.factory.createParameterDeclaration(undefined, undefined, 'variables', undefined,
|
|
43
|
+
ts.factory.createParameterDeclaration(undefined, undefined, 'variables', undefined, ts.factory.createTypeReferenceNode(variablesName)),
|
|
33
44
|
ts.factory.createParameterDeclaration(undefined, undefined, 'requestContext', undefined, ts.factory.createTypeReferenceNode('TRequestContext')),
|
|
34
|
-
],
|
|
35
|
-
|
|
45
|
+
], ts.factory.createTypeReferenceNode('Promise', [
|
|
46
|
+
createReturnTypeNode(resultName, returnType)
|
|
47
|
+
]), ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), generateFunctionBlock(operationName, returnType)));
|
|
36
48
|
});
|
|
49
|
+
const gqlClientImports = [
|
|
50
|
+
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('Executor')),
|
|
51
|
+
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('RequestContext'))
|
|
52
|
+
];
|
|
53
|
+
if (shouldIncludeExecuteResultType) {
|
|
54
|
+
gqlClientImports.push(ts.factory.createImportSpecifier(true, undefined, ts.factory.createIdentifier('ExecuteResult')));
|
|
55
|
+
}
|
|
37
56
|
return [
|
|
57
|
+
ts.factory.createIdentifier('// @ts-nocheck'),
|
|
38
58
|
...config.importDeclarations,
|
|
39
|
-
ts.factory.createImportDeclaration([], ts.factory.createImportClause(true, undefined, ts.factory.createNamedImports(
|
|
40
|
-
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('Executor')),
|
|
41
|
-
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('RequestContext'))
|
|
42
|
-
])), ts.factory.createStringLiteral('@vladimirdev635/gql-client')),
|
|
43
|
-
ts.factory.createImportDeclaration([], ts.factory.createImportClause(false, undefined, ts.factory.createNamedImports([
|
|
44
|
-
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('z'))
|
|
45
|
-
])), ts.factory.createStringLiteral('zod/v4')),
|
|
59
|
+
ts.factory.createImportDeclaration([], ts.factory.createImportClause(true, undefined, ts.factory.createNamedImports(gqlClientImports)), ts.factory.createStringLiteral('@vladimirdev635/gql-client')),
|
|
46
60
|
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)),
|
|
47
61
|
ts.factory.createIdentifier('\n'),
|
|
48
62
|
ts.factory.createFunctionDeclaration(ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Export), undefined, 'createSdk', [ts.factory.createTypeParameterDeclaration(undefined, 'TRequestContext', ts.factory.createTypeReferenceNode('RequestContext'))], [ts.factory.createParameterDeclaration(undefined, undefined, 'executor', undefined, ts.factory.createTypeReferenceNode('Executor', [
|
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
|
-
function generateVariablesTypeNode(operationName) {
|
|
3
|
-
return ts.factory.createTypeReferenceNode('z.infer', [
|
|
4
|
-
ts.factory.createIndexedAccessTypeNode(ts.factory.createParenthesizedType(ts.factory.createTypeQueryNode(ts.factory.createIdentifier(operationName))), ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral('variablesSchema')))
|
|
5
|
-
]);
|
|
6
|
-
}
|
|
7
2
|
function generateFunctionBlock(operationName, lazy) {
|
|
8
3
|
if (!lazy) {
|
|
9
4
|
return ts.factory.createCallExpression(ts.factory.createIdentifier('useOperation'), undefined, [
|
|
@@ -18,43 +13,51 @@ function generateFunctionBlock(operationName, lazy) {
|
|
|
18
13
|
ts.factory.createIdentifier(operationName)
|
|
19
14
|
]);
|
|
20
15
|
}
|
|
21
|
-
function generateArrowFunction(operationName, lazy) {
|
|
16
|
+
function generateArrowFunction(operationName, variablesName, resultName, lazy) {
|
|
22
17
|
const parameters = [];
|
|
18
|
+
let resultType = ts.factory.createTypeReferenceNode('LazyOperationState', [
|
|
19
|
+
ts.factory.createTypeReferenceNode(resultName)
|
|
20
|
+
]);
|
|
23
21
|
if (!lazy) {
|
|
24
|
-
|
|
22
|
+
resultType = ts.factory.createTypeReferenceNode('OperationState', [
|
|
23
|
+
ts.factory.createTypeReferenceNode(resultName)
|
|
24
|
+
]);
|
|
25
|
+
parameters.push(ts.factory.createParameterDeclaration(undefined, undefined, 'variables', undefined, ts.factory.createTypeReferenceNode(variablesName)), ts.factory.createParameterDeclaration(undefined, undefined, 'requestContext', undefined, ts.factory.createTypeReferenceNode('TRequestContext')));
|
|
25
26
|
}
|
|
26
|
-
return ts.factory.createArrowFunction(undefined, undefined, parameters,
|
|
27
|
+
return ts.factory.createArrowFunction(undefined, undefined, parameters, resultType, ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), generateFunctionBlock(operationName, lazy));
|
|
27
28
|
}
|
|
28
29
|
export function generateNodes(config, context) {
|
|
29
30
|
const graphqlImports = [];
|
|
30
31
|
const nodes = Object.values(context.schema.client.operations)
|
|
31
32
|
.map(operation => {
|
|
32
33
|
const operationName = operation.name + 'Operation';
|
|
33
|
-
|
|
34
|
+
const variablesName = operation.name + 'Variables';
|
|
35
|
+
const resultName = operation.name + 'Result';
|
|
36
|
+
graphqlImports.push(operationName, variablesName, resultName);
|
|
34
37
|
return [
|
|
35
|
-
ts.factory.createPropertyAssignment('use' + operationName, generateArrowFunction(operationName, false)),
|
|
36
|
-
ts.factory.createPropertyAssignment('useLazy' + operationName, generateArrowFunction(operationName, true))
|
|
38
|
+
ts.factory.createPropertyAssignment('use' + operationName, generateArrowFunction(operationName, variablesName, resultName, false)),
|
|
39
|
+
ts.factory.createPropertyAssignment('useLazy' + operationName, generateArrowFunction(operationName, variablesName, resultName, true))
|
|
37
40
|
];
|
|
38
41
|
}).flat();
|
|
39
42
|
return [
|
|
43
|
+
ts.factory.createIdentifier('// @ts-nocheck'),
|
|
40
44
|
...config.importDeclarations,
|
|
41
45
|
ts.factory.createImportDeclaration([], ts.factory.createImportClause(false, undefined, ts.factory.createNamedImports([
|
|
42
46
|
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('useOperation')),
|
|
43
|
-
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('useLazyOperation'))
|
|
47
|
+
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('useLazyOperation')),
|
|
48
|
+
ts.factory.createImportSpecifier(true, undefined, ts.factory.createIdentifier('OperationState')),
|
|
49
|
+
ts.factory.createImportSpecifier(true, undefined, ts.factory.createIdentifier('LazyOperationState'))
|
|
44
50
|
])), ts.factory.createStringLiteral('@vladimirdev635/gql-client-react')),
|
|
45
51
|
ts.factory.createImportDeclaration([], ts.factory.createImportClause(true, undefined, ts.factory.createNamedImports([
|
|
46
52
|
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('Executor')),
|
|
47
53
|
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('RequestContext'))
|
|
48
54
|
])), ts.factory.createStringLiteral('@vladimirdev635/gql-client')),
|
|
49
|
-
ts.factory.createImportDeclaration([], ts.factory.createImportClause(false, undefined, ts.factory.createNamedImports([
|
|
50
|
-
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('z'))
|
|
51
|
-
])), ts.factory.createStringLiteral('zod/v4')),
|
|
52
55
|
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)),
|
|
53
56
|
ts.factory.createIdentifier('\n'),
|
|
54
57
|
ts.factory.createFunctionDeclaration(ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Export), undefined, 'createSdk', [ts.factory.createTypeParameterDeclaration(undefined, 'TRequestContext', ts.factory.createTypeReferenceNode('RequestContext'))], [ts.factory.createParameterDeclaration(undefined, undefined, 'executor', undefined, ts.factory.createTypeReferenceNode('Executor', [
|
|
55
58
|
ts.factory.createTypeReferenceNode('TRequestContext')
|
|
56
59
|
]))], undefined, ts.factory.createBlock([
|
|
57
|
-
ts.factory.createReturnStatement(ts.factory.
|
|
60
|
+
ts.factory.createReturnStatement(ts.factory.createObjectLiteralExpression(nodes, true))
|
|
58
61
|
], true))
|
|
59
62
|
];
|
|
60
63
|
}
|
|
@@ -2,7 +2,7 @@ import ts from 'typescript';
|
|
|
2
2
|
import { z } from 'zod/v4';
|
|
3
3
|
import { objectNonCallableFieldSpecSchema } from '@/schema/server.js';
|
|
4
4
|
import { inputFieldSpecSchema } from '@/schema/shared.js';
|
|
5
|
-
export declare function generateNonCallableFieldSpec(scalars: string[], spec: z.infer<typeof objectNonCallableFieldSpecSchema> | z.infer<typeof inputFieldSpecSchema>): ts.
|
|
5
|
+
export declare function generateNonCallableFieldSpec(scalars: string[], spec: z.infer<typeof objectNonCallableFieldSpecSchema> | z.infer<typeof inputFieldSpecSchema>): ts.TypeReferenceNode | ts.IndexedAccessTypeNode | ts.ArrayTypeNode;
|
|
6
6
|
export declare function wrapInMaybeIfNullable(spec: ts.TypeNode, nullable: boolean): ts.TypeNode;
|
|
7
7
|
export declare function generateZodInferTypeAlias(name: string, typeName: string): ts.TypeAliasDeclaration;
|
|
8
8
|
export declare function generateSchemaName(name: string): string;
|
|
@@ -2,5 +2,5 @@ import ts from 'typescript';
|
|
|
2
2
|
export declare function createQuestionTokenIfNullable(nullable: boolean): ts.PunctuationToken<ts.SyntaxKind.QuestionToken> | undefined;
|
|
3
3
|
export declare function createTypenamePropertySignature(name: string, optional: boolean, alias: string | null): ts.PropertySignature;
|
|
4
4
|
export declare function generateScalarReference(name: string): ts.IndexedAccessTypeNode;
|
|
5
|
-
export declare function generateTypeReferenceNode(scalars: string[], name: string): ts.
|
|
5
|
+
export declare function generateTypeReferenceNode(scalars: string[], name: string): ts.TypeReferenceNode | ts.IndexedAccessTypeNode;
|
|
6
6
|
export declare function generateStringOrTemplate(value: string, values: string[]): ts.StringLiteral | ts.TemplateExpression;
|