Skip to content

Commit 3103b7a

Browse files
authored
[typescript][typescript-resolvers] Fix enumValues not considering namingConvention in certain scenarios (2) (#10657)
* Ensure enumValues is consistent in typescript and typescript-resolvers * Add a note to default alias import not doing anything * Add changeset
1 parent 91e5886 commit 3103b7a

6 files changed

Lines changed: 27 additions & 16 deletions

File tree

.changeset/tasty-waves-trade.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': patch
4+
'@graphql-codegen/typescript-resolvers': patch
5+
---
6+
7+
Fix namingConvention not being applied consistently

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -941,10 +941,7 @@ export class BaseResolversVisitor<
941941
if (isEnumType(schemaType) && this.config.enumValues[typeName]) {
942942
const isExternalFile = !!this.config.enumValues[typeName].sourceFile;
943943
prev[typeName] = isExternalFile
944-
? this.convertName(this.config.enumValues[typeName].typeIdentifier, {
945-
useTypesPrefix: false,
946-
useTypesSuffix: false,
947-
})
944+
? this.config.enumValues[typeName].typeIdentifierConverted
948945
: this.config.enumValues[typeName].sourceIdentifier;
949946
} else if (hasDefaultMapper && !hasPlaceholder(this.config.defaultMapper.type)) {
950947
prev[typeName] = applyWrapper(this.config.defaultMapper.type);
@@ -1350,7 +1347,7 @@ export class BaseResolversVisitor<
13501347
const exists = groupedMappers[source] ? !!groupedMappers[source].find(m => m.identifier === identifier) : false;
13511348
const existsFromEnums = !!Object.keys(this.config.enumValues)
13521349
.map(key => this.config.enumValues[key])
1353-
.find(o => o.sourceFile === source && o.typeIdentifier === identifier);
1350+
.find(o => o.sourceFile === source && o.typeIdentifierConverted === identifier);
13541351

13551352
return exists || existsFromEnums;
13561353
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ export class BaseTypesVisitor<
961961
return this._getScalar(typeAsString, isVisitingInputType ? 'input' : 'output');
962962
}
963963
if (this.config.enumValues[typeAsString]) {
964-
return this.config.enumValues[typeAsString].typeIdentifier;
964+
return this.config.enumValues[typeAsString].typeIdentifierConverted;
965965
}
966966

967967
const schemaType = this._schema.getType(typeAsString);

packages/plugins/typescript/operations/tests/ts-documents.standalone.enum.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ describe('TypeScript Operations Plugin - Enum', () => {
847847
typesPrefix: 'I',
848848
namingConvention: { enumValues: 'change-case-all#constantCase' },
849849
enumValues: {
850-
UserRole: './files#default as IUserRole',
850+
UserRole: './files#default as UserRole', // NOTE: `as UserRole` doesn't do anything here, this is here to demonstrate that it's the same as './files#default'
851851
},
852852
},
853853
{ outputFile: '' }

packages/plugins/typescript/resolvers/tests/ts-resolvers.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -685,8 +685,8 @@ __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
685685
expect(mergedOutputs).not.toContain(`NotMapped: NotMapped;`);
686686
expect(mergedOutputs).toContain(`A: MyA;`);
687687
expect(mergedOutputs).toContain(`B: GQL_B;`);
688-
expect(mergedOutputs).toContain(`C: C;`);
689-
expect(mergedOutputs).toContain(`import { MyC as C } from '../enums.js';`);
688+
expect(mergedOutputs).toContain(`C: GQL_C;`);
689+
expect(mergedOutputs).toContain(`import { MyC as GQL_C } from '../enums.js';`);
690690
});
691691

692692
it('Should allow to generate optional __resolveType', async () => {
@@ -2375,7 +2375,7 @@ export type ResolverFn<TResult, TParent, TContext, TArgs> = (
23752375
{ outputFile: 'graphql.ts' }
23762376
)) as Types.ComplexPluginOutput;
23772377

2378-
expect(output.content).toContain(`export type GqlAuthDirectiveArgs = {\n role?: Maybe<UserRole>;\n};`);
2378+
expect(output.content).toContain(`export type GqlAuthDirectiveArgs = {\n role?: Maybe<GqlUserRole>;\n};`); // Note: `GqlUserRole` will be imported from `@org/package` by `typescript` plugin
23792379
expect(output.content).toContain(
23802380
`export type GqlAuthDirectiveResolver<Result, Parent, ContextType = any, Args = GqlAuthDirectiveArgs> = DirectiveResolverFn<Result, Parent, ContextType, Args>;`
23812381
);

packages/plugins/typescript/typescript/tests/typescript.spec.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -895,13 +895,13 @@ describe('TypeScript', () => {
895895
typesPrefix: 'I',
896896
namingConvention: { enumValues: 'change-case-all#constantCase' },
897897
enumValues: {
898-
MyEnum: './files#default as MyEnum',
898+
MyEnum: './files#default as MyEnum', // NOTE: `as MyEnum` doesn't do anything, this is here to demonstrate that it's the same as './files#default'
899899
},
900900
},
901901
{ outputFile: '' }
902902
)) as Types.ComplexPluginOutput;
903903

904-
expect(result.prepend[0]).toBe(`import MyEnum from './files';`);
904+
expect(result.prepend[0]).toBe(`import IMyEnum from './files';`);
905905
});
906906

907907
it('#4834 - enum members should be quoted if numeric', async () => {
@@ -948,14 +948,21 @@ describe('TypeScript', () => {
948948
{ outputFile: '' }
949949
)) as Types.ComplexPluginOutput;
950950

951+
expect(result.prepend).toMatchInlineSnapshot(`
952+
[
953+
"import { MyEnum as IMyEnum } from './files';",
954+
"export type Maybe<T> = T | null;",
955+
"export type InputMaybe<T> = Maybe<T>;",
956+
]
957+
`);
951958
expect(result.content).toBeSimilarStringTo(`export type ITest = {
952959
__typename?: 'Test';
953-
t?: Maybe<MyEnum>;
960+
t?: Maybe<IMyEnum>;
954961
test?: Maybe<Scalars['String']['output']>;
955962
};`);
956963

957964
expect(result.content).toBeSimilarStringTo(`export type ITestTestArgs = {
958-
a?: InputMaybe<MyEnum>;
965+
a?: InputMaybe<IMyEnum>;
959966
};`);
960967
});
961968

@@ -987,9 +994,9 @@ describe('TypeScript', () => {
987994
},
988995
{ outputFile: '' }
989996
)) as Types.ComplexPluginOutput;
990-
expect(result.prepend).toContain(`import { MyEnum } from './files';`);
997+
expect(result.prepend).toContain(`import { MyEnum as GQL_MyEnum } from './files';`);
991998
expect(result.content).toContain(`enum GQL_OtherEnum {`);
992-
expect(result.content).toContain(`a?: Maybe<MyEnum>;`);
999+
expect(result.content).toContain(`a?: Maybe<GQL_MyEnum>;`);
9931000
expect(result.content).toContain(`b?: Maybe<GQL_OtherEnum>`);
9941001
});
9951002

0 commit comments

Comments
 (0)