diff --git a/packages/core/e2e/custom-field-relations.e2e-spec.ts b/packages/core/e2e/custom-field-relations.e2e-spec.ts index a33ec2e180..e35153454a 100644 --- a/packages/core/e2e/custom-field-relations.e2e-spec.ts +++ b/packages/core/e2e/custom-field-relations.e2e-spec.ts @@ -343,6 +343,43 @@ describe('Custom field relations', () => { }); }); + it('ProductVariant without a specified property value returns null', async () => { + const { createProduct } = await adminClient.query(gql` + mutation { + createProduct( + input: { + translations: [ + { + languageCode: en + name: "Product with empty custom fields" + description: "" + slug: "product-with-empty-custom-fields" + } + ] + } + ) { + id + } + } + `); + + const { product } = await adminClient.query(gql` + query { + product(id: "${createProduct.id}") { + id + customFields { + cfProductVariant{ + price + currencyCode + priceWithTax + } + } + } + }`); + + expect(product.customFields.cfProductVariant).toEqual(null); + }); + describe('entity-specific implementation', () => { function assertCustomFieldIds(customFields: any, single: string, multi: string[]) { expect(customFields.single).toEqual({ id: single }); 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 c008731fb6..28b9b14f57 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 @@ -63,6 +63,8 @@ export class CustomFieldRelationResolverService { result: VendureEntity | VendureEntity[] | null, fieldDef: RelationCustomFieldConfig, ) { + if (result == null) return null; + if (fieldDef.entity === ProductVariant) { if (Array.isArray(result)) { await Promise.all(result.map(r => this.applyVariantPrices(ctx, r as any)));