@graphql-codegen/typescript 5.0.9 → 6.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/cjs/index.js +4 -2
- package/cjs/typescript-variables-to-object.js +3 -2
- package/cjs/visitor.js +38 -124
- package/esm/index.js +4 -2
- package/esm/typescript-variables-to-object.js +3 -2
- package/esm/visitor.js +37 -123
- package/package.json +6 -6
- package/typings/index.d.cts +1 -1
- package/typings/index.d.ts +1 -1
- package/typings/typescript-variables-to-object.d.cts +2 -2
- package/typings/typescript-variables-to-object.d.ts +2 -2
- package/typings/visitor.d.cts +1 -11
- package/typings/visitor.d.ts +1 -11
package/cjs/index.js
CHANGED
|
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.plugin = void 0;
|
|
4
4
|
exports.includeIntrospectionTypesDefinitions = includeIntrospectionTypesDefinitions;
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
6
|
+
const graphql_1 = require("graphql");
|
|
6
7
|
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
7
8
|
const schema_ast_1 = require("@graphql-codegen/schema-ast");
|
|
8
|
-
const graphql_1 = require("graphql");
|
|
9
9
|
const introspection_visitor_js_1 = require("./introspection-visitor.js");
|
|
10
10
|
const visitor_js_1 = require("./visitor.js");
|
|
11
11
|
tslib_1.__exportStar(require("./config.js"), exports);
|
|
@@ -56,7 +56,9 @@ function includeIntrospectionTypesDefinitions(schema, documents, config) {
|
|
|
56
56
|
collectTypes(type);
|
|
57
57
|
}
|
|
58
58
|
const visitor = new introspection_visitor_js_1.TsIntrospectionVisitor(schema, config, typesToInclude);
|
|
59
|
-
const result = (0, plugin_helpers_1.oldVisit)((0, graphql_1.parse)((0, graphql_1.printIntrospectionSchema)(schema)), {
|
|
59
|
+
const result = (0, plugin_helpers_1.oldVisit)((0, graphql_1.parse)((0, graphql_1.printIntrospectionSchema)(schema)), {
|
|
60
|
+
leave: visitor,
|
|
61
|
+
});
|
|
60
62
|
// recursively go through each `usedTypes` and their children and collect all used types
|
|
61
63
|
// we don't care about Interfaces, Unions and others, but Objects and Enums
|
|
62
64
|
function collectTypes(type) {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TypeScriptOperationVariablesToObject = void 0;
|
|
4
|
-
const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
|
5
4
|
const graphql_1 = require("graphql");
|
|
5
|
+
const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
|
6
6
|
class TypeScriptOperationVariablesToObject extends visitor_plugin_common_1.OperationVariablesToObject {
|
|
7
7
|
_avoidOptionals;
|
|
8
8
|
_immutableTypes;
|
|
@@ -48,7 +48,8 @@ class TypeScriptOperationVariablesToObject extends visitor_plugin_common_1.Opera
|
|
|
48
48
|
}
|
|
49
49
|
getAvoidOption(isNonNullType, hasDefaultValue) {
|
|
50
50
|
const options = this._avoidOptionals;
|
|
51
|
-
return ((options.object || !options.defaultValue) && hasDefaultValue) ||
|
|
51
|
+
return (((options.object || !options.defaultValue) && hasDefaultValue) ||
|
|
52
|
+
(!options.object && !isNonNullType));
|
|
52
53
|
}
|
|
53
54
|
getPunctuation() {
|
|
54
55
|
return ';';
|
package/cjs/visitor.js
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TsVisitor =
|
|
3
|
+
exports.TsVisitor = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
-
const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
|
6
5
|
const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
|
|
7
6
|
const graphql_1 = require("graphql");
|
|
7
|
+
const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
|
8
8
|
const typescript_variables_to_object_js_1 = require("./typescript-variables-to-object.js");
|
|
9
|
-
exports.EXACT_SIGNATURE = `type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };`;
|
|
10
|
-
exports.MAKE_OPTIONAL_SIGNATURE = `type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };`;
|
|
11
|
-
exports.MAKE_MAYBE_SIGNATURE = `type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };`;
|
|
12
|
-
exports.MAKE_EMPTY_SIGNATURE = `type MakeEmpty<T extends { [key: string]: unknown }, K extends keyof T> = { [_ in K]?: never };`;
|
|
13
|
-
exports.MAKE_INCREMENTAL_SIGNATURE = `type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };`;
|
|
14
9
|
class TsVisitor extends visitor_plugin_common_1.BaseTypesVisitor {
|
|
15
10
|
constructor(schema, pluginConfig, additionalConfig = {}) {
|
|
16
11
|
super(schema, pluginConfig, {
|
|
@@ -81,15 +76,7 @@ class TsVisitor extends visitor_plugin_common_1.BaseTypesVisitor {
|
|
|
81
76
|
getWrapperDefinitions() {
|
|
82
77
|
if (this.config.onlyEnums)
|
|
83
78
|
return [];
|
|
84
|
-
const definitions = [
|
|
85
|
-
this.getMaybeValue(),
|
|
86
|
-
this.getInputMaybeValue(),
|
|
87
|
-
this.getExactDefinition(),
|
|
88
|
-
this.getMakeOptionalDefinition(),
|
|
89
|
-
this.getMakeMaybeDefinition(),
|
|
90
|
-
this.getMakeEmptyDefinition(),
|
|
91
|
-
this.getIncrementalDefinition(),
|
|
92
|
-
];
|
|
79
|
+
const definitions = [this.getMaybeValue(), this.getInputMaybeValue()];
|
|
93
80
|
if (this.config.wrapFieldDefinitions) {
|
|
94
81
|
definitions.push(this.getFieldWrapperValue());
|
|
95
82
|
}
|
|
@@ -98,25 +85,6 @@ class TsVisitor extends visitor_plugin_common_1.BaseTypesVisitor {
|
|
|
98
85
|
}
|
|
99
86
|
return definitions;
|
|
100
87
|
}
|
|
101
|
-
getExactDefinition() {
|
|
102
|
-
if (this.config.onlyEnums)
|
|
103
|
-
return '';
|
|
104
|
-
return `${this.getExportPrefix()}${exports.EXACT_SIGNATURE}`;
|
|
105
|
-
}
|
|
106
|
-
getMakeOptionalDefinition() {
|
|
107
|
-
return `${this.getExportPrefix()}${exports.MAKE_OPTIONAL_SIGNATURE}`;
|
|
108
|
-
}
|
|
109
|
-
getMakeMaybeDefinition() {
|
|
110
|
-
if (this.config.onlyEnums)
|
|
111
|
-
return '';
|
|
112
|
-
return `${this.getExportPrefix()}${exports.MAKE_MAYBE_SIGNATURE}`;
|
|
113
|
-
}
|
|
114
|
-
getMakeEmptyDefinition() {
|
|
115
|
-
return `${this.getExportPrefix()}${exports.MAKE_EMPTY_SIGNATURE}`;
|
|
116
|
-
}
|
|
117
|
-
getIncrementalDefinition() {
|
|
118
|
-
return `${this.getExportPrefix()}${exports.MAKE_INCREMENTAL_SIGNATURE}`;
|
|
119
|
-
}
|
|
120
88
|
getMaybeValue() {
|
|
121
89
|
return `${this.getExportPrefix()}type Maybe<T> = ${this.config.maybeValue};`;
|
|
122
90
|
}
|
|
@@ -155,12 +123,14 @@ class TsVisitor extends visitor_plugin_common_1.BaseTypesVisitor {
|
|
|
155
123
|
let withFutureAddedValue = [];
|
|
156
124
|
if (this.config.futureProofUnions) {
|
|
157
125
|
withFutureAddedValue = [
|
|
158
|
-
this.config.immutableTypes
|
|
126
|
+
this.config.immutableTypes
|
|
127
|
+
? `{ readonly __typename?: "%other" }`
|
|
128
|
+
: `{ __typename?: "%other" }`,
|
|
159
129
|
];
|
|
160
130
|
}
|
|
161
131
|
const originalNode = parent[key];
|
|
162
132
|
const possibleTypes = originalNode.types
|
|
163
|
-
.map(t =>
|
|
133
|
+
.map(t => this.scalars[t.name.value] ? this._getScalar(t.name.value, 'output') : this.convertName(t))
|
|
164
134
|
.concat(...withFutureAddedValue)
|
|
165
135
|
.join(' | ');
|
|
166
136
|
return new visitor_plugin_common_1.DeclarationBlock(this._declarationBlockConfig)
|
|
@@ -184,7 +154,7 @@ class TsVisitor extends visitor_plugin_common_1.BaseTypesVisitor {
|
|
|
184
154
|
: node.type;
|
|
185
155
|
const originalFieldNode = parent[key];
|
|
186
156
|
const addOptionalSign = !this.config.avoidOptionals.field && originalFieldNode.type.kind !== graphql_1.Kind.NON_NULL_TYPE;
|
|
187
|
-
const comment =
|
|
157
|
+
const comment = (0, visitor_plugin_common_1.getNodeComment)(node);
|
|
188
158
|
const { type } = this.config.declarationKind;
|
|
189
159
|
return (comment +
|
|
190
160
|
(0, visitor_plugin_common_1.indent)(`${this.config.immutableTypes ? 'readonly ' : ''}${node.name.value}${addOptionalSign ? '?' : ''}: ${typeString}${this.getPunctuation(type)}`));
|
|
@@ -194,7 +164,7 @@ class TsVisitor extends visitor_plugin_common_1.BaseTypesVisitor {
|
|
|
194
164
|
const addOptionalSign = !this.config.avoidOptionals.inputValue &&
|
|
195
165
|
(originalFieldNode.type.kind !== graphql_1.Kind.NON_NULL_TYPE ||
|
|
196
166
|
(!this.config.avoidOptionals.defaultValue && node.defaultValue !== undefined));
|
|
197
|
-
const comment =
|
|
167
|
+
const comment = (0, visitor_plugin_common_1.getNodeComment)(node);
|
|
198
168
|
const declarationKind = this.config.declarationKind.type;
|
|
199
169
|
let type = node.type;
|
|
200
170
|
if (node.directives && this.config.directiveArgumentAndInputFieldMappings) {
|
|
@@ -226,93 +196,37 @@ class TsVisitor extends visitor_plugin_common_1.BaseTypesVisitor {
|
|
|
226
196
|
}
|
|
227
197
|
EnumTypeDefinition(node) {
|
|
228
198
|
const enumName = node.name.value;
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
}
|
|
233
|
-
const getValueFromConfig = (enumValue) => {
|
|
234
|
-
if (typeof this.config.enumValues[enumName]?.mappedValues?.[enumValue] !== 'undefined') {
|
|
235
|
-
return this.config.enumValues[enumName].mappedValues[enumValue];
|
|
199
|
+
const outputType = (() => {
|
|
200
|
+
if (this.config.enumsAsTypes) {
|
|
201
|
+
return 'string-literal';
|
|
236
202
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
this.config.
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
.
|
|
261
|
-
.join('\n')).string;
|
|
262
|
-
}
|
|
263
|
-
if (this.config.numericEnums) {
|
|
264
|
-
const block = new visitor_plugin_common_1.DeclarationBlock(this._declarationBlockConfig)
|
|
265
|
-
.export()
|
|
266
|
-
.withComment(node.description?.value)
|
|
267
|
-
.withName(enumTypeName)
|
|
268
|
-
.asKind('enum')
|
|
269
|
-
.withBlock(node.values
|
|
270
|
-
.map((enumOption, i) => {
|
|
271
|
-
const valueFromConfig = getValueFromConfig(enumOption.name.value);
|
|
272
|
-
const enumValue = valueFromConfig ?? i;
|
|
273
|
-
const comment = (0, visitor_plugin_common_1.transformComment)(enumOption.description?.value, 1);
|
|
274
|
-
const optionName = this.makeValidEnumIdentifier(this.convertName(enumOption, {
|
|
275
|
-
useTypesPrefix: false,
|
|
276
|
-
transformUnderscore: true,
|
|
277
|
-
}));
|
|
278
|
-
return comment + (0, visitor_plugin_common_1.indent)(optionName) + ` = ${enumValue}`;
|
|
279
|
-
})
|
|
280
|
-
.concat(...withFutureAddedValue)
|
|
281
|
-
.join(',\n')).string;
|
|
282
|
-
return block;
|
|
283
|
-
}
|
|
284
|
-
if (this.config.enumsAsConst) {
|
|
285
|
-
const typeName = `export type ${enumTypeName} = typeof ${enumTypeName}[keyof typeof ${enumTypeName}];`;
|
|
286
|
-
const enumAsConst = new visitor_plugin_common_1.DeclarationBlock({
|
|
287
|
-
...this._declarationBlockConfig,
|
|
288
|
-
blockTransformer: block => {
|
|
289
|
-
return block + ' as const';
|
|
203
|
+
if (this.config.numericEnums) {
|
|
204
|
+
return 'native-numeric';
|
|
205
|
+
}
|
|
206
|
+
if (this.config.enumsAsConst) {
|
|
207
|
+
return 'const';
|
|
208
|
+
}
|
|
209
|
+
return this.config.constEnums ? 'native-const' : 'native';
|
|
210
|
+
})();
|
|
211
|
+
return (0, visitor_plugin_common_1.convertSchemaEnumToDeclarationBlockString)({
|
|
212
|
+
schema: this._schema,
|
|
213
|
+
node,
|
|
214
|
+
declarationBlockConfig: this._declarationBlockConfig,
|
|
215
|
+
enumName,
|
|
216
|
+
enumValues: this.config.enumValues,
|
|
217
|
+
futureProofEnums: this.config.futureProofEnums,
|
|
218
|
+
ignoreEnumValuesFromSchema: this.config.ignoreEnumValuesFromSchema,
|
|
219
|
+
outputType,
|
|
220
|
+
naming: {
|
|
221
|
+
convert: this.config.convert,
|
|
222
|
+
options: {
|
|
223
|
+
typesPrefix: this.config.typesPrefix,
|
|
224
|
+
typesSuffix: this.config.typesSuffix,
|
|
225
|
+
useTypesPrefix: this.config.enumPrefix,
|
|
226
|
+
useTypesSuffix: this.config.enumSuffix,
|
|
290
227
|
},
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
.asKind('const')
|
|
294
|
-
.withName(enumTypeName)
|
|
295
|
-
.withComment(node.description?.value)
|
|
296
|
-
.withBlock(node.values
|
|
297
|
-
.map(enumOption => {
|
|
298
|
-
const optionName = this.makeValidEnumIdentifier(this.convertName(enumOption, {
|
|
299
|
-
useTypesPrefix: false,
|
|
300
|
-
transformUnderscore: true,
|
|
301
|
-
}));
|
|
302
|
-
const comment = (0, visitor_plugin_common_1.transformComment)(enumOption.description?.value, 1);
|
|
303
|
-
const name = enumOption.name.value;
|
|
304
|
-
const enumValue = getValueFromConfig(name) ?? name;
|
|
305
|
-
return comment + (0, visitor_plugin_common_1.indent)(`${optionName}: ${(0, visitor_plugin_common_1.wrapWithSingleQuotes)(enumValue)}`);
|
|
306
|
-
})
|
|
307
|
-
.join(',\n')).string;
|
|
308
|
-
return [enumAsConst, typeName].join('\n');
|
|
309
|
-
}
|
|
310
|
-
return new visitor_plugin_common_1.DeclarationBlock(this._declarationBlockConfig)
|
|
311
|
-
.export()
|
|
312
|
-
.asKind(this.config.constEnums ? 'const enum' : 'enum')
|
|
313
|
-
.withName(enumTypeName)
|
|
314
|
-
.withComment(node.description?.value)
|
|
315
|
-
.withBlock(this.buildEnumValuesBlock(enumName, node.values)).string;
|
|
228
|
+
},
|
|
229
|
+
});
|
|
316
230
|
}
|
|
317
231
|
getPunctuation(_declarationKind) {
|
|
318
232
|
return ';';
|
package/esm/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { getNamedType, isIntrospectionType, isObjectType, parse, printIntrospectionSchema, TypeInfo, visit, visitWithTypeInfo, } from 'graphql';
|
|
1
2
|
import { oldVisit } from '@graphql-codegen/plugin-helpers';
|
|
2
3
|
import { transformSchemaAST } from '@graphql-codegen/schema-ast';
|
|
3
|
-
import { getNamedType, isIntrospectionType, isObjectType, parse, printIntrospectionSchema, TypeInfo, visit, visitWithTypeInfo, } from 'graphql';
|
|
4
4
|
import { TsIntrospectionVisitor } from './introspection-visitor.js';
|
|
5
5
|
import { TsVisitor } from './visitor.js';
|
|
6
6
|
export * from './config.js';
|
|
@@ -50,7 +50,9 @@ export function includeIntrospectionTypesDefinitions(schema, documents, config)
|
|
|
50
50
|
collectTypes(type);
|
|
51
51
|
}
|
|
52
52
|
const visitor = new TsIntrospectionVisitor(schema, config, typesToInclude);
|
|
53
|
-
const result = oldVisit(parse(printIntrospectionSchema(schema)), {
|
|
53
|
+
const result = oldVisit(parse(printIntrospectionSchema(schema)), {
|
|
54
|
+
leave: visitor,
|
|
55
|
+
});
|
|
54
56
|
// recursively go through each `usedTypes` and their children and collect all used types
|
|
55
57
|
// we don't care about Interfaces, Unions and others, but Objects and Enums
|
|
56
58
|
function collectTypes(type) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { OperationVariablesToObject, } from '@graphql-codegen/visitor-plugin-common';
|
|
2
1
|
import { Kind } from 'graphql';
|
|
2
|
+
import { OperationVariablesToObject, } from '@graphql-codegen/visitor-plugin-common';
|
|
3
3
|
export class TypeScriptOperationVariablesToObject extends OperationVariablesToObject {
|
|
4
4
|
_avoidOptionals;
|
|
5
5
|
_immutableTypes;
|
|
@@ -45,7 +45,8 @@ export class TypeScriptOperationVariablesToObject extends OperationVariablesToOb
|
|
|
45
45
|
}
|
|
46
46
|
getAvoidOption(isNonNullType, hasDefaultValue) {
|
|
47
47
|
const options = this._avoidOptionals;
|
|
48
|
-
return ((options.object || !options.defaultValue) && hasDefaultValue) ||
|
|
48
|
+
return (((options.object || !options.defaultValue) && hasDefaultValue) ||
|
|
49
|
+
(!options.object && !isNonNullType));
|
|
49
50
|
}
|
|
50
51
|
getPunctuation() {
|
|
51
52
|
return ';';
|
package/esm/visitor.js
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
|
-
import { BaseTypesVisitor, DeclarationBlock, getConfigValue, indent, isOneOfInputObjectType, normalizeAvoidOptionals, transformComment, wrapWithSingleQuotes, } from '@graphql-codegen/visitor-plugin-common';
|
|
2
1
|
import autoBind from 'auto-bind';
|
|
3
2
|
import { GraphQLObjectType, isEnumType, Kind, } from 'graphql';
|
|
3
|
+
import { BaseTypesVisitor, convertSchemaEnumToDeclarationBlockString, DeclarationBlock, getConfigValue, getNodeComment, indent, isOneOfInputObjectType, normalizeAvoidOptionals, } from '@graphql-codegen/visitor-plugin-common';
|
|
4
4
|
import { TypeScriptOperationVariablesToObject } from './typescript-variables-to-object.js';
|
|
5
|
-
export const EXACT_SIGNATURE = `type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };`;
|
|
6
|
-
export const MAKE_OPTIONAL_SIGNATURE = `type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };`;
|
|
7
|
-
export const MAKE_MAYBE_SIGNATURE = `type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };`;
|
|
8
|
-
export const MAKE_EMPTY_SIGNATURE = `type MakeEmpty<T extends { [key: string]: unknown }, K extends keyof T> = { [_ in K]?: never };`;
|
|
9
|
-
export const MAKE_INCREMENTAL_SIGNATURE = `type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };`;
|
|
10
5
|
export class TsVisitor extends BaseTypesVisitor {
|
|
11
6
|
constructor(schema, pluginConfig, additionalConfig = {}) {
|
|
12
7
|
super(schema, pluginConfig, {
|
|
@@ -77,15 +72,7 @@ export class TsVisitor extends BaseTypesVisitor {
|
|
|
77
72
|
getWrapperDefinitions() {
|
|
78
73
|
if (this.config.onlyEnums)
|
|
79
74
|
return [];
|
|
80
|
-
const definitions = [
|
|
81
|
-
this.getMaybeValue(),
|
|
82
|
-
this.getInputMaybeValue(),
|
|
83
|
-
this.getExactDefinition(),
|
|
84
|
-
this.getMakeOptionalDefinition(),
|
|
85
|
-
this.getMakeMaybeDefinition(),
|
|
86
|
-
this.getMakeEmptyDefinition(),
|
|
87
|
-
this.getIncrementalDefinition(),
|
|
88
|
-
];
|
|
75
|
+
const definitions = [this.getMaybeValue(), this.getInputMaybeValue()];
|
|
89
76
|
if (this.config.wrapFieldDefinitions) {
|
|
90
77
|
definitions.push(this.getFieldWrapperValue());
|
|
91
78
|
}
|
|
@@ -94,25 +81,6 @@ export class TsVisitor extends BaseTypesVisitor {
|
|
|
94
81
|
}
|
|
95
82
|
return definitions;
|
|
96
83
|
}
|
|
97
|
-
getExactDefinition() {
|
|
98
|
-
if (this.config.onlyEnums)
|
|
99
|
-
return '';
|
|
100
|
-
return `${this.getExportPrefix()}${EXACT_SIGNATURE}`;
|
|
101
|
-
}
|
|
102
|
-
getMakeOptionalDefinition() {
|
|
103
|
-
return `${this.getExportPrefix()}${MAKE_OPTIONAL_SIGNATURE}`;
|
|
104
|
-
}
|
|
105
|
-
getMakeMaybeDefinition() {
|
|
106
|
-
if (this.config.onlyEnums)
|
|
107
|
-
return '';
|
|
108
|
-
return `${this.getExportPrefix()}${MAKE_MAYBE_SIGNATURE}`;
|
|
109
|
-
}
|
|
110
|
-
getMakeEmptyDefinition() {
|
|
111
|
-
return `${this.getExportPrefix()}${MAKE_EMPTY_SIGNATURE}`;
|
|
112
|
-
}
|
|
113
|
-
getIncrementalDefinition() {
|
|
114
|
-
return `${this.getExportPrefix()}${MAKE_INCREMENTAL_SIGNATURE}`;
|
|
115
|
-
}
|
|
116
84
|
getMaybeValue() {
|
|
117
85
|
return `${this.getExportPrefix()}type Maybe<T> = ${this.config.maybeValue};`;
|
|
118
86
|
}
|
|
@@ -151,12 +119,14 @@ export class TsVisitor extends BaseTypesVisitor {
|
|
|
151
119
|
let withFutureAddedValue = [];
|
|
152
120
|
if (this.config.futureProofUnions) {
|
|
153
121
|
withFutureAddedValue = [
|
|
154
|
-
this.config.immutableTypes
|
|
122
|
+
this.config.immutableTypes
|
|
123
|
+
? `{ readonly __typename?: "%other" }`
|
|
124
|
+
: `{ __typename?: "%other" }`,
|
|
155
125
|
];
|
|
156
126
|
}
|
|
157
127
|
const originalNode = parent[key];
|
|
158
128
|
const possibleTypes = originalNode.types
|
|
159
|
-
.map(t =>
|
|
129
|
+
.map(t => this.scalars[t.name.value] ? this._getScalar(t.name.value, 'output') : this.convertName(t))
|
|
160
130
|
.concat(...withFutureAddedValue)
|
|
161
131
|
.join(' | ');
|
|
162
132
|
return new DeclarationBlock(this._declarationBlockConfig)
|
|
@@ -180,7 +150,7 @@ export class TsVisitor extends BaseTypesVisitor {
|
|
|
180
150
|
: node.type;
|
|
181
151
|
const originalFieldNode = parent[key];
|
|
182
152
|
const addOptionalSign = !this.config.avoidOptionals.field && originalFieldNode.type.kind !== Kind.NON_NULL_TYPE;
|
|
183
|
-
const comment =
|
|
153
|
+
const comment = getNodeComment(node);
|
|
184
154
|
const { type } = this.config.declarationKind;
|
|
185
155
|
return (comment +
|
|
186
156
|
indent(`${this.config.immutableTypes ? 'readonly ' : ''}${node.name.value}${addOptionalSign ? '?' : ''}: ${typeString}${this.getPunctuation(type)}`));
|
|
@@ -190,7 +160,7 @@ export class TsVisitor extends BaseTypesVisitor {
|
|
|
190
160
|
const addOptionalSign = !this.config.avoidOptionals.inputValue &&
|
|
191
161
|
(originalFieldNode.type.kind !== Kind.NON_NULL_TYPE ||
|
|
192
162
|
(!this.config.avoidOptionals.defaultValue && node.defaultValue !== undefined));
|
|
193
|
-
const comment =
|
|
163
|
+
const comment = getNodeComment(node);
|
|
194
164
|
const declarationKind = this.config.declarationKind.type;
|
|
195
165
|
let type = node.type;
|
|
196
166
|
if (node.directives && this.config.directiveArgumentAndInputFieldMappings) {
|
|
@@ -222,93 +192,37 @@ export class TsVisitor extends BaseTypesVisitor {
|
|
|
222
192
|
}
|
|
223
193
|
EnumTypeDefinition(node) {
|
|
224
194
|
const enumName = node.name.value;
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
}
|
|
229
|
-
const getValueFromConfig = (enumValue) => {
|
|
230
|
-
if (typeof this.config.enumValues[enumName]?.mappedValues?.[enumValue] !== 'undefined') {
|
|
231
|
-
return this.config.enumValues[enumName].mappedValues[enumValue];
|
|
195
|
+
const outputType = (() => {
|
|
196
|
+
if (this.config.enumsAsTypes) {
|
|
197
|
+
return 'string-literal';
|
|
232
198
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
this.config.
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
.
|
|
257
|
-
.join('\n')).string;
|
|
258
|
-
}
|
|
259
|
-
if (this.config.numericEnums) {
|
|
260
|
-
const block = new DeclarationBlock(this._declarationBlockConfig)
|
|
261
|
-
.export()
|
|
262
|
-
.withComment(node.description?.value)
|
|
263
|
-
.withName(enumTypeName)
|
|
264
|
-
.asKind('enum')
|
|
265
|
-
.withBlock(node.values
|
|
266
|
-
.map((enumOption, i) => {
|
|
267
|
-
const valueFromConfig = getValueFromConfig(enumOption.name.value);
|
|
268
|
-
const enumValue = valueFromConfig ?? i;
|
|
269
|
-
const comment = transformComment(enumOption.description?.value, 1);
|
|
270
|
-
const optionName = this.makeValidEnumIdentifier(this.convertName(enumOption, {
|
|
271
|
-
useTypesPrefix: false,
|
|
272
|
-
transformUnderscore: true,
|
|
273
|
-
}));
|
|
274
|
-
return comment + indent(optionName) + ` = ${enumValue}`;
|
|
275
|
-
})
|
|
276
|
-
.concat(...withFutureAddedValue)
|
|
277
|
-
.join(',\n')).string;
|
|
278
|
-
return block;
|
|
279
|
-
}
|
|
280
|
-
if (this.config.enumsAsConst) {
|
|
281
|
-
const typeName = `export type ${enumTypeName} = typeof ${enumTypeName}[keyof typeof ${enumTypeName}];`;
|
|
282
|
-
const enumAsConst = new DeclarationBlock({
|
|
283
|
-
...this._declarationBlockConfig,
|
|
284
|
-
blockTransformer: block => {
|
|
285
|
-
return block + ' as const';
|
|
199
|
+
if (this.config.numericEnums) {
|
|
200
|
+
return 'native-numeric';
|
|
201
|
+
}
|
|
202
|
+
if (this.config.enumsAsConst) {
|
|
203
|
+
return 'const';
|
|
204
|
+
}
|
|
205
|
+
return this.config.constEnums ? 'native-const' : 'native';
|
|
206
|
+
})();
|
|
207
|
+
return convertSchemaEnumToDeclarationBlockString({
|
|
208
|
+
schema: this._schema,
|
|
209
|
+
node,
|
|
210
|
+
declarationBlockConfig: this._declarationBlockConfig,
|
|
211
|
+
enumName,
|
|
212
|
+
enumValues: this.config.enumValues,
|
|
213
|
+
futureProofEnums: this.config.futureProofEnums,
|
|
214
|
+
ignoreEnumValuesFromSchema: this.config.ignoreEnumValuesFromSchema,
|
|
215
|
+
outputType,
|
|
216
|
+
naming: {
|
|
217
|
+
convert: this.config.convert,
|
|
218
|
+
options: {
|
|
219
|
+
typesPrefix: this.config.typesPrefix,
|
|
220
|
+
typesSuffix: this.config.typesSuffix,
|
|
221
|
+
useTypesPrefix: this.config.enumPrefix,
|
|
222
|
+
useTypesSuffix: this.config.enumSuffix,
|
|
286
223
|
},
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
.asKind('const')
|
|
290
|
-
.withName(enumTypeName)
|
|
291
|
-
.withComment(node.description?.value)
|
|
292
|
-
.withBlock(node.values
|
|
293
|
-
.map(enumOption => {
|
|
294
|
-
const optionName = this.makeValidEnumIdentifier(this.convertName(enumOption, {
|
|
295
|
-
useTypesPrefix: false,
|
|
296
|
-
transformUnderscore: true,
|
|
297
|
-
}));
|
|
298
|
-
const comment = transformComment(enumOption.description?.value, 1);
|
|
299
|
-
const name = enumOption.name.value;
|
|
300
|
-
const enumValue = getValueFromConfig(name) ?? name;
|
|
301
|
-
return comment + indent(`${optionName}: ${wrapWithSingleQuotes(enumValue)}`);
|
|
302
|
-
})
|
|
303
|
-
.join(',\n')).string;
|
|
304
|
-
return [enumAsConst, typeName].join('\n');
|
|
305
|
-
}
|
|
306
|
-
return new DeclarationBlock(this._declarationBlockConfig)
|
|
307
|
-
.export()
|
|
308
|
-
.asKind(this.config.constEnums ? 'const enum' : 'enum')
|
|
309
|
-
.withName(enumTypeName)
|
|
310
|
-
.withComment(node.description?.value)
|
|
311
|
-
.withBlock(this.buildEnumValuesBlock(enumName, node.values)).string;
|
|
224
|
+
},
|
|
225
|
+
});
|
|
312
226
|
}
|
|
313
227
|
getPunctuation(_declarationKind) {
|
|
314
228
|
return ';';
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-codegen/typescript",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.1",
|
|
4
4
|
"description": "GraphQL Code Generator plugin for generating TypeScript types",
|
|
5
5
|
"peerDependencies": {
|
|
6
6
|
"graphql": "^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@graphql-codegen/plugin-helpers": "^
|
|
10
|
-
"@graphql-codegen/schema-ast": "^
|
|
11
|
-
"@graphql-codegen/visitor-plugin-common": "^
|
|
12
|
-
"auto-bind": "
|
|
13
|
-
"tslib": "~2.
|
|
9
|
+
"@graphql-codegen/plugin-helpers": "^7.0.0",
|
|
10
|
+
"@graphql-codegen/schema-ast": "^6.0.0",
|
|
11
|
+
"@graphql-codegen/visitor-plugin-common": "^7.0.1",
|
|
12
|
+
"auto-bind": "^5.0.0",
|
|
13
|
+
"tslib": "~2.8.0"
|
|
14
14
|
},
|
|
15
15
|
"repository": {
|
|
16
16
|
"type": "git",
|
package/typings/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { PluginFunction, Types } from '@graphql-codegen/plugin-helpers';
|
|
2
1
|
import { GraphQLSchema } from 'graphql';
|
|
2
|
+
import { PluginFunction, Types } from '@graphql-codegen/plugin-helpers';
|
|
3
3
|
import { TypeScriptPluginConfig } from './config.cjs';
|
|
4
4
|
export * from './config.cjs';
|
|
5
5
|
export * from './introspection-visitor.cjs';
|
package/typings/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { PluginFunction, Types } from '@graphql-codegen/plugin-helpers';
|
|
2
1
|
import { GraphQLSchema } from 'graphql';
|
|
2
|
+
import { PluginFunction, Types } from '@graphql-codegen/plugin-helpers';
|
|
3
3
|
import { TypeScriptPluginConfig } from './config.js';
|
|
4
4
|
export * from './config.js';
|
|
5
5
|
export * from './introspection-visitor.js';
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { ConvertNameFn, NormalizedAvoidOptionalsConfig, NormalizedScalarsMap, OperationVariablesToObject, ParsedDirectiveArgumentAndInputFieldMappings, ParsedEnumValuesMap } from '@graphql-codegen/visitor-plugin-common';
|
|
2
1
|
import { TypeNode } from 'graphql';
|
|
2
|
+
import { ConvertNameFn, NormalizedAvoidOptionalsConfig, NormalizedScalarsMap, OperationVariablesToObject, ParsedDirectiveArgumentAndInputFieldMappings, ParsedEnumValuesMap } from '@graphql-codegen/visitor-plugin-common';
|
|
3
3
|
export declare class TypeScriptOperationVariablesToObject extends OperationVariablesToObject {
|
|
4
4
|
private _avoidOptionals;
|
|
5
5
|
private _immutableTypes;
|
|
6
6
|
private _maybeType;
|
|
7
7
|
constructor(_scalars: NormalizedScalarsMap, _convertName: ConvertNameFn, _avoidOptionals: NormalizedAvoidOptionalsConfig, _immutableTypes: boolean, _namespacedImportName?: string | null, _enumNames?: string[], _enumPrefix?: boolean, _enumSuffix?: boolean, _enumValues?: ParsedEnumValuesMap, _applyCoercion?: boolean, _directiveArgumentAndInputFieldMappings?: ParsedDirectiveArgumentAndInputFieldMappings, _maybeType?: string);
|
|
8
|
-
|
|
8
|
+
protected clearOptional(str: string): string;
|
|
9
9
|
wrapAstTypeWithModifiers(baseType: string, typeNode: TypeNode, applyCoercion?: boolean): string;
|
|
10
10
|
protected formatFieldString(fieldName: string, isNonNullType: boolean, hasDefaultValue: boolean): string;
|
|
11
11
|
protected formatTypeString(fieldType: string, isNonNullType: boolean, hasDefaultValue: boolean): string;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { ConvertNameFn, NormalizedAvoidOptionalsConfig, NormalizedScalarsMap, OperationVariablesToObject, ParsedDirectiveArgumentAndInputFieldMappings, ParsedEnumValuesMap } from '@graphql-codegen/visitor-plugin-common';
|
|
2
1
|
import { TypeNode } from 'graphql';
|
|
2
|
+
import { ConvertNameFn, NormalizedAvoidOptionalsConfig, NormalizedScalarsMap, OperationVariablesToObject, ParsedDirectiveArgumentAndInputFieldMappings, ParsedEnumValuesMap } from '@graphql-codegen/visitor-plugin-common';
|
|
3
3
|
export declare class TypeScriptOperationVariablesToObject extends OperationVariablesToObject {
|
|
4
4
|
private _avoidOptionals;
|
|
5
5
|
private _immutableTypes;
|
|
6
6
|
private _maybeType;
|
|
7
7
|
constructor(_scalars: NormalizedScalarsMap, _convertName: ConvertNameFn, _avoidOptionals: NormalizedAvoidOptionalsConfig, _immutableTypes: boolean, _namespacedImportName?: string | null, _enumNames?: string[], _enumPrefix?: boolean, _enumSuffix?: boolean, _enumValues?: ParsedEnumValuesMap, _applyCoercion?: boolean, _directiveArgumentAndInputFieldMappings?: ParsedDirectiveArgumentAndInputFieldMappings, _maybeType?: string);
|
|
8
|
-
|
|
8
|
+
protected clearOptional(str: string): string;
|
|
9
9
|
wrapAstTypeWithModifiers(baseType: string, typeNode: TypeNode, applyCoercion?: boolean): string;
|
|
10
10
|
protected formatFieldString(fieldName: string, isNonNullType: boolean, hasDefaultValue: boolean): string;
|
|
11
11
|
protected formatTypeString(fieldType: string, isNonNullType: boolean, hasDefaultValue: boolean): string;
|
package/typings/visitor.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { BaseTypesVisitor, DeclarationKind, NormalizedAvoidOptionalsConfig, ParsedTypesConfig } from '@graphql-codegen/visitor-plugin-common';
|
|
2
1
|
import { EnumTypeDefinitionNode, FieldDefinitionNode, GraphQLSchema, InputValueDefinitionNode, ListTypeNode, NamedTypeNode, NonNullTypeNode, TypeDefinitionNode, UnionTypeDefinitionNode } from 'graphql';
|
|
2
|
+
import { BaseTypesVisitor, DeclarationKind, NormalizedAvoidOptionalsConfig, ParsedTypesConfig } from '@graphql-codegen/visitor-plugin-common';
|
|
3
3
|
import { TypeScriptPluginConfig } from './config.cjs';
|
|
4
4
|
export interface TypeScriptPluginParsedConfig extends ParsedTypesConfig {
|
|
5
5
|
avoidOptionals: NormalizedAvoidOptionalsConfig;
|
|
@@ -17,20 +17,10 @@ export interface TypeScriptPluginParsedConfig extends ParsedTypesConfig {
|
|
|
17
17
|
noExport: boolean;
|
|
18
18
|
useImplementingTypes: boolean;
|
|
19
19
|
}
|
|
20
|
-
export declare const EXACT_SIGNATURE = "type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };";
|
|
21
|
-
export declare const MAKE_OPTIONAL_SIGNATURE = "type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };";
|
|
22
|
-
export declare const MAKE_MAYBE_SIGNATURE = "type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };";
|
|
23
|
-
export declare const MAKE_EMPTY_SIGNATURE = "type MakeEmpty<T extends { [key: string]: unknown }, K extends keyof T> = { [_ in K]?: never };";
|
|
24
|
-
export declare const MAKE_INCREMENTAL_SIGNATURE = "type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };";
|
|
25
20
|
export declare class TsVisitor<TRawConfig extends TypeScriptPluginConfig = TypeScriptPluginConfig, TParsedConfig extends TypeScriptPluginParsedConfig = TypeScriptPluginParsedConfig> extends BaseTypesVisitor<TRawConfig, TParsedConfig> {
|
|
26
21
|
constructor(schema: GraphQLSchema, pluginConfig: TRawConfig, additionalConfig?: Partial<TParsedConfig>);
|
|
27
22
|
protected _getTypeForNode(node: NamedTypeNode, isVisitingInputType: boolean): string;
|
|
28
23
|
getWrapperDefinitions(): string[];
|
|
29
|
-
getExactDefinition(): string;
|
|
30
|
-
getMakeOptionalDefinition(): string;
|
|
31
|
-
getMakeMaybeDefinition(): string;
|
|
32
|
-
getMakeEmptyDefinition(): string;
|
|
33
|
-
getIncrementalDefinition(): string;
|
|
34
24
|
getMaybeValue(): string;
|
|
35
25
|
getInputMaybeValue(): string;
|
|
36
26
|
protected clearOptional(str: string): string;
|
package/typings/visitor.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { BaseTypesVisitor, DeclarationKind, NormalizedAvoidOptionalsConfig, ParsedTypesConfig } from '@graphql-codegen/visitor-plugin-common';
|
|
2
1
|
import { EnumTypeDefinitionNode, FieldDefinitionNode, GraphQLSchema, InputValueDefinitionNode, ListTypeNode, NamedTypeNode, NonNullTypeNode, TypeDefinitionNode, UnionTypeDefinitionNode } from 'graphql';
|
|
2
|
+
import { BaseTypesVisitor, DeclarationKind, NormalizedAvoidOptionalsConfig, ParsedTypesConfig } from '@graphql-codegen/visitor-plugin-common';
|
|
3
3
|
import { TypeScriptPluginConfig } from './config.js';
|
|
4
4
|
export interface TypeScriptPluginParsedConfig extends ParsedTypesConfig {
|
|
5
5
|
avoidOptionals: NormalizedAvoidOptionalsConfig;
|
|
@@ -17,20 +17,10 @@ export interface TypeScriptPluginParsedConfig extends ParsedTypesConfig {
|
|
|
17
17
|
noExport: boolean;
|
|
18
18
|
useImplementingTypes: boolean;
|
|
19
19
|
}
|
|
20
|
-
export declare const EXACT_SIGNATURE = "type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };";
|
|
21
|
-
export declare const MAKE_OPTIONAL_SIGNATURE = "type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };";
|
|
22
|
-
export declare const MAKE_MAYBE_SIGNATURE = "type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };";
|
|
23
|
-
export declare const MAKE_EMPTY_SIGNATURE = "type MakeEmpty<T extends { [key: string]: unknown }, K extends keyof T> = { [_ in K]?: never };";
|
|
24
|
-
export declare const MAKE_INCREMENTAL_SIGNATURE = "type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };";
|
|
25
20
|
export declare class TsVisitor<TRawConfig extends TypeScriptPluginConfig = TypeScriptPluginConfig, TParsedConfig extends TypeScriptPluginParsedConfig = TypeScriptPluginParsedConfig> extends BaseTypesVisitor<TRawConfig, TParsedConfig> {
|
|
26
21
|
constructor(schema: GraphQLSchema, pluginConfig: TRawConfig, additionalConfig?: Partial<TParsedConfig>);
|
|
27
22
|
protected _getTypeForNode(node: NamedTypeNode, isVisitingInputType: boolean): string;
|
|
28
23
|
getWrapperDefinitions(): string[];
|
|
29
|
-
getExactDefinition(): string;
|
|
30
|
-
getMakeOptionalDefinition(): string;
|
|
31
|
-
getMakeMaybeDefinition(): string;
|
|
32
|
-
getMakeEmptyDefinition(): string;
|
|
33
|
-
getIncrementalDefinition(): string;
|
|
34
24
|
getMaybeValue(): string;
|
|
35
25
|
getInputMaybeValue(): string;
|
|
36
26
|
protected clearOptional(str: string): string;
|