@graphql-codegen/typescript 6.0.0-alpha-20251124142004-67952cd52352f89ebcdb97bfca7c34bc89fbacc3 → 6.0.0-alpha-20251125123407-8babe46fb9b33e9f9a377cd50c9580282e7981d3
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/visitor.js +29 -87
- package/esm/visitor.js +30 -88
- package/package.json +2 -2
package/cjs/visitor.js
CHANGED
|
@@ -177,7 +177,7 @@ class TsVisitor extends visitor_plugin_common_1.BaseTypesVisitor {
|
|
|
177
177
|
: node.type;
|
|
178
178
|
const originalFieldNode = parent[key];
|
|
179
179
|
const addOptionalSign = !this.config.avoidOptionals.field && originalFieldNode.type.kind !== graphql_1.Kind.NON_NULL_TYPE;
|
|
180
|
-
const comment =
|
|
180
|
+
const comment = (0, visitor_plugin_common_1.getNodeComment)(node);
|
|
181
181
|
const { type } = this.config.declarationKind;
|
|
182
182
|
return (comment +
|
|
183
183
|
(0, visitor_plugin_common_1.indent)(`${this.config.immutableTypes ? 'readonly ' : ''}${node.name.value}${addOptionalSign ? '?' : ''}: ${typeString}${this.getPunctuation(type)}`));
|
|
@@ -187,7 +187,7 @@ class TsVisitor extends visitor_plugin_common_1.BaseTypesVisitor {
|
|
|
187
187
|
const addOptionalSign = !this.config.avoidOptionals.inputValue &&
|
|
188
188
|
(originalFieldNode.type.kind !== graphql_1.Kind.NON_NULL_TYPE ||
|
|
189
189
|
(!this.config.avoidOptionals.defaultValue && node.defaultValue !== undefined));
|
|
190
|
-
const comment =
|
|
190
|
+
const comment = (0, visitor_plugin_common_1.getNodeComment)(node);
|
|
191
191
|
const declarationKind = this.config.declarationKind.type;
|
|
192
192
|
let type = node.type;
|
|
193
193
|
if (node.directives && this.config.directiveArgumentAndInputFieldMappings) {
|
|
@@ -219,93 +219,35 @@ class TsVisitor extends visitor_plugin_common_1.BaseTypesVisitor {
|
|
|
219
219
|
}
|
|
220
220
|
EnumTypeDefinition(node) {
|
|
221
221
|
const enumName = node.name.value;
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
}
|
|
226
|
-
const getValueFromConfig = (enumValue) => {
|
|
227
|
-
if (typeof this.config.enumValues[enumName]?.mappedValues?.[enumValue] !== 'undefined') {
|
|
228
|
-
return this.config.enumValues[enumName].mappedValues[enumValue];
|
|
222
|
+
const outputType = (() => {
|
|
223
|
+
if (this.config.enumsAsTypes) {
|
|
224
|
+
return 'string-literal';
|
|
229
225
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
this.config.
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
226
|
+
if (this.config.numericEnums) {
|
|
227
|
+
return 'native-numeric';
|
|
228
|
+
}
|
|
229
|
+
if (this.config.enumsAsConst) {
|
|
230
|
+
return 'const';
|
|
231
|
+
}
|
|
232
|
+
return this.config.constEnums ? 'native-const' : 'native';
|
|
233
|
+
})();
|
|
234
|
+
return (0, visitor_plugin_common_1.convertSchemaEnumToDeclarationBlockString)({
|
|
235
|
+
schema: this._schema,
|
|
236
|
+
node,
|
|
237
|
+
declarationBlockConfig: this._declarationBlockConfig,
|
|
238
|
+
enumName,
|
|
239
|
+
enumValues: this.config.enumValues,
|
|
240
|
+
futureProofEnums: this.config.futureProofEnums,
|
|
241
|
+
ignoreEnumValuesFromSchema: this.config.ignoreEnumValuesFromSchema,
|
|
242
|
+
outputType,
|
|
243
|
+
naming: {
|
|
244
|
+
convert: this.config.convert,
|
|
245
|
+
typesPrefix: this.config.typesPrefix,
|
|
246
|
+
typesSuffix: this.config.typesSuffix,
|
|
247
|
+
useTypesPrefix: this.config.enumPrefix,
|
|
248
|
+
useTypesSuffix: this.config.enumSuffix,
|
|
249
|
+
},
|
|
238
250
|
});
|
|
239
|
-
if (this.config.enumsAsTypes) {
|
|
240
|
-
return new visitor_plugin_common_1.DeclarationBlock(this._declarationBlockConfig)
|
|
241
|
-
.export()
|
|
242
|
-
.asKind('type')
|
|
243
|
-
.withComment(node.description?.value)
|
|
244
|
-
.withName(enumTypeName)
|
|
245
|
-
.withContent('\n' +
|
|
246
|
-
node.values
|
|
247
|
-
.map(enumOption => {
|
|
248
|
-
const name = enumOption.name.value;
|
|
249
|
-
const enumValue = getValueFromConfig(name) ?? name;
|
|
250
|
-
const comment = (0, visitor_plugin_common_1.transformComment)(enumOption.description?.value, 1);
|
|
251
|
-
return comment + (0, visitor_plugin_common_1.indent)('| ' + (0, visitor_plugin_common_1.wrapWithSingleQuotes)(enumValue));
|
|
252
|
-
})
|
|
253
|
-
.concat(...withFutureAddedValue)
|
|
254
|
-
.join('\n')).string;
|
|
255
|
-
}
|
|
256
|
-
if (this.config.numericEnums) {
|
|
257
|
-
const block = new visitor_plugin_common_1.DeclarationBlock(this._declarationBlockConfig)
|
|
258
|
-
.export()
|
|
259
|
-
.withComment(node.description?.value)
|
|
260
|
-
.withName(enumTypeName)
|
|
261
|
-
.asKind('enum')
|
|
262
|
-
.withBlock(node.values
|
|
263
|
-
.map((enumOption, i) => {
|
|
264
|
-
const valueFromConfig = getValueFromConfig(enumOption.name.value);
|
|
265
|
-
const enumValue = valueFromConfig ?? i;
|
|
266
|
-
const comment = (0, visitor_plugin_common_1.transformComment)(enumOption.description?.value, 1);
|
|
267
|
-
const optionName = this.makeValidEnumIdentifier(this.convertName(enumOption, {
|
|
268
|
-
useTypesPrefix: false,
|
|
269
|
-
transformUnderscore: true,
|
|
270
|
-
}));
|
|
271
|
-
return comment + (0, visitor_plugin_common_1.indent)(optionName) + ` = ${enumValue}`;
|
|
272
|
-
})
|
|
273
|
-
.concat(...withFutureAddedValue)
|
|
274
|
-
.join(',\n')).string;
|
|
275
|
-
return block;
|
|
276
|
-
}
|
|
277
|
-
if (this.config.enumsAsConst) {
|
|
278
|
-
const typeName = `export type ${enumTypeName} = typeof ${enumTypeName}[keyof typeof ${enumTypeName}];`;
|
|
279
|
-
const enumAsConst = new visitor_plugin_common_1.DeclarationBlock({
|
|
280
|
-
...this._declarationBlockConfig,
|
|
281
|
-
blockTransformer: block => {
|
|
282
|
-
return block + ' as const';
|
|
283
|
-
},
|
|
284
|
-
})
|
|
285
|
-
.export()
|
|
286
|
-
.asKind('const')
|
|
287
|
-
.withName(enumTypeName)
|
|
288
|
-
.withComment(node.description?.value)
|
|
289
|
-
.withBlock(node.values
|
|
290
|
-
.map(enumOption => {
|
|
291
|
-
const optionName = this.makeValidEnumIdentifier(this.convertName(enumOption, {
|
|
292
|
-
useTypesPrefix: false,
|
|
293
|
-
transformUnderscore: true,
|
|
294
|
-
}));
|
|
295
|
-
const comment = (0, visitor_plugin_common_1.transformComment)(enumOption.description?.value, 1);
|
|
296
|
-
const name = enumOption.name.value;
|
|
297
|
-
const enumValue = getValueFromConfig(name) ?? name;
|
|
298
|
-
return comment + (0, visitor_plugin_common_1.indent)(`${optionName}: ${(0, visitor_plugin_common_1.wrapWithSingleQuotes)(enumValue)}`);
|
|
299
|
-
})
|
|
300
|
-
.join(',\n')).string;
|
|
301
|
-
return [enumAsConst, typeName].join('\n');
|
|
302
|
-
}
|
|
303
|
-
return new visitor_plugin_common_1.DeclarationBlock(this._declarationBlockConfig)
|
|
304
|
-
.export()
|
|
305
|
-
.asKind(this.config.constEnums ? 'const enum' : 'enum')
|
|
306
|
-
.withName(enumTypeName)
|
|
307
|
-
.withComment(node.description?.value)
|
|
308
|
-
.withBlock(this.buildEnumValuesBlock(enumName, node.values)).string;
|
|
309
251
|
}
|
|
310
252
|
getPunctuation(_declarationKind) {
|
|
311
253
|
return ';';
|
package/esm/visitor.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BaseTypesVisitor, DeclarationBlock, getConfigValue, indent, isOneOfInputObjectType, normalizeAvoidOptionals,
|
|
1
|
+
import { BaseTypesVisitor, convertSchemaEnumToDeclarationBlockString, DeclarationBlock, getConfigValue, getNodeComment, indent, isOneOfInputObjectType, normalizeAvoidOptionals, } from '@graphql-codegen/visitor-plugin-common';
|
|
2
2
|
import autoBind from 'auto-bind';
|
|
3
3
|
import { GraphQLObjectType, isEnumType, Kind, } from 'graphql';
|
|
4
4
|
import { TypeScriptOperationVariablesToObject } from './typescript-variables-to-object.js';
|
|
@@ -173,7 +173,7 @@ export class TsVisitor extends BaseTypesVisitor {
|
|
|
173
173
|
: node.type;
|
|
174
174
|
const originalFieldNode = parent[key];
|
|
175
175
|
const addOptionalSign = !this.config.avoidOptionals.field && originalFieldNode.type.kind !== Kind.NON_NULL_TYPE;
|
|
176
|
-
const comment =
|
|
176
|
+
const comment = getNodeComment(node);
|
|
177
177
|
const { type } = this.config.declarationKind;
|
|
178
178
|
return (comment +
|
|
179
179
|
indent(`${this.config.immutableTypes ? 'readonly ' : ''}${node.name.value}${addOptionalSign ? '?' : ''}: ${typeString}${this.getPunctuation(type)}`));
|
|
@@ -183,7 +183,7 @@ export class TsVisitor extends BaseTypesVisitor {
|
|
|
183
183
|
const addOptionalSign = !this.config.avoidOptionals.inputValue &&
|
|
184
184
|
(originalFieldNode.type.kind !== Kind.NON_NULL_TYPE ||
|
|
185
185
|
(!this.config.avoidOptionals.defaultValue && node.defaultValue !== undefined));
|
|
186
|
-
const comment =
|
|
186
|
+
const comment = getNodeComment(node);
|
|
187
187
|
const declarationKind = this.config.declarationKind.type;
|
|
188
188
|
let type = node.type;
|
|
189
189
|
if (node.directives && this.config.directiveArgumentAndInputFieldMappings) {
|
|
@@ -215,93 +215,35 @@ export class TsVisitor extends BaseTypesVisitor {
|
|
|
215
215
|
}
|
|
216
216
|
EnumTypeDefinition(node) {
|
|
217
217
|
const enumName = node.name.value;
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
}
|
|
222
|
-
const getValueFromConfig = (enumValue) => {
|
|
223
|
-
if (typeof this.config.enumValues[enumName]?.mappedValues?.[enumValue] !== 'undefined') {
|
|
224
|
-
return this.config.enumValues[enumName].mappedValues[enumValue];
|
|
218
|
+
const outputType = (() => {
|
|
219
|
+
if (this.config.enumsAsTypes) {
|
|
220
|
+
return 'string-literal';
|
|
225
221
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
this.config.
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
222
|
+
if (this.config.numericEnums) {
|
|
223
|
+
return 'native-numeric';
|
|
224
|
+
}
|
|
225
|
+
if (this.config.enumsAsConst) {
|
|
226
|
+
return 'const';
|
|
227
|
+
}
|
|
228
|
+
return this.config.constEnums ? 'native-const' : 'native';
|
|
229
|
+
})();
|
|
230
|
+
return convertSchemaEnumToDeclarationBlockString({
|
|
231
|
+
schema: this._schema,
|
|
232
|
+
node,
|
|
233
|
+
declarationBlockConfig: this._declarationBlockConfig,
|
|
234
|
+
enumName,
|
|
235
|
+
enumValues: this.config.enumValues,
|
|
236
|
+
futureProofEnums: this.config.futureProofEnums,
|
|
237
|
+
ignoreEnumValuesFromSchema: this.config.ignoreEnumValuesFromSchema,
|
|
238
|
+
outputType,
|
|
239
|
+
naming: {
|
|
240
|
+
convert: this.config.convert,
|
|
241
|
+
typesPrefix: this.config.typesPrefix,
|
|
242
|
+
typesSuffix: this.config.typesSuffix,
|
|
243
|
+
useTypesPrefix: this.config.enumPrefix,
|
|
244
|
+
useTypesSuffix: this.config.enumSuffix,
|
|
245
|
+
},
|
|
234
246
|
});
|
|
235
|
-
if (this.config.enumsAsTypes) {
|
|
236
|
-
return new DeclarationBlock(this._declarationBlockConfig)
|
|
237
|
-
.export()
|
|
238
|
-
.asKind('type')
|
|
239
|
-
.withComment(node.description?.value)
|
|
240
|
-
.withName(enumTypeName)
|
|
241
|
-
.withContent('\n' +
|
|
242
|
-
node.values
|
|
243
|
-
.map(enumOption => {
|
|
244
|
-
const name = enumOption.name.value;
|
|
245
|
-
const enumValue = getValueFromConfig(name) ?? name;
|
|
246
|
-
const comment = transformComment(enumOption.description?.value, 1);
|
|
247
|
-
return comment + indent('| ' + wrapWithSingleQuotes(enumValue));
|
|
248
|
-
})
|
|
249
|
-
.concat(...withFutureAddedValue)
|
|
250
|
-
.join('\n')).string;
|
|
251
|
-
}
|
|
252
|
-
if (this.config.numericEnums) {
|
|
253
|
-
const block = new DeclarationBlock(this._declarationBlockConfig)
|
|
254
|
-
.export()
|
|
255
|
-
.withComment(node.description?.value)
|
|
256
|
-
.withName(enumTypeName)
|
|
257
|
-
.asKind('enum')
|
|
258
|
-
.withBlock(node.values
|
|
259
|
-
.map((enumOption, i) => {
|
|
260
|
-
const valueFromConfig = getValueFromConfig(enumOption.name.value);
|
|
261
|
-
const enumValue = valueFromConfig ?? i;
|
|
262
|
-
const comment = transformComment(enumOption.description?.value, 1);
|
|
263
|
-
const optionName = this.makeValidEnumIdentifier(this.convertName(enumOption, {
|
|
264
|
-
useTypesPrefix: false,
|
|
265
|
-
transformUnderscore: true,
|
|
266
|
-
}));
|
|
267
|
-
return comment + indent(optionName) + ` = ${enumValue}`;
|
|
268
|
-
})
|
|
269
|
-
.concat(...withFutureAddedValue)
|
|
270
|
-
.join(',\n')).string;
|
|
271
|
-
return block;
|
|
272
|
-
}
|
|
273
|
-
if (this.config.enumsAsConst) {
|
|
274
|
-
const typeName = `export type ${enumTypeName} = typeof ${enumTypeName}[keyof typeof ${enumTypeName}];`;
|
|
275
|
-
const enumAsConst = new DeclarationBlock({
|
|
276
|
-
...this._declarationBlockConfig,
|
|
277
|
-
blockTransformer: block => {
|
|
278
|
-
return block + ' as const';
|
|
279
|
-
},
|
|
280
|
-
})
|
|
281
|
-
.export()
|
|
282
|
-
.asKind('const')
|
|
283
|
-
.withName(enumTypeName)
|
|
284
|
-
.withComment(node.description?.value)
|
|
285
|
-
.withBlock(node.values
|
|
286
|
-
.map(enumOption => {
|
|
287
|
-
const optionName = this.makeValidEnumIdentifier(this.convertName(enumOption, {
|
|
288
|
-
useTypesPrefix: false,
|
|
289
|
-
transformUnderscore: true,
|
|
290
|
-
}));
|
|
291
|
-
const comment = transformComment(enumOption.description?.value, 1);
|
|
292
|
-
const name = enumOption.name.value;
|
|
293
|
-
const enumValue = getValueFromConfig(name) ?? name;
|
|
294
|
-
return comment + indent(`${optionName}: ${wrapWithSingleQuotes(enumValue)}`);
|
|
295
|
-
})
|
|
296
|
-
.join(',\n')).string;
|
|
297
|
-
return [enumAsConst, typeName].join('\n');
|
|
298
|
-
}
|
|
299
|
-
return new DeclarationBlock(this._declarationBlockConfig)
|
|
300
|
-
.export()
|
|
301
|
-
.asKind(this.config.constEnums ? 'const enum' : 'enum')
|
|
302
|
-
.withName(enumTypeName)
|
|
303
|
-
.withComment(node.description?.value)
|
|
304
|
-
.withBlock(this.buildEnumValuesBlock(enumName, node.values)).string;
|
|
305
247
|
}
|
|
306
248
|
getPunctuation(_declarationKind) {
|
|
307
249
|
return ';';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-codegen/typescript",
|
|
3
|
-
"version": "6.0.0-alpha-
|
|
3
|
+
"version": "6.0.0-alpha-20251125123407-8babe46fb9b33e9f9a377cd50c9580282e7981d3",
|
|
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"
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"dependencies": {
|
|
9
9
|
"@graphql-codegen/plugin-helpers": "^6.0.0",
|
|
10
10
|
"@graphql-codegen/schema-ast": "^5.0.0",
|
|
11
|
-
"@graphql-codegen/visitor-plugin-common": "
|
|
11
|
+
"@graphql-codegen/visitor-plugin-common": "7.0.0-alpha-20251125123407-8babe46fb9b33e9f9a377cd50c9580282e7981d3",
|
|
12
12
|
"auto-bind": "~4.0.0",
|
|
13
13
|
"tslib": "~2.6.0"
|
|
14
14
|
},
|