Browse Source

feat(server): Add ancestorFacetValues property to ProductCategory

Michael Bromley 7 years ago
parent
commit
050aafa695

File diff suppressed because it is too large
+ 0 - 0
schema.json


+ 39 - 0
server/e2e/product-category.e2e-spec.ts

@@ -137,6 +137,33 @@ describe('ProductCategory resolver', () => {
                 'T_4',
             ]);
         });
+
+        it('resolves ancestorFacetValues at root', async () => {
+            const result = await client.query(GET_ANCESTOR_FACET_VALUES, { id: electronicsCategory.id });
+            if (!result.productCategory) {
+                fail(`did not return the category`);
+                return;
+            }
+            expect(result.productCategory.ancestorFacetValues.map(v => v.id)).toEqual([]);
+        });
+
+        it('resolves ancestorFacetValues 1 level deep', async () => {
+            const result = await client.query(GET_ANCESTOR_FACET_VALUES, { id: laptopsCategory.id });
+            if (!result.productCategory) {
+                fail(`did not return the category`);
+                return;
+            }
+            expect(result.productCategory.ancestorFacetValues.map(v => v.id)).toEqual(['T_1']);
+        });
+
+        it('resolves ancestorFacetValues 2 levels deep', async () => {
+            const result = await client.query(GET_ANCESTOR_FACET_VALUES, { id: appleCategory.id });
+            if (!result.productCategory) {
+                fail(`did not return the category`);
+                return;
+            }
+            expect(result.productCategory.ancestorFacetValues.map(v => v.id)).toEqual(['T_1', 'T_2']);
+        });
     });
 
     describe('updateProductCategory', () => {
@@ -295,3 +322,15 @@ const GET_DECENDANT_FACET_VALUES = gql`
         }
     }
 `;
+
+const GET_ANCESTOR_FACET_VALUES = gql`
+    query GetAncestorFacetValues($id: ID!) {
+        productCategory(id: $id) {
+            id
+            ancestorFacetValues {
+                id
+                name
+            }
+        }
+    }
+`;

+ 10 - 0
server/src/api/resolvers/product-category.resolver.ts

@@ -56,6 +56,16 @@ export class ProductCategoryResolver {
         return this.facetValueService.findByCategoryIds(ctx, descendants.map(d => d.id));
     }
 
+    @ResolveProperty()
+    async ancestorFacetValues(
+        @Ctx() ctx: RequestContext,
+        @Parent() category: ProductCategory,
+    ): Promise<Array<Translated<FacetValue>>> {
+        const categoryId = this.idCodecService.decode(category.id);
+        const ancestors = await this.productCategoryService.getAncestors(categoryId, ctx);
+        return this.facetValueService.findByCategoryIds(ctx, ancestors.map(d => d.id));
+    }
+
     @Mutation()
     @Allow(Permission.CreateCatalog)
     @Decode('assetIds', 'featuredAssetId', 'parentId', 'facetValueIds')

+ 1 - 0
server/src/entity/product-category/product-category.graphql

@@ -12,6 +12,7 @@ type ProductCategory implements Node {
     children: [ProductCategory!]
     facetValues: [FacetValue!]!
     descendantFacetValues: [FacetValue!]!
+    ancestorFacetValues: [FacetValue!]!
     translations: [ProductCategoryTranslation!]!
 }
 

+ 3 - 0
server/src/service/services/facet-value.service.ts

@@ -62,6 +62,9 @@ export class FacetValueService {
     }
 
     async findByCategoryIds(ctx: RequestContext, ids: ID[]): Promise<Array<Translated<FacetValue>>> {
+        if (ids.length === 0) {
+            return [];
+        }
         const facetValues = await this.connection
             .getRepository(FacetValue)
             .createQueryBuilder('facetValue')

+ 3 - 1
shared/generated-types.ts

@@ -1,5 +1,5 @@
 // tslint:disable
-// Generated in 2019-02-14T13:50:27+01:00
+// Generated in 2019-02-14T14:24:26+01:00
 export type Maybe<T> = T | null;
 
 
@@ -5280,6 +5280,8 @@ export interface ProductCategory extends Node {
   
   descendantFacetValues: FacetValue[];
   
+  ancestorFacetValues: FacetValue[];
+  
   translations: ProductCategoryTranslation[];
   
   customFields?: Maybe<Json>;

Some files were not shown because too many files changed in this diff