@vladimirdev635/gql-codegen 0.0.108 → 0.0.110
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 +7 -1
- package/actors/ts/gql-client/actor.js +1 -1
- package/actors/ts/gql-client/generators/help-nodes/build-subscription-callback-function.d.ts +4 -0
- package/actors/ts/gql-client/generators/help-nodes/build-subscription-callback-function.js +35 -0
- package/actors/ts/gql-client/generators/help-nodes/build-sync-callback-function.d.ts +4 -0
- package/actors/ts/gql-client/generators/help-nodes/build-sync-callback-function.js +35 -0
- package/actors/ts/gql-client/generators/help-nodes/callback-arrow-func.d.ts +3 -0
- package/actors/ts/gql-client/generators/help-nodes/callback-arrow-func.js +15 -0
- package/actors/ts/gql-client/generators/help-nodes/index.d.ts +9 -0
- package/actors/ts/gql-client/generators/help-nodes/index.js +71 -0
- package/actors/ts/gql-client/generators/help-nodes/sdk-operations-type-node.d.ts +4 -0
- package/actors/ts/gql-client/generators/help-nodes/sdk-operations-type-node.js +17 -0
- package/actors/ts/gql-client/generators/help-nodes/sdk-type.d.ts +7 -0
- package/actors/ts/gql-client/generators/help-nodes/sdk-type.js +21 -0
- package/actors/ts/gql-client/generators/help-nodes/subscription-method-func-alias.d.ts +3 -0
- package/actors/ts/gql-client/generators/help-nodes/subscription-method-func-alias.js +14 -0
- package/actors/ts/gql-client/generators/help-nodes/sync-method-func-alias.d.ts +3 -0
- package/actors/ts/gql-client/generators/help-nodes/sync-method-func-alias.js +13 -0
- package/actors/ts/gql-client/generators/main.d.ts +1 -1
- package/actors/ts/gql-client/generators/main.js +41 -71
- package/actors/ts/gql-client/generators/operation-function-block.d.ts +3 -0
- package/actors/ts/gql-client/generators/operation-function-block.js +21 -0
- package/actors/ts/gql-client/generators/operation-return-type.d.ts +4 -0
- package/actors/ts/gql-client/generators/operation-return-type.js +16 -0
- package/actors/ts/gql-client-framework/actor.d.ts +42 -0
- package/actors/ts/gql-client-framework/actor.js +11 -0
- package/actors/ts/gql-client-framework/generators/help-nodes/index.d.ts +8 -0
- package/actors/ts/gql-client-framework/generators/help-nodes/index.js +55 -0
- package/actors/ts/gql-client-framework/generators/help-nodes/lazy-hook-builder.d.ts +4 -0
- package/actors/ts/gql-client-framework/generators/help-nodes/lazy-hook-builder.js +44 -0
- package/actors/ts/gql-client-framework/generators/help-nodes/sdk-operations-type-node.d.ts +4 -0
- package/actors/ts/gql-client-framework/generators/help-nodes/sdk-operations-type-node.js +45 -0
- package/actors/ts/gql-client-framework/generators/help-nodes/sdk-type.d.ts +7 -0
- package/actors/ts/gql-client-framework/generators/help-nodes/sdk-type.js +21 -0
- package/actors/ts/gql-client-framework/generators/help-nodes/subscription-hook-builders.d.ts +4 -0
- package/actors/ts/gql-client-framework/generators/help-nodes/subscription-hook-builders.js +52 -0
- package/actors/ts/gql-client-framework/generators/help-nodes/sync-hook-builders.d.ts +4 -0
- package/actors/ts/gql-client-framework/generators/help-nodes/sync-hook-builders.js +52 -0
- package/actors/ts/gql-client-framework/generators/main.js +88 -0
- package/actors/ts/gql-client-framework/index.d.ts +1 -0
- package/actors/ts/gql-client-framework/index.js +1 -0
- package/actors/ts/gql-client-react/actor.d.ts +5 -12
- package/actors/ts/gql-client-react/actor.js +10 -9
- package/actors/ts/gql-client-react/index.d.ts +1 -1
- package/actors/ts/gql-client-vue/actor.d.ts +5 -12
- package/actors/ts/gql-client-vue/actor.js +27 -9
- package/actors/ts/gql-client-vue/index.d.ts +1 -1
- package/actors/ts/graphql/actor.d.ts +6 -5
- package/actors/ts/graphql/actor.js +4 -4
- package/actors/ts/graphql/generators/client/fragments.d.ts +3 -3
- package/actors/ts/graphql/generators/client/fragments.js +24 -21
- package/actors/ts/graphql/generators/client/main.d.ts +3 -3
- package/actors/ts/graphql/generators/client/main.js +1 -18
- package/actors/ts/graphql/generators/client/operations.d.ts +4 -3
- package/actors/ts/graphql/generators/client/operations.js +22 -24
- package/actors/ts/graphql/generators/main.d.ts +2 -2
- package/actors/ts/graphql/generators/main.js +1 -1
- package/actors/ts/graphql/generators/server/inputs.d.ts +1 -1
- package/actors/ts/graphql/generators/server/inputs.js +6 -6
- package/actors/ts/graphql/generators/server/main.d.ts +2 -2
- package/actors/ts/graphql/generators/server/objects.js +2 -2
- package/actors/ts/graphql/generators/server/scalars/additional.d.ts +1 -0
- package/actors/ts/graphql/generators/server/scalars/additional.js +2 -1
- package/actors/ts/graphql/generators/server/shared.d.ts +3 -2
- package/actors/ts/graphql/generators/server/shared.js +10 -1
- package/actors/ts/graphql/generators/server/unions.js +2 -2
- package/actors/ts/graphql/index.d.ts +1 -1
- package/actors/ts/graphql/index.js +1 -1
- package/actors/ts/index.d.ts +2 -1
- package/actors/ts/index.js +1 -0
- package/actors/ts/shared.d.ts +5 -0
- package/actors/ts/utils.d.ts +2 -4
- package/actors/ts/utils.js +4 -3
- package/package.json +1 -1
- package/schema/client/operation.d.ts +3 -0
- package/schema/utils.d.ts +6 -6
- package/schema/utils.js +8 -6
- package/actors/ts/gql-client-react/generators/main.d.ts +0 -4
- package/actors/ts/gql-client-react/generators/main.js +0 -121
- package/actors/ts/gql-client-vue/generators/main.js +0 -135
- /package/actors/ts/{gql-client-vue → gql-client-framework}/generators/main.d.ts +0 -0
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { PathOrFileDescriptor } from 'fs';
|
|
2
2
|
import ts from 'typescript';
|
|
3
3
|
import { Actor, ActorContext } from '../../../config.js';
|
|
4
|
-
import { TSActorConfig } from '../shared.js';
|
|
4
|
+
import { ClientTypeNameBuilders, TSActorConfig } from '../shared.js';
|
|
5
|
+
import { OperationType } from '../../../schema/client/operation.js';
|
|
5
6
|
export type OperationReturnType = 'ExecuteResult' | 'ExecuteResult.result';
|
|
6
7
|
export interface SDKConfig {
|
|
7
8
|
defaultOperationReturnType: OperationReturnType;
|
|
@@ -9,6 +10,11 @@ export interface SDKConfig {
|
|
|
9
10
|
queriesKey: string;
|
|
10
11
|
mutationsKey: string;
|
|
11
12
|
subscriptionsKey: string;
|
|
13
|
+
gqlSyncMethodFuncTypeName: string;
|
|
14
|
+
gqlSubscriptionMethodFuncTypeName: string;
|
|
15
|
+
operationRequestsTypeNameBuilder: (type: OperationType) => string;
|
|
16
|
+
typeName: string;
|
|
17
|
+
clientTypeNameBuilders: ClientTypeNameBuilders;
|
|
12
18
|
}
|
|
13
19
|
export interface Config extends TSActorConfig {
|
|
14
20
|
outPath: PathOrFileDescriptor;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { writeFileSync } from 'fs';
|
|
2
|
-
import { renderNodes } from '../shared.js';
|
|
2
|
+
import { renderNodes, } from '../shared.js';
|
|
3
3
|
import { generateNodes } from './generators/main.js';
|
|
4
4
|
async function actor(config, context) {
|
|
5
5
|
const nodes = generateNodes(config, context);
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { Config, OperationReturnType } from '../../actor.js';
|
|
3
|
+
export declare function createBuildSubscriptionCallbackFunctionName(returnType: OperationReturnType): string;
|
|
4
|
+
export declare function generateBuildSubscriptionCallbackFunction(config: Config, returnType: OperationReturnType): ts.Node;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/* oxlint-disable max-lines */
|
|
2
|
+
import ts from 'typescript';
|
|
3
|
+
import { generateCallbackArrowFunction } from './callback-arrow-func.js';
|
|
4
|
+
export function createBuildSubscriptionCallbackFunctionName(returnType) {
|
|
5
|
+
switch (returnType) {
|
|
6
|
+
case 'ExecuteResult':
|
|
7
|
+
return 'buildSubscriptionExecuteResultCallback';
|
|
8
|
+
case 'ExecuteResult.result':
|
|
9
|
+
return 'buildSubscriptionResultCallback';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export function generateBuildSubscriptionCallbackFunction(config, returnType) {
|
|
13
|
+
return ts.factory.createFunctionDeclaration(undefined, undefined, ts.factory.createIdentifier(createBuildSubscriptionCallbackFunctionName(returnType)), [
|
|
14
|
+
ts.factory.createTypeParameterDeclaration(undefined, ts.factory.createIdentifier('TExecutor'), ts.factory.createTypeReferenceNode('types.IExecutor', [
|
|
15
|
+
ts.factory.createTypeReferenceNode('TRequestContext'),
|
|
16
|
+
]), undefined),
|
|
17
|
+
ts.factory.createTypeParameterDeclaration(undefined, ts.factory.createIdentifier('TRequestContext'), ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('types.RequestContext'), undefined), undefined),
|
|
18
|
+
ts.factory.createTypeParameterDeclaration(undefined, ts.factory.createIdentifier('V'), undefined, undefined),
|
|
19
|
+
ts.factory.createTypeParameterDeclaration(undefined, ts.factory.createIdentifier('R'), undefined, undefined),
|
|
20
|
+
], [
|
|
21
|
+
ts.factory.createParameterDeclaration(undefined, undefined, ts.factory.createIdentifier('executor'), undefined, ts.factory.createTypeReferenceNode('TExecutor'), undefined),
|
|
22
|
+
ts.factory.createParameterDeclaration(undefined, undefined, ts.factory.createIdentifier('operation'), undefined, ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('types.SubscriptionOperation'), [
|
|
23
|
+
ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('V'), undefined),
|
|
24
|
+
ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('R'), undefined),
|
|
25
|
+
]), undefined),
|
|
26
|
+
], ts.factory.createTypeReferenceNode(ts.factory.createIdentifier(config.sdk.gqlSubscriptionMethodFuncTypeName), [
|
|
27
|
+
ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('TRequestContext'), undefined),
|
|
28
|
+
ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('V'), undefined),
|
|
29
|
+
returnType === 'ExecuteResult.result'
|
|
30
|
+
? ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('R'))
|
|
31
|
+
: ts.factory.createTypeReferenceNode('types.ExecuteResult', [ts.factory.createTypeReferenceNode('R')]),
|
|
32
|
+
]), ts.factory.createBlock([
|
|
33
|
+
ts.factory.createReturnStatement(generateCallbackArrowFunction(config, 'V', 'R', true, returnType)),
|
|
34
|
+
], true));
|
|
35
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { Config, OperationReturnType } from '../../actor.js';
|
|
3
|
+
export declare function createBuildSyncCallbackFunctionName(returnType: OperationReturnType): string;
|
|
4
|
+
export declare function generateBuildSyncCallbackFunction(config: Config, returnType: OperationReturnType): ts.Node;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/* oxlint-disable max-lines */
|
|
2
|
+
import ts from 'typescript';
|
|
3
|
+
import { generateCallbackArrowFunction } from './callback-arrow-func.js';
|
|
4
|
+
export function createBuildSyncCallbackFunctionName(returnType) {
|
|
5
|
+
switch (returnType) {
|
|
6
|
+
case 'ExecuteResult':
|
|
7
|
+
return 'buildSyncExecuteResultCallback';
|
|
8
|
+
case 'ExecuteResult.result':
|
|
9
|
+
return 'buildSyncResultCallback';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export function generateBuildSyncCallbackFunction(config, returnType) {
|
|
13
|
+
return ts.factory.createFunctionDeclaration(undefined, undefined, ts.factory.createIdentifier(createBuildSyncCallbackFunctionName(returnType)), [
|
|
14
|
+
ts.factory.createTypeParameterDeclaration(undefined, ts.factory.createIdentifier('TExecutor'), ts.factory.createTypeReferenceNode('types.IExecutor', [
|
|
15
|
+
ts.factory.createTypeReferenceNode('TRequestContext'),
|
|
16
|
+
]), undefined),
|
|
17
|
+
ts.factory.createTypeParameterDeclaration(undefined, ts.factory.createIdentifier('TRequestContext'), ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('types.RequestContext'), undefined), undefined),
|
|
18
|
+
ts.factory.createTypeParameterDeclaration(undefined, ts.factory.createIdentifier('V'), undefined, undefined),
|
|
19
|
+
ts.factory.createTypeParameterDeclaration(undefined, ts.factory.createIdentifier('R'), undefined, undefined),
|
|
20
|
+
], [
|
|
21
|
+
ts.factory.createParameterDeclaration(undefined, undefined, ts.factory.createIdentifier('executor'), undefined, ts.factory.createTypeReferenceNode('TExecutor'), undefined),
|
|
22
|
+
ts.factory.createParameterDeclaration(undefined, undefined, ts.factory.createIdentifier('operation'), undefined, ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('types.SyncOperation'), [
|
|
23
|
+
ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('V'), undefined),
|
|
24
|
+
ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('R'), undefined),
|
|
25
|
+
]), undefined),
|
|
26
|
+
], ts.factory.createTypeReferenceNode(ts.factory.createIdentifier(config.sdk.gqlSyncMethodFuncTypeName), [
|
|
27
|
+
ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('TRequestContext'), undefined),
|
|
28
|
+
ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('V'), undefined),
|
|
29
|
+
returnType === 'ExecuteResult.result'
|
|
30
|
+
? ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('R'))
|
|
31
|
+
: ts.factory.createTypeReferenceNode('types.ExecuteResult', [ts.factory.createTypeReferenceNode('R')]),
|
|
32
|
+
]), ts.factory.createBlock([
|
|
33
|
+
ts.factory.createReturnStatement(generateCallbackArrowFunction(config, 'V', 'R', false, returnType)),
|
|
34
|
+
], true));
|
|
35
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { Config, OperationReturnType } from '../../actor.js';
|
|
3
|
+
export declare function generateCallbackArrowFunction(config: Config, variablesTypeName: string, resultTypeName: string, isSubscription: boolean, returnType: OperationReturnType): ts.ArrowFunction;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { generateFunctionBlock } from '../operation-function-block.js';
|
|
3
|
+
import { createReturnTypeNode } from '../operation-return-type.js';
|
|
4
|
+
export function generateCallbackArrowFunction(config, variablesTypeName, resultTypeName, isSubscription, returnType) {
|
|
5
|
+
const funcArgs = [
|
|
6
|
+
ts.factory.createParameterDeclaration(undefined, undefined, 'variables', undefined, ts.factory.createTypeReferenceNode(variablesTypeName)),
|
|
7
|
+
ts.factory.createParameterDeclaration(undefined, undefined, 'requestContext', undefined, ts.factory.createTypeReferenceNode('TRequestContext')),
|
|
8
|
+
];
|
|
9
|
+
if (isSubscription) {
|
|
10
|
+
funcArgs.push(ts.factory.createParameterDeclaration(undefined, undefined, 'controller', undefined, ts.factory.createTypeReferenceNode('AbortController')));
|
|
11
|
+
}
|
|
12
|
+
return ts.factory.createArrowFunction(ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Async), undefined, funcArgs, ts.factory.createTypeReferenceNode('Promise', [
|
|
13
|
+
createReturnTypeNode(resultTypeName, returnType, isSubscription),
|
|
14
|
+
]), ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), generateFunctionBlock(isSubscription, returnType));
|
|
15
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ActorContext } from '../../../../../config.js';
|
|
2
|
+
import { Config, OperationReturnType } from '../../actor.js';
|
|
3
|
+
import ts from 'typescript';
|
|
4
|
+
export declare function hasReturnType(config: Config, returnType: OperationReturnType): boolean;
|
|
5
|
+
export declare function generateHelpNodes(config: Config, context: ActorContext, state: {
|
|
6
|
+
hasQueries: boolean;
|
|
7
|
+
hasMutations: boolean;
|
|
8
|
+
hasSubscriptions: boolean;
|
|
9
|
+
}): ts.Node[];
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { generateSdkTypeNode } from './sdk-operations-type-node.js';
|
|
2
|
+
import { generateSdkType } from './sdk-type.js';
|
|
3
|
+
import { generateBuildSyncCallbackFunction } from './build-sync-callback-function.js';
|
|
4
|
+
import { generateBuildSubscriptionCallbackFunction } from './build-subscription-callback-function.js';
|
|
5
|
+
import { generateSyncMethodFuncAlias } from './sync-method-func-alias.js';
|
|
6
|
+
import { generateSubscriptionMethodFuncAlias } from './subscription-method-func-alias.js';
|
|
7
|
+
function generateSdkTypeNodes(config, context, state) {
|
|
8
|
+
return [
|
|
9
|
+
...(state.hasQueries
|
|
10
|
+
? [
|
|
11
|
+
generateSdkTypeNode(config, 'QUERY', context.schema.client.operations),
|
|
12
|
+
]
|
|
13
|
+
: []),
|
|
14
|
+
...(state.hasMutations
|
|
15
|
+
? [
|
|
16
|
+
generateSdkTypeNode(config, 'MUTATION', context.schema.client.operations),
|
|
17
|
+
]
|
|
18
|
+
: []),
|
|
19
|
+
...(state.hasSubscriptions
|
|
20
|
+
? [
|
|
21
|
+
generateSdkTypeNode(config, 'SUBSCRIPTION', context.schema.client.operations),
|
|
22
|
+
]
|
|
23
|
+
: []),
|
|
24
|
+
];
|
|
25
|
+
}
|
|
26
|
+
export function hasReturnType(config, returnType) {
|
|
27
|
+
return (config.sdk.defaultOperationReturnType == returnType ||
|
|
28
|
+
Object.values(config.sdk.operationReturnTypeMapping).includes(returnType));
|
|
29
|
+
}
|
|
30
|
+
export function generateHelpNodes(config, context, state) {
|
|
31
|
+
const hasExecuteResultType = hasReturnType(config, 'ExecuteResult');
|
|
32
|
+
const hasResultType = hasReturnType(config, 'ExecuteResult.result');
|
|
33
|
+
return [
|
|
34
|
+
...(state.hasQueries || state.hasMutations
|
|
35
|
+
? [generateSyncMethodFuncAlias(config)]
|
|
36
|
+
: []),
|
|
37
|
+
...(state.hasSubscriptions
|
|
38
|
+
? [generateSubscriptionMethodFuncAlias(config)]
|
|
39
|
+
: []),
|
|
40
|
+
...generateSdkTypeNodes(config, context, state),
|
|
41
|
+
generateSdkType(config, state),
|
|
42
|
+
...(state.hasQueries || state.hasMutations
|
|
43
|
+
? [
|
|
44
|
+
...(hasExecuteResultType
|
|
45
|
+
? [
|
|
46
|
+
generateBuildSyncCallbackFunction(config, 'ExecuteResult'),
|
|
47
|
+
]
|
|
48
|
+
: []),
|
|
49
|
+
...(hasResultType
|
|
50
|
+
? [
|
|
51
|
+
generateBuildSyncCallbackFunction(config, 'ExecuteResult.result'),
|
|
52
|
+
]
|
|
53
|
+
: []),
|
|
54
|
+
]
|
|
55
|
+
: []),
|
|
56
|
+
...(state.hasSubscriptions
|
|
57
|
+
? [
|
|
58
|
+
...(hasExecuteResultType
|
|
59
|
+
? [
|
|
60
|
+
generateBuildSubscriptionCallbackFunction(config, 'ExecuteResult'),
|
|
61
|
+
]
|
|
62
|
+
: []),
|
|
63
|
+
...(hasResultType
|
|
64
|
+
? [
|
|
65
|
+
generateBuildSubscriptionCallbackFunction(config, 'ExecuteResult.result'),
|
|
66
|
+
]
|
|
67
|
+
: []),
|
|
68
|
+
]
|
|
69
|
+
: []),
|
|
70
|
+
];
|
|
71
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Operation, OperationType } from '../../../../../schema/client/operation.js';
|
|
2
|
+
import ts from 'typescript';
|
|
3
|
+
import { Config } from '../../actor.js';
|
|
4
|
+
export declare function generateSdkTypeNode(config: Config, type: OperationType, operations: Record<string, Operation>): ts.InterfaceDeclaration;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
function generateOperationPropertySignature(config, type, operation) {
|
|
3
|
+
return ts.factory.createPropertySignature(undefined, ts.factory.createIdentifier(operation.name), undefined, ts.factory.createTypeReferenceNode(type !== 'SUBSCRIPTION'
|
|
4
|
+
? config.sdk.gqlSyncMethodFuncTypeName
|
|
5
|
+
: config.sdk.gqlSubscriptionMethodFuncTypeName, [
|
|
6
|
+
ts.factory.createTypeReferenceNode('TRequestContext'),
|
|
7
|
+
ts.factory.createTypeReferenceNode(operation.name + 'Variables'),
|
|
8
|
+
ts.factory.createTypeReferenceNode(operation.name + 'Result', undefined),
|
|
9
|
+
]));
|
|
10
|
+
}
|
|
11
|
+
export function generateSdkTypeNode(config, type, operations) {
|
|
12
|
+
return ts.factory.createInterfaceDeclaration([ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], ts.factory.createIdentifier(config.sdk.operationRequestsTypeNameBuilder(type)), [
|
|
13
|
+
ts.factory.createTypeParameterDeclaration(undefined, ts.factory.createIdentifier('TRequestContext'), undefined, undefined),
|
|
14
|
+
], undefined, Object.values(operations)
|
|
15
|
+
.filter((op) => op.type == type)
|
|
16
|
+
.map((op) => generateOperationPropertySignature(config, type, op)));
|
|
17
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
function generateSdkPropertySignature(key, typeName) {
|
|
3
|
+
return ts.factory.createPropertySignature(undefined, ts.factory.createIdentifier(key), undefined, ts.factory.createTypeReferenceNode(ts.factory.createIdentifier(typeName), [
|
|
4
|
+
ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('TRequestContext'), undefined),
|
|
5
|
+
]));
|
|
6
|
+
}
|
|
7
|
+
export function generateSdkType(config, state) {
|
|
8
|
+
const properties = [];
|
|
9
|
+
if (state.hasQueries) {
|
|
10
|
+
properties.push(generateSdkPropertySignature(config.sdk.queriesKey, config.sdk.operationRequestsTypeNameBuilder('QUERY')));
|
|
11
|
+
}
|
|
12
|
+
if (state.hasMutations) {
|
|
13
|
+
properties.push(generateSdkPropertySignature(config.sdk.mutationsKey, config.sdk.operationRequestsTypeNameBuilder('MUTATION')));
|
|
14
|
+
}
|
|
15
|
+
if (state.hasSubscriptions) {
|
|
16
|
+
properties.push(generateSdkPropertySignature(config.sdk.subscriptionsKey, config.sdk.operationRequestsTypeNameBuilder('SUBSCRIPTION')));
|
|
17
|
+
}
|
|
18
|
+
return ts.factory.createInterfaceDeclaration([ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], ts.factory.createIdentifier(config.sdk.typeName), [
|
|
19
|
+
ts.factory.createTypeParameterDeclaration(undefined, ts.factory.createIdentifier('TRequestContext'), undefined, undefined),
|
|
20
|
+
], undefined, properties);
|
|
21
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
export function generateSubscriptionMethodFuncAlias(config) {
|
|
3
|
+
return ts.factory.createTypeAliasDeclaration(undefined, ts.factory.createIdentifier(config.sdk.gqlSubscriptionMethodFuncTypeName), [
|
|
4
|
+
ts.factory.createTypeParameterDeclaration(undefined, ts.factory.createIdentifier('TRequestContext'), undefined, undefined),
|
|
5
|
+
ts.factory.createTypeParameterDeclaration(undefined, ts.factory.createIdentifier('V'), undefined, undefined),
|
|
6
|
+
ts.factory.createTypeParameterDeclaration(undefined, ts.factory.createIdentifier('R'), undefined, undefined),
|
|
7
|
+
], ts.factory.createFunctionTypeNode(undefined, [
|
|
8
|
+
ts.factory.createParameterDeclaration(undefined, undefined, ts.factory.createIdentifier('variables'), undefined, ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('V'), undefined), undefined),
|
|
9
|
+
ts.factory.createParameterDeclaration(undefined, undefined, ts.factory.createIdentifier('context'), undefined, ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('TRequestContext'), undefined), undefined),
|
|
10
|
+
ts.factory.createParameterDeclaration(undefined, undefined, ts.factory.createIdentifier('controller'), undefined, ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('AbortController'), undefined), undefined),
|
|
11
|
+
], ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('Promise'), [
|
|
12
|
+
ts.factory.createTypeReferenceNode('types.SubOpAsyncIterable', [ts.factory.createTypeReferenceNode('R')]),
|
|
13
|
+
])));
|
|
14
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
export function generateSyncMethodFuncAlias(config) {
|
|
3
|
+
return ts.factory.createTypeAliasDeclaration(undefined, ts.factory.createIdentifier(config.sdk.gqlSyncMethodFuncTypeName), [
|
|
4
|
+
ts.factory.createTypeParameterDeclaration(undefined, ts.factory.createIdentifier('TRequestContext'), undefined, undefined),
|
|
5
|
+
ts.factory.createTypeParameterDeclaration(undefined, ts.factory.createIdentifier('V'), undefined, undefined),
|
|
6
|
+
ts.factory.createTypeParameterDeclaration(undefined, ts.factory.createIdentifier('R'), undefined, undefined),
|
|
7
|
+
], ts.factory.createFunctionTypeNode(undefined, [
|
|
8
|
+
ts.factory.createParameterDeclaration(undefined, undefined, ts.factory.createIdentifier('variables'), undefined, ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('V'), undefined), undefined),
|
|
9
|
+
ts.factory.createParameterDeclaration(undefined, undefined, ts.factory.createIdentifier('context'), undefined, ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('TRequestContext'), undefined), undefined),
|
|
10
|
+
], ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('Promise'), [
|
|
11
|
+
ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('R'), undefined),
|
|
12
|
+
])));
|
|
13
|
+
}
|
|
@@ -1,65 +1,31 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
ts.factory.createIdentifier('requestContext'),
|
|
7
|
-
];
|
|
8
|
-
if (operationType === 'SUBSCRIPTION') {
|
|
9
|
-
callArgs.push(ts.factory.createIdentifier('controller'));
|
|
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));
|
|
14
|
-
if (returnType === 'ExecuteResult') {
|
|
15
|
-
return ts.factory.createBlock([ts.factory.createReturnStatement(awaitExpression)], true);
|
|
16
|
-
}
|
|
17
|
-
return ts.factory.createBlock([
|
|
18
|
-
ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
19
|
-
ts.factory.createVariableDeclaration('executorResult', undefined, undefined, awaitExpression),
|
|
20
|
-
], ts.NodeFlags.Const)),
|
|
21
|
-
ts.factory.createReturnStatement(ts.factory.createPropertyAccessChain(ts.factory.createIdentifier('executorResult'), undefined, 'result')),
|
|
22
|
-
], true);
|
|
23
|
-
}
|
|
24
|
-
function createReturnTypeNode(resultName, returnType, operationType) {
|
|
25
|
-
const rOpType = ts.factory.createTypeReferenceNode(resultName);
|
|
26
|
-
const rType = operationType === 'SUBSCRIPTION'
|
|
27
|
-
? ts.factory.createTypeReferenceNode('SubOpAsyncIterable', [
|
|
28
|
-
rOpType,
|
|
29
|
-
])
|
|
30
|
-
: rOpType;
|
|
31
|
-
if (returnType === 'ExecuteResult.result')
|
|
32
|
-
return rType;
|
|
33
|
-
return ts.factory.createTypeReferenceNode('ExecuteResult', [rType]);
|
|
34
|
-
}
|
|
35
|
-
function getReturnTypeFromConfig(config, operationName) {
|
|
36
|
-
return (config.sdk.operationReturnTypeMapping[operationName] ||
|
|
37
|
-
config.sdk.defaultOperationReturnType);
|
|
38
|
-
}
|
|
2
|
+
import { createBuildSubscriptionCallbackFunctionName } from './help-nodes/build-subscription-callback-function.js';
|
|
3
|
+
import { createBuildSyncCallbackFunctionName } from './help-nodes/build-sync-callback-function.js';
|
|
4
|
+
import { generateHelpNodes } from './help-nodes/index.js';
|
|
5
|
+
import { getReturnTypeFromConfig } from './operation-return-type.js';
|
|
39
6
|
export function generateNodes(config, context) {
|
|
40
7
|
const graphqlImports = [];
|
|
41
|
-
let shouldIncludeExecuteResultType = false;
|
|
42
8
|
const queryNodes = [];
|
|
43
9
|
const mutationNodes = [];
|
|
44
10
|
const subscriptionNodes = [];
|
|
45
11
|
for (const operation of Object.values(context.schema.client.operations)) {
|
|
46
|
-
const operationName = operation.name
|
|
47
|
-
const variablesName = operation.name
|
|
48
|
-
const resultName = operation.name
|
|
49
|
-
graphqlImports.push(operationName, variablesName, resultName);
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
ts.factory.
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
])
|
|
12
|
+
const operationName = config.sdk.clientTypeNameBuilders.operationTypeName(operation.name);
|
|
13
|
+
const variablesName = config.sdk.clientTypeNameBuilders.variablesTypeName(operation.name);
|
|
14
|
+
const resultName = config.sdk.clientTypeNameBuilders.resultTypeName(operation.name);
|
|
15
|
+
graphqlImports.push(ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier(operationName)), ts.factory.createImportSpecifier(true, undefined, ts.factory.createIdentifier(variablesName)), ts.factory.createImportSpecifier(true, undefined, ts.factory.createIdentifier(resultName)));
|
|
16
|
+
const operationReturnType = getReturnTypeFromConfig(config, operation.name);
|
|
17
|
+
const functionName = operation.type === 'SUBSCRIPTION'
|
|
18
|
+
? createBuildSubscriptionCallbackFunctionName(operationReturnType)
|
|
19
|
+
: createBuildSyncCallbackFunctionName(operationReturnType);
|
|
20
|
+
const propAssignment = ts.factory.createPropertyAssignment(operation.name, ts.factory.createCallExpression(ts.factory.createIdentifier(functionName), [
|
|
21
|
+
ts.factory.createTypeReferenceNode('TExecutor', undefined),
|
|
22
|
+
ts.factory.createTypeReferenceNode('TRequestContext', undefined),
|
|
23
|
+
ts.factory.createTypeReferenceNode(variablesName, undefined),
|
|
24
|
+
ts.factory.createTypeReferenceNode(resultName, undefined),
|
|
25
|
+
], [
|
|
26
|
+
ts.factory.createIdentifier('executor'),
|
|
27
|
+
ts.factory.createIdentifier(operationName),
|
|
28
|
+
]));
|
|
63
29
|
switch (operation.type) {
|
|
64
30
|
case 'QUERY': {
|
|
65
31
|
queryNodes.push(propAssignment);
|
|
@@ -76,36 +42,40 @@ export function generateNodes(config, context) {
|
|
|
76
42
|
}
|
|
77
43
|
}
|
|
78
44
|
const gqlClientImports = [
|
|
79
|
-
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('
|
|
80
|
-
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('RequestContext')),
|
|
45
|
+
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('types')),
|
|
81
46
|
];
|
|
82
|
-
if (shouldIncludeExecuteResultType) {
|
|
83
|
-
gqlClientImports.push(ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('ExecuteResult')));
|
|
84
|
-
}
|
|
85
47
|
const returnObjectNodes = [];
|
|
86
|
-
|
|
48
|
+
const state = {
|
|
49
|
+
hasQueries: queryNodes.length !== 0,
|
|
50
|
+
hasMutations: mutationNodes.length !== 0,
|
|
51
|
+
hasSubscriptions: subscriptionNodes.length !== 0,
|
|
52
|
+
};
|
|
53
|
+
if (state.hasQueries) {
|
|
87
54
|
returnObjectNodes.push(ts.factory.createPropertyAssignment(config.sdk.queriesKey, ts.factory.createObjectLiteralExpression(queryNodes, true)));
|
|
88
55
|
}
|
|
89
|
-
if (
|
|
56
|
+
if (state.hasMutations) {
|
|
90
57
|
returnObjectNodes.push(ts.factory.createPropertyAssignment(config.sdk.mutationsKey, ts.factory.createObjectLiteralExpression(mutationNodes, true)));
|
|
91
58
|
}
|
|
92
|
-
if (
|
|
93
|
-
gqlClientImports.push(ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('SubOpAsyncIterable')));
|
|
59
|
+
if (state.hasSubscriptions) {
|
|
94
60
|
returnObjectNodes.push(ts.factory.createPropertyAssignment(config.sdk.subscriptionsKey, ts.factory.createObjectLiteralExpression(subscriptionNodes, true)));
|
|
95
61
|
}
|
|
96
62
|
return [
|
|
97
|
-
ts.factory.createIdentifier('// @ts-nocheck'),
|
|
98
63
|
...config.importDeclarations,
|
|
99
|
-
ts.factory.createImportDeclaration([], ts.factory.createImportClause(true, undefined, ts.factory.createNamedImports(gqlClientImports)), ts.factory.createStringLiteral('@vladimirdev635/gql-client
|
|
100
|
-
ts.factory.createImportDeclaration(undefined, ts.factory.createImportClause(
|
|
64
|
+
ts.factory.createImportDeclaration([], ts.factory.createImportClause(true, undefined, ts.factory.createNamedImports(gqlClientImports)), ts.factory.createStringLiteral('@vladimirdev635/gql-client')),
|
|
65
|
+
ts.factory.createImportDeclaration(undefined, ts.factory.createImportClause(undefined, undefined, ts.factory.createNamedImports(graphqlImports)), ts.factory.createStringLiteral(config.graphqlModulePath)),
|
|
66
|
+
ts.factory.createIdentifier('\n'),
|
|
67
|
+
...generateHelpNodes(config, context, state),
|
|
101
68
|
ts.factory.createIdentifier('\n'),
|
|
102
69
|
ts.factory.createFunctionDeclaration(ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Export), undefined, 'createSdk', [
|
|
103
|
-
ts.factory.createTypeParameterDeclaration(undefined, '
|
|
104
|
-
], [
|
|
105
|
-
ts.factory.createParameterDeclaration(undefined, undefined, 'executor', undefined, ts.factory.createTypeReferenceNode('IExecutor', [
|
|
70
|
+
ts.factory.createTypeParameterDeclaration(undefined, 'TExecutor', ts.factory.createTypeReferenceNode('types.IExecutor', [
|
|
106
71
|
ts.factory.createTypeReferenceNode('TRequestContext'),
|
|
107
72
|
])),
|
|
108
|
-
|
|
73
|
+
ts.factory.createTypeParameterDeclaration(undefined, 'TRequestContext', ts.factory.createTypeReferenceNode('types.RequestContext')),
|
|
74
|
+
], [
|
|
75
|
+
ts.factory.createParameterDeclaration(undefined, undefined, 'executor', undefined, ts.factory.createTypeReferenceNode('TExecutor')),
|
|
76
|
+
], ts.factory.createTypeReferenceNode('SdkType', [
|
|
77
|
+
ts.factory.createTypeReferenceNode('TRequestContext'),
|
|
78
|
+
]), ts.factory.createBlock([
|
|
109
79
|
ts.factory.createReturnStatement(ts.factory.createAsExpression(ts.factory.createObjectLiteralExpression(returnObjectNodes, true), ts.factory.createTypeReferenceNode('const'))),
|
|
110
80
|
], true)),
|
|
111
81
|
];
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
export function generateFunctionBlock(isSubscription, returnType) {
|
|
3
|
+
const callArgs = [
|
|
4
|
+
ts.factory.createIdentifier('operation'),
|
|
5
|
+
ts.factory.createIdentifier('variables'),
|
|
6
|
+
ts.factory.createIdentifier('requestContext'),
|
|
7
|
+
];
|
|
8
|
+
if (isSubscription) {
|
|
9
|
+
callArgs.push(ts.factory.createIdentifier('controller'));
|
|
10
|
+
}
|
|
11
|
+
const awaitExpression = ts.factory.createAwaitExpression(ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('executor'), isSubscription ? 'executeSubscription' : 'executeSync'), undefined, callArgs));
|
|
12
|
+
if (returnType === 'ExecuteResult') {
|
|
13
|
+
return ts.factory.createBlock([ts.factory.createReturnStatement(awaitExpression)], true);
|
|
14
|
+
}
|
|
15
|
+
return ts.factory.createBlock([
|
|
16
|
+
ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
17
|
+
ts.factory.createVariableDeclaration('executorResult', undefined, undefined, awaitExpression),
|
|
18
|
+
], ts.NodeFlags.Const)),
|
|
19
|
+
ts.factory.createReturnStatement(ts.factory.createPropertyAccessChain(ts.factory.createIdentifier('executorResult'), undefined, 'result')),
|
|
20
|
+
], true);
|
|
21
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { Config, OperationReturnType } from '../actor.js';
|
|
3
|
+
export declare function createReturnTypeNode(resultName: string, returnType: OperationReturnType, isSubscription: boolean): ts.TypeReferenceNode;
|
|
4
|
+
export declare function getReturnTypeFromConfig(config: Config, operationName: string): OperationReturnType;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
export function createReturnTypeNode(resultName, returnType, isSubscription) {
|
|
3
|
+
const rOpType = ts.factory.createTypeReferenceNode(resultName);
|
|
4
|
+
const rType = isSubscription
|
|
5
|
+
? ts.factory.createTypeReferenceNode('types.SubOpAsyncIterable', [
|
|
6
|
+
rOpType,
|
|
7
|
+
])
|
|
8
|
+
: rOpType;
|
|
9
|
+
if (returnType === 'ExecuteResult.result')
|
|
10
|
+
return rType;
|
|
11
|
+
return ts.factory.createTypeReferenceNode('types.ExecuteResult', [rType]);
|
|
12
|
+
}
|
|
13
|
+
export function getReturnTypeFromConfig(config, operationName) {
|
|
14
|
+
return (config.sdk.operationReturnTypeMapping[operationName] ||
|
|
15
|
+
config.sdk.defaultOperationReturnType);
|
|
16
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Actor, ActorContext } from '../../../config.js';
|
|
2
|
+
import { OperationType } from '../../../schema/client/operation.js';
|
|
3
|
+
import { PathOrFileDescriptor } from 'fs';
|
|
4
|
+
import ts from 'typescript';
|
|
5
|
+
import { ClientTypeNameBuilders, TSActorConfig } from '../shared.js';
|
|
6
|
+
export interface HookNameBuilders {
|
|
7
|
+
query: {
|
|
8
|
+
immediate: (operationName: string) => string;
|
|
9
|
+
lazy: (operationName: string) => string;
|
|
10
|
+
};
|
|
11
|
+
mutation: {
|
|
12
|
+
lazy: (operationName: string) => string;
|
|
13
|
+
};
|
|
14
|
+
subscription: {
|
|
15
|
+
immediate: (operationName: string) => string;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export interface SDKConfig {
|
|
19
|
+
typeName: string;
|
|
20
|
+
queriesKey: string;
|
|
21
|
+
mutationsKey: string;
|
|
22
|
+
subscriptionsKey: string;
|
|
23
|
+
clientTypeNameBuilders: ClientTypeNameBuilders;
|
|
24
|
+
hookNameBuilders: HookNameBuilders;
|
|
25
|
+
operationHooksTypeNameBuilder: (type: OperationType) => string;
|
|
26
|
+
lazyHookBuilderName: string;
|
|
27
|
+
lazyHookTypeName: string;
|
|
28
|
+
syncHookBuilderName: string;
|
|
29
|
+
syncHookTypeName: string;
|
|
30
|
+
subscriptionHookBuilderName: string;
|
|
31
|
+
subscriptionHookTypeName: string;
|
|
32
|
+
buildVariablesType: (variablesTypeNode: ts.TypeReferenceNode) => ts.TypeNode;
|
|
33
|
+
buildRequestContextType: (requestContextTypeNode: ts.TypeReferenceNode) => ts.TypeNode;
|
|
34
|
+
}
|
|
35
|
+
export interface Config extends TSActorConfig {
|
|
36
|
+
outPath: PathOrFileDescriptor;
|
|
37
|
+
importDeclarations: ts.ImportDeclaration[];
|
|
38
|
+
frameworkImportName: string;
|
|
39
|
+
graphqlModulePath: string;
|
|
40
|
+
sdk: SDKConfig;
|
|
41
|
+
}
|
|
42
|
+
export declare function build(config: Config): 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 actor(config, context) {
|
|
5
|
+
const nodes = generateNodes(config, context);
|
|
6
|
+
const code = await renderNodes(config, nodes);
|
|
7
|
+
writeFileSync(config.outPath, code);
|
|
8
|
+
}
|
|
9
|
+
export function build(config) {
|
|
10
|
+
return (context) => actor(config, context);
|
|
11
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ActorContext } from '../../../../../config.js';
|
|
2
|
+
import { Config } from '../../actor.js';
|
|
3
|
+
import ts from 'typescript';
|
|
4
|
+
export declare function generateHelpNodes(config: Config, context: ActorContext, state: {
|
|
5
|
+
hasQueries: boolean;
|
|
6
|
+
hasMutations: boolean;
|
|
7
|
+
hasSubscriptions: boolean;
|
|
8
|
+
}): ts.Node[];
|