@vladimirdev635/gql-codegen 0.0.25 → 0.0.27
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/generators/main.js +9 -4
- package/actors/ts/gql-client-react/actor.d.ts +10 -0
- package/actors/ts/gql-client-react/actor.js +11 -0
- package/actors/ts/gql-client-react/generators/main.d.ts +4 -0
- package/actors/ts/gql-client-react/generators/main.js +60 -0
- package/actors/ts/graphql/generators/client/main.js +2 -0
- package/actors/ts/graphql/generators/client/operations.js +2 -0
- package/package.json +4 -4
|
@@ -7,7 +7,8 @@ function generateVariablesTypeNode(operationName) {
|
|
|
7
7
|
function generateFunctionBlock(operationName, returnType) {
|
|
8
8
|
const awaitExpression = ts.factory.createAwaitExpression(ts.factory.createCallExpression(ts.factory.createIdentifier('executor'), undefined, [
|
|
9
9
|
ts.factory.createIdentifier(operationName),
|
|
10
|
-
ts.factory.createIdentifier('variables')
|
|
10
|
+
ts.factory.createIdentifier('variables'),
|
|
11
|
+
ts.factory.createIdentifier('requestContext')
|
|
11
12
|
]));
|
|
12
13
|
if (returnType === 'ExecutorResult') {
|
|
13
14
|
return ts.factory.createBlock([
|
|
@@ -29,20 +30,24 @@ export function generateNodes(config, context) {
|
|
|
29
30
|
graphqlImports.push(operationName);
|
|
30
31
|
return ts.factory.createPropertyAssignment(operation.name, ts.factory.createArrowFunction(ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Async), undefined, [
|
|
31
32
|
ts.factory.createParameterDeclaration(undefined, undefined, 'variables', undefined, generateVariablesTypeNode(operationName)),
|
|
33
|
+
ts.factory.createParameterDeclaration(undefined, undefined, 'requestContext', undefined, ts.factory.createTypeReferenceNode('TRequestContext')),
|
|
32
34
|
], undefined, ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), generateFunctionBlock(operationName, config.sdk.operationReturnTypeMapping[operation.name] ||
|
|
33
35
|
config.sdk.defaultOperationReturnType)));
|
|
34
36
|
});
|
|
35
37
|
return [
|
|
36
38
|
...config.importDeclarations,
|
|
37
|
-
ts.factory.createImportDeclaration([], ts.factory.createImportClause(
|
|
38
|
-
ts.factory.createImportSpecifier(
|
|
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'))
|
|
39
42
|
])), ts.factory.createStringLiteral('@vladimirdev635/gql-client')),
|
|
40
43
|
ts.factory.createImportDeclaration([], ts.factory.createImportClause(false, undefined, ts.factory.createNamedImports([
|
|
41
44
|
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('z'))
|
|
42
45
|
])), ts.factory.createStringLiteral('zod/v4')),
|
|
43
46
|
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)),
|
|
44
47
|
ts.factory.createIdentifier('\n'),
|
|
45
|
-
ts.factory.createFunctionDeclaration(ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Export), undefined, 'createSdk', [], [ts.factory.createParameterDeclaration(undefined, undefined, 'executor', undefined, ts.factory.createTypeReferenceNode('Executor'
|
|
48
|
+
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', [
|
|
49
|
+
ts.factory.createTypeReferenceNode('TRequestContext')
|
|
50
|
+
]))], undefined, ts.factory.createBlock([
|
|
46
51
|
ts.factory.createReturnStatement(ts.factory.createAsExpression(ts.factory.createObjectLiteralExpression(nodes, true), ts.factory.createTypeReferenceNode('const')))
|
|
47
52
|
], true))
|
|
48
53
|
];
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { PathOrFileDescriptor } from 'fs';
|
|
2
|
+
import { Actor, ActorContext } from '@/config.js';
|
|
3
|
+
import { TSActorConfig } from '../shared.js';
|
|
4
|
+
import ts from 'typescript';
|
|
5
|
+
export interface GQLClientReactActorConfig extends TSActorConfig {
|
|
6
|
+
outPath: PathOrFileDescriptor;
|
|
7
|
+
importDeclarations: ts.ImportDeclaration[];
|
|
8
|
+
graphqlModulePath: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function buildGQLClientReactActor(config: GQLClientReactActorConfig): Actor<ActorContext>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { writeFileSync } from 'fs';
|
|
2
|
+
import { renderNodes } from '../shared.js';
|
|
3
|
+
import { generateNodes } from './generators/main.js';
|
|
4
|
+
async function gqlClientReactActor(config, context) {
|
|
5
|
+
const nodes = generateNodes(config, context);
|
|
6
|
+
const code = await renderNodes(config, nodes);
|
|
7
|
+
writeFileSync(config.outPath, code);
|
|
8
|
+
}
|
|
9
|
+
export function buildGQLClientReactActor(config) {
|
|
10
|
+
return context => gqlClientReactActor(config, context);
|
|
11
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
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
|
+
function generateFunctionBlock(operationName, lazy) {
|
|
8
|
+
if (!lazy) {
|
|
9
|
+
return ts.factory.createCallExpression(ts.factory.createIdentifier('useOperation'), undefined, [
|
|
10
|
+
ts.factory.createIdentifier('executor'),
|
|
11
|
+
ts.factory.createIdentifier(operationName),
|
|
12
|
+
ts.factory.createIdentifier('variables'),
|
|
13
|
+
ts.factory.createIdentifier('requestContext')
|
|
14
|
+
]);
|
|
15
|
+
}
|
|
16
|
+
return ts.factory.createCallExpression(ts.factory.createIdentifier('useLazyOperation'), undefined, [
|
|
17
|
+
ts.factory.createIdentifier('executor'),
|
|
18
|
+
ts.factory.createIdentifier(operationName)
|
|
19
|
+
]);
|
|
20
|
+
}
|
|
21
|
+
function generateArrowFunction(operationName, lazy) {
|
|
22
|
+
const parameters = [];
|
|
23
|
+
if (!lazy) {
|
|
24
|
+
parameters.push(ts.factory.createParameterDeclaration(undefined, undefined, 'variables', undefined, generateVariablesTypeNode(operationName)), ts.factory.createParameterDeclaration(undefined, undefined, 'requestContext', undefined, ts.factory.createTypeReferenceNode('TRequestContext')));
|
|
25
|
+
}
|
|
26
|
+
return ts.factory.createArrowFunction(undefined, undefined, parameters, undefined, ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), generateFunctionBlock(operationName, lazy));
|
|
27
|
+
}
|
|
28
|
+
export function generateNodes(config, context) {
|
|
29
|
+
const graphqlImports = [];
|
|
30
|
+
const nodes = Object.values(context.schema.client.operations)
|
|
31
|
+
.map(operation => {
|
|
32
|
+
const operationName = operation.name + 'Operation';
|
|
33
|
+
graphqlImports.push(operationName);
|
|
34
|
+
return [
|
|
35
|
+
ts.factory.createPropertyAssignment('use' + operationName, generateArrowFunction(operationName, false)),
|
|
36
|
+
ts.factory.createPropertyAssignment('useLazy' + operationName, generateArrowFunction(operationName, true))
|
|
37
|
+
];
|
|
38
|
+
}).flat();
|
|
39
|
+
return [
|
|
40
|
+
...config.importDeclarations,
|
|
41
|
+
ts.factory.createImportDeclaration([], ts.factory.createImportClause(false, undefined, ts.factory.createNamedImports([
|
|
42
|
+
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('useOperation')),
|
|
43
|
+
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('useLazyOperation'))
|
|
44
|
+
])), ts.factory.createStringLiteral('@vladimirdev635/gql-client-react')),
|
|
45
|
+
ts.factory.createImportDeclaration([], ts.factory.createImportClause(true, undefined, ts.factory.createNamedImports([
|
|
46
|
+
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('Executor')),
|
|
47
|
+
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('RequestContext'))
|
|
48
|
+
])), 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
|
+
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
|
+
ts.factory.createIdentifier('\n'),
|
|
54
|
+
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
|
+
ts.factory.createTypeReferenceNode('TRequestContext')
|
|
56
|
+
]))], undefined, ts.factory.createBlock([
|
|
57
|
+
ts.factory.createReturnStatement(ts.factory.createAsExpression(ts.factory.createObjectLiteralExpression(nodes, true), ts.factory.createTypeReferenceNode('const')))
|
|
58
|
+
], true))
|
|
59
|
+
];
|
|
60
|
+
}
|
|
@@ -2,6 +2,8 @@ import ts from 'typescript';
|
|
|
2
2
|
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
|
+
ts.factory.createPropertySignature(undefined, 'name', undefined, ts.factory.createTypeReferenceNode('string')),
|
|
6
|
+
ts.factory.createPropertySignature(undefined, 'type', undefined, ts.factory.createUnionTypeNode(['QUERY', 'MUTATION', 'SUBSCRIPTION'].map(v => ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(v))))),
|
|
5
7
|
ts.factory.createPropertySignature(undefined, 'document', undefined, ts.factory.createTypeReferenceNode('string')),
|
|
6
8
|
ts.factory.createPropertySignature(undefined, 'variablesSchema', undefined, ts.factory.createTypeReferenceNode('z.ZodType')),
|
|
7
9
|
ts.factory.createPropertySignature(undefined, 'resultSchema', undefined, ts.factory.createTypeReferenceNode('z.ZodType'))
|
|
@@ -15,6 +15,8 @@ function parametersToFields(parameters) {
|
|
|
15
15
|
}
|
|
16
16
|
function generateOperationNode(schema, operation) {
|
|
17
17
|
return ts.factory.createVariableStatement(ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Export), ts.factory.createVariableDeclarationList([ts.factory.createVariableDeclaration(ts.factory.createIdentifier(operation.name + 'Operation'), undefined, undefined, ts.factory.createSatisfiesExpression(ts.factory.createAsExpression(ts.factory.createObjectLiteralExpression([
|
|
18
|
+
ts.factory.createPropertyAssignment('name', ts.factory.createStringLiteral(operation.name)),
|
|
19
|
+
ts.factory.createPropertyAssignment('type', ts.factory.createStringLiteral(operation.type)),
|
|
18
20
|
ts.factory.createPropertyAssignment('document', ts.factory.createStringLiteral([
|
|
19
21
|
operation.sourceText,
|
|
20
22
|
...extractFragmentSourceTextsInSpec(schema, operation.fragmentSpec)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vladimirdev635/gql-codegen",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.27",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"types": "./index.d.ts",
|
|
@@ -25,7 +25,8 @@
|
|
|
25
25
|
"description": "",
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"pino": "^9.7.0",
|
|
28
|
-
"typescript": "^5.8.3"
|
|
28
|
+
"typescript": "^5.8.3",
|
|
29
|
+
"zod": "^3.25.76"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
32
|
"@babel/eslint-parser": "^7.27.5",
|
|
@@ -35,7 +36,6 @@
|
|
|
35
36
|
"@typescript-eslint/eslint-plugin": "^8.34.0",
|
|
36
37
|
"eslint": "^9.28.0",
|
|
37
38
|
"jiti": "^2.4.2",
|
|
38
|
-
"vitest": "^3.2.4"
|
|
39
|
-
"zod": "^3.25.74"
|
|
39
|
+
"vitest": "^3.2.4"
|
|
40
40
|
}
|
|
41
41
|
}
|