Skip to content

Commit 8f1c0ec

Browse files
committed
feat(products): filter product items by a single item_type
## Context The product items 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. ## Description Replace the item_types list filter with a single item_type on ProductItemsQuery and the resolver, so the GraphQL and REST surfaces are consistent.
1 parent 819383f commit 8f1c0ec

6 files changed

Lines changed: 18 additions & 26 deletions

File tree

app/graphql/resolvers/product_items_resolver.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ class ProductItemsResolver < Resolvers::BaseResolver
99

1010
description "Query product items of an organization"
1111

12-
argument :item_types, [Types::ProductItems::ItemTypeEnum], required: false
12+
argument :item_type, Types::ProductItems::ItemTypeEnum, required: false
1313
argument :limit, Integer, required: false
1414
argument :page, Integer, required: false
1515
argument :product_id, ID, required: false
1616
argument :search_term, String, required: false
1717

1818
type Types::ProductItems::Object.collection_type, null: false
1919

20-
def resolve(page: nil, limit: nil, search_term: nil, product_id: nil, item_types: nil)
20+
def resolve(page: nil, limit: nil, search_term: nil, product_id: nil, item_type: nil)
2121
result = ::ProductItemsQuery.call(
2222
organization: current_organization,
2323
search_term:,
2424
pagination: {page:, limit:},
25-
filters: {product_id:, item_types:}
25+
filters: {product_id:, item_type:}
2626
)
2727

2828
result.product_items

app/queries/product_items_query.rb

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

33
class ProductItemsQuery < BaseQuery
44
Result = BaseResult[:product_items]
5-
Filters = BaseFilters[:product_id, :item_types]
5+
Filters = BaseFilters[:product_id, :item_type]
66

77
def call
88
product_items = base_scope.result
99
product_items = paginate(product_items)
1010
product_items = apply_consistent_ordering(product_items)
1111

1212
product_items = with_product(product_items) if filters.product_id.present?
13-
product_items = with_item_types(product_items) if filters.item_types.present?
13+
product_items = with_item_type(product_items) if filters.item_type.present?
1414

1515
result.product_items = product_items
1616
result
@@ -36,7 +36,7 @@ def with_product(scope)
3636
scope.where(product_id: filters.product_id)
3737
end
3838

39-
def with_item_types(scope)
40-
scope.where(item_type: filters.item_types)
39+
def with_item_type(scope)
40+
scope.where(item_type: filters.item_type)
4141
end
4242
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/product_items_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, $itemTypes: [ProductItemTypeEnum!], $productId: ID) {
24-
productItems(limit: 5, searchTerm: $searchTerm, itemTypes: $itemTypes, productId: $productId) {
23+
query($searchTerm: String, $itemType: ProductItemTypeEnum, $productId: ID) {
24+
productItems(limit: 5, searchTerm: $searchTerm, itemType: $itemType, productId: $productId) {
2525
collection { id name code itemType }
2626
metadata { currentPage totalCount }
2727
}
@@ -45,7 +45,7 @@
4545
end
4646

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

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

spec/queries/product_items_query_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
end
3232
end
3333

34-
context "with an item_types filter" do
35-
let(:filters) { {item_types: %w[fixed]} }
34+
context "with an item_type filter" do
35+
let(:filters) { {item_type: "fixed"} }
3636

37-
it "returns only items of those types" do
37+
it "returns only items of that type" do
3838
expect(result.product_items).to eq([fixed_item])
3939
end
4040
end

0 commit comments

Comments
 (0)