From 09dd7df5e345d90b5bf7d171eabcbb103bc07543 Mon Sep 17 00:00:00 2001 From: armitjs Date: Tue, 17 Oct 2023 07:27:15 -0500 Subject: [PATCH] fix(core): Fix custom field resolver for eager translatable relation (#2457) Fixes #2453 --- .../api/common/custom-field-relation-resolver.service.ts | 8 ++++++++ packages/core/src/api/config/generate-resolvers.ts | 8 +++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/core/src/api/common/custom-field-relation-resolver.service.ts b/packages/core/src/api/common/custom-field-relation-resolver.service.ts index 27ebba13ec..c008731fb6 100644 --- a/packages/core/src/api/common/custom-field-relation-resolver.service.ts +++ b/packages/core/src/api/common/custom-field-relation-resolver.service.ts @@ -55,6 +55,14 @@ export class CustomFieldRelationResolverService { const result = fieldDef.list ? await qb.getMany() : await qb.getOne(); + return await this.translateEntity(ctx, result, fieldDef); + } + + async translateEntity( + ctx: RequestContext, + result: VendureEntity | VendureEntity[] | null, + fieldDef: RelationCustomFieldConfig, + ) { if (fieldDef.entity === ProductVariant) { if (Array.isArray(result)) { await Promise.all(result.map(r => this.applyVariantPrices(ctx, r as any))); diff --git a/packages/core/src/api/config/generate-resolvers.ts b/packages/core/src/api/config/generate-resolvers.ts index 065c747294..bfde3f677c 100644 --- a/packages/core/src/api/config/generate-resolvers.ts +++ b/packages/core/src/api/config/generate-resolvers.ts @@ -207,10 +207,12 @@ function generateCustomFieldRelationResolvers( args: any, context: any, ) => { - if (source[fieldDef.name] != null) { - return source[fieldDef.name]; - } const ctx: RequestContext = context.req[REQUEST_CONTEXT_KEY]; + const eagerEntity = source[fieldDef.name]; + // If the relation is eager-loaded, we can simply try to translate this relation entity if they have translations + if (eagerEntity != null) { + return customFieldRelationResolverService.translateEntity(ctx, eagerEntity, fieldDef); + } const entityId = source[ENTITY_ID_KEY]; return customFieldRelationResolverService.resolveRelation({ ctx,