Skip to content

Commit

Permalink
feat(core): Add productVariant query to Admin API
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Aug 13, 2020
1 parent 3bb4eb8 commit 72b6ccd
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2887,6 +2887,8 @@ export type Query = {
products: ProductList;
/** Get a Product either by id or slug. If neither id nor slug is speicified, an error will result. */
product?: Maybe<Product>;
/** Get a ProductVariant by id */
productVariant?: Maybe<ProductVariant>;
promotion?: Maybe<Promotion>;
promotions: PromotionList;
promotionConditions: Array<ConfigurableOperationDefinition>;
Expand Down Expand Up @@ -3017,6 +3019,10 @@ export type QueryProductArgs = {
slug?: Maybe<Scalars['String']>;
};

export type QueryProductVariantArgs = {
id: Scalars['ID'];
};

export type QueryPromotionArgs = {
id: Scalars['ID'];
};
Expand Down
7 changes: 7 additions & 0 deletions packages/common/src/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2976,6 +2976,8 @@ export type Query = {
products: ProductList;
/** Get a Product either by id or slug. If neither id nor slug is speicified, an error will result. */
product?: Maybe<Product>;
/** Get a ProductVariant by id */
productVariant?: Maybe<ProductVariant>;
promotion?: Maybe<Promotion>;
promotions: PromotionList;
promotionConditions: Array<ConfigurableOperationDefinition>;
Expand Down Expand Up @@ -3134,6 +3136,11 @@ export type QueryProductArgs = {
};


export type QueryProductVariantArgs = {
id: Scalars['ID'];
};


export type QueryPromotionArgs = {
id: Scalars['ID'];
};
Expand Down
20 changes: 20 additions & 0 deletions packages/core/e2e/graphql/generated-e2e-admin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2887,6 +2887,8 @@ export type Query = {
products: ProductList;
/** Get a Product either by id or slug. If neither id nor slug is speicified, an error will result. */
product?: Maybe<Product>;
/** Get a ProductVariant by id */
productVariant?: Maybe<ProductVariant>;
promotion?: Maybe<Promotion>;
promotions: PromotionList;
promotionConditions: Array<ConfigurableOperationDefinition>;
Expand Down Expand Up @@ -3017,6 +3019,10 @@ export type QueryProductArgs = {
slug?: Maybe<Scalars['String']>;
};

export type QueryProductVariantArgs = {
id: Scalars['ID'];
};

export type QueryPromotionArgs = {
id: Scalars['ID'];
};
Expand Down Expand Up @@ -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 = {
id: Scalars['ID'];
};
Expand Down Expand Up @@ -6955,6 +6969,12 @@ export namespace GetOptionGroup {
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 type Variables = DeletePromotionMutationVariables;
export type Mutation = DeletePromotionMutation;
Expand Down
34 changes: 34 additions & 0 deletions packages/core/e2e/product.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
GetOptionGroup,
GetProductList,
GetProductSimple,
GetProductVariant,
GetProductWithVariants,
LanguageCode,
ProductWithVariants,
Expand Down Expand Up @@ -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', () => {
let newProduct: ProductWithVariants.Fragment;
let newProductWithAssets: ProductWithVariants.Fragment;
Expand Down Expand Up @@ -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 changes: 10 additions & 0 deletions packages/core/src/api/resolvers/admin/product.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Permission,
QueryProductArgs,
QueryProductsArgs,
QueryProductVariantArgs,
} from '@vendure/common/lib/generated-types';
import { PaginatedList } from '@vendure/common/lib/shared-types';

Expand Down Expand Up @@ -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()
@Allow(Permission.CreateCatalog)
async createProduct(
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/api/schema/admin-api/product.api.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ type Query {
products(options: ProductListOptions): ProductList!
"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
"Get a ProductVariant by id"
productVariant(id: ID!): ProductVariant
}

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

export type QueryProductVariantArgs = {
id: Scalars['ID'];
};

export type QueryPromotionArgs = {
id: Scalars['ID'];
};
Expand Down

0 comments on commit 72b6ccd

Please sign in to comment.