Jelajahi Sumber

fix(core): Resolve collection assets field

Fixes #157
Michael Bromley 6 tahun lalu
induk
melakukan
e32895f278

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

@@ -26,6 +26,7 @@ import {
     GetCollectionProducts,
     GetCollections,
     GetCollectionsForProducts,
+    GetCollectionsWithAssets,
     GetFacetValues,
     GetProductCollections,
     GetProductsWithVariantIds,
@@ -276,6 +277,22 @@ describe('Collection resolver', () => {
         expect(updateCollection).toMatchSnapshot();
     });
 
+    it('collections.assets', async () => {
+        const { collections } = await client.query<GetCollectionsWithAssets.Query>(gql`
+            query GetCollectionsWithAssets {
+                collections {
+                    items {
+                        assets {
+                            name
+                        }
+                    }
+                }
+            }
+        `);
+
+        expect(collections.items[0].assets).toBeDefined();
+    });
+
     describe('moveCollection', () => {
         it('moves a collection to a new parent', async () => {
             const result = await client.query<MoveCollection.Mutation, MoveCollection.Variables>(

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

@@ -967,6 +967,7 @@ export type CustomFields = {
     Facet: Array<CustomFieldConfig>;
     FacetValue: Array<CustomFieldConfig>;
     GlobalSettings: Array<CustomFieldConfig>;
+    Order: Array<CustomFieldConfig>;
     OrderLine: Array<CustomFieldConfig>;
     Product: Array<CustomFieldConfig>;
     ProductOption: Array<CustomFieldConfig>;
@@ -2097,6 +2098,7 @@ export type Order = Node & {
     totalBeforeTax: Scalars['Int'];
     total: Scalars['Int'];
     history: HistoryEntryList;
+    customFields?: Maybe<Scalars['JSON']>;
 };
 
 export type OrderHistoryArgs = {
@@ -3350,6 +3352,16 @@ export type GetCustomerCountQuery = { __typename?: 'Query' } & {
     customers: { __typename?: 'CustomerList' } & Pick<CustomerList, 'totalItems'>;
 };
 
+export type GetCollectionsWithAssetsQueryVariables = {};
+
+export type GetCollectionsWithAssetsQuery = { __typename?: 'Query' } & {
+    collections: { __typename?: 'CollectionList' } & {
+        items: Array<
+            { __typename?: 'Collection' } & { assets: Array<{ __typename?: 'Asset' } & Pick<Asset, 'name'>> }
+        >;
+    };
+};
+
 export type GetProductsWithVariantIdsQueryVariables = {};
 
 export type GetProductsWithVariantIdsQuery = { __typename?: 'Query' } & {
@@ -5016,6 +5028,16 @@ export namespace GetCustomerCount {
     export type Customers = GetCustomerCountQuery['customers'];
 }
 
+export namespace GetCollectionsWithAssets {
+    export type Variables = GetCollectionsWithAssetsQueryVariables;
+    export type Query = GetCollectionsWithAssetsQuery;
+    export type Collections = GetCollectionsWithAssetsQuery['collections'];
+    export type Items = NonNullable<GetCollectionsWithAssetsQuery['collections']['items'][0]>;
+    export type Assets = NonNullable<
+        (NonNullable<GetCollectionsWithAssetsQuery['collections']['items'][0]>)['assets'][0]
+    >;
+}
+
 export namespace GetProductsWithVariantIds {
     export type Variables = GetProductsWithVariantIdsQueryVariables;
     export type Query = GetProductsWithVariantIdsQuery;

+ 2 - 0
packages/core/e2e/graphql/generated-e2e-shop-types.ts

@@ -684,6 +684,7 @@ export type CustomFields = {
     Facet: Array<CustomFieldConfig>;
     FacetValue: Array<CustomFieldConfig>;
     GlobalSettings: Array<CustomFieldConfig>;
+    Order: Array<CustomFieldConfig>;
     OrderLine: Array<CustomFieldConfig>;
     Product: Array<CustomFieldConfig>;
     ProductOption: Array<CustomFieldConfig>;
@@ -1469,6 +1470,7 @@ export type Order = Node & {
     totalBeforeTax: Scalars['Int'];
     total: Scalars['Int'];
     history: HistoryEntryList;
+    customFields?: Maybe<Scalars['JSON']>;
 };
 
 export type OrderHistoryArgs = {

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

@@ -66,10 +66,21 @@ export class CollectionEntityResolver {
     }
 
     @ResolveProperty()
-    async featuredAsset(@Ctx() ctx: RequestContext, @Parent() collection: Collection): Promise<Asset | undefined> {
+    async featuredAsset(
+        @Ctx() ctx: RequestContext,
+        @Parent() collection: Collection,
+    ): Promise<Asset | undefined> {
         if (collection.featuredAsset) {
             return collection.featuredAsset;
         }
         return this.assetService.getFeaturedAsset(collection);
     }
+
+    @ResolveProperty()
+    async assets(@Ctx() ctx: RequestContext, @Parent() collection: Collection): Promise<Asset[] | undefined> {
+        if (collection.assets) {
+            return collection.assets;
+        }
+        return this.assetService.getEntityAssets(collection);
+    }
 }

+ 10 - 0
packages/core/src/service/services/asset.service.ts

@@ -68,6 +68,16 @@ export class AssetService {
         return entityWithFeaturedAsset && entityWithFeaturedAsset.featuredAsset;
     }
 
+    async getEntityAssets<T extends EntityWithAssets>(entity: T): Promise<Asset[] | undefined> {
+        const entityType = Object.getPrototypeOf(entity).constructor;
+        const entityWithFeaturedAsset = await this.connection
+            .getRepository<EntityWithAssets>(entityType)
+            .findOne(entity.id, {
+                relations: ['assets'],
+            });
+        return entityWithFeaturedAsset && entityWithFeaturedAsset.assets;
+    }
+
     /**
      * Create an Asset based on a file uploaded via the GraphQL API.
      */