Browse Source

chore(core): Reuse existing functions, and handle caching issue

Will Nahmens 1 week ago
parent
commit
bcaf8efd73

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

@@ -74,18 +74,31 @@ export class CollectionEntityResolver {
         // Check if count was pre-loaded in findAll
         const cachedCount = (collection as Collection & { __productVariantCount: number })
             .__productVariantCount;
-        const isCountOnlyRequest = !options?.skip && !options?.take && !options?.filter && !options?.sort;
+        // Only treat as count-only when explicit flag is provided
+        const isCountOnlyRequest = (options as any)?.countOnly === true;
 
         if (isCountOnlyRequest && cachedCount !== undefined) {
-            // Return cached count without querying
+            // Return cached count without querying for explicit count-only requests
             return {
                 items: [],
                 totalItems: cachedCount,
             };
         }
 
-        // Fall back to querying for full list or when cache not available
-        return this.productVariantService.getVariantsByCollectionId(ctx, collection.id, options, relations);
+        // Fetch items normally (cachedCount will optimize the totalItems query if available)
+        const result = await this.productVariantService.getVariantsByCollectionId(
+            ctx,
+            collection.id,
+            options,
+            relations,
+        );
+
+        // Use cached count to avoid extra query if available and no filters applied
+        if (cachedCount !== undefined && !options?.filter) {
+            result.totalItems = cachedCount;
+        }
+
+        return result;
     }
 
     @ResolveField()

+ 6 - 15
packages/core/src/service/services/collection.service.ts

@@ -53,6 +53,7 @@ import { moveToIndex } from '../helpers/utils/move-to-index';
 
 import { AssetService } from './asset.service';
 import { ChannelService } from './channel.service';
+import { ProductVariantService } from './product-variant.service';
 import { RoleService } from './role.service';
 
 export type ApplyCollectionFiltersJobData = {
@@ -99,6 +100,7 @@ export class CollectionService implements OnModuleInit {
         private translator: TranslatorService,
         private roleService: RoleService,
         private requestContextService: RequestContextService,
+        private productVariantService: ProductVariantService,
     ) {}
 
     /**
@@ -221,23 +223,12 @@ export class CollectionService implements OnModuleInit {
             // when the dashboard queries productVariants { totalItems } e.g. the collection list query
             if (items.length > 0) {
                 const collectionIds = items.map(c => c.id);
-                const variantCounts = await this.connection
-                    .getRepository(ctx, ProductVariant)
-                    .createQueryBuilder('variant')
-                    .select('collection.id', 'collectionId')
-                    .addSelect('COUNT(DISTINCT variant.id)', 'count')
-                    .innerJoin('variant.collections', 'collection')
-                    .innerJoin('variant.product', 'product')
-                    .where('collection.id IN (:...ids)', { ids: collectionIds })
-                    .andWhere('variant.deletedAt IS NULL')
-                    .andWhere('product.deletedAt IS NULL')
-                    .groupBy('collection.id')
-                    .getRawMany();
+                const countMap = await this.productVariantService.getVariantCountsByCollectionIds(
+                    ctx,
+                    collectionIds,
+                );
 
                 // Store counts on collection objects so the resolver can use them
-                const countMap = new Map(
-                    variantCounts.map(row => [row.collectionId, Number.parseInt(row.count, 10)]),
-                );
                 items.forEach(collection => {
                     (collection as CollectionWithVariantCount).__productVariantCount =
                         countMap.get(collection.id) ?? 0;