Skip to content

Commit 91e5886

Browse files
authored
[typescript-operations] Fix enumValues not considering namingConvention in certain scenarios (#10656)
* Set up test * Refactor naming functions to visitor-plugin-common/naming * Parse EnumValues with applying namingConvention on the schema type and use it for imports * Ensure converted enum type is used across imports, exports and Result * Fix enum-values.spec tests * Update implementation to match expected * Update enumValues tests * Ensure correct naming convention being applied to Input, Variables and Result types * Create a section for enumValues * add changeset
1 parent b352e12 commit 91e5886

14 files changed

Lines changed: 698 additions & 424 deletions

File tree

.changeset/whole-eagles-switch.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@graphql-codegen/visitor-plugin-common': patch
3+
'@graphql-codegen/typescript-operations': patch
4+
'@graphql-codegen/typescript': patch
5+
---
6+
7+
Fix namingConvention not being applied consistently in imports, Variables, Input and Result

packages/plugins/other/visitor-plugin-common/src/base-resolvers-visitor.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -743,10 +743,6 @@ export class BaseResolversVisitor<
743743
optionalResolveType: getConfigValue(rawConfig.optionalResolveType, false),
744744
federation: getConfigValue(rawConfig.federation, false),
745745
resolverTypeWrapperSignature: getConfigValue(rawConfig.resolverTypeWrapperSignature, 'Promise<T> | T'),
746-
enumValues: parseEnumValues({
747-
schema: _schema,
748-
mapOrStr: rawConfig.enumValues,
749-
}),
750746
addUnderscoreToArgsType: getConfigValue(rawConfig.addUnderscoreToArgsType, false),
751747
addInterfaceFieldResolverTypes: getConfigValue(rawConfig.addInterfaceFieldResolverTypes, false),
752748
contextType: parseMapper(rawConfig.contextType || 'any', 'ContextType'),
@@ -771,6 +767,20 @@ export class BaseResolversVisitor<
771767
...additionalConfig,
772768
} as TPluginConfig);
773769

770+
this.config.enumValues = parseEnumValues({
771+
schema: _schema,
772+
mapOrStr: rawConfig.enumValues,
773+
naming: {
774+
convert: this.config.convert,
775+
options: {
776+
typesPrefix: this.config.typesPrefix,
777+
typesSuffix: this.config.typesSuffix,
778+
useTypesPrefix: this.config.enumPrefix,
779+
useTypesSuffix: this.config.enumSuffix,
780+
},
781+
},
782+
});
783+
774784
autoBind(this);
775785
this._federation = new ApolloFederation({
776786
enabled: this.config.federation,

packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -505,11 +505,6 @@ export class BaseTypesVisitor<
505505
onlyEnums: getConfigValue(rawConfig.onlyEnums, false),
506506
onlyOperationTypes: getConfigValue(rawConfig.onlyOperationTypes, false),
507507
addUnderscoreToArgsType: getConfigValue(rawConfig.addUnderscoreToArgsType, false),
508-
enumValues: parseEnumValues({
509-
schema: _schema,
510-
mapOrStr: rawConfig.enumValues,
511-
ignoreEnumValuesFromSchema: rawConfig.ignoreEnumValuesFromSchema,
512-
}),
513508
ignoreEnumValuesFromSchema: getConfigValue(rawConfig.ignoreEnumValuesFromSchema, false),
514509
declarationKind: normalizeDeclarationKind(rawConfig.declarationKind),
515510
scalars: buildScalarsFromConfig(_schema, rawConfig, defaultScalars),
@@ -526,6 +521,21 @@ export class BaseTypesVisitor<
526521
...additionalConfig,
527522
});
528523

524+
this.config.enumValues = parseEnumValues({
525+
schema: _schema,
526+
mapOrStr: rawConfig.enumValues,
527+
ignoreEnumValuesFromSchema: this.config.ignoreEnumValuesFromSchema,
528+
naming: {
529+
convert: this.config.convert,
530+
options: {
531+
typesPrefix: this.config.typesPrefix,
532+
typesSuffix: this.config.typesSuffix,
533+
useTypesPrefix: this.config.enumPrefix,
534+
useTypesSuffix: this.config.enumSuffix,
535+
},
536+
},
537+
});
538+
529539
// Note: Missing directive mappers but not a problem since always overriden by implementors
530540
this._argumentsTransformer = new OperationVariablesToObject(this.scalars, this.convertName);
531541
}
@@ -856,10 +866,12 @@ export class BaseTypesVisitor<
856866
schema: this._schema,
857867
naming: {
858868
convert: this.config.convert,
859-
typesPrefix: this.config.typesPrefix,
860-
useTypesPrefix: this.config.enumPrefix,
861-
typesSuffix: this.config.typesSuffix,
862-
useTypesSuffix: this.config.enumSuffix,
869+
options: {
870+
typesPrefix: this.config.typesPrefix,
871+
useTypesPrefix: this.config.enumPrefix,
872+
typesSuffix: this.config.typesSuffix,
873+
useTypesSuffix: this.config.enumSuffix,
874+
},
863875
},
864876
ignoreEnumValuesFromSchema: this.config.ignoreEnumValuesFromSchema,
865877
declarationBlockConfig: this._declarationBlockConfig,

packages/plugins/other/visitor-plugin-common/src/convert-schema-enum-to-declaration-block-string.ts

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
getNodeComment,
99
wrapWithSingleQuotes,
1010
} from './utils.js';
11+
import { convertName } from './naming.js';
1112

1213
export interface ConvertSchemaEnumToDeclarationBlockString {
1314
schema: GraphQLSchema;
@@ -18,10 +19,12 @@ export interface ConvertSchemaEnumToDeclarationBlockString {
1819
ignoreEnumValuesFromSchema: boolean;
1920
naming: {
2021
convert: ConvertFn;
21-
typesPrefix: string;
22-
typesSuffix: string;
23-
useTypesPrefix?: boolean;
24-
useTypesSuffix?: boolean;
22+
options: {
23+
typesPrefix: string;
24+
typesSuffix: string;
25+
useTypesPrefix?: boolean;
26+
useTypesSuffix?: boolean;
27+
};
2528
};
2629

2730
outputType: 'string-literal' | 'native-numeric' | 'const' | 'native-const' | 'native';
@@ -40,7 +43,7 @@ export const convertSchemaEnumToDeclarationBlockString = ({
4043
naming,
4144
}: ConvertSchemaEnumToDeclarationBlockString): string => {
4245
if (enumValues[enumName]?.sourceFile) {
43-
return `export { ${enumValues[enumName].typeIdentifier} };\n`;
46+
return `export { ${enumValues[enumName].typeIdentifierConverted} };\n`;
4447
}
4548

4649
const getValueFromConfig = (enumValue: string | number) => {
@@ -53,13 +56,8 @@ export const convertSchemaEnumToDeclarationBlockString = ({
5356
const withFutureAddedValue = [futureProofEnums ? [indent('| ' + wrapWithSingleQuotes('%future added value'))] : []];
5457

5558
const enumTypeName = convertName({
56-
options: {
57-
typesPrefix: naming.typesPrefix,
58-
typesSuffix: naming.typesSuffix,
59-
useTypesPrefix: naming.useTypesPrefix,
60-
useTypesSuffix: naming.useTypesSuffix,
61-
},
6259
convert: () => naming.convert(node),
60+
options: naming.options,
6361
});
6462

6563
if (outputType === 'string-literal') {
@@ -98,8 +96,8 @@ export const convertSchemaEnumToDeclarationBlockString = ({
9896
const optionName = makeValidEnumIdentifier(
9997
convertName({
10098
options: {
101-
typesPrefix: naming.typesPrefix,
102-
typesSuffix: naming.typesSuffix,
99+
typesPrefix: naming.options.typesPrefix,
100+
typesSuffix: naming.options.typesSuffix,
103101
useTypesPrefix: false,
104102
},
105103
convert: () => naming.convert(enumOption, { transformUnderscore: true }),
@@ -130,8 +128,8 @@ export const convertSchemaEnumToDeclarationBlockString = ({
130128
const optionName = makeValidEnumIdentifier(
131129
convertName({
132130
options: {
133-
typesPrefix: naming.typesPrefix,
134-
typesSuffix: naming.typesPrefix,
131+
typesPrefix: naming.options.typesPrefix,
132+
typesSuffix: naming.options.typesPrefix,
135133
},
136134
convert: () =>
137135
naming.convert(enumOption, {
@@ -195,8 +193,8 @@ export const buildEnumValuesBlock = ({
195193
convertName({
196194
options: {
197195
useTypesPrefix: false,
198-
typesPrefix: naming.typesPrefix,
199-
typesSuffix: naming.typesSuffix,
196+
typesPrefix: naming.options.typesPrefix,
197+
typesSuffix: naming.options.typesSuffix,
200198
},
201199
convert: () =>
202200
naming.convert(enumOption, {
@@ -236,33 +234,3 @@ const makeValidEnumIdentifier = (identifier: string): string => {
236234
}
237235
return identifier;
238236
};
239-
240-
const convertName = ({
241-
convert,
242-
options,
243-
}: {
244-
options: {
245-
typesPrefix: string;
246-
useTypesPrefix?: boolean;
247-
typesSuffix: string;
248-
useTypesSuffix?: boolean;
249-
};
250-
convert: () => string;
251-
}): string => {
252-
const useTypesPrefix = typeof options.useTypesPrefix === 'boolean' ? options.useTypesPrefix : true;
253-
const useTypesSuffix = typeof options.useTypesSuffix === 'boolean' ? options.useTypesSuffix : true;
254-
255-
let convertedName = '';
256-
257-
if (useTypesPrefix) {
258-
convertedName += options.typesPrefix;
259-
}
260-
261-
convertedName += convert();
262-
263-
if (useTypesSuffix) {
264-
convertedName += options.typesSuffix;
265-
}
266-
267-
return convertedName;
268-
};

packages/plugins/other/visitor-plugin-common/src/enum-values.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { GraphQLEnumType, GraphQLSchema, isEnumType } from 'graphql';
22
import { parseMapper } from './mappers.js';
3-
import { EnumValuesMap, ParsedEnumValuesMap } from './types.js';
3+
import type { ConvertFn, EnumValuesMap, ParsedEnumValuesMap } from './types.js';
4+
import { convertName } from './naming.js';
45

56
function escapeString(str: string) {
67
return str.replace(/\\/g, '\\\\').replace(/\n/g, '\\n').replace(/'/g, "\\'");
@@ -10,10 +11,20 @@ export function parseEnumValues({
1011
schema,
1112
mapOrStr = {},
1213
ignoreEnumValuesFromSchema,
14+
naming,
1315
}: {
1416
schema: GraphQLSchema;
1517
mapOrStr: EnumValuesMap;
1618
ignoreEnumValuesFromSchema?: boolean;
19+
naming: {
20+
convert: ConvertFn;
21+
options: {
22+
typesPrefix: string;
23+
typesSuffix: string;
24+
useTypesPrefix?: boolean;
25+
useTypesSuffix?: boolean;
26+
};
27+
};
1728
}): ParsedEnumValuesMap {
1829
const allTypes = schema.getTypeMap();
1930
const allEnums = Object.keys(allTypes).filter(t => isEnumType(allTypes[t]));
@@ -42,7 +53,7 @@ export function parseEnumValues({
4253
);
4354
}
4455

45-
return Object.keys(mapOrStr).reduce((prev, gqlIdentifier) => {
56+
return Object.keys(mapOrStr).reduce<ParsedEnumValuesMap>((prev, gqlIdentifier) => {
4657
const pointer = mapOrStr[gqlIdentifier];
4758

4859
if (typeof pointer === 'string') {
@@ -53,6 +64,10 @@ export function parseEnumValues({
5364
[gqlIdentifier]: {
5465
isDefault: mapper.isExternal && mapper.default,
5566
typeIdentifier: gqlIdentifier,
67+
typeIdentifierConverted: convertName({
68+
convert: () => naming.convert(gqlIdentifier),
69+
options: naming.options,
70+
}),
5671
sourceFile: mapper.isExternal ? mapper.source : null,
5772
sourceIdentifier: mapper.type,
5873
importIdentifier: mapper.isExternal ? mapper.import : null,
@@ -66,6 +81,10 @@ export function parseEnumValues({
6681
[gqlIdentifier]: {
6782
isDefault: false,
6883
typeIdentifier: gqlIdentifier,
84+
typeIdentifierConverted: convertName({
85+
convert: () => naming.convert(gqlIdentifier),
86+
options: naming.options,
87+
}),
6988
sourceFile: null,
7089
sourceIdentifier: null,
7190
importIdentifier: null,
@@ -77,24 +96,28 @@ export function parseEnumValues({
7796
`Invalid "enumValues" configuration \n
7897
Enum "${gqlIdentifier}": expected string or object (with enum values mapping)`
7998
);
80-
}, {} as ParsedEnumValuesMap);
99+
}, {});
81100
}
82101
if (typeof mapOrStr === 'string') {
83102
return allEnums
84103
.filter(enumName => !enumName.startsWith('__'))
85-
.reduce((prev, enumName) => {
104+
.reduce<ParsedEnumValuesMap>((prev, enumName) => {
86105
return {
87106
...prev,
88107
[enumName]: {
89108
isDefault: false,
90109
typeIdentifier: enumName,
110+
typeIdentifierConverted: convertName({
111+
convert: () => naming.convert(enumName),
112+
options: naming.options,
113+
}),
91114
sourceFile: mapOrStr,
92115
sourceIdentifier: enumName,
93116
importIdentifier: enumName,
94117
mappedValues: null,
95118
},
96119
};
97-
}, {} as ParsedEnumValuesMap);
120+
}, {});
98121
}
99122

100123
return {};

packages/plugins/other/visitor-plugin-common/src/imports.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@ export function getEnumsImports({
115115
useTypeImports: boolean;
116116
}): string[] {
117117
function handleEnumValueMapper({
118-
typeIdentifier,
118+
typeIdentifierConverted,
119119
importIdentifier,
120120
sourceIdentifier,
121121
sourceFile,
122122
useTypeImports,
123123
}: {
124-
typeIdentifier: string;
124+
typeIdentifierConverted: string;
125125
importIdentifier: string | null;
126126
sourceIdentifier: string | null;
127127
sourceFile: string | null;
@@ -132,12 +132,16 @@ export function getEnumsImports({
132132
// { enumValues: { MyEnum: './my-file#NS.NestedEnum' } }
133133
return [
134134
buildTypeImport({ identifier: importIdentifier || sourceIdentifier, source: sourceFile, useTypeImports }),
135-
`import ${typeIdentifier} = ${sourceIdentifier};`,
135+
`import ${typeIdentifierConverted} = ${sourceIdentifier};`,
136136
];
137137
}
138-
if (sourceIdentifier !== typeIdentifier) {
138+
if (sourceIdentifier !== typeIdentifierConverted) {
139139
return [
140-
buildTypeImport({ identifier: `${sourceIdentifier} as ${typeIdentifier}`, source: sourceFile, useTypeImports }),
140+
buildTypeImport({
141+
identifier: `${sourceIdentifier} as ${typeIdentifierConverted}`,
142+
source: sourceFile,
143+
useTypeImports,
144+
}),
141145
];
142146
}
143147
return [buildTypeImport({ identifier: importIdentifier || sourceIdentifier, source: sourceFile, useTypeImports })];
@@ -150,7 +154,7 @@ export function getEnumsImports({
150154
if (mappedValue.isDefault) {
151155
return [
152156
buildTypeImport({
153-
identifier: mappedValue.typeIdentifier,
157+
identifier: mappedValue.typeIdentifierConverted,
154158
source: mappedValue.sourceFile,
155159
asDefault: true,
156160
useTypeImports,
@@ -159,7 +163,7 @@ export function getEnumsImports({
159163
}
160164

161165
return handleEnumValueMapper({
162-
typeIdentifier: mappedValue.typeIdentifier,
166+
typeIdentifierConverted: mappedValue.typeIdentifierConverted,
163167
importIdentifier: mappedValue.importIdentifier,
164168
sourceIdentifier: mappedValue.sourceIdentifier,
165169
sourceFile: mappedValue.sourceFile,

0 commit comments

Comments
 (0)