Skip to content

Commit

Permalink
Ensure union types go through federation and handle applyNamespacedIm…
Browse files Browse the repository at this point in the history
…port correctly
  • Loading branch information
eddeee888 committed Feb 28, 2023
1 parent 869aef4 commit 09b4d59
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -855,20 +855,23 @@ export class BaseResolversVisitor<
}

const allSchemaTypes = this._schema.getTypeMap();
const typeNames = this._federation.filterTypeNames(Object.keys(allSchemaTypes));

const unionTypes = typeNames.reduce((res, typeName) => {
const schemaType = allSchemaTypes[typeName];

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

// 1. If mapped without plachoder, just use it without doing extra checks
// 1. If mapped without placehoder, just use it without doing extra checks
if (isUnionMemberMapped && !hasPlaceholder(isUnionMemberMapped.type)) {
return isUnionMemberMapped.type;
}

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

// 2b. Find fields to Omit if needed.
// - If no field to Omit, "type with maybe Omit" is typescript type i.e. no Omit
Expand All @@ -889,7 +892,6 @@ export class BaseResolversVisitor<
// 2d. If has default mapper with placeholder, use the "type with maybe Omit" as {T}
const hasDefaultMapper = !!this.config.defaultMapper?.type;
const isScalar = this.config.scalars[typeName];

if (hasDefaultMapper && hasPlaceholder(this.config.defaultMapper.type)) {
const finalTypename = isScalar ? this._getScalar(typeName) : unionMemberValue;
return replacePlaceholder(this.config.defaultMapper.type, finalTypename);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ export type DirectiveResolverFn<TResult = {}, TParent = {}, TContext = {}, TArgs
/** Mapping of union types */
export type ResolversUnionTypes = ResolversObject<{
ChildUnion: ( Child ) | ( MyOtherType );
MyUnion: ( Omit<MyType, 'unionChild'> & { unionChild?: Types.Maybe<ResolversTypes['ChildUnion']> } ) | ( MyOtherType );
ChildUnion: ( Types.Child ) | ( Types.MyOtherType );
MyUnion: ( Omit<Types.MyType, 'unionChild'> & { unionChild?: Types.Maybe<ResolversTypes['ChildUnion']> } ) | ( Types.MyOtherType );
}>;
/** Mapping between all available schema types and the resolvers types */
Expand Down

0 comments on commit 09b4d59

Please sign in to comment.