Skip to content

Commit

Permalink
perf(core): Optimize resolution of featuredAsset fields
Browse files Browse the repository at this point in the history
Previously, if one of the entities had no assigned featuredAsset, the value
to the resolver would be `null`, which would cause _another_ DB lookup.
Additionally, we are now preventing unnecessary loading of eager relations
when loading a featuredAsset.
  • Loading branch information
michaelbromley committed Sep 9, 2024
1 parent ad30b55 commit d7bd446
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class CollectionEntityResolver {
@Ctx() ctx: RequestContext,
@Parent() collection: Collection,
): Promise<Asset | undefined> {
if (collection.featuredAsset) {
if (collection.featuredAsset !== undefined) {
return collection.featuredAsset;
}
return this.assetService.getFeaturedAsset(ctx, collection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class OrderLineEntityResolver {
@Ctx() ctx: RequestContext,
@Parent() orderLine: OrderLine,
): Promise<Asset | undefined> {
if (orderLine.featuredAsset) {
if (orderLine.featuredAsset !== undefined) {
return orderLine.featuredAsset;
} else {
return this.assetService.getFeaturedAsset(ctx, orderLine);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class ProductEntityResolver {

@ResolveField()
async featuredAsset(@Ctx() ctx: RequestContext, @Parent() product: Product): Promise<Asset | undefined> {
if (product.featuredAsset) {
if (product.featuredAsset !== undefined) {
return product.featuredAsset;
}
return this.assetService.getFeaturedAsset(ctx, product);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import { Asset, Channel, FacetValue, Product, ProductOption, StockLevel, TaxRate
import { ProductVariant } from '../../../entity/product-variant/product-variant.entity';
import { StockMovement } from '../../../entity/stock-movement/stock-movement.entity';
import { LocaleStringHydrator } from '../../../service/helpers/locale-string-hydrator/locale-string-hydrator';
import { StockLevelService } from '../../../service/services/stock-level.service';
import { AssetService } from '../../../service/services/asset.service';
import { ProductVariantService } from '../../../service/services/product-variant.service';
import { StockLevelService } from '../../../service/services/stock-level.service';
import { StockMovementService } from '../../../service/services/stock-movement.service';
import { ApiType } from '../../common/get-api-type';
import { RequestContext } from '../../common/request-context';
Expand Down Expand Up @@ -103,7 +103,7 @@ export class ProductVariantEntityResolver {
@Ctx() ctx: RequestContext,
@Parent() productVariant: ProductVariant,
): Promise<Asset | undefined> {
if (productVariant.featuredAsset) {
if (productVariant.featuredAsset !== undefined) {
return productVariant.featuredAsset;
}
return this.assetService.getFeaturedAsset(ctx, productVariant);
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/service/services/asset.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export class AssetService {
ctx.channelId,
{
relations: ['featuredAsset'],
loadEagerRelations: false,
},
);
} else {
Expand All @@ -178,6 +179,7 @@ export class AssetService {
relations: {
featuredAsset: true,
},
loadEagerRelations: false,
// TODO: satisfies
} as FindOneOptions<T>)
.then(result => result ?? undefined);
Expand Down

0 comments on commit d7bd446

Please sign in to comment.