@graphql-codegen/typescript-jit-sdk 1.0.0-alpha-f01e08cd9.0 → 1.0.1
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/index.js +30 -24
- package/index.mjs +30 -24
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -43,17 +43,17 @@ class JitSdkVisitor extends visitorPluginCommon.ClientSideBaseVisitor {
|
|
|
43
43
|
get sdkContent() {
|
|
44
44
|
const compiledQueries = [];
|
|
45
45
|
const sdkMethods = [];
|
|
46
|
+
let hasSubscription = false;
|
|
46
47
|
for (const o of this._operationsToInclude) {
|
|
47
48
|
const operationName = o.node.name.value;
|
|
48
49
|
const compiledQueryVariableName = `${operationName}Compiled`;
|
|
49
50
|
compiledQueries.push(visitorPluginCommon.indentMultiline(`const ${compiledQueryVariableName} = compileQuery(schema, ${this.config.documentMode === visitorPluginCommon.DocumentMode.string
|
|
50
51
|
? `parse(${o.documentVariableName})`
|
|
51
52
|
: o.documentVariableName}, '${operationName}');
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
`, 2));
|
|
53
|
+
if(!(isCompiledQuery(${compiledQueryVariableName}))) {
|
|
54
|
+
const originalErrors = ${compiledQueryVariableName}?.errors?.map(error => error.originalError || error) || [];
|
|
55
|
+
throw new AggregateError(originalErrors, \`Failed to compile ${operationName}: \\n\\t\${originalErrors.join('\\n\\t')}\`);
|
|
56
|
+
}`, 2));
|
|
57
57
|
const optionalVariables = !o.node.variableDefinitions ||
|
|
58
58
|
o.node.variableDefinitions.length === 0 ||
|
|
59
59
|
o.node.variableDefinitions.every(v => v.type.kind !== graphql.Kind.NON_NULL_TYPE || v.defaultValue);
|
|
@@ -62,27 +62,33 @@ class JitSdkVisitor extends visitorPluginCommon.ClientSideBaseVisitor {
|
|
|
62
62
|
const returnType = o.operationType === 'Subscription'
|
|
63
63
|
? `AsyncIterableIterator<${o.operationResultType}> | ${o.operationResultType}`
|
|
64
64
|
: o.operationResultType;
|
|
65
|
+
if (o.operationType === 'Subscription') {
|
|
66
|
+
hasSubscription = true;
|
|
67
|
+
}
|
|
65
68
|
sdkMethods.push(visitorPluginCommon.indentMultiline(`async ${operationName}(variables${optionalVariables ? '?' : ''}: ${o.operationVariablesTypes}, context = globalContext, root = globalRoot): Promise<${returnType}> {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
+
const result = await ${compiledQueryVariableName}.${methodName}(root, context, variables);
|
|
70
|
+
return ${handlerName}(result, '${operationName}');
|
|
71
|
+
}`, 2));
|
|
69
72
|
}
|
|
70
|
-
return
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
73
|
+
return `${hasSubscription
|
|
74
|
+
? `async function handleSubscriptionResult<T>(resultIterator: AsyncIterableIterator<ExecutionResult> | ExecutionResult, operationName: string) {
|
|
75
|
+
if (isAsyncIterable(resultIterator)) {
|
|
76
|
+
return mapAsyncIterator<any, T>(resultIterator, result => handleExecutionResult(result, operationName));
|
|
77
|
+
} else {
|
|
78
|
+
return handleExecutionResult<T>(resultIterator, operationName);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
`
|
|
82
|
+
: ``}function handleExecutionResult<T>(result: ExecutionResult, operationName: string) {
|
|
83
|
+
if (result.errors) {
|
|
84
|
+
const originalErrors = result.errors.map(error => error.originalError|| error);
|
|
85
|
+
throw new AggregateError(originalErrors, \`Failed to execute \${operationName}: \\n\\t\${originalErrors.join('\\n\\t')}\`);
|
|
86
|
+
}
|
|
87
|
+
return result.data as T;
|
|
88
|
+
}
|
|
89
|
+
export function getSdk<C = any, R = any>(schema: GraphQLSchema, globalContext?: C, globalRoot?: R) {
|
|
90
|
+
${compiledQueries.join('\n\n')}
|
|
91
|
+
|
|
86
92
|
return {
|
|
87
93
|
${sdkMethods.join(',\n')}
|
|
88
94
|
};
|
package/index.mjs
CHANGED
|
@@ -37,17 +37,17 @@ class JitSdkVisitor extends ClientSideBaseVisitor {
|
|
|
37
37
|
get sdkContent() {
|
|
38
38
|
const compiledQueries = [];
|
|
39
39
|
const sdkMethods = [];
|
|
40
|
+
let hasSubscription = false;
|
|
40
41
|
for (const o of this._operationsToInclude) {
|
|
41
42
|
const operationName = o.node.name.value;
|
|
42
43
|
const compiledQueryVariableName = `${operationName}Compiled`;
|
|
43
44
|
compiledQueries.push(indentMultiline(`const ${compiledQueryVariableName} = compileQuery(schema, ${this.config.documentMode === DocumentMode.string
|
|
44
45
|
? `parse(${o.documentVariableName})`
|
|
45
46
|
: o.documentVariableName}, '${operationName}');
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
`, 2));
|
|
47
|
+
if(!(isCompiledQuery(${compiledQueryVariableName}))) {
|
|
48
|
+
const originalErrors = ${compiledQueryVariableName}?.errors?.map(error => error.originalError || error) || [];
|
|
49
|
+
throw new AggregateError(originalErrors, \`Failed to compile ${operationName}: \\n\\t\${originalErrors.join('\\n\\t')}\`);
|
|
50
|
+
}`, 2));
|
|
51
51
|
const optionalVariables = !o.node.variableDefinitions ||
|
|
52
52
|
o.node.variableDefinitions.length === 0 ||
|
|
53
53
|
o.node.variableDefinitions.every(v => v.type.kind !== Kind.NON_NULL_TYPE || v.defaultValue);
|
|
@@ -56,27 +56,33 @@ class JitSdkVisitor extends ClientSideBaseVisitor {
|
|
|
56
56
|
const returnType = o.operationType === 'Subscription'
|
|
57
57
|
? `AsyncIterableIterator<${o.operationResultType}> | ${o.operationResultType}`
|
|
58
58
|
: o.operationResultType;
|
|
59
|
+
if (o.operationType === 'Subscription') {
|
|
60
|
+
hasSubscription = true;
|
|
61
|
+
}
|
|
59
62
|
sdkMethods.push(indentMultiline(`async ${operationName}(variables${optionalVariables ? '?' : ''}: ${o.operationVariablesTypes}, context = globalContext, root = globalRoot): Promise<${returnType}> {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
+
const result = await ${compiledQueryVariableName}.${methodName}(root, context, variables);
|
|
64
|
+
return ${handlerName}(result, '${operationName}');
|
|
65
|
+
}`, 2));
|
|
63
66
|
}
|
|
64
|
-
return
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
67
|
+
return `${hasSubscription
|
|
68
|
+
? `async function handleSubscriptionResult<T>(resultIterator: AsyncIterableIterator<ExecutionResult> | ExecutionResult, operationName: string) {
|
|
69
|
+
if (isAsyncIterable(resultIterator)) {
|
|
70
|
+
return mapAsyncIterator<any, T>(resultIterator, result => handleExecutionResult(result, operationName));
|
|
71
|
+
} else {
|
|
72
|
+
return handleExecutionResult<T>(resultIterator, operationName);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
`
|
|
76
|
+
: ``}function handleExecutionResult<T>(result: ExecutionResult, operationName: string) {
|
|
77
|
+
if (result.errors) {
|
|
78
|
+
const originalErrors = result.errors.map(error => error.originalError|| error);
|
|
79
|
+
throw new AggregateError(originalErrors, \`Failed to execute \${operationName}: \\n\\t\${originalErrors.join('\\n\\t')}\`);
|
|
80
|
+
}
|
|
81
|
+
return result.data as T;
|
|
82
|
+
}
|
|
83
|
+
export function getSdk<C = any, R = any>(schema: GraphQLSchema, globalContext?: C, globalRoot?: R) {
|
|
84
|
+
${compiledQueries.join('\n\n')}
|
|
85
|
+
|
|
80
86
|
return {
|
|
81
87
|
${sdkMethods.join(',\n')}
|
|
82
88
|
};
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-codegen/typescript-jit-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "GraphQL Code Generator plugin for generating a ready-to-use SDK that uses GraphQL JIT",
|
|
5
5
|
"peerDependencies": {
|
|
6
|
-
"@graphql-tools/utils": "8.2.4",
|
|
6
|
+
"@graphql-tools/utils": "^8.2.4",
|
|
7
7
|
"graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0",
|
|
8
8
|
"graphql-jit": "^0.6.0",
|
|
9
9
|
"graphql-tag": "^2.0.0"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@graphql-codegen/plugin-helpers": "^2.
|
|
13
|
-
"@graphql-codegen/visitor-plugin-common": "2.
|
|
12
|
+
"@graphql-codegen/plugin-helpers": "^2.2.0",
|
|
13
|
+
"@graphql-codegen/visitor-plugin-common": "2.4.0",
|
|
14
14
|
"auto-bind": "~4.0.0",
|
|
15
15
|
"tslib": "~2.3.0"
|
|
16
16
|
},
|