Skip to content

Commit

Permalink
perf(core): Improve performance of Product.facetValues resolver (#2239)
Browse files Browse the repository at this point in the history
  • Loading branch information
goroka authored Jun 28, 2023
1 parent b25ddcd commit a0e891a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions packages/core/src/api/resolvers/entity/product-entity.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ProductVariant } from '../../../entity/product-variant/product-variant.
import { LocaleStringHydrator } from '../../../service/helpers/locale-string-hydrator/locale-string-hydrator';
import { AssetService } from '../../../service/services/asset.service';
import { CollectionService } from '../../../service/services/collection.service';
import { FacetValueService } from '../../../service/services/facet-value.service';
import { ProductOptionGroupService } from '../../../service/services/product-option-group.service';
import { ProductVariantService } from '../../../service/services/product-variant.service';
import { ProductService } from '../../../service/services/product.service';
Expand All @@ -32,6 +33,7 @@ export class ProductEntityResolver {
private productOptionGroupService: ProductOptionGroupService,
private assetService: AssetService,
private productService: ProductService,
private facetValueService: FacetValueService,
private localeStringHydrator: LocaleStringHydrator,
) {}

Expand Down Expand Up @@ -113,15 +115,16 @@ export class ProductEntityResolver {
} else {
facetValues = await this.productService.getFacetValuesForProduct(ctx, product.id);
}
return facetValues.filter(fv => {
if (!fv.channels.find(c => idsAreEqual(c.id, ctx.channelId))) {
return false;
}
if (apiType === 'shop' && fv.facet.isPrivate) {
return false;
}
return true;
});
const filteredFacetValues = await this.facetValueService.findByIds(
ctx,
facetValues.map(facetValue => facetValue.id),
);

if (apiType === 'shop') {
return filteredFacetValues.filter(fv => !fv.facet.isPrivate);
} else {
return filteredFacetValues;
}
}

@ResolveField()
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/service/services/product.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export class ProductService {
.getRepository(ctx, Product)
.findOne({
where: { id: productId },
relations: ['facetValues', 'facetValues.facet', 'facetValues.channels'],
relations: ['facetValues'],
})
.then(variant =>
!variant ? [] : variant.facetValues.map(o => this.translator.translate(o, ctx, ['facet'])),
Expand Down

0 comments on commit a0e891a

Please sign in to comment.