Bläddra i källkod

feat(core): Add Facet queries to Shop API (#1016)

Closes #1013
Artem Danilov 4 år sedan
förälder
incheckning
d6a049c6cd

+ 37 - 0
packages/common/src/generated-shop-types.ts

@@ -40,6 +40,10 @@ export type Query = {
   eligibleShippingMethods: Array<ShippingMethodQuote>;
   /** Returns a list of payment methods and their eligibility based on the current active Order */
   eligiblePaymentMethods: Array<PaymentMethodQuote>;
+  /** A list of Facets available to the shop */
+  facets: FacetList;
+  /** Returns a Facet by its id */
+  facet?: Maybe<Facet>;
   /** Returns information about the current authenticated User */
   me?: Maybe<CurrentUser>;
   /** Returns the possible next states that the activeOrder can transition to */
@@ -76,6 +80,16 @@ export type QueryCollectionArgs = {
 };
 
 
+export type QueryFacetsArgs = {
+  options?: Maybe<FacetListOptions>;
+};
+
+
+export type QueryFacetArgs = {
+  id: Scalars['ID'];
+};
+
+
 export type QueryOrderArgs = {
   id: Scalars['ID'];
 };
@@ -2764,6 +2778,13 @@ export type CollectionListOptions = {
   filter?: Maybe<CollectionFilterParameter>;
 };
 
+export type FacetListOptions = {
+  skip?: Maybe<Scalars['Int']>;
+  take?: Maybe<Scalars['Int']>;
+  sort?: Maybe<FacetSortParameter>;
+  filter?: Maybe<FacetFilterParameter>;
+};
+
 export type OrderListOptions = {
   skip?: Maybe<Scalars['Int']>;
   take?: Maybe<Scalars['Int']>;
@@ -2924,6 +2945,22 @@ export type CollectionSortParameter = {
   description?: Maybe<SortOrder>;
 };
 
+export type FacetFilterParameter = {
+  createdAt?: Maybe<DateOperators>;
+  updatedAt?: Maybe<DateOperators>;
+  languageCode?: Maybe<StringOperators>;
+  name?: Maybe<StringOperators>;
+  code?: Maybe<StringOperators>;
+};
+
+export type FacetSortParameter = {
+  id?: Maybe<SortOrder>;
+  createdAt?: Maybe<SortOrder>;
+  updatedAt?: Maybe<SortOrder>;
+  name?: Maybe<SortOrder>;
+  code?: Maybe<SortOrder>;
+};
+
 export type ProductFilterParameter = {
   createdAt?: Maybe<DateOperators>;
   updatedAt?: Maybe<DateOperators>;

+ 11 - 2
packages/core/e2e/facet.e2e-spec.ts

@@ -32,7 +32,7 @@ import {
     ASSIGN_PRODUCT_TO_CHANNEL,
     CREATE_CHANNEL,
     CREATE_FACET,
-    GET_FACET_LIST,
+    GET_FACET_LIST, GET_FACET_LIST_SIMPLE,
     GET_PRODUCT_WITH_VARIANTS,
     UPDATE_FACET,
     UPDATE_PRODUCT,
@@ -43,7 +43,7 @@ import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
 // tslint:disable:no-non-null-assertion
 
 describe('Facet resolver', () => {
-    const { server, adminClient } = createTestEnvironment(testConfig);
+    const { server, adminClient, shopClient } = createTestEnvironment(testConfig);
 
     let brandFacet: FacetWithValues.Fragment;
     let speakerTypeFacet: FacetWithValues.Fragment;
@@ -85,6 +85,7 @@ describe('Facet resolver', () => {
             input: {
                 id: speakerTypeFacet.id,
                 translations: [{ languageCode: LanguageCode.en, name: 'Speaker Category' }],
+                isPrivate: true
             },
         });
 
@@ -157,6 +158,14 @@ describe('Facet resolver', () => {
         speakerTypeFacet = items[1];
     });
 
+    it('facets by shop-api', async () => {
+        const result = await shopClient.query<GetFacetList.Query>(GET_FACET_LIST_SIMPLE);
+
+        const { items } = result.facets;
+        expect(items.length).toBe(1);
+        expect(items[0].name).toBe('category');
+    });
+
     it('facet', async () => {
         const result = await adminClient.query<GetFacetWithValues.Query, GetFacetWithValues.Variables>(
             GET_FACET_WITH_VALUES,

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 665 - 518
packages/core/e2e/graphql/generated-e2e-admin-types.ts


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

@@ -39,6 +39,10 @@ export type Query = {
   eligibleShippingMethods: Array<ShippingMethodQuote>;
   /** Returns a list of payment methods and their eligibility based on the current active Order */
   eligiblePaymentMethods: Array<PaymentMethodQuote>;
+  /** A list of Facets available to the shop */
+  facets: FacetList;
+  /** Returns a Facet by its id */
+  facet?: Maybe<Facet>;
   /** Returns information about the current authenticated User */
   me?: Maybe<CurrentUser>;
   /** Returns the possible next states that the activeOrder can transition to */
@@ -75,6 +79,16 @@ export type QueryCollectionArgs = {
 };
 
 
+export type QueryFacetsArgs = {
+  options?: Maybe<FacetListOptions>;
+};
+
+
+export type QueryFacetArgs = {
+  id: Scalars['ID'];
+};
+
+
 export type QueryOrderArgs = {
   id: Scalars['ID'];
 };
@@ -2646,6 +2660,13 @@ export type CollectionListOptions = {
   filter?: Maybe<CollectionFilterParameter>;
 };
 
+export type FacetListOptions = {
+  skip?: Maybe<Scalars['Int']>;
+  take?: Maybe<Scalars['Int']>;
+  sort?: Maybe<FacetSortParameter>;
+  filter?: Maybe<FacetFilterParameter>;
+};
+
 export type OrderListOptions = {
   skip?: Maybe<Scalars['Int']>;
   take?: Maybe<Scalars['Int']>;
@@ -2806,6 +2827,22 @@ export type CollectionSortParameter = {
   description?: Maybe<SortOrder>;
 };
 
+export type FacetFilterParameter = {
+  createdAt?: Maybe<DateOperators>;
+  updatedAt?: Maybe<DateOperators>;
+  languageCode?: Maybe<StringOperators>;
+  name?: Maybe<StringOperators>;
+  code?: Maybe<StringOperators>;
+};
+
+export type FacetSortParameter = {
+  id?: Maybe<SortOrder>;
+  createdAt?: Maybe<SortOrder>;
+  updatedAt?: Maybe<SortOrder>;
+  name?: Maybe<SortOrder>;
+  code?: Maybe<SortOrder>;
+};
+
 export type ProductFilterParameter = {
   createdAt?: Maybe<DateOperators>;
   updatedAt?: Maybe<DateOperators>;

+ 12 - 0
packages/core/e2e/graphql/shared-definitions.ts

@@ -248,6 +248,18 @@ export const GET_FACET_LIST = gql`
     ${FACET_WITH_VALUES_FRAGMENT}
 `;
 
+export const GET_FACET_LIST_SIMPLE = gql`
+    query GetFacetListSimple($options: FacetListOptions) {
+        facets(options: $options) {
+            items {
+                id
+                name
+            }
+            totalItems
+        }
+    }
+`;
+
 export const DELETE_PRODUCT = gql`
     mutation DeleteProduct($id: ID!) {
         deleteProduct(id: $id) {

+ 32 - 1
packages/core/src/api/resolvers/shop/shop-products.resolver.ts

@@ -2,6 +2,8 @@ import { Args, Query, Resolver } from '@nestjs/graphql';
 import {
     QueryCollectionArgs,
     QueryCollectionsArgs,
+    QueryFacetArgs,
+    QueryFacetsArgs,
     QueryProductArgs,
     SearchResponse,
 } from '@vendure/common/lib/generated-shop-types';
@@ -13,8 +15,9 @@ import { InternalServerError, UserInputError } from '../../../common/error/error
 import { ListQueryOptions } from '../../../common/types/common-types';
 import { Translated } from '../../../common/types/locale-types';
 import { Collection } from '../../../entity/collection/collection.entity';
+import { Facet } from '../../../entity/facet/facet.entity';
 import { Product } from '../../../entity/product/product.entity';
-import { CollectionService } from '../../../service';
+import { CollectionService, FacetService } from '../../../service';
 import { FacetValueService } from '../../../service/services/facet-value.service';
 import { ProductVariantService } from '../../../service/services/product-variant.service';
 import { ProductService } from '../../../service/services/product.service';
@@ -28,6 +31,7 @@ export class ShopProductsResolver {
         private productVariantService: ProductVariantService,
         private facetValueService: FacetValueService,
         private collectionService: CollectionService,
+        private facetService: FacetService,
     ) {}
 
     @Query()
@@ -109,4 +113,31 @@ export class ShopProductsResolver {
     async search(...args: any): Promise<Omit<SearchResponse, 'facetValues'>> {
         throw new InternalServerError(`error.no-search-plugin-configured`);
     }
+
+    @Query()
+    async facets(
+        @Ctx() ctx: RequestContext,
+        @Args() args: QueryFacetsArgs,
+    ): Promise<PaginatedList<Translated<Facet>>> {
+        const options: ListQueryOptions<Facet> = {
+            ...args.options,
+            filter: {
+                ...(args.options && args.options.filter),
+                isPrivate: { eq: false },
+            },
+        };
+        return this.facetService.findAll(ctx, options || undefined);
+    }
+
+    @Query()
+    async facet(
+        @Ctx() ctx: RequestContext,
+        @Args() args: QueryFacetArgs,
+    ): Promise<Translated<Facet> | undefined> {
+        const facet = await this.facetService.findOne(ctx, args.id);
+        if (facet && facet.isPrivate) {
+            return;
+        }
+        return facet;
+    }
 }

+ 7 - 0
packages/core/src/api/schema/shop-api/shop.api.graphql

@@ -19,6 +19,10 @@ type Query {
     eligibleShippingMethods: [ShippingMethodQuote!]!
     "Returns a list of payment methods and their eligibility based on the current active Order"
     eligiblePaymentMethods: [PaymentMethodQuote!]!
+    "A list of Facets available to the shop"
+    facets(options: FacetListOptions): FacetList!
+    "Returns a Facet by its id"
+    facet(id: ID!): Facet
     "Returns information about the current authenticated User"
     me: CurrentUser
     "Returns the possible next states that the activeOrder can transition to"
@@ -174,6 +178,9 @@ input PaymentInput {
 # generated by generateListOptions function
 input CollectionListOptions
 
+# generated by generateListOptions function
+input FacetListOptions
+
 # generated by generateListOptions function
 input OrderListOptions
 

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 0 - 0
schema-shop.json


Vissa filer visades inte eftersom för många filer har ändrats