Browse Source

fix(core): Add product translation to product variant entity resolver (#2644)

oliver 2 years ago
parent
commit
9289a1c62c

+ 31 - 1
packages/core/e2e/custom-fields.e2e-spec.ts

@@ -470,7 +470,7 @@ describe('Custom fields', () => {
                         }
                     }
                 `);
-            }, 'The custom field "stringWithOptions" value ["tiny"] is invalid. Valid options are [\'small\', \'medium\', \'large\']'),
+            }, "The custom field \"stringWithOptions\" value [\"tiny\"] is invalid. Valid options are ['small', 'medium', 'large']"),
         );
 
         it('valid string option', async () => {
@@ -951,6 +951,36 @@ describe('Custom fields', () => {
         );
     });
 
+    describe('product on productVariant entity', () => {
+        it('is translated', async () => {
+            const { productVariants } = await adminClient.query(gql`
+                query {
+                    productVariants(productId: "T_1") {
+                        items {
+                            product {
+                                name
+                                id
+                                customFields {
+                                    localeStringWithDefault
+                                    stringWithDefault
+                                }
+                            }
+                        }
+                    }
+                }
+            `);
+
+            expect(productVariants.items[0].product).toEqual({
+                id: 'T_1',
+                name: 'Laptop',
+                customFields: {
+                    localeStringWithDefault: 'hola',
+                    stringWithDefault: 'hello',
+                },
+            });
+        });
+    });
+
     describe('unique constraint', () => {
         it('setting unique value works', async () => {
             const result = await adminClient.query(

+ 2 - 1
packages/core/src/api/resolvers/entity/product-variant-entity.resolver.ts

@@ -79,9 +79,10 @@ export class ProductVariantEntityResolver {
         @Ctx() ctx: RequestContext,
         @Parent() productVariant: ProductVariant,
     ): Promise<Product | undefined> {
-        if (productVariant.product) {
+        if (productVariant.product.name) {
             return productVariant.product;
         }
+
         return this.requestContextCache.get(
             ctx,
             `ProductVariantEntityResolver.product(${productVariant.productId})`,

+ 10 - 3
packages/core/src/service/services/product-variant.service.ts

@@ -291,9 +291,16 @@ export class ProductVariantService {
      * page, this method returns only the Product itself.
      */
     async getProductForVariant(ctx: RequestContext, variant: ProductVariant): Promise<Translated<Product>> {
-        const product = await this.connection.getEntityOrThrow(ctx, Product, variant.productId, {
-            includeSoftDeleted: true,
-        });
+        let product;
+
+        if (!variant.product) {
+            product = await this.connection.getEntityOrThrow(ctx, Product, variant.productId, {
+                includeSoftDeleted: true,
+            });
+        } else {
+            product = variant.product;
+        }
+
         return this.translator.translate(product, ctx);
     }