Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Reduce N+1 queries for products API controllers

Damian Legawiecec826d910fb4
diff --git a/api/app/controllers/concerns/spree/api/v2/product_list_includes.rb b/api/app/controllers/concerns/spree/api/v2/product_list_includes.rb
new file mode 100644
index 0000000000..867aebd7ab
--- /dev/null
+++ b/api/app/controllers/concerns/spree/api/v2/product_list_includes.rb
@@ -0,0 +1,23 @@
+module Spree
+ module Api
+ module V2
+ module ProductListIncludes
+ def product_list_includes
+ variant_includes = {
+ prices: [],
+ option_values: :option_type,
+ images: []
+ }
+
+ {
+ product_properties: [],
+ option_types: [],
+ variant_images: [],
+ master: variant_includes,
+ variants: variant_includes
+ }
+ end
+ end
+ end
+ end
+end
diff --git a/api/app/controllers/spree/api/v2/platform/products_controller.rb b/api/app/controllers/spree/api/v2/platform/products_controller.rb
index ab70dd5106..843ccc03a9 100644
--- a/api/app/controllers/spree/api/v2/platform/products_controller.rb
+++ b/api/app/controllers/spree/api/v2/platform/products_controller.rb
@@ -3,6 +3,8 @@ module Spree
module V2
module Platform
class ProductsController < ResourceController
+ include ::Spree::Api::V2::ProductListIncludes
+
private
def model_class
@@ -10,15 +12,7 @@ module Spree
end
def scope_includes
- {
- master: :default_price,
- variants: [],
- variant_images: [],
- taxons: [],
- product_properties: :property,
- option_types: :option_values,
- variants_including_master: %i[default_price option_values]
- }
+ product_list_includes
end
end
end
diff --git a/api/app/controllers/spree/api/v2/storefront/products_controller.rb b/api/app/controllers/spree/api/v2/storefront/products_controller.rb
index 970c9c7cac..946e5b6d36 100644
--- a/api/app/controllers/spree/api/v2/storefront/products_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/products_controller.rb
@@ -3,6 +3,8 @@ module Spree
module V2
module Storefront
class ProductsController < ::Spree::Api::V2::ResourceController
+ include ::Spree::Api::V2::ProductListIncludes
+
protected
def sorted_collection
@@ -38,15 +40,7 @@ module Spree
end
def scope_includes
- {
- master: :default_price,
- variants: [],
- variant_images: [],
- taxons: [],
- product_properties: :property,
- option_types: :option_values,
- variants_including_master: %i[default_price option_values]
- }
+ product_list_includes
end
end
end