Просмотр исходного кода

fix(core): Fix collection.parent resolver in Postgres

Fixes #361
Michael Bromley 5 лет назад
Родитель
Сommit
f3feb7ccf4

+ 4 - 4
packages/admin-ui/src/lib/core/src/common/generated-types.ts

@@ -593,7 +593,7 @@ export type CreateZoneInput = {
 /**
  * @description
  * ISO 4217 currency code
- *
+ * 
  * @docsCategory common
  */
 export enum CurrencyCode {
@@ -1388,7 +1388,7 @@ export type JobSortParameter = {
 /**
  * @description
  * The state of a Job in the JobQueue
- *
+ * 
  * @docsCategory common
  */
 export enum JobState {
@@ -1406,7 +1406,7 @@ export enum JobState {
  * region or script modifier (e.g. de_AT). The selection available is based
  * on the [Unicode CLDR summary list](https://unicode-org.github.io/cldr-staging/charts/37/summary/root.html)
  * and includes the major spoken languages of the world and any widely-used variants.
- *
+ * 
  * @docsCategory common
  */
 export enum LanguageCode {
@@ -2581,7 +2581,7 @@ export type PaymentMethodSortParameter = {
  * @description
  * Permissions for administrators and customers. Used to control access to
  * GraphQL resolvers via the {@link Allow} decorator.
- *
+ * 
  * @docsCategory common
  */
 export enum Permission {

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

@@ -32,6 +32,7 @@ import {
     GetCollectionsWithAssets,
     GetFacetValues,
     GetProductCollections,
+    GetProductCollectionsWithParent,
     GetProductsWithVariantIds,
     LanguageCode,
     MoveCollection,
@@ -50,6 +51,7 @@ import {
 } from './graphql/shared-definitions';
 import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
 import { awaitRunningJobs } from './utils/await-running-jobs';
+import { sortById } from './utils/test-order-utils';
 
 describe('Collection resolver', () => {
     const { server, adminClient } = createTestEnvironment({
@@ -381,6 +383,44 @@ describe('Collection resolver', () => {
         expect(result.collection.parent!.name).toBe('Electronics');
     });
 
+    // Tests fix for https://github.com/vendure-ecommerce/vendure/issues/361
+    it('parent field resolved by CollectionEntityResolver', async () => {
+        const { product } = await adminClient.query<
+            GetProductCollectionsWithParent.Query,
+            GetProductCollectionsWithParent.Variables
+        >(GET_PRODUCT_COLLECTIONS_WITH_PARENT, {
+            id: 'T_1',
+        });
+
+        expect(product?.collections.length).toBe(3);
+        expect(product?.collections.sort(sortById)).toEqual([
+            {
+                id: 'T_3',
+                name: 'Electronics',
+                parent: {
+                    id: 'T_1',
+                    name: '__root_collection__',
+                },
+            },
+            {
+                id: 'T_4',
+                name: 'Computers',
+                parent: {
+                    id: 'T_3',
+                    name: 'Electronics',
+                },
+            },
+            {
+                id: 'T_5',
+                name: 'Pear',
+                parent: {
+                    id: 'T_4',
+                    name: 'Computers',
+                },
+            },
+        ]);
+    });
+
     it('children field', async () => {
         const result = await adminClient.query<GetCollection.Query, GetCollection.Variables>(GET_COLLECTION, {
             id: electronicsCollection.id,
@@ -1423,3 +1463,19 @@ const GET_PRODUCT_COLLECTIONS = gql`
         }
     }
 `;
+
+const GET_PRODUCT_COLLECTIONS_WITH_PARENT = gql`
+    query GetProductCollectionsWithParent($id: ID!) {
+        product(id: $id) {
+            id
+            collections {
+                id
+                name
+                parent {
+                    id
+                    name
+                }
+            }
+        }
+    }
+`;

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

@@ -3837,6 +3837,22 @@ export type GetProductCollectionsQuery = { __typename?: 'Query' } & {
     >;
 };
 
+export type GetProductCollectionsWithParentQueryVariables = {
+    id: Scalars['ID'];
+};
+
+export type GetProductCollectionsWithParentQuery = { __typename?: 'Query' } & {
+    product?: Maybe<
+        { __typename?: 'Product' } & Pick<Product, 'id'> & {
+                collections: Array<
+                    { __typename?: 'Collection' } & Pick<Collection, 'id' | 'name'> & {
+                            parent?: Maybe<{ __typename?: 'Collection' } & Pick<Collection, 'id' | 'name'>>;
+                        }
+                >;
+            }
+    >;
+};
+
 export type DeleteCountryMutationVariables = {
     id: Scalars['ID'];
 };
@@ -5843,6 +5859,18 @@ export namespace GetProductCollections {
     >;
 }
 
+export namespace GetProductCollectionsWithParent {
+    export type Variables = GetProductCollectionsWithParentQueryVariables;
+    export type Query = GetProductCollectionsWithParentQuery;
+    export type Product = NonNullable<GetProductCollectionsWithParentQuery['product']>;
+    export type Collections = NonNullable<
+        NonNullable<GetProductCollectionsWithParentQuery['product']>['collections'][0]
+    >;
+    export type Parent = NonNullable<
+        NonNullable<NonNullable<GetProductCollectionsWithParentQuery['product']>['collections'][0]>['parent']
+    >;
+}
+
 export namespace DeleteCountry {
     export type Variables = DeleteCountryMutationVariables;
     export type Mutation = DeleteCountryMutation;

+ 3 - 1
packages/core/src/service/services/collection.service.ts

@@ -151,6 +151,8 @@ export class CollectionService implements OnModuleInit {
     }
 
     async getParent(ctx: RequestContext, collectionId: ID): Promise<Collection | undefined> {
+        const parentIdSelect =
+            this.connection.options.type === 'postgres' ? '"child"."parentId"' : 'child.parentId';
         const parent = await this.connection
             .getRepository(Collection)
             .createQueryBuilder('collection')
@@ -159,7 +161,7 @@ export class CollectionService implements OnModuleInit {
                 qb =>
                     `collection.id = ${qb
                         .subQuery()
-                        .select('child.parentId')
+                        .select(parentIdSelect)
                         .from(Collection, 'child')
                         .where('child.id = :id', { id: collectionId })
                         .getQuery()}`,