Ver Fonte

fix(core): Fix custom field relation for ProductVariant when value is null (#2727)

Fixes #2723
Andrii Nutskovskyi há 1 ano atrás
pai
commit
b4f8a55e11

+ 37 - 0
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 });

+ 2 - 0
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)));