Skip to content

Commit

Permalink
fix(core): Add product translation to product variant entity resolver (
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverqx authored Jan 26, 2024
1 parent ef91835 commit 9289a1c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
32 changes: 31 additions & 1 deletion packages/core/e2e/custom-fields.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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})`,
Expand Down
13 changes: 10 additions & 3 deletions packages/core/src/service/services/product-variant.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 9289a1c

Please sign in to comment.