Skip to content

Commit f821e8a

Browse files
authored
fix: allow mappers to override root types (Query, Mutation, Subscription) (#10512)
* fix: allow mappers to override root types (Query, Mutation, Subscription) Previously, root types were checked before mappers, which prevented mappers from working on root types. This fix moves the mapper check before the root type check, allowing mappers to override rootValueType when configured. This is particularly useful when a GraphQL schema has a root Subscription type that is also a regular object type (e.g., Stripe subscription), and you want to use a custom mapper type instead of the default Record<PropertyKey, never>. * chore: add changeset for mapper fix
1 parent 9e70bcb commit f821e8a

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

.changeset/few-worms-talk.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+
---
4+
5+
fix: allow mappers to override root types (Query, Mutation, Subscription)
6+
7+
Previously, mappers configured for root types were ignored because root types were checked before mappers. This fix moves the mapper check before the root type check, allowing mappers to override `rootValueType` when configured.

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -971,15 +971,20 @@ export class BaseResolversVisitor<
971971
const isScalar = this.config.scalars[typeName];
972972
const hasDefaultMapper = !!this.config.defaultMapper?.type;
973973

974+
// Check for mappers first, even for root types, to allow overriding rootValueType
975+
if (isMapped && this.config.mappers[typeName].type && !hasPlaceholder(this.config.mappers[typeName].type)) {
976+
this.markMapperAsUsed(typeName);
977+
prev[typeName] = applyWrapper(this.config.mappers[typeName].type);
978+
979+
return prev;
980+
}
981+
974982
if (isRootType) {
975983
prev[typeName] = applyWrapper(this.config.rootValueType.type);
976984

977985
return prev;
978986
}
979-
if (isMapped && this.config.mappers[typeName].type && !hasPlaceholder(this.config.mappers[typeName].type)) {
980-
this.markMapperAsUsed(typeName);
981-
prev[typeName] = applyWrapper(this.config.mappers[typeName].type);
982-
} else if (isEnumType(schemaType) && this.config.enumValues[typeName]) {
987+
if (isEnumType(schemaType) && this.config.enumValues[typeName]) {
983988
const isExternalFile = !!this.config.enumValues[typeName].sourceFile;
984989
prev[typeName] = isExternalFile
985990
? this.convertName(this.config.enumValues[typeName].typeIdentifier, {

0 commit comments

Comments
 (0)