Skip to content

Commit

Permalink
feat(core): TaxCategories query now returns PaginatedList
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The `taxCategories` mutation now returns a PaginatedList rather than
a simple array of TaxCategory objects.
  • Loading branch information
michaelbromley committed May 3, 2023
1 parent d7a3447 commit ddcd0fc
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4483,7 +4483,7 @@ export type Query = {
stockLocations: StockLocationList;
tag: Tag;
tags: TagList;
taxCategories: Array<TaxCategory>;
taxCategories: TaxCategoryList;
taxCategory?: Maybe<TaxCategory>;
taxRate?: Maybe<TaxRate>;
taxRates: TaxRateList;
Expand Down Expand Up @@ -4689,6 +4689,10 @@ export type QueryTagsArgs = {
options?: InputMaybe<TagListOptions>;
};

export type QueryTaxCategoriesArgs = {
options?: InputMaybe<TaxCategoryListOptions>;
};

export type QueryTaxCategoryArgs = {
id: Scalars['ID'];
};
Expand Down Expand Up @@ -5379,6 +5383,39 @@ export type TaxCategory = Node & {
updatedAt: Scalars['DateTime'];
};

export type TaxCategoryFilterParameter = {
createdAt?: InputMaybe<DateOperators>;
id?: InputMaybe<IdOperators>;
isDefault?: InputMaybe<BooleanOperators>;
name?: InputMaybe<StringOperators>;
updatedAt?: InputMaybe<DateOperators>;
};

export type TaxCategoryList = PaginatedList & {
items: Array<TaxCategory>;
totalItems: Scalars['Int'];
};

export type TaxCategoryListOptions = {
/** Allows the results to be filtered */
filter?: InputMaybe<TaxCategoryFilterParameter>;
/** Specifies whether multiple "filter" arguments should be combines with a logical AND or OR operation. Defaults to AND. */
filterOperator?: InputMaybe<LogicalOperator>;
/** Skips the first n results, for use in pagination */
skip?: InputMaybe<Scalars['Int']>;
/** Specifies which properties to sort the results by */
sort?: InputMaybe<TaxCategorySortParameter>;
/** Takes n results, for use in pagination */
take?: InputMaybe<Scalars['Int']>;
};

export type TaxCategorySortParameter = {
createdAt?: InputMaybe<SortOrder>;
id?: InputMaybe<SortOrder>;
name?: InputMaybe<SortOrder>;
updatedAt?: InputMaybe<SortOrder>;
};

export type TaxLine = {
description: Scalars['String'];
taxRate: Scalars['Float'];
Expand Down
41 changes: 40 additions & 1 deletion packages/common/src/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4712,7 +4712,7 @@ export type Query = {
stockLocations: StockLocationList;
tag: Tag;
tags: TagList;
taxCategories: Array<TaxCategory>;
taxCategories: TaxCategoryList;
taxCategory?: Maybe<TaxCategory>;
taxRate?: Maybe<TaxRate>;
taxRates: TaxRateList;
Expand Down Expand Up @@ -4967,6 +4967,11 @@ export type QueryTagsArgs = {
};


export type QueryTaxCategoriesArgs = {
options?: InputMaybe<TaxCategoryListOptions>;
};


export type QueryTaxCategoryArgs = {
id: Scalars['ID'];
};
Expand Down Expand Up @@ -5679,6 +5684,40 @@ export type TaxCategory = Node & {
updatedAt: Scalars['DateTime'];
};

export type TaxCategoryFilterParameter = {
createdAt?: InputMaybe<DateOperators>;
id?: InputMaybe<IdOperators>;
isDefault?: InputMaybe<BooleanOperators>;
name?: InputMaybe<StringOperators>;
updatedAt?: InputMaybe<DateOperators>;
};

export type TaxCategoryList = PaginatedList & {
__typename?: 'TaxCategoryList';
items: Array<TaxCategory>;
totalItems: Scalars['Int'];
};

export type TaxCategoryListOptions = {
/** Allows the results to be filtered */
filter?: InputMaybe<TaxCategoryFilterParameter>;
/** Specifies whether multiple "filter" arguments should be combines with a logical AND or OR operation. Defaults to AND. */
filterOperator?: InputMaybe<LogicalOperator>;
/** Skips the first n results, for use in pagination */
skip?: InputMaybe<Scalars['Int']>;
/** Specifies which properties to sort the results by */
sort?: InputMaybe<TaxCategorySortParameter>;
/** Takes n results, for use in pagination */
take?: InputMaybe<Scalars['Int']>;
};

export type TaxCategorySortParameter = {
createdAt?: InputMaybe<SortOrder>;
id?: InputMaybe<SortOrder>;
name?: InputMaybe<SortOrder>;
updatedAt?: InputMaybe<SortOrder>;
};

export type TaxLine = {
__typename?: 'TaxLine';
description: Scalars['String'];
Expand Down
41 changes: 39 additions & 2 deletions packages/core/e2e/graphql/generated-e2e-admin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4483,7 +4483,7 @@ export type Query = {
stockLocations: StockLocationList;
tag: Tag;
tags: TagList;
taxCategories: Array<TaxCategory>;
taxCategories: TaxCategoryList;
taxCategory?: Maybe<TaxCategory>;
taxRate?: Maybe<TaxRate>;
taxRates: TaxRateList;
Expand Down Expand Up @@ -4689,6 +4689,10 @@ export type QueryTagsArgs = {
options?: InputMaybe<TagListOptions>;
};

export type QueryTaxCategoriesArgs = {
options?: InputMaybe<TaxCategoryListOptions>;
};

export type QueryTaxCategoryArgs = {
id: Scalars['ID'];
};
Expand Down Expand Up @@ -5379,6 +5383,39 @@ export type TaxCategory = Node & {
updatedAt: Scalars['DateTime'];
};

export type TaxCategoryFilterParameter = {
createdAt?: InputMaybe<DateOperators>;
id?: InputMaybe<IdOperators>;
isDefault?: InputMaybe<BooleanOperators>;
name?: InputMaybe<StringOperators>;
updatedAt?: InputMaybe<DateOperators>;
};

export type TaxCategoryList = PaginatedList & {
items: Array<TaxCategory>;
totalItems: Scalars['Int'];
};

export type TaxCategoryListOptions = {
/** Allows the results to be filtered */
filter?: InputMaybe<TaxCategoryFilterParameter>;
/** Specifies whether multiple "filter" arguments should be combines with a logical AND or OR operation. Defaults to AND. */
filterOperator?: InputMaybe<LogicalOperator>;
/** Skips the first n results, for use in pagination */
skip?: InputMaybe<Scalars['Int']>;
/** Specifies which properties to sort the results by */
sort?: InputMaybe<TaxCategorySortParameter>;
/** Takes n results, for use in pagination */
take?: InputMaybe<Scalars['Int']>;
};

export type TaxCategorySortParameter = {
createdAt?: InputMaybe<SortOrder>;
id?: InputMaybe<SortOrder>;
name?: InputMaybe<SortOrder>;
updatedAt?: InputMaybe<SortOrder>;
};

export type TaxLine = {
description: Scalars['String'];
taxRate: Scalars['Float'];
Expand Down Expand Up @@ -11231,7 +11268,7 @@ export type DeleteTagMutation = { deleteTag: { message?: string | null; result:
export type GetTaxCategoryListQueryVariables = Exact<{ [key: string]: never }>;

export type GetTaxCategoryListQuery = {
taxCategories: Array<{ id: string; name: string; isDefault: boolean }>;
taxCategories: { items: Array<{ id: string; name: string; isDefault: boolean }> };
};

export type GetTaxCategoryQueryVariables = Exact<{
Expand Down
8 changes: 5 additions & 3 deletions packages/core/e2e/tax-category.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ describe('TaxCategory resolver', () => {
const GET_TAX_CATEGORY_LIST = gql`
query GetTaxCategoryList {
taxCategories {
id
name
isDefault
items {
id
name
isDefault
}
}
}
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import {
MutationDeleteTaxCategoryArgs,
MutationUpdateTaxCategoryArgs,
Permission,
QueryTaxCategoriesArgs,
QueryTaxCategoryArgs,
} from '@vendure/common/lib/generated-types';
import { PaginatedList } from '@vendure/common/lib/shared-types';

import { TaxCategory } from '../../../entity/tax-category/tax-category.entity';
import { TaxCategoryService } from '../../../service/services/tax-category.service';
Expand All @@ -26,8 +28,11 @@ export class TaxCategoryResolver {
Permission.ReadProduct,
Permission.ReadTaxCategory,
)
async taxCategories(@Ctx() ctx: RequestContext): Promise<TaxCategory[]> {
return this.taxCategoryService.findAll(ctx);
async taxCategories(
@Ctx() ctx: RequestContext,
@Args() args: QueryTaxCategoriesArgs,
): Promise<PaginatedList<TaxCategory>> {
return this.taxCategoryService.findAll(ctx, args.options || undefined);
}

@Query()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
type Query {
taxCategories: [TaxCategory!]!
taxCategories(options: TaxCategoryListOptions): TaxCategoryList!
taxCategory(id: ID!): TaxCategory
}

type TaxCategoryList implements PaginatedList {
items: [TaxCategory!]!
totalItems: Int!
}

input TaxCategoryListOptions

type Mutation {
"Create a new TaxCategory"
createTaxCategory(input: CreateTaxCategoryInput!): TaxCategory!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export class Importer {
featuredAssetId: variantAssets.length ? variantAssets[0].id : undefined,
assetIds: variantAssets.map(a => a.id),
sku: variant.sku,
taxCategoryId: this.getMatchingTaxCategoryId(variant.taxCategory, taxCategories),
taxCategoryId: this.getMatchingTaxCategoryId(variant.taxCategory, taxCategories.items),
stockOnHand: variant.stockOnHand,
trackInventory: variant.trackInventory,
optionIds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ export class ProductVariantService {
taxCategory = await this.connection.getEntityOrThrow(ctx, TaxCategory, taxCategoryId);
} else {
const taxCategories = await this.taxCategoryService.findAll(ctx);
taxCategory = taxCategories.find(t => t.isDefault === true) ?? taxCategories[0];
taxCategory = taxCategories.items.find(t => t.isDefault === true) ?? taxCategories.items[0];
}
if (!taxCategory) {
// there is no TaxCategory set up, so create a default
Expand Down
24 changes: 20 additions & 4 deletions packages/core/src/service/services/tax-category.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import {
DeletionResult,
UpdateTaxCategoryInput,
} from '@vendure/common/lib/generated-types';
import { ID } from '@vendure/common/lib/shared-types';
import { ID, PaginatedList } from '@vendure/common/lib/shared-types';

import { RequestContext } from '../../api/common/request-context';
import { EntityNotFoundError } from '../../common/error/errors';
import { ListQueryOptions } from '../../common/index';
import { assertFound } from '../../common/utils';
import { TransactionalConnection } from '../../connection/transactional-connection';
import { Tag } from '../../entity/index';
import { TaxCategory } from '../../entity/tax-category/tax-category.entity';
import { TaxRate } from '../../entity/tax-rate/tax-rate.entity';
import { EventBus } from '../../event-bus';
import { TaxCategoryEvent } from '../../event-bus/events/tax-category-event';
import { ListQueryBuilder } from '../helpers/list-query-builder/list-query-builder';
import { patchEntity } from '../helpers/utils/patch-entity';

/**
Expand All @@ -25,10 +28,23 @@ import { patchEntity } from '../helpers/utils/patch-entity';
*/
@Injectable()
export class TaxCategoryService {
constructor(private connection: TransactionalConnection, private eventBus: EventBus) {}
constructor(
private connection: TransactionalConnection,
private eventBus: EventBus,
private listQueryBuilder: ListQueryBuilder,
) {}

findAll(ctx: RequestContext): Promise<TaxCategory[]> {
return this.connection.getRepository(ctx, TaxCategory).find();
findAll(
ctx: RequestContext,
options?: ListQueryOptions<TaxCategory>,
): Promise<PaginatedList<TaxCategory>> {
return this.listQueryBuilder
.build(TaxCategory, options, { ctx })
.getManyAndCount()
.then(([items, totalItems]) => ({
items,
totalItems,
}));
}

findOne(ctx: RequestContext, taxCategoryId: ID): Promise<TaxCategory | undefined> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4483,7 +4483,7 @@ export type Query = {
stockLocations: StockLocationList;
tag: Tag;
tags: TagList;
taxCategories: Array<TaxCategory>;
taxCategories: TaxCategoryList;
taxCategory?: Maybe<TaxCategory>;
taxRate?: Maybe<TaxRate>;
taxRates: TaxRateList;
Expand Down Expand Up @@ -4689,6 +4689,10 @@ export type QueryTagsArgs = {
options?: InputMaybe<TagListOptions>;
};

export type QueryTaxCategoriesArgs = {
options?: InputMaybe<TaxCategoryListOptions>;
};

export type QueryTaxCategoryArgs = {
id: Scalars['ID'];
};
Expand Down Expand Up @@ -5379,6 +5383,39 @@ export type TaxCategory = Node & {
updatedAt: Scalars['DateTime'];
};

export type TaxCategoryFilterParameter = {
createdAt?: InputMaybe<DateOperators>;
id?: InputMaybe<IdOperators>;
isDefault?: InputMaybe<BooleanOperators>;
name?: InputMaybe<StringOperators>;
updatedAt?: InputMaybe<DateOperators>;
};

export type TaxCategoryList = PaginatedList & {
items: Array<TaxCategory>;
totalItems: Scalars['Int'];
};

export type TaxCategoryListOptions = {
/** Allows the results to be filtered */
filter?: InputMaybe<TaxCategoryFilterParameter>;
/** Specifies whether multiple "filter" arguments should be combines with a logical AND or OR operation. Defaults to AND. */
filterOperator?: InputMaybe<LogicalOperator>;
/** Skips the first n results, for use in pagination */
skip?: InputMaybe<Scalars['Int']>;
/** Specifies which properties to sort the results by */
sort?: InputMaybe<TaxCategorySortParameter>;
/** Takes n results, for use in pagination */
take?: InputMaybe<Scalars['Int']>;
};

export type TaxCategorySortParameter = {
createdAt?: InputMaybe<SortOrder>;
id?: InputMaybe<SortOrder>;
name?: InputMaybe<SortOrder>;
updatedAt?: InputMaybe<SortOrder>;
};

export type TaxLine = {
description: Scalars['String'];
taxRate: Scalars['Float'];
Expand Down
Loading

0 comments on commit ddcd0fc

Please sign in to comment.