Skip to content

Commit 11d05e3

Browse files
authored
fix(resolvers): fix conflict between typesPrefix: true and enumPrefix: false (#7626)
* fix(resolvers): fix conflict between `typesPrefix: true` and `enumPrefix: false` * Create weak-radios-join.md * test(resolvers): naming
1 parent 985a41a commit 11d05e3

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

‎.changeset/weak-radios-join.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@graphql-codegen/visitor-plugin-common": patch
3+
---
4+
5+
fix(resolvers): fix conflict between `typesPrefix: true` and `enumPrefix: false`

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,9 +563,11 @@ export class BaseResolversVisitor<
563563
.getTypes()
564564
.map(type => getTypeToUse(type.name))
565565
.join(' | ');
566+
} else if (isEnumType(schemaType)) {
567+
prev[typeName] = this.convertName(typeName, { useTypesPrefix: this.config.enumPrefix }, true);
566568
} else {
567569
shouldApplyOmit = true;
568-
prev[typeName] = this.convertName(typeName, { useTypesPrefix: this.config.enumPrefix }, true);
570+
prev[typeName] = this.convertName(typeName, {}, true);
569571
}
570572

571573
if (shouldApplyOmit && prev[typeName] !== 'any' && isObjectType(schemaType)) {

‎packages/plugins/other/visitor-plugin-common/tests/create-resolvers-fields.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,41 @@ export type ResolversParentTypes = {
4545
Boolean: Scalars['Boolean']
4646
String: Scalars['String']
4747
};
48+
`
49+
);
50+
});
51+
52+
it('generates proper types when typesPrefix is used along with `enumPrefix: false`', () => {
53+
/**
54+
* This makes sure that https://github.com/dotansimha/graphql-code-generator/issues/6709 doesn't occur again.
55+
* The result looked like this without the fix:
56+
* export type ResolversParentTypes = {
57+
* Query: {}
58+
* A: Omit<A, 'b'> & { b?: Maybe<ResolversParentTypes['B']> }
59+
* Boolean: Scalars['Boolean']
60+
* String: Scalars['String']
61+
* };
62+
*/
63+
const visitor = new BaseResolversVisitor(
64+
{
65+
mappers: {
66+
B: './some-file#B',
67+
},
68+
typesPrefix: 'I',
69+
enumPrefix: false,
70+
},
71+
{} as ParsedResolversConfig,
72+
schema
73+
);
74+
75+
expect(visitor.buildResolversParentTypes()).toEqual(
76+
`/** Mapping between all available schema types and the resolvers parents */
77+
export type IResolversParentTypes = {
78+
Query: {}
79+
A: IA
80+
Boolean: Scalars['Boolean']
81+
String: Scalars['String']
82+
};
4883
`
4984
);
5085
});

0 commit comments

Comments
 (0)