Skip to content

Commit

Permalink
feat(core): Add ProductVariant.product field & resolver
Browse files Browse the repository at this point in the history
Relates to #378
  • Loading branch information
michaelbromley committed Jul 6, 2020
1 parent 6836b71 commit 0334848
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 40 deletions.
7 changes: 6 additions & 1 deletion packages/admin-ui/src/lib/core/src/common/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1786,6 +1786,7 @@ export type Mutation = {
assignProductsToChannel: Array<Product>;
/** Assign a Role to an Administrator */
assignRoleToAdministrator: Administrator;
/** Authenticates the user using a named authentication strategy */
authenticate: LoginResult;
cancelOrder: Order;
/** Create a new Administrator */
Expand Down Expand Up @@ -1868,7 +1869,10 @@ export type Mutation = {
deleteZone: DeletionResponse;
fulfillOrder: Fulfillment;
importProducts?: Maybe<ImportInfo>;
/** @deprecated Use `authenticate` mutation with the 'native' strategy instead. */
/**
* Authenticates the user using the native authentication strategy. This mutation
* is an alias for `authenticate({ native: { ... }})`
*/
login: LoginResult;
logout: Scalars['Boolean'];
/** Move a Collection to a different parent or index */
Expand Down Expand Up @@ -2811,6 +2815,7 @@ export type ProductVariant = Node & {
trackInventory: Scalars['Boolean'];
stockMovements: StockMovementList;
id: Scalars['ID'];
product: Product;
productId: Scalars['ID'];
createdAt: Scalars['DateTime'];
updatedAt: Scalars['DateTime'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ const result: IntrospectionResultData = {
name: 'Return',
},
{
name: 'TaxRate',
name: 'Product',
},
{
name: 'TaxCategory',
name: 'ProductOptionGroup',
},
{
name: 'ProductOption',
Expand All @@ -112,16 +112,16 @@ const result: IntrospectionResultData = {
name: 'Facet',
},
{
name: 'Job',
name: 'TaxRate',
},
{
name: 'PaymentMethod',
name: 'TaxCategory',
},
{
name: 'Product',
name: 'Job',
},
{
name: 'ProductOptionGroup',
name: 'PaymentMethod',
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,12 @@ export type Mutation = {
deleteAsset: DeletionResponse;
/** Delete multiple Assets */
deleteAssets: DeletionResponse;
/**
* Authenticates the user using the native authentication strategy. This mutation
* is an alias for `authenticate({ native: { ... }})`
*/
login: LoginResult;
/** Authenticates the user using a named authentication strategy */
authenticate: LoginResult;
logout: Scalars['Boolean'];
/** Create a new Channel */
Expand Down Expand Up @@ -2684,6 +2689,7 @@ export type ProductVariant = Node & {
trackInventory: Scalars['Boolean'];
stockMovements: StockMovementList;
id: Scalars['ID'];
product: Product;
productId: Scalars['ID'];
createdAt: Scalars['DateTime'];
updatedAt: Scalars['DateTime'];
Expand Down
7 changes: 6 additions & 1 deletion packages/common/src/generated-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1356,8 +1356,12 @@ export type Mutation = {
setOrderShippingMethod?: Maybe<Order>;
addPaymentToOrder?: Maybe<Order>;
setCustomerForOrder?: Maybe<Order>;
/** @deprecated Use `authenticate` mutation with the 'native' strategy instead. */
/**
* Authenticates the user using the native authentication strategy. This mutation
* is an alias for `authenticate({ native: { ... }})`
*/
login: LoginResult;
/** Authenticates the user using a named authentication strategy */
authenticate: LoginResult;
logout: Scalars['Boolean'];
/**
Expand Down Expand Up @@ -1857,6 +1861,7 @@ export type ProductTranslation = {
export type ProductVariant = Node & {
__typename?: 'ProductVariant';
id: Scalars['ID'];
product: Product;
productId: Scalars['ID'];
createdAt: Scalars['DateTime'];
updatedAt: Scalars['DateTime'];
Expand Down
14 changes: 10 additions & 4 deletions packages/common/src/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ export type CreateZoneInput = {
/**
* @description
* ISO 4217 currency code
*
*
* @docsCategory common
*/
export enum CurrencyCode {
Expand Down Expand Up @@ -1393,7 +1393,7 @@ export type JobSortParameter = {
/**
* @description
* The state of a Job in the JobQueue
*
*
* @docsCategory common
*/
export enum JobState {
Expand All @@ -1411,7 +1411,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 {
Expand Down Expand Up @@ -1782,7 +1782,12 @@ export type Mutation = {
deleteAsset: DeletionResponse;
/** Delete multiple Assets */
deleteAssets: DeletionResponse;
/**
* Authenticates the user using the native authentication strategy. This mutation
* is an alias for `authenticate({ native: { ... }})`
*/
login: LoginResult;
/** Authenticates the user using a named authentication strategy */
authenticate: LoginResult;
logout: Scalars['Boolean'];
/** Create a new Channel */
Expand Down Expand Up @@ -2587,7 +2592,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 {
Expand Down Expand Up @@ -2770,6 +2775,7 @@ export type ProductVariant = Node & {
trackInventory: Scalars['Boolean'];
stockMovements: StockMovementList;
id: Scalars['ID'];
product: Product;
productId: Scalars['ID'];
createdAt: Scalars['DateTime'];
updatedAt: Scalars['DateTime'];
Expand Down
15 changes: 15 additions & 0 deletions packages/core/e2e/graphql/generated-e2e-admin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,12 @@ export type Mutation = {
deleteAsset: DeletionResponse;
/** Delete multiple Assets */
deleteAssets: DeletionResponse;
/**
* Authenticates the user using the native authentication strategy. This mutation
* is an alias for `authenticate({ native: { ... }})`
*/
login: LoginResult;
/** Authenticates the user using a named authentication strategy */
authenticate: LoginResult;
logout: Scalars['Boolean'];
/** Create a new Channel */
Expand Down Expand Up @@ -2684,6 +2689,7 @@ export type ProductVariant = Node & {
trackInventory: Scalars['Boolean'];
stockMovements: StockMovementList;
id: Scalars['ID'];
product: Product;
productId: Scalars['ID'];
createdAt: Scalars['DateTime'];
updatedAt: Scalars['DateTime'];
Expand Down Expand Up @@ -5406,6 +5412,10 @@ export type DeleteRoleMutation = { __typename?: 'Mutation' } & {
deleteRole: { __typename?: 'DeletionResponse' } & Pick<DeletionResponse, 'result' | 'message'>;
};

export type LogoutMutationVariables = {};

export type LogoutMutation = { __typename?: 'Mutation' } & Pick<Mutation, 'logout'>;

export type ShippingMethodFragment = { __typename?: 'ShippingMethod' } & Pick<
ShippingMethod,
'id' | 'code' | 'description'
Expand Down Expand Up @@ -6955,6 +6965,11 @@ export namespace DeleteRole {
export type DeleteRole = DeleteRoleMutation['deleteRole'];
}

export namespace Logout {
export type Variables = LogoutMutationVariables;
export type Mutation = LogoutMutation;
}

export namespace ShippingMethod {
export type Fragment = ShippingMethodFragment;
export type Calculator = ShippingMethodFragment['calculator'];
Expand Down
7 changes: 6 additions & 1 deletion packages/core/e2e/graphql/generated-e2e-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1356,8 +1356,12 @@ export type Mutation = {
setOrderShippingMethod?: Maybe<Order>;
addPaymentToOrder?: Maybe<Order>;
setCustomerForOrder?: Maybe<Order>;
/** @deprecated Use `authenticate` mutation with the 'native' strategy instead. */
/**
* Authenticates the user using the native authentication strategy. This mutation
* is an alias for `authenticate({ native: { ... }})`
*/
login: LoginResult;
/** Authenticates the user using a named authentication strategy */
authenticate: LoginResult;
logout: Scalars['Boolean'];
/**
Expand Down Expand Up @@ -1857,6 +1861,7 @@ export type ProductTranslation = {
export type ProductVariant = Node & {
__typename?: 'ProductVariant';
id: Scalars['ID'];
product: Product;
productId: Scalars['ID'];
createdAt: Scalars['DateTime'];
updatedAt: Scalars['DateTime'];
Expand Down
2 changes: 1 addition & 1 deletion packages/core/e2e/session-management.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('Session caching', () => {

await adminClient.query(
gql`
mutation {
mutation Logout {
logout
}
`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Parent, ResolveField, Resolver } from '@nestjs/graphql';

import { Translated } from '../../../common/types/locale-types';
import { assertFound } from '../../../common/utils';
import { Asset, OrderLine, ProductVariant } from '../../../entity';
import { AssetService, ProductVariantService } from '../../../service';
import { RequestContext } from '../../common/request-context';
Expand All @@ -15,8 +13,11 @@ export class OrderLineEntityResolver {
async productVariant(
@Ctx() ctx: RequestContext,
@Parent() orderLine: OrderLine,
): Promise<Translated<ProductVariant>> {
return assertFound(this.productVariantService.findOne(ctx, orderLine.productVariant.id));
): Promise<ProductVariant> {
if (orderLine.productVariant) {
return orderLine.productVariant;
}
return this.productVariantService.getVariantByOrderLineId(ctx, orderLine.id);
}

@ResolveField()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { StockMovementListOptions } from '@vendure/common/lib/generated-types';
import { PaginatedList } from '@vendure/common/lib/shared-types';

import { Translated } from '../../../common/types/locale-types';
import { Asset, FacetValue, ProductOption } from '../../../entity';
import { Asset, FacetValue, Product, ProductOption } from '../../../entity';
import { ProductVariant } from '../../../entity/product-variant/product-variant.entity';
import { StockMovement } from '../../../entity/stock-movement/stock-movement.entity';
import { AssetService } from '../../../service/services/asset.service';
Expand All @@ -18,6 +18,17 @@ import { Ctx } from '../../decorators/request-context.decorator';
export class ProductVariantEntityResolver {
constructor(private productVariantService: ProductVariantService, private assetService: AssetService) {}

@ResolveField()
async product(
@Ctx() ctx: RequestContext,
@Parent() productVariant: ProductVariant,
): Promise<Product | undefined> {
if (productVariant.product) {
return productVariant.product;
}
return this.productVariantService.getProductForVariant(ctx, productVariant);
}

@ResolveField()
async assets(
@Ctx() ctx: RequestContext,
Expand Down Expand Up @@ -61,7 +72,7 @@ export class ProductVariantEntityResolver {
facetValues = await this.productVariantService.getFacetValuesForVariant(ctx, productVariant.id);
}
if (apiType === 'shop') {
facetValues = facetValues.filter(fv => !fv.facet.isPrivate);
facetValues = facetValues.filter((fv) => !fv.facet.isPrivate);
}
return facetValues;
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/api/schema/type/product.type.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type ProductList implements PaginatedList {

type ProductVariant implements Node {
id: ID!
product: Product!
productId: ID!
createdAt: DateTime!
updatedAt: DateTime!
Expand Down
Loading

0 comments on commit 0334848

Please sign in to comment.