@vladimirdev635/gql-codegen 0.0.49 → 0.0.50
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.
|
@@ -6,6 +6,9 @@ export type OperationReturnType = 'ExecuteResult' | 'ExecuteResult.result';
|
|
|
6
6
|
export interface SDKConfig {
|
|
7
7
|
defaultOperationReturnType: OperationReturnType;
|
|
8
8
|
operationReturnTypeMapping: Record<string, OperationReturnType>;
|
|
9
|
+
queriesKey: string;
|
|
10
|
+
mutationsKey: string;
|
|
11
|
+
subscriptionsKey: string;
|
|
9
12
|
}
|
|
10
13
|
export interface GQLClientActorConfig extends TSActorConfig {
|
|
11
14
|
outPath: PathOrFileDescriptor;
|
|
@@ -33,12 +33,10 @@ function getReturnTypeFromConfig(config, operationName) {
|
|
|
33
33
|
export function generateNodes(config, context) {
|
|
34
34
|
const graphqlImports = [];
|
|
35
35
|
let shouldIncludeExecuteResultType = false;
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
shouldIncludeSubOpAsyncIterable = true;
|
|
41
|
-
}
|
|
36
|
+
const queryNodes = [];
|
|
37
|
+
const mutationNodes = [];
|
|
38
|
+
const subscriptionNodes = [];
|
|
39
|
+
for (const operation of Object.values(context.schema.client.operations)) {
|
|
42
40
|
const operationName = operation.name + 'Operation';
|
|
43
41
|
const variablesName = operation.name + 'Variables';
|
|
44
42
|
const resultName = operation.name + 'Result';
|
|
@@ -46,23 +44,45 @@ export function generateNodes(config, context) {
|
|
|
46
44
|
const returnType = getReturnTypeFromConfig(config, operation.name);
|
|
47
45
|
if (returnType === 'ExecuteResult')
|
|
48
46
|
shouldIncludeExecuteResultType = true;
|
|
49
|
-
|
|
47
|
+
const propAssignment = ts.factory.createPropertyAssignment(operation.name, ts.factory.createArrowFunction(ts.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Async), undefined, [
|
|
50
48
|
ts.factory.createParameterDeclaration(undefined, undefined, 'variables', undefined, ts.factory.createTypeReferenceNode(variablesName)),
|
|
51
49
|
ts.factory.createParameterDeclaration(undefined, undefined, 'requestContext', undefined, ts.factory.createTypeReferenceNode('TRequestContext')),
|
|
52
50
|
], ts.factory.createTypeReferenceNode('Promise', [
|
|
53
51
|
createReturnTypeNode(resultName, returnType, operation.type)
|
|
54
52
|
]), ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), generateFunctionBlock(operationName, returnType)));
|
|
55
|
-
|
|
53
|
+
switch (operation.type) {
|
|
54
|
+
case 'QUERY': {
|
|
55
|
+
queryNodes.push(propAssignment);
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
case 'MUTATION': {
|
|
59
|
+
mutationNodes.push(propAssignment);
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
case 'SUBSCRIPTION': {
|
|
63
|
+
subscriptionNodes.push(propAssignment);
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
56
68
|
const gqlClientImports = [
|
|
57
69
|
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('Executor')),
|
|
58
70
|
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('RequestContext'))
|
|
59
71
|
];
|
|
60
|
-
if (shouldIncludeSubOpAsyncIterable) {
|
|
61
|
-
gqlClientImports.push(ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('SubOpAsyncIterable')));
|
|
62
|
-
}
|
|
63
72
|
if (shouldIncludeExecuteResultType) {
|
|
64
73
|
gqlClientImports.push(ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('ExecuteResult')));
|
|
65
74
|
}
|
|
75
|
+
const returnObjectNodes = [];
|
|
76
|
+
if (queryNodes.length !== 0) {
|
|
77
|
+
returnObjectNodes.push(ts.factory.createPropertyAssignment(config.sdk.queriesKey, ts.factory.createObjectLiteralExpression(queryNodes, true)));
|
|
78
|
+
}
|
|
79
|
+
if (mutationNodes.length !== 0) {
|
|
80
|
+
returnObjectNodes.push(ts.factory.createPropertyAssignment(config.sdk.mutationsKey, ts.factory.createObjectLiteralExpression(mutationNodes, true)));
|
|
81
|
+
}
|
|
82
|
+
if (subscriptionNodes.length !== 0) {
|
|
83
|
+
gqlClientImports.push(ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('SubOpAsyncIterable')));
|
|
84
|
+
returnObjectNodes.push(ts.factory.createPropertyAssignment(config.sdk.subscriptionsKey, ts.factory.createObjectLiteralExpression(subscriptionNodes, true)));
|
|
85
|
+
}
|
|
66
86
|
return [
|
|
67
87
|
ts.factory.createIdentifier('// @ts-nocheck'),
|
|
68
88
|
...config.importDeclarations,
|
|
@@ -72,7 +92,7 @@ export function generateNodes(config, context) {
|
|
|
72
92
|
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', [
|
|
73
93
|
ts.factory.createTypeReferenceNode('TRequestContext')
|
|
74
94
|
]))], undefined, ts.factory.createBlock([
|
|
75
|
-
ts.factory.createReturnStatement(ts.factory.createAsExpression(ts.factory.createObjectLiteralExpression(
|
|
95
|
+
ts.factory.createReturnStatement(ts.factory.createAsExpression(ts.factory.createObjectLiteralExpression(returnObjectNodes, true), ts.factory.createTypeReferenceNode('const')))
|
|
76
96
|
], true))
|
|
77
97
|
];
|
|
78
98
|
}
|
|
@@ -6,5 +6,10 @@ export interface GQLClientReactActorConfig extends TSActorConfig {
|
|
|
6
6
|
outPath: PathOrFileDescriptor;
|
|
7
7
|
importDeclarations: ts.ImportDeclaration[];
|
|
8
8
|
graphqlModulePath: string;
|
|
9
|
+
sdk: {
|
|
10
|
+
queriesKey: string;
|
|
11
|
+
mutationsKey: string;
|
|
12
|
+
subscriptionsKey: string;
|
|
13
|
+
};
|
|
9
14
|
}
|
|
10
15
|
export declare function buildGQLClientReactActor(config: GQLClientReactActorConfig): Actor<ActorContext>;
|
|
@@ -52,34 +52,48 @@ function generateArrowFunction(operationName, variablesName, resultName, type) {
|
|
|
52
52
|
}
|
|
53
53
|
export function generateNodes(config, context) {
|
|
54
54
|
const graphqlImports = [];
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
hasSubscriptions = true;
|
|
55
|
+
const queryNodes = [];
|
|
56
|
+
const mutationNodes = [];
|
|
57
|
+
const subscriptionNodes = [];
|
|
58
|
+
for (const operation of Object.values(context.schema.client.operations)) {
|
|
60
59
|
const operationName = operation.name + 'Operation';
|
|
61
60
|
const variablesName = operation.name + 'Variables';
|
|
62
61
|
const resultName = operation.name + 'Result';
|
|
63
62
|
graphqlImports.push(operationName, variablesName, resultName);
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
ts.factory.createPropertyAssignment('use' +
|
|
67
|
-
|
|
63
|
+
switch (operation.type) {
|
|
64
|
+
case 'SUBSCRIPTION': {
|
|
65
|
+
subscriptionNodes.push(ts.factory.createPropertyAssignment('use' + operation.name, generateArrowFunction(operationName, variablesName, resultName, 'SUBSCRIPTION')));
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
case 'MUTATION': {
|
|
69
|
+
mutationNodes.push(ts.factory.createPropertyAssignment('use' + operation.name, generateArrowFunction(operationName, variablesName, resultName, 'LAZY')));
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
case 'QUERY': {
|
|
73
|
+
queryNodes.push(ts.factory.createPropertyAssignment('use' + operation.name, generateArrowFunction(operationName, variablesName, resultName, 'SYNC')), ts.factory.createPropertyAssignment('useLazy' + operation.name, generateArrowFunction(operationName, variablesName, resultName, 'LAZY')));
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
68
76
|
}
|
|
69
|
-
|
|
70
|
-
ts.factory.createPropertyAssignment('use' + operationName, generateArrowFunction(operationName, variablesName, resultName, 'SYNC')),
|
|
71
|
-
ts.factory.createPropertyAssignment('useLazy' + operationName, generateArrowFunction(operationName, variablesName, resultName, 'LAZY'))
|
|
72
|
-
];
|
|
73
|
-
}).flat();
|
|
77
|
+
}
|
|
74
78
|
const gqlClientReactImports = [
|
|
75
79
|
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('useOperation')),
|
|
76
80
|
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('useLazyOperation')),
|
|
77
81
|
ts.factory.createImportSpecifier(true, undefined, ts.factory.createIdentifier('OperationState')),
|
|
78
82
|
ts.factory.createImportSpecifier(true, undefined, ts.factory.createIdentifier('UseLazyOperationReturnType'))
|
|
79
83
|
];
|
|
80
|
-
if (
|
|
84
|
+
if (subscriptionNodes.length !== 0) {
|
|
81
85
|
gqlClientReactImports.push(ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('useSubscription')), ts.factory.createImportSpecifier(true, undefined, ts.factory.createIdentifier('SubOpAsyncIterable')));
|
|
82
86
|
}
|
|
87
|
+
const returnObjectNodes = [];
|
|
88
|
+
if (queryNodes.length !== 0) {
|
|
89
|
+
returnObjectNodes.push(ts.factory.createPropertyAssignment(config.sdk.queriesKey, ts.factory.createObjectLiteralExpression(queryNodes, true)));
|
|
90
|
+
}
|
|
91
|
+
if (mutationNodes.length !== 0) {
|
|
92
|
+
returnObjectNodes.push(ts.factory.createPropertyAssignment(config.sdk.mutationsKey, ts.factory.createObjectLiteralExpression(mutationNodes, true)));
|
|
93
|
+
}
|
|
94
|
+
if (subscriptionNodes.length !== 0) {
|
|
95
|
+
returnObjectNodes.push(ts.factory.createPropertyAssignment(config.sdk.subscriptionsKey, ts.factory.createObjectLiteralExpression(subscriptionNodes, true)));
|
|
96
|
+
}
|
|
83
97
|
return [
|
|
84
98
|
ts.factory.createIdentifier('// @ts-nocheck'),
|
|
85
99
|
...config.importDeclarations,
|
|
@@ -93,7 +107,7 @@ export function generateNodes(config, context) {
|
|
|
93
107
|
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', [
|
|
94
108
|
ts.factory.createTypeReferenceNode('TRequestContext')
|
|
95
109
|
]))], undefined, ts.factory.createBlock([
|
|
96
|
-
ts.factory.createReturnStatement(ts.factory.createObjectLiteralExpression(
|
|
110
|
+
ts.factory.createReturnStatement(ts.factory.createObjectLiteralExpression(returnObjectNodes, true))
|
|
97
111
|
], true))
|
|
98
112
|
];
|
|
99
113
|
}
|