Browse Source

fix(core): Correctly handle nested parent collection query in Shop API

Fixes #981
Michael Bromley 4 years ago
parent
commit
2445e48e51

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

@@ -26,6 +26,7 @@ import {
     GetAssetList,
     GetCollection,
     GetCollectionBreadcrumbs,
+    GetCollectionNestedParents,
     GetCollectionProducts,
     GetCollections,
     GetCollectionsForProducts,
@@ -580,6 +581,15 @@ describe('Collection resolver', () => {
             ]);
         });
 
+        // https://github.com/vendure-ecommerce/vendure/issues/981
+        it('nested parent field in shop API', async () => {
+            const { collections } = await shopClient.query<GetCollectionNestedParents.Query>(
+                GET_COLLECTION_NESTED_PARENTS,
+            );
+
+            expect(collections.items[0].parent?.name).toBe(ROOT_COLLECTION_NAME);
+        });
+
         it('children field', async () => {
             const result = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
                 GET_COLLECTION,
@@ -1877,3 +1887,23 @@ const GET_PRODUCT_COLLECTIONS_WITH_PARENT = gql`
         }
     }
 `;
+
+const GET_COLLECTION_NESTED_PARENTS = gql`
+    query GetCollectionNestedParents {
+        collections {
+            items {
+                id
+                name
+                parent {
+                    name
+                    parent {
+                        name
+                        parent {
+                            name
+                        }
+                    }
+                }
+            }
+        }
+    }
+`;

File diff suppressed because it is too large
+ 518 - 665
packages/core/e2e/graphql/generated-e2e-admin-types.ts


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

@@ -72,13 +72,13 @@ export class CollectionEntityResolver {
         @Parent() collection: Collection,
         @Api() apiType: ApiType,
     ): Promise<Collection | undefined> {
-        let parent: Collection;
+        let parent: Collection | undefined;
         if (collection.parent) {
             parent = collection.parent;
         } else {
-            parent = (await this.collectionService.getParent(ctx, collection.id)) as any;
+            parent = await this.collectionService.getParent(ctx, collection.id);
         }
-        return apiType === 'shop' && parent.isPrivate ? undefined : parent;
+        return apiType === 'shop' && parent?.isPrivate ? undefined : parent;
     }
 
     @ResolveField()

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