Skip to content
7 changes: 7 additions & 0 deletions .changeset/whole-eagles-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@graphql-codegen/visitor-plugin-common': patch
'@graphql-codegen/typescript-operations': patch
'@graphql-codegen/typescript': patch
---

Fix namingConvention not being applied consistently in imports, Variables, Input and Result
Original file line number Diff line number Diff line change
Expand Up @@ -743,10 +743,6 @@ export class BaseResolversVisitor<
optionalResolveType: getConfigValue(rawConfig.optionalResolveType, false),
federation: getConfigValue(rawConfig.federation, false),
resolverTypeWrapperSignature: getConfigValue(rawConfig.resolverTypeWrapperSignature, 'Promise<T> | T'),
enumValues: parseEnumValues({
schema: _schema,
mapOrStr: rawConfig.enumValues,
}),
addUnderscoreToArgsType: getConfigValue(rawConfig.addUnderscoreToArgsType, false),
addInterfaceFieldResolverTypes: getConfigValue(rawConfig.addInterfaceFieldResolverTypes, false),
contextType: parseMapper(rawConfig.contextType || 'any', 'ContextType'),
Expand All @@ -771,6 +767,20 @@ export class BaseResolversVisitor<
...additionalConfig,
} as TPluginConfig);

this.config.enumValues = parseEnumValues({
schema: _schema,
mapOrStr: rawConfig.enumValues,
naming: {
convert: this.config.convert,
options: {
typesPrefix: this.config.typesPrefix,
typesSuffix: this.config.typesSuffix,
useTypesPrefix: this.config.enumPrefix,
useTypesSuffix: this.config.enumSuffix,
},
},
});
Comment on lines +770 to +782
Copy link
Copy Markdown
Collaborator Author

@eddeee888 eddeee888 Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enumValues now parses the type with naming convention, so we need to pass naming convention config in.

IMO the naming config and function are a bit confusing to use atm. However, we can fix it internally later to keep scope maintainable in this PR/major release


autoBind(this);
this._federation = new ApolloFederation({
enabled: this.config.federation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,6 @@ export class BaseTypesVisitor<
onlyEnums: getConfigValue(rawConfig.onlyEnums, false),
onlyOperationTypes: getConfigValue(rawConfig.onlyOperationTypes, false),
addUnderscoreToArgsType: getConfigValue(rawConfig.addUnderscoreToArgsType, false),
enumValues: parseEnumValues({
schema: _schema,
mapOrStr: rawConfig.enumValues,
ignoreEnumValuesFromSchema: rawConfig.ignoreEnumValuesFromSchema,
}),
ignoreEnumValuesFromSchema: getConfigValue(rawConfig.ignoreEnumValuesFromSchema, false),
declarationKind: normalizeDeclarationKind(rawConfig.declarationKind),
scalars: buildScalarsFromConfig(_schema, rawConfig, defaultScalars),
Expand All @@ -526,6 +521,21 @@ export class BaseTypesVisitor<
...additionalConfig,
});

this.config.enumValues = parseEnumValues({
schema: _schema,
mapOrStr: rawConfig.enumValues,
ignoreEnumValuesFromSchema: this.config.ignoreEnumValuesFromSchema,
naming: {
convert: this.config.convert,
options: {
typesPrefix: this.config.typesPrefix,
typesSuffix: this.config.typesSuffix,
useTypesPrefix: this.config.enumPrefix,
useTypesSuffix: this.config.enumSuffix,
},
},
});

// Note: Missing directive mappers but not a problem since always overriden by implementors
this._argumentsTransformer = new OperationVariablesToObject(this.scalars, this.convertName);
}
Expand Down Expand Up @@ -856,10 +866,12 @@ export class BaseTypesVisitor<
schema: this._schema,
naming: {
convert: this.config.convert,
typesPrefix: this.config.typesPrefix,
useTypesPrefix: this.config.enumPrefix,
typesSuffix: this.config.typesSuffix,
useTypesSuffix: this.config.enumSuffix,
options: {
typesPrefix: this.config.typesPrefix,
useTypesPrefix: this.config.enumPrefix,
typesSuffix: this.config.typesSuffix,
useTypesSuffix: this.config.enumSuffix,
},
},
ignoreEnumValuesFromSchema: this.config.ignoreEnumValuesFromSchema,
declarationBlockConfig: this._declarationBlockConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getNodeComment,
wrapWithSingleQuotes,
} from './utils.js';
import { convertName } from './naming.js';

export interface ConvertSchemaEnumToDeclarationBlockString {
schema: GraphQLSchema;
Expand All @@ -18,10 +19,12 @@ export interface ConvertSchemaEnumToDeclarationBlockString {
ignoreEnumValuesFromSchema: boolean;
naming: {
convert: ConvertFn;
typesPrefix: string;
typesSuffix: string;
useTypesPrefix?: boolean;
useTypesSuffix?: boolean;
options: {
typesPrefix: string;
typesSuffix: string;
useTypesPrefix?: boolean;
useTypesSuffix?: boolean;
};
Comment on lines +22 to +27
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grouping options here make it a bit easier to read

};

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

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

const enumTypeName = convertName({
options: {
typesPrefix: naming.typesPrefix,
typesSuffix: naming.typesSuffix,
useTypesPrefix: naming.useTypesPrefix,
useTypesSuffix: naming.useTypesSuffix,
},
convert: () => naming.convert(node),
options: naming.options,
});

if (outputType === 'string-literal') {
Expand Down Expand Up @@ -98,8 +96,8 @@ export const convertSchemaEnumToDeclarationBlockString = ({
const optionName = makeValidEnumIdentifier(
convertName({
options: {
typesPrefix: naming.typesPrefix,
typesSuffix: naming.typesSuffix,
typesPrefix: naming.options.typesPrefix,
typesSuffix: naming.options.typesSuffix,
useTypesPrefix: false,
},
convert: () => naming.convert(enumOption, { transformUnderscore: true }),
Expand Down Expand Up @@ -130,8 +128,8 @@ export const convertSchemaEnumToDeclarationBlockString = ({
const optionName = makeValidEnumIdentifier(
convertName({
options: {
typesPrefix: naming.typesPrefix,
typesSuffix: naming.typesPrefix,
typesPrefix: naming.options.typesPrefix,
typesSuffix: naming.options.typesPrefix,
},
convert: () =>
naming.convert(enumOption, {
Expand Down Expand Up @@ -195,8 +193,8 @@ export const buildEnumValuesBlock = ({
convertName({
options: {
useTypesPrefix: false,
typesPrefix: naming.typesPrefix,
typesSuffix: naming.typesSuffix,
typesPrefix: naming.options.typesPrefix,
typesSuffix: naming.options.typesSuffix,
},
convert: () =>
naming.convert(enumOption, {
Expand Down Expand Up @@ -236,33 +234,3 @@ const makeValidEnumIdentifier = (identifier: string): string => {
}
return identifier;
};

const convertName = ({
convert,
options,
}: {
options: {
typesPrefix: string;
useTypesPrefix?: boolean;
typesSuffix: string;
useTypesSuffix?: boolean;
};
convert: () => string;
}): string => {
const useTypesPrefix = typeof options.useTypesPrefix === 'boolean' ? options.useTypesPrefix : true;
const useTypesSuffix = typeof options.useTypesSuffix === 'boolean' ? options.useTypesSuffix : true;

let convertedName = '';

if (useTypesPrefix) {
convertedName += options.typesPrefix;
}

convertedName += convert();

if (useTypesSuffix) {
convertedName += options.typesSuffix;
}

return convertedName;
};
Comment on lines -240 to -268
Copy link
Copy Markdown
Collaborator Author

@eddeee888 eddeee888 Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is moved to visitor-plugin-common here

33 changes: 28 additions & 5 deletions packages/plugins/other/visitor-plugin-common/src/enum-values.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GraphQLEnumType, GraphQLSchema, isEnumType } from 'graphql';
import { parseMapper } from './mappers.js';
import { EnumValuesMap, ParsedEnumValuesMap } from './types.js';
import type { ConvertFn, EnumValuesMap, ParsedEnumValuesMap } from './types.js';
import { convertName } from './naming.js';

function escapeString(str: string) {
return str.replace(/\\/g, '\\\\').replace(/\n/g, '\\n').replace(/'/g, "\\'");
Expand All @@ -10,10 +11,20 @@ export function parseEnumValues({
schema,
mapOrStr = {},
ignoreEnumValuesFromSchema,
naming,
}: {
schema: GraphQLSchema;
mapOrStr: EnumValuesMap;
ignoreEnumValuesFromSchema?: boolean;
naming: {
convert: ConvertFn;
options: {
typesPrefix: string;
typesSuffix: string;
useTypesPrefix?: boolean;
useTypesSuffix?: boolean;
};
};
}): ParsedEnumValuesMap {
const allTypes = schema.getTypeMap();
const allEnums = Object.keys(allTypes).filter(t => isEnumType(allTypes[t]));
Expand Down Expand Up @@ -42,7 +53,7 @@ export function parseEnumValues({
);
}

return Object.keys(mapOrStr).reduce((prev, gqlIdentifier) => {
return Object.keys(mapOrStr).reduce<ParsedEnumValuesMap>((prev, gqlIdentifier) => {
const pointer = mapOrStr[gqlIdentifier];

if (typeof pointer === 'string') {
Expand All @@ -53,6 +64,10 @@ export function parseEnumValues({
[gqlIdentifier]: {
isDefault: mapper.isExternal && mapper.default,
typeIdentifier: gqlIdentifier,
typeIdentifierConverted: convertName({
convert: () => naming.convert(gqlIdentifier),
options: naming.options,
}),
sourceFile: mapper.isExternal ? mapper.source : null,
sourceIdentifier: mapper.type,
importIdentifier: mapper.isExternal ? mapper.import : null,
Expand All @@ -66,6 +81,10 @@ export function parseEnumValues({
[gqlIdentifier]: {
isDefault: false,
typeIdentifier: gqlIdentifier,
typeIdentifierConverted: convertName({
convert: () => naming.convert(gqlIdentifier),
options: naming.options,
}),
sourceFile: null,
sourceIdentifier: null,
importIdentifier: null,
Expand All @@ -77,24 +96,28 @@ export function parseEnumValues({
`Invalid "enumValues" configuration \n
Enum "${gqlIdentifier}": expected string or object (with enum values mapping)`
);
}, {} as ParsedEnumValuesMap);
}, {});
}
if (typeof mapOrStr === 'string') {
return allEnums
.filter(enumName => !enumName.startsWith('__'))
.reduce((prev, enumName) => {
.reduce<ParsedEnumValuesMap>((prev, enumName) => {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer using .reduce's generic to avoid as SomeType usage at the bottom

return {
...prev,
[enumName]: {
isDefault: false,
typeIdentifier: enumName,
typeIdentifierConverted: convertName({
convert: () => naming.convert(enumName),
options: naming.options,
}),
sourceFile: mapOrStr,
sourceIdentifier: enumName,
importIdentifier: enumName,
mappedValues: null,
},
};
}, {} as ParsedEnumValuesMap);
}, {});
}

return {};
Expand Down
18 changes: 11 additions & 7 deletions packages/plugins/other/visitor-plugin-common/src/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ export function getEnumsImports({
useTypeImports: boolean;
}): string[] {
function handleEnumValueMapper({
typeIdentifier,
typeIdentifierConverted,
importIdentifier,
sourceIdentifier,
sourceFile,
useTypeImports,
}: {
typeIdentifier: string;
typeIdentifierConverted: string;
importIdentifier: string | null;
sourceIdentifier: string | null;
sourceFile: string | null;
Expand All @@ -132,12 +132,16 @@ export function getEnumsImports({
// { enumValues: { MyEnum: './my-file#NS.NestedEnum' } }
return [
buildTypeImport({ identifier: importIdentifier || sourceIdentifier, source: sourceFile, useTypeImports }),
`import ${typeIdentifier} = ${sourceIdentifier};`,
`import ${typeIdentifierConverted} = ${sourceIdentifier};`,
];
}
if (sourceIdentifier !== typeIdentifier) {
if (sourceIdentifier !== typeIdentifierConverted) {
return [
buildTypeImport({ identifier: `${sourceIdentifier} as ${typeIdentifier}`, source: sourceFile, useTypeImports }),
buildTypeImport({
identifier: `${sourceIdentifier} as ${typeIdentifierConverted}`,
source: sourceFile,
useTypeImports,
}),
];
}
return [buildTypeImport({ identifier: importIdentifier || sourceIdentifier, source: sourceFile, useTypeImports })];
Expand All @@ -150,7 +154,7 @@ export function getEnumsImports({
if (mappedValue.isDefault) {
return [
buildTypeImport({
identifier: mappedValue.typeIdentifier,
identifier: mappedValue.typeIdentifierConverted,
source: mappedValue.sourceFile,
asDefault: true,
useTypeImports,
Expand All @@ -159,7 +163,7 @@ export function getEnumsImports({
}

return handleEnumValueMapper({
typeIdentifier: mappedValue.typeIdentifier,
typeIdentifierConverted: mappedValue.typeIdentifierConverted,
importIdentifier: mappedValue.importIdentifier,
sourceIdentifier: mappedValue.sourceIdentifier,
sourceFile: mappedValue.sourceFile,
Expand Down
Loading