Skip to content

Commit

Permalink
Fix reference being assigned mappers incorrectly
Browse files Browse the repository at this point in the history
  • Loading branch information
eddeee888 committed Dec 10, 2024
1 parent 5724234 commit bdf4331
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,28 @@ export class BaseResolversVisitor<
).string;
}

public buildFederationTypes(): string {
const federationMeta = this._federation.getMeta();

if (Object.keys(federationMeta).length === 0) {
return '';
}

const declarationKind = 'type';
return new DeclarationBlock(this._declarationBlockConfig)
.export()
.asKind(declarationKind)
.withName(this.convertName('FederationTypes'))
.withComment('Mapping of federation types')
.withBlock(
Object.keys(federationMeta)
.map(typeName => {
return indent(`${typeName}: ${this.convertName(typeName)}${this.getPunctuation(declarationKind)}`);
})
.join('\n')
).string;
}

public get schema(): GraphQLSchema {
return this._schema;
}
Expand Down Expand Up @@ -1498,6 +1520,7 @@ export class BaseResolversVisitor<
fieldNode: original,
parentType,
parentTypeSignature: this.getParentTypeForSignature(node),
federationTypeSignature: 'FederationType',
});
const mappedTypeKey = isSubscriptionType ? `${mappedType}, "${node.name}"` : mappedType;

Expand Down Expand Up @@ -1619,10 +1642,19 @@ export class BaseResolversVisitor<
);
}

const genericTypes: string[] = [
`ContextType = ${this.config.contextType.type}`,
this.transformParentGenericType(parentType),
];
if (this._federation.getMeta()[typeName]) {
const typeRef = `${this.convertName('FederationTypes')}['${typeName}']`;
genericTypes.push(`FederationType extends ${typeRef} = ${typeRef}`);
}

const block = new DeclarationBlock(this._declarationBlockConfig)
.export()
.asKind(declarationKind)
.withName(name, `<ContextType = ${this.config.contextType.type}, ${this.transformParentGenericType(parentType)}>`)
.withName(name, `<${genericTypes.join(', ')}>`)
.withBlock(fieldsContent.join('\n'));

this._collectedResolvers[node.name as any] = {
Expand Down
2 changes: 2 additions & 0 deletions packages/plugins/typescript/resolvers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ export type DirectiveResolverFn<TResult = {}, TParent = {}, TContext = {}, TArgs
) => TResult | Promise<TResult>;
`;

const federationTypes = visitor.buildFederationTypes();
const resolversTypeMapping = visitor.buildResolversTypes();
const resolversParentTypeMapping = visitor.buildResolversParentTypes();
const resolversUnionTypesMapping = visitor.buildResolversUnionTypes();
Expand Down Expand Up @@ -287,6 +288,7 @@ export type DirectiveResolverFn<TResult = {}, TParent = {}, TContext = {}, TArgs
prepend,
content: [
header,
federationTypes,
resolversUnionTypesMapping,
resolversInterfaceTypesMapping,
resolversTypeMapping,
Expand Down
6 changes: 4 additions & 2 deletions packages/utils/plugins-helpers/src/federation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,12 @@ export class ApolloFederation {
fieldNode,
parentType,
parentTypeSignature,
federationTypeSignature,
}: {
fieldNode: FieldDefinitionNode;
parentType: GraphQLNamedType;
parentTypeSignature: string;
federationTypeSignature: string;
}) {
if (
this.enabled &&
Expand All @@ -177,12 +179,12 @@ export class ApolloFederation {

// Look for @requires and see what the service needs and gets
const requires = getDirectivesByName('requires', fieldNode).map(this.extractFieldSet);
const requiredFields = this.translateFieldSet(merge({}, ...requires), parentTypeSignature);
const requiredFields = this.translateFieldSet(merge({}, ...requires), federationTypeSignature);

// @key() @key() - "primary keys" in Federation
const primaryKeys = resolvableKeyDirectives.map(def => {
const fields = this.extractFieldSet(def);
return this.translateFieldSet(fields, parentTypeSignature);
return this.translateFieldSet(fields, federationTypeSignature);
});

const [open, close] = primaryKeys.length > 1 ? ['(', ')'] : ['', ''];
Expand Down

0 comments on commit bdf4331

Please sign in to comment.