Forráskód Böngészése

fix(core): Correct ordering of Collection.children

Fixes #1595
Michael Bromley 3 éve
szülő
commit
476fb5e7e0

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

@@ -856,6 +856,28 @@ describe('Collection resolver', () => {
             ),
         );
 
+        // https://github.com/vendure-ecommerce/vendure/issues/1595
+        it('children correctly ordered', async () => {
+            await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
+                input: {
+                    collectionId: computersCollection.id,
+                    parentId: 'T_1',
+                    index: 4,
+                },
+            });
+            const result = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
+                GET_COLLECTION,
+                {
+                    id: 'T_1',
+                },
+            );
+            if (!result.collection) {
+                fail(`did not return the collection`);
+                return;
+            }
+            expect(result.collection.children?.map(c => (c as any).position)).toEqual([0, 1, 2, 3, 4, 5]);
+        });
+
         async function getChildrenOf(parentId: string): Promise<Array<{ name: string; id: string }>> {
             const result = await adminClient.query<GetCollections.Query>(GET_COLLECTIONS);
             return result.collections.items.filter(i => i.parent!.id === parentId);

+ 1 - 0
packages/core/e2e/graphql/fragments.ts

@@ -191,6 +191,7 @@ export const COLLECTION_FRAGMENT = gql`
         children {
             id
             name
+            position
         }
     }
     ${ASSET_FRAGMENT}

+ 1 - 1
packages/core/e2e/graphql/generated-e2e-admin-types.ts

@@ -5637,7 +5637,7 @@ export type CollectionFragment = Pick<
     filters: Array<ConfigurableOperationFragment>;
     translations: Array<Pick<CollectionTranslation, 'id' | 'languageCode' | 'name' | 'slug' | 'description'>>;
     parent?: Maybe<Pick<Collection, 'id' | 'name'>>;
-    children?: Maybe<Array<Pick<Collection, 'id' | 'name'>>>;
+    children?: Maybe<Array<Pick<Collection, 'id' | 'name' | 'position'>>>;
 };
 
 export type FacetValueFragment = Pick<FacetValue, 'id' | 'languageCode' | 'code' | 'name'> & {

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

@@ -91,7 +91,7 @@ export class CollectionEntityResolver {
     ): Promise<Collection[]> {
         let children: Collection[] = [];
         if (collection.children) {
-            children = collection.children;
+            children = collection.children.sort((a, b) => a.position - b.position);
         } else {
             children = (await this.collectionService.getChildren(ctx, collection.id)) as any;
         }