Skip to content

Commit

Permalink
Fix: naming of imported enums
Browse files Browse the repository at this point in the history
  • Loading branch information
gilgardosh committed Nov 6, 2024
1 parent 876ea0c commit 9bd6e48
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -873,9 +873,13 @@ export class BaseResolversVisitor<
this.markMapperAsUsed(typeName);
prev[typeName] = applyWrapper(this.config.mappers[typeName].type);
} else if (isEnumType(schemaType) && this.config.enumValues[typeName]) {
prev[typeName] =
this.config.enumValues[typeName].sourceIdentifier ||
this.convertName(this.config.enumValues[typeName].typeIdentifier);
const isExternalFile = !!this.config.enumValues[typeName].sourceFile;
prev[typeName] = isExternalFile
? this.convertName(this.config.enumValues[typeName].typeIdentifier, {
useTypesPrefix: false,
useTypesSuffix: false,
})
: this.config.enumValues[typeName].sourceIdentifier;
} else if (hasDefaultMapper && !hasPlaceholder(this.config.defaultMapper.type)) {
prev[typeName] = applyWrapper(this.config.defaultMapper.type);
} else if (isScalar) {
Expand Down
20 changes: 17 additions & 3 deletions packages/plugins/typescript/resolvers/tests/ts-resolvers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
const testSchema = buildSchema(/* GraphQL */ `
type Query {
a: A
c: C
}
enum A {
Expand All @@ -606,11 +607,17 @@ __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
type B {
a: String
}
enum C {
Y
Z
}
`);

const config = {
enumValues: {
A: 'MyA',
C: '../enums.js#MyC',
},
typesPrefix: 'GQL_',
};
Expand All @@ -622,9 +629,13 @@ __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;

expect(mergedOutputs).not.toContain(`A: A;`);
expect(mergedOutputs).not.toContain(`A: GQL_A;`);
expect(mergedOutputs).not.toContain(`C: GQL_MyC;`);
expect(mergedOutputs).toContain(`NotMapped: GQL_NotMapped;`);
expect(mergedOutputs).not.toContain(`NotMapped: NotMapped;`);
expect(mergedOutputs).toContain(`A: MyA;`);
expect(mergedOutputs).toContain(`B: GQL_B;`);
expect(mergedOutputs).toContain(`C: C;`);
expect(mergedOutputs).toContain(`import { MyC as C } from '../enums.js';`);
});

it('Should allow to generate optional __resolveType', async () => {
Expand Down Expand Up @@ -2260,7 +2271,7 @@ export type ResolverFn<TResult, TParent, TContext, TArgs> = (
ProjectRoleDetail: '../entities#ProjectRole',
},
enumValues: {
ProjectRole: '../entities#ProjectRole',
ProjectRole: '../entities#AnotherProjectRole',
},
};

Expand All @@ -2272,8 +2283,11 @@ export type ResolverFn<TResult, TParent, TContext, TArgs> = (
expect(output.prepend.filter(t => t.includes('import')).length).toBe(2);
expect(output.prepend.filter(t => t.includes('ProjectRole')).length).toBe(0);
expect(tsContent.prepend.filter(t => t.includes('ProjectRole')).length).toBe(1);
expect(tsContent.prepend.includes(`import { ProjectRole } from '../entities';`)).toBeTruthy();
expect(output.prepend.includes(`import { ProjectRole } from '../entities';`)).toBeFalsy();
expect(output.content.includes('AnotherProjectRole')).toBeFalsy();
expect(
tsContent.prepend.includes(`import { AnotherProjectRole as ProjectRole } from '../entities';`)
).toBeTruthy();
expect(output.prepend.includes(`import { AnotherProjectRole as ProjectRole } from '../entities';`)).toBeFalsy();
});

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

0 comments on commit 9bd6e48

Please sign in to comment.