Skip to content

Commit

Permalink
feat(core): Add SKU filtering to products list in Admin API
Browse files Browse the repository at this point in the history
Relates to #2519
  • Loading branch information
michaelbromley committed Dec 18, 2023
1 parent 6ca3265 commit 876d1ec
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4529,6 +4529,7 @@ export type ProductFilterParameter = {
id?: InputMaybe<IdOperators>;
languageCode?: InputMaybe<StringOperators>;
name?: InputMaybe<StringOperators>;
sku?: InputMaybe<StringOperators>;
slug?: InputMaybe<StringOperators>;
updatedAt?: InputMaybe<DateOperators>;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4344,6 +4344,7 @@ export type ProductFilterParameter = {
id?: InputMaybe<IdOperators>;
languageCode?: InputMaybe<StringOperators>;
name?: InputMaybe<StringOperators>;
sku?: InputMaybe<StringOperators>;
slug?: InputMaybe<StringOperators>;
updatedAt?: InputMaybe<DateOperators>;
};
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/generated-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2559,6 +2559,7 @@ export type ProductVariantListArgs = {
export type ProductFilterParameter = {
createdAt?: InputMaybe<DateOperators>;
description?: InputMaybe<StringOperators>;
enabled?: InputMaybe<BooleanOperators>;
id?: InputMaybe<IdOperators>;
languageCode?: InputMaybe<StringOperators>;
name?: InputMaybe<StringOperators>;
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4454,6 +4454,7 @@ export type ProductFilterParameter = {
id?: InputMaybe<IdOperators>;
languageCode?: InputMaybe<StringOperators>;
name?: InputMaybe<StringOperators>;
sku?: InputMaybe<StringOperators>;
slug?: InputMaybe<StringOperators>;
updatedAt?: InputMaybe<DateOperators>;
};
Expand Down
1 change: 1 addition & 0 deletions packages/core/e2e/graphql/generated-e2e-admin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4344,6 +4344,7 @@ export type ProductFilterParameter = {
id?: InputMaybe<IdOperators>;
languageCode?: InputMaybe<StringOperators>;
name?: InputMaybe<StringOperators>;
sku?: InputMaybe<StringOperators>;
slug?: InputMaybe<StringOperators>;
updatedAt?: InputMaybe<DateOperators>;
};
Expand Down
2 changes: 2 additions & 0 deletions packages/core/e2e/graphql/generated-e2e-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2454,6 +2454,7 @@ export type Product = Node & {
createdAt: Scalars['DateTime']['output'];
customFields?: Maybe<Scalars['JSON']['output']>;
description: Scalars['String']['output'];
enabled: Scalars['Boolean']['output'];
facetValues: Array<FacetValue>;
featuredAsset?: Maybe<Asset>;
id: Scalars['ID']['output'];
Expand All @@ -2477,6 +2478,7 @@ export type ProductVariantListArgs = {
export type ProductFilterParameter = {
createdAt?: InputMaybe<DateOperators>;
description?: InputMaybe<StringOperators>;
enabled?: InputMaybe<BooleanOperators>;
id?: InputMaybe<IdOperators>;
languageCode?: InputMaybe<StringOperators>;
name?: InputMaybe<StringOperators>;
Expand Down
18 changes: 18 additions & 0 deletions packages/core/e2e/product.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,24 @@ describe('Product resolver', () => {
expect(result.products.items[0].name).toBe('Cruiser Skateboard');
});

it('filters by sku admin', async () => {
const result = await adminClient.query<
Codegen.GetProductListQuery,
Codegen.GetProductListQueryVariables
>(GET_PRODUCT_LIST, {
options: {
filter: {
sku: {
contains: 'IHD455T1',
},
},
},
});

expect(result.products.items.length).toBe(1);
expect(result.products.items[0].name).toBe('Hard Drive');
});

it('sorts by name shop', async () => {
const result = await shopClient.query<
Codegen.GetProductListQuery,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/api/schema/admin-api/product.api.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ input ProductListOptions

input ProductFilterParameter {
facetValueId: IDOperators
sku: StringOperators
}

# generated by generateListOptions function
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/service/services/product.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,15 @@ export class ProductService {
const effectiveRelations = relations || this.relations;
const customPropertyMap: { [name: string]: string } = {};
const hasFacetValueIdFilter = !!(options as ProductListOptions)?.filter?.facetValueId;
const hasSkuFilter = !!(options as ProductListOptions)?.filter?.sku;
if (hasFacetValueIdFilter) {
effectiveRelations.push('facetValues');
customPropertyMap.facetValueId = 'facetValues.id';
}
if (hasSkuFilter) {
effectiveRelations.push('variants');
customPropertyMap.sku = 'variants.sku';
}
return this.listQueryBuilder
.build(Product, options, {
relations: effectiveRelations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4344,6 +4344,7 @@ export type ProductFilterParameter = {
id?: InputMaybe<IdOperators>;
languageCode?: InputMaybe<StringOperators>;
name?: InputMaybe<StringOperators>;
sku?: InputMaybe<StringOperators>;
slug?: InputMaybe<StringOperators>;
updatedAt?: InputMaybe<DateOperators>;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4344,6 +4344,7 @@ export type ProductFilterParameter = {
id?: InputMaybe<IdOperators>;
languageCode?: InputMaybe<StringOperators>;
name?: InputMaybe<StringOperators>;
sku?: InputMaybe<StringOperators>;
slug?: InputMaybe<StringOperators>;
updatedAt?: InputMaybe<DateOperators>;
};
Expand Down
2 changes: 2 additions & 0 deletions packages/payments-plugin/e2e/graphql/generated-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2454,6 +2454,7 @@ export type Product = Node & {
createdAt: Scalars['DateTime']['output'];
customFields?: Maybe<Scalars['JSON']['output']>;
description: Scalars['String']['output'];
enabled: Scalars['Boolean']['output'];
facetValues: Array<FacetValue>;
featuredAsset?: Maybe<Asset>;
id: Scalars['ID']['output'];
Expand All @@ -2477,6 +2478,7 @@ export type ProductVariantListArgs = {
export type ProductFilterParameter = {
createdAt?: InputMaybe<DateOperators>;
description?: InputMaybe<StringOperators>;
enabled?: InputMaybe<BooleanOperators>;
id?: InputMaybe<IdOperators>;
languageCode?: InputMaybe<StringOperators>;
name?: InputMaybe<StringOperators>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2592,6 +2592,7 @@ export type Product = Node & {
createdAt: Scalars['DateTime']['output'];
customFields?: Maybe<Scalars['JSON']['output']>;
description: Scalars['String']['output'];
enabled: Scalars['Boolean']['output'];
facetValues: Array<FacetValue>;
featuredAsset?: Maybe<Asset>;
id: Scalars['ID']['output'];
Expand All @@ -2615,6 +2616,7 @@ export type ProductVariantListArgs = {
export type ProductFilterParameter = {
createdAt?: InputMaybe<DateOperators>;
description?: InputMaybe<StringOperators>;
enabled?: InputMaybe<BooleanOperators>;
id?: InputMaybe<IdOperators>;
languageCode?: InputMaybe<StringOperators>;
name?: InputMaybe<StringOperators>;
Expand Down
2 changes: 1 addition & 1 deletion schema-admin.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion schema-shop.json

Large diffs are not rendered by default.

0 comments on commit 876d1ec

Please sign in to comment.