Przeglądaj źródła

fix(core): Implement property resolver for Collection.children

Michael Bromley 6 lat temu
rodzic
commit
e5f614ed7f

+ 2 - 2
packages/core/e2e/__snapshots__/collection.e2e-spec.ts.snap

@@ -22,7 +22,7 @@ Object {
       "type": "IMAGE",
       "type": "IMAGE",
     },
     },
   ],
   ],
-  "children": null,
+  "children": Array [],
   "description": "",
   "description": "",
   "featuredAsset": Object {
   "featuredAsset": Object {
     "fileSize": 4,
     "fileSize": 4,
@@ -78,7 +78,7 @@ Object {
       "type": "IMAGE",
       "type": "IMAGE",
     },
     },
   ],
   ],
-  "children": null,
+  "children": Array [],
   "description": "Apple stuff ",
   "description": "Apple stuff ",
   "featuredAsset": Object {
   "featuredAsset": Object {
     "fileSize": 4,
     "fileSize": 4,

+ 23 - 0
packages/core/e2e/collection.e2e-spec.ts

@@ -165,6 +165,29 @@ describe('Collection resolver', () => {
         expect(result.collection.id).toBe(computersCollection.id);
         expect(result.collection.id).toBe(computersCollection.id);
     });
     });
 
 
+    it('parent field', async () => {
+        const result = await client.query<GetCollection.Query, GetCollection.Variables>(GET_COLLECTION, {
+            id: computersCollection.id,
+        });
+        if (!result.collection) {
+            fail(`did not return the collection`);
+            return;
+        }
+        expect(result.collection.parent!.name).toBe('Electronics');
+    });
+
+    it('children field', async () => {
+        const result = await client.query<GetCollection.Query, GetCollection.Variables>(GET_COLLECTION, {
+            id: electronicsCollection.id,
+        });
+        if (!result.collection) {
+            fail(`did not return the collection`);
+            return;
+        }
+        expect(result.collection.children!.length).toBe(1);
+        expect(result.collection.children![0].name).toBe('Computers');
+    });
+
     it('breadcrumbs', async () => {
     it('breadcrumbs', async () => {
         const result = await client.query<GetCollectionBreadcrumbs.Query, GetCollectionBreadcrumbs.Variables>(
         const result = await client.query<GetCollectionBreadcrumbs.Query, GetCollectionBreadcrumbs.Variables>(
             GET_COLLECTION_BREADCRUMBS,
             GET_COLLECTION_BREADCRUMBS,

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

@@ -54,4 +54,12 @@ export class CollectionEntityResolver {
         }
         }
         return this.collectionService.getParent(ctx, collection.id) as any;
         return this.collectionService.getParent(ctx, collection.id) as any;
     }
     }
+
+    @ResolveProperty()
+    async children(@Ctx() ctx: RequestContext, @Parent() collection: Collection): Promise<Collection[]> {
+        if (collection.children) {
+            return collection.children;
+        }
+        return this.collectionService.getChildren(ctx, collection.id) as any;
+    }
 }
 }

+ 11 - 4
packages/core/src/service/services/collection.service.ts

@@ -124,6 +124,10 @@ export class CollectionService implements OnModuleInit {
         return parent && translateDeep(parent, ctx.languageCode);
         return parent && translateDeep(parent, ctx.languageCode);
     }
     }
 
 
+    async getChildren(ctx: RequestContext, collectionId: ID): Promise<Collection[]> {
+        return this.getDescendants(ctx, collectionId, 1);
+    }
+
     async getBreadcrumbs(
     async getBreadcrumbs(
         ctx: RequestContext,
         ctx: RequestContext,
         collection: Collection,
         collection: Collection,
@@ -160,16 +164,19 @@ export class CollectionService implements OnModuleInit {
     }
     }
 
 
     /**
     /**
-     * Returns the descendants of a Collection as a flat array.
+     * Returns the descendants of a Collection as a flat array. The depth of the traversal can be limited
+     * with the maxDepth argument. So to get only the immediate children, set maxDepth = 1.
      */
      */
-    async getDescendants(ctx: RequestContext, rootId: ID): Promise<Array<Translated<Collection>>> {
-        const getChildren = async (id: ID, _descendants: Collection[] = []) => {
+    async getDescendants(ctx: RequestContext, rootId: ID, maxDepth: number = Number.MAX_SAFE_INTEGER): Promise<Array<Translated<Collection>>> {
+        const getChildren = async (id: ID, _descendants: Collection[] = [], depth = 1) => {
             const children = await this.connection
             const children = await this.connection
                 .getRepository(Collection)
                 .getRepository(Collection)
                 .find({ where: { parent: { id } } });
                 .find({ where: { parent: { id } } });
             for (const child of children) {
             for (const child of children) {
                 _descendants.push(child);
                 _descendants.push(child);
-                await getChildren(child.id, _descendants);
+                if (depth < maxDepth) {
+                    await getChildren(child.id, _descendants, depth++);
+                }
             }
             }
             return _descendants;
             return _descendants;
         };
         };