-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[typescript-operations] Fix enumValues not considering namingConvention in certain scenarios #10656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
db629a6
28c0d4d
f89e5a6
799c071
674dec1
6a4098f
a43808a
cb7f5bf
f2055d8
40d48c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import { | |
| getNodeComment, | ||
| wrapWithSingleQuotes, | ||
| } from './utils.js'; | ||
| import { convertName } from './naming.js'; | ||
|
|
||
| export interface ConvertSchemaEnumToDeclarationBlockString { | ||
| schema: GraphQLSchema; | ||
|
|
@@ -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
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'; | ||
|
|
@@ -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) => { | ||
|
|
@@ -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') { | ||
|
|
@@ -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 }), | ||
|
|
@@ -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, { | ||
|
|
@@ -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, { | ||
|
|
@@ -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
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is moved to |
||
| 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, "\\'"); | ||
|
|
@@ -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])); | ||
|
|
@@ -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') { | ||
|
|
@@ -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, | ||
|
|
@@ -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, | ||
|
|
@@ -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) => { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer using |
||
| 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 {}; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
enumValuesnow 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