Skip to content

Commit 1de10ba

Browse files
committed
Ensure union types go through federation and handle applyNamespacedImport correctly
1 parent 406ba38 commit 1de10ba

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -855,20 +855,23 @@ export class BaseResolversVisitor<
855855
}
856856

857857
const allSchemaTypes = this._schema.getTypeMap();
858+
const typeNames = this._federation.filterTypeNames(Object.keys(allSchemaTypes));
859+
860+
const unionTypes = typeNames.reduce((res, typeName) => {
861+
const schemaType = allSchemaTypes[typeName];
858862

859-
const unionTypes = Object.entries(allSchemaTypes).reduce((res, [typeName, schemaType]) => {
860863
if (isUnionType(schemaType)) {
861864
const referencedTypes = schemaType.getTypes().map(unionMemberType => {
862865
const isUnionMemberMapped = this.config.mappers[unionMemberType.name];
863866

864-
// 1. If mapped without plachoder, just use it without doing extra checks
867+
// 1. If mapped without placehoder, just use it without doing extra checks
865868
if (isUnionMemberMapped && !hasPlaceholder(isUnionMemberMapped.type)) {
866869
return isUnionMemberMapped.type;
867870
}
868871

869872
// 2. Work out value for union member type
870873
// 2a. By default, use the typescript type
871-
let unionMemberValue = this.convertName(unionMemberType.name);
874+
let unionMemberValue = this.convertName(unionMemberType.name, {}, true);
872875

873876
// 2b. Find fields to Omit if needed.
874877
// - If no field to Omit, "type with maybe Omit" is typescript type i.e. no Omit
@@ -889,7 +892,6 @@ export class BaseResolversVisitor<
889892
// 2d. If has default mapper with placeholder, use the "type with maybe Omit" as {T}
890893
const hasDefaultMapper = !!this.config.defaultMapper?.type;
891894
const isScalar = this.config.scalars[typeName];
892-
893895
if (hasDefaultMapper && hasPlaceholder(this.config.defaultMapper.type)) {
894896
const finalTypename = isScalar ? this._getScalar(typeName) : unionMemberValue;
895897
return replacePlaceholder(this.config.defaultMapper.type, finalTypename);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ export type DirectiveResolverFn<TResult = {}, TParent = {}, TContext = {}, TArgs
339339
340340
/** Mapping of union types */
341341
export type ResolversUnionTypes = ResolversObject<{
342-
ChildUnion: ( Child ) | ( MyOtherType );
343-
MyUnion: ( Omit<MyType, 'unionChild'> & { unionChild?: Types.Maybe<ResolversTypes['ChildUnion']> } ) | ( MyOtherType );
342+
ChildUnion: ( Types.Child ) | ( Types.MyOtherType );
343+
MyUnion: ( Omit<Types.MyType, 'unionChild'> & { unionChild?: Types.Maybe<ResolversTypes['ChildUnion']> } ) | ( Types.MyOtherType );
344344
}>;
345345
346346
/** Mapping between all available schema types and the resolvers types */

0 commit comments

Comments
 (0)