Browse Source

feat(core): Add `productVariant` query to Admin API

Michael Bromley 5 years ago
parent
commit
72b6ccd2a6

+ 6 - 0
packages/asset-server-plugin/e2e/graphql/generated-e2e-asset-server-plugin-types.ts

@@ -2887,6 +2887,8 @@ export type Query = {
     products: ProductList;
     products: ProductList;
     /** Get a Product either by id or slug. If neither id nor slug is speicified, an error will result. */
     /** Get a Product either by id or slug. If neither id nor slug is speicified, an error will result. */
     product?: Maybe<Product>;
     product?: Maybe<Product>;
+    /** Get a ProductVariant by id */
+    productVariant?: Maybe<ProductVariant>;
     promotion?: Maybe<Promotion>;
     promotion?: Maybe<Promotion>;
     promotions: PromotionList;
     promotions: PromotionList;
     promotionConditions: Array<ConfigurableOperationDefinition>;
     promotionConditions: Array<ConfigurableOperationDefinition>;
@@ -3017,6 +3019,10 @@ export type QueryProductArgs = {
     slug?: Maybe<Scalars['String']>;
     slug?: Maybe<Scalars['String']>;
 };
 };
 
 
+export type QueryProductVariantArgs = {
+    id: Scalars['ID'];
+};
+
 export type QueryPromotionArgs = {
 export type QueryPromotionArgs = {
     id: Scalars['ID'];
     id: Scalars['ID'];
 };
 };

+ 7 - 0
packages/common/src/generated-types.ts

@@ -2976,6 +2976,8 @@ export type Query = {
   products: ProductList;
   products: ProductList;
   /** Get a Product either by id or slug. If neither id nor slug is speicified, an error will result. */
   /** Get a Product either by id or slug. If neither id nor slug is speicified, an error will result. */
   product?: Maybe<Product>;
   product?: Maybe<Product>;
+  /** Get a ProductVariant by id */
+  productVariant?: Maybe<ProductVariant>;
   promotion?: Maybe<Promotion>;
   promotion?: Maybe<Promotion>;
   promotions: PromotionList;
   promotions: PromotionList;
   promotionConditions: Array<ConfigurableOperationDefinition>;
   promotionConditions: Array<ConfigurableOperationDefinition>;
@@ -3134,6 +3136,11 @@ export type QueryProductArgs = {
 };
 };
 
 
 
 
+export type QueryProductVariantArgs = {
+  id: Scalars['ID'];
+};
+
+
 export type QueryPromotionArgs = {
 export type QueryPromotionArgs = {
   id: Scalars['ID'];
   id: Scalars['ID'];
 };
 };

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

@@ -2887,6 +2887,8 @@ export type Query = {
     products: ProductList;
     products: ProductList;
     /** Get a Product either by id or slug. If neither id nor slug is speicified, an error will result. */
     /** Get a Product either by id or slug. If neither id nor slug is speicified, an error will result. */
     product?: Maybe<Product>;
     product?: Maybe<Product>;
+    /** Get a ProductVariant by id */
+    productVariant?: Maybe<ProductVariant>;
     promotion?: Maybe<Promotion>;
     promotion?: Maybe<Promotion>;
     promotions: PromotionList;
     promotions: PromotionList;
     promotionConditions: Array<ConfigurableOperationDefinition>;
     promotionConditions: Array<ConfigurableOperationDefinition>;
@@ -3017,6 +3019,10 @@ export type QueryProductArgs = {
     slug?: Maybe<Scalars['String']>;
     slug?: Maybe<Scalars['String']>;
 };
 };
 
 
+export type QueryProductVariantArgs = {
+    id: Scalars['ID'];
+};
+
 export type QueryPromotionArgs = {
 export type QueryPromotionArgs = {
     id: Scalars['ID'];
     id: Scalars['ID'];
 };
 };
@@ -5370,6 +5376,14 @@ export type GetOptionGroupQuery = { __typename?: 'Query' } & {
     >;
     >;
 };
 };
 
 
+export type GetProductVariantQueryVariables = {
+    id: Scalars['ID'];
+};
+
+export type GetProductVariantQuery = { __typename?: 'Query' } & {
+    productVariant?: Maybe<{ __typename?: 'ProductVariant' } & Pick<ProductVariant, 'id' | 'name'>>;
+};
+
 export type DeletePromotionMutationVariables = {
 export type DeletePromotionMutationVariables = {
     id: Scalars['ID'];
     id: Scalars['ID'];
 };
 };
@@ -6955,6 +6969,12 @@ export namespace GetOptionGroup {
     export type Options = NonNullable<NonNullable<GetOptionGroupQuery['productOptionGroup']>['options'][0]>;
     export type Options = NonNullable<NonNullable<GetOptionGroupQuery['productOptionGroup']>['options'][0]>;
 }
 }
 
 
+export namespace GetProductVariant {
+    export type Variables = GetProductVariantQueryVariables;
+    export type Query = GetProductVariantQuery;
+    export type ProductVariant = NonNullable<GetProductVariantQuery['productVariant']>;
+}
+
 export namespace DeletePromotion {
 export namespace DeletePromotion {
     export type Variables = DeletePromotionMutationVariables;
     export type Variables = DeletePromotionMutationVariables;
     export type Mutation = DeletePromotionMutation;
     export type Mutation = DeletePromotionMutation;

+ 34 - 0
packages/core/e2e/product.e2e-spec.ts

@@ -19,6 +19,7 @@ import {
     GetOptionGroup,
     GetOptionGroup,
     GetProductList,
     GetProductList,
     GetProductSimple,
     GetProductSimple,
+    GetProductVariant,
     GetProductWithVariants,
     GetProductWithVariants,
     LanguageCode,
     LanguageCode,
     ProductWithVariants,
     ProductWithVariants,
@@ -312,6 +313,30 @@ describe('Product resolver', () => {
         });
         });
     });
     });
 
 
+    describe('productVariant query', () => {
+        it('by id', async () => {
+            const { productVariant } = await adminClient.query<
+                GetProductVariant.Query,
+                GetProductVariant.Variables
+            >(GET_PRODUCT_VARIANT, {
+                id: 'T_1',
+            });
+
+            expect(productVariant?.id).toBe('T_1');
+            expect(productVariant?.name).toBe('Laptop 13 inch 8GB');
+        });
+        it('returns null when id not found', async () => {
+            const { productVariant } = await adminClient.query<
+                GetProductVariant.Query,
+                GetProductVariant.Variables
+            >(GET_PRODUCT_VARIANT, {
+                id: 'T_999',
+            });
+
+            expect(productVariant).toBeNull();
+        });
+    });
+
     describe('product mutation', () => {
     describe('product mutation', () => {
         let newProduct: ProductWithVariants.Fragment;
         let newProduct: ProductWithVariants.Fragment;
         let newProductWithAssets: ProductWithVariants.Fragment;
         let newProductWithAssets: ProductWithVariants.Fragment;
@@ -1149,3 +1174,12 @@ export const GET_OPTION_GROUP = gql`
         }
         }
     }
     }
 `;
 `;
+
+export const GET_PRODUCT_VARIANT = gql`
+    query GetProductVariant($id: ID!) {
+        productVariant(id: $id) {
+            id
+            name
+        }
+    }
+`;

+ 10 - 0
packages/core/src/api/resolvers/admin/product.resolver.ts

@@ -14,6 +14,7 @@ import {
     Permission,
     Permission,
     QueryProductArgs,
     QueryProductArgs,
     QueryProductsArgs,
     QueryProductsArgs,
+    QueryProductVariantArgs,
 } from '@vendure/common/lib/generated-types';
 } from '@vendure/common/lib/generated-types';
 import { PaginatedList } from '@vendure/common/lib/shared-types';
 import { PaginatedList } from '@vendure/common/lib/shared-types';
 
 
@@ -64,6 +65,15 @@ export class ProductResolver {
         }
         }
     }
     }
 
 
+    @Query()
+    @Allow(Permission.ReadCatalog)
+    async productVariant(
+        @Ctx() ctx: RequestContext,
+        @Args() args: QueryProductVariantArgs,
+    ): Promise<Translated<ProductVariant> | undefined> {
+        return this.productVariantService.findOne(ctx, args.id);
+    }
+
     @Mutation()
     @Mutation()
     @Allow(Permission.CreateCatalog)
     @Allow(Permission.CreateCatalog)
     async createProduct(
     async createProduct(

+ 2 - 0
packages/core/src/api/schema/admin-api/product.api.graphql

@@ -2,6 +2,8 @@ type Query {
     products(options: ProductListOptions): ProductList!
     products(options: ProductListOptions): ProductList!
     "Get a Product either by id or slug. If neither id nor slug is speicified, an error will result."
     "Get a Product either by id or slug. If neither id nor slug is speicified, an error will result."
     product(id: ID, slug: String): Product
     product(id: ID, slug: String): Product
+    "Get a ProductVariant by id"
+    productVariant(id: ID!): ProductVariant
 }
 }
 
 
 type Mutation {
 type Mutation {

+ 6 - 0
packages/elasticsearch-plugin/e2e/graphql/generated-e2e-elasticsearch-plugin-types.ts

@@ -2887,6 +2887,8 @@ export type Query = {
     products: ProductList;
     products: ProductList;
     /** Get a Product either by id or slug. If neither id nor slug is speicified, an error will result. */
     /** Get a Product either by id or slug. If neither id nor slug is speicified, an error will result. */
     product?: Maybe<Product>;
     product?: Maybe<Product>;
+    /** Get a ProductVariant by id */
+    productVariant?: Maybe<ProductVariant>;
     promotion?: Maybe<Promotion>;
     promotion?: Maybe<Promotion>;
     promotions: PromotionList;
     promotions: PromotionList;
     promotionConditions: Array<ConfigurableOperationDefinition>;
     promotionConditions: Array<ConfigurableOperationDefinition>;
@@ -3017,6 +3019,10 @@ export type QueryProductArgs = {
     slug?: Maybe<Scalars['String']>;
     slug?: Maybe<Scalars['String']>;
 };
 };
 
 
+export type QueryProductVariantArgs = {
+    id: Scalars['ID'];
+};
+
 export type QueryPromotionArgs = {
 export type QueryPromotionArgs = {
     id: Scalars['ID'];
     id: Scalars['ID'];
 };
 };