Browse Source

perf(core): Optimize resolution of featuredAsset fields

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.
Michael Bromley 1 year ago
parent
commit
d7bd446df1

+ 1 - 1
packages/core/src/api/resolvers/entity/collection-entity.resolver.ts

@@ -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);

+ 1 - 1
packages/core/src/api/resolvers/entity/order-line-entity.resolver.ts

@@ -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);

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

@@ -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);

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

@@ -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';
@@ -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);

+ 2 - 0
packages/core/src/service/services/asset.service.ts

@@ -168,6 +168,7 @@ export class AssetService {
                 ctx.channelId,
                 {
                     relations: ['featuredAsset'],
+                    loadEagerRelations: false,
                 },
             );
         } else {
@@ -178,6 +179,7 @@ export class AssetService {
                     relations: {
                         featuredAsset: true,
                     },
+                    loadEagerRelations: false,
                     // TODO: satisfies
                 } as FindOneOptions<T>)
                 .then(result => result ?? undefined);