Skip to content

Commit 09cb7d3

Browse files
authored
Fix: naming of imported enums (#10185)
1 parent 876ea0c commit 09cb7d3

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -873,9 +873,13 @@ export class BaseResolversVisitor<
873873
this.markMapperAsUsed(typeName);
874874
prev[typeName] = applyWrapper(this.config.mappers[typeName].type);
875875
} else if (isEnumType(schemaType) && this.config.enumValues[typeName]) {
876-
prev[typeName] =
877-
this.config.enumValues[typeName].sourceIdentifier ||
878-
this.convertName(this.config.enumValues[typeName].typeIdentifier);
876+
const isExternalFile = !!this.config.enumValues[typeName].sourceFile;
877+
prev[typeName] = isExternalFile
878+
? this.convertName(this.config.enumValues[typeName].typeIdentifier, {
879+
useTypesPrefix: false,
880+
useTypesSuffix: false,
881+
})
882+
: this.config.enumValues[typeName].sourceIdentifier;
879883
} else if (hasDefaultMapper && !hasPlaceholder(this.config.defaultMapper.type)) {
880884
prev[typeName] = applyWrapper(this.config.defaultMapper.type);
881885
} else if (isScalar) {

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
590590
const testSchema = buildSchema(/* GraphQL */ `
591591
type Query {
592592
a: A
593+
c: C
593594
}
594595
595596
enum A {
@@ -606,11 +607,17 @@ __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
606607
type B {
607608
a: String
608609
}
610+
611+
enum C {
612+
Y
613+
Z
614+
}
609615
`);
610616

611617
const config = {
612618
enumValues: {
613619
A: 'MyA',
620+
C: '../enums.js#MyC',
614621
},
615622
typesPrefix: 'GQL_',
616623
};
@@ -622,9 +629,13 @@ __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
622629

623630
expect(mergedOutputs).not.toContain(`A: A;`);
624631
expect(mergedOutputs).not.toContain(`A: GQL_A;`);
632+
expect(mergedOutputs).not.toContain(`C: GQL_MyC;`);
625633
expect(mergedOutputs).toContain(`NotMapped: GQL_NotMapped;`);
626634
expect(mergedOutputs).not.toContain(`NotMapped: NotMapped;`);
635+
expect(mergedOutputs).toContain(`A: MyA;`);
627636
expect(mergedOutputs).toContain(`B: GQL_B;`);
637+
expect(mergedOutputs).toContain(`C: C;`);
638+
expect(mergedOutputs).toContain(`import { MyC as C } from '../enums.js';`);
628639
});
629640

630641
it('Should allow to generate optional __resolveType', async () => {
@@ -2260,7 +2271,7 @@ export type ResolverFn<TResult, TParent, TContext, TArgs> = (
22602271
ProjectRoleDetail: '../entities#ProjectRole',
22612272
},
22622273
enumValues: {
2263-
ProjectRole: '../entities#ProjectRole',
2274+
ProjectRole: '../entities#AnotherProjectRole',
22642275
},
22652276
};
22662277

@@ -2272,8 +2283,11 @@ export type ResolverFn<TResult, TParent, TContext, TArgs> = (
22722283
expect(output.prepend.filter(t => t.includes('import')).length).toBe(2);
22732284
expect(output.prepend.filter(t => t.includes('ProjectRole')).length).toBe(0);
22742285
expect(tsContent.prepend.filter(t => t.includes('ProjectRole')).length).toBe(1);
2275-
expect(tsContent.prepend.includes(`import { ProjectRole } from '../entities';`)).toBeTruthy();
2276-
expect(output.prepend.includes(`import { ProjectRole } from '../entities';`)).toBeFalsy();
2286+
expect(output.content.includes('AnotherProjectRole')).toBeFalsy();
2287+
expect(
2288+
tsContent.prepend.includes(`import { AnotherProjectRole as ProjectRole } from '../entities';`)
2289+
).toBeTruthy();
2290+
expect(output.prepend.includes(`import { AnotherProjectRole as ProjectRole } from '../entities';`)).toBeFalsy();
22772291
});
22782292

22792293
it('#3264 - enumValues is not being applied to directive resolver', async () => {

0 commit comments

Comments
 (0)