Skip to content

Commit aef7943

Browse files
committed
feat(products): filter products by a single product_type
The products resolver accepted a list of item types while the REST endpoint only ever filters by one. With just two item types, filtering by both is equivalent to no filter, so a single value is enough. Replace the product_types list filter with a single product_type on ProductsQuery and the resolver, so the GraphQL and REST surfaces are consistent.
1 parent 42a84b0 commit aef7943

6 files changed

Lines changed: 18 additions & 26 deletions

File tree

app/graphql/resolvers/products_resolver.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ class ProductsResolver < Resolvers::BaseResolver
1212
argument :limit, Integer, required: false
1313
argument :page, Integer, required: false
1414
argument :product_category_ids, [ID], required: false
15-
argument :product_types, [Types::Products::ProductTypeEnum], required: false
15+
argument :product_type, Types::Products::ProductTypeEnum, required: false
1616
argument :search_term, String, required: false
1717
argument :without_product_category, Boolean, required: false
1818

1919
type Types::Products::Object.collection_type, null: false
2020

21-
def resolve(page: nil, limit: nil, search_term: nil, product_category_ids: nil, without_product_category: nil, product_types: nil)
21+
def resolve(page: nil, limit: nil, search_term: nil, product_category_ids: nil, without_product_category: nil, product_type: nil)
2222
result = ::ProductsQuery.call(
2323
organization: current_organization,
2424
search_term:,
2525
pagination: {page:, limit:},
26-
filters: {product_category_ids:, without_product_category:, product_types:}
26+
filters: {product_category_ids:, without_product_category:, product_type:}
2727
)
2828

2929
result.products

app/queries/products_query.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class ProductsQuery < BaseQuery
44
Result = BaseResult[:products]
5-
Filters = BaseFilters[:product_category_ids, :without_product_category, :product_types]
5+
Filters = BaseFilters[:product_category_ids, :without_product_category, :product_type]
66

77
def call
88
products = base_scope.result
@@ -12,7 +12,7 @@ def call
1212
if filters.product_category_ids.present? || filters.without_product_category.present?
1313
products = with_product_category(products)
1414
end
15-
products = with_product_types(products) if filters.product_types.present?
15+
products = with_product_type(products) if filters.product_type.present?
1616

1717
result.products = products
1818
result
@@ -45,7 +45,7 @@ def with_product_category(scope)
4545
end
4646
end
4747

48-
def with_product_types(scope)
49-
scope.where(product_type: filters.product_types)
48+
def with_product_type(scope)
49+
scope.where(product_type: filters.product_type)
5050
end
5151
end

schema.graphql

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

schema.json

Lines changed: 4 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/graphql/resolvers/products_resolver_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
let(:query) do
2222
<<~GQL
23-
query($searchTerm: String, $productTypes: [ProductTypeEnum!], $productCategoryIds: [ID!], $withoutProductCategory: Boolean) {
24-
products(limit: 5, searchTerm: $searchTerm, productTypes: $productTypes, productCategoryIds: $productCategoryIds, withoutProductCategory: $withoutProductCategory) {
23+
query($searchTerm: String, $productType: ProductTypeEnum, $productCategoryIds: [ID!], $withoutProductCategory: Boolean) {
24+
products(limit: 5, searchTerm: $searchTerm, productType: $productType, productCategoryIds: $productCategoryIds, withoutProductCategory: $withoutProductCategory) {
2525
collection { id name code productType }
2626
metadata { currentPage totalCount }
2727
}
@@ -45,7 +45,7 @@
4545
end
4646

4747
context "with an item type filter" do
48-
let(:variables) { {productTypes: %w[fixed]} }
48+
let(:variables) { {productType: "fixed"} }
4949

5050
it "returns only matching items" do
5151
expect(execution["data"]["products"]["collection"].map { it["id"] }).to eq([fixed_item.id])

spec/queries/products_query_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@
4747
end
4848
end
4949

50-
context "with an product_types filter" do
51-
let(:filters) { {product_types: %w[fixed]} }
50+
context "with an product_type filter" do
51+
let(:filters) { {product_type: "fixed"} }
5252

53-
it "returns only items of those types" do
53+
it "returns only items of that type" do
5454
expect(result.products).to eq([fixed_item])
5555
end
5656
end

0 commit comments

Comments
 (0)