Browse Source

fix(core): Fix filtering on list queries of tree entities

Fixes #3107. This reinstates the correct use of the joiner string
which was accidentally lost in commit bcfcf7d6.
Michael Bromley 1 year ago
parent
commit
227da05392

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

@@ -735,6 +735,21 @@ describe('Collection resolver', () => {
                 139900, 219900, 229900,
             ]);
         });
+
+        // https://github.com/vendure-ecommerce/vendure/issues/3107
+        it('collection list with translations, filtered by name', async () => {
+            const { collections } = await adminClient.query(GET_COLLECTION_LIST_WITH_TRANSLATIONS, {
+                options: {
+                    filter: {
+                        name: {
+                            eq: 'Electronics',
+                        },
+                    },
+                },
+            });
+
+            expect(collections.items.length).toBeDefined();
+        });
     });
 
     describe('moveCollection', () => {
@@ -2413,6 +2428,21 @@ export const GET_COLLECTION_LIST = gql`
     ${COLLECTION_FRAGMENT}
 `;
 
+export const GET_COLLECTION_LIST_WITH_TRANSLATIONS = gql`
+    query GetCollectionListWithTranslations($options: CollectionListOptions) {
+        collections(options: $options) {
+            items {
+                id
+                name
+                translations {
+                    id
+                    name
+                }
+            }
+        }
+    }
+`;
+
 export const MOVE_COLLECTION = gql`
     mutation MoveCollection($input: MoveCollectionInput!) {
         moveCollection(input: $input) {

+ 38 - 17
packages/core/e2e/list-query-builder.e2e-spec.ts

@@ -56,14 +56,16 @@ describe('ListQueryBuilder', () => {
 
             expect(testEntities.totalItems).toBe(6);
             expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
-            expect(testEntities.items.map((i: any) => i.name)).toEqual(expect.arrayContaining([
-                'apple',
-                'bike',
-                'cake',
-                'dog',
-                'egg',
-                'baum', // if default en lang does not exist, use next available lang
-            ]));
+            expect(testEntities.items.map((i: any) => i.name)).toEqual(
+                expect.arrayContaining([
+                    'apple',
+                    'bike',
+                    'cake',
+                    'dog',
+                    'egg',
+                    'baum', // if default en lang does not exist, use next available lang
+                ]),
+            );
         });
 
         it('all de', async () => {
@@ -77,14 +79,16 @@ describe('ListQueryBuilder', () => {
 
             expect(testEntities.totalItems).toBe(6);
             expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
-            expect(testEntities.items.map((i: any) => i.name)).toEqual(expect.arrayContaining([
-                'apfel',
-                'fahrrad',
-                'kuchen',
-                'hund',
-                'egg', // falls back to en translation when de doesn't exist
-                'baum',
-            ]));
+            expect(testEntities.items.map((i: any) => i.name)).toEqual(
+                expect.arrayContaining([
+                    'apfel',
+                    'fahrrad',
+                    'kuchen',
+                    'hund',
+                    'egg', // falls back to en translation when de doesn't exist
+                    'baum',
+                ]),
+            );
         });
 
         it('take', async () => {
@@ -278,6 +282,20 @@ describe('ListQueryBuilder', () => {
             expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
         });
 
+        it('filtering on translatable string', async () => {
+            const { testEntities } = await adminClient.query(GET_LIST_WITH_TRANSLATIONS, {
+                options: {
+                    filter: {
+                        name: {
+                            contains: 'g',
+                        },
+                    },
+                },
+            });
+
+            expect(getItemLabels(testEntities.items)).toEqual(['D', 'E']);
+        });
+
         describe('regex', () => {
             it('simple substring', async () => {
                 const { testEntities } = await adminClient.query(GET_LIST, {
@@ -1208,7 +1226,10 @@ describe('ListQueryBuilder', () => {
     // https://github.com/vendure-ecommerce/vendure/issues/1586
     it('using the getMany() of the resulting QueryBuilder', async () => {
         const { testEntitiesGetMany } = await adminClient.query(GET_ARRAY_LIST, {});
-        const actualPrices = testEntitiesGetMany.sort(sortById).map((x: any) => x.price).sort((a: number, b: number) => a - b);
+        const actualPrices = testEntitiesGetMany
+            .sort(sortById)
+            .map((x: any) => x.price)
+            .sort((a: number, b: number) => a - b);
         const expectedPrices = [11, 9, 22, 14, 13, 33].sort((a, b) => a - b);
         expect(actualPrices).toEqual(expectedPrices);
     });

+ 1 - 1
packages/core/src/service/helpers/utils/tree-relations-qb-joiner.ts

@@ -110,7 +110,7 @@ export function joinTreeRelationsDynamically<T extends VendureEntity>(
         }
         const nextAlias = DriverUtils.buildAlias(
             qb.connection.driver,
-            { shorten: false },
+            { shorten: false, joiner: joinConnector },
             currentAlias,
             part.replace(/\./g, '_'),
         );