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.

Fix/storefront product api performance with includes (#12647)

Damian Legawiecffd3d3bd83f3
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
index 885f9148a3..085d1d89ae 100644
--- a/api/app/controllers/concerns/spree/api/v2/product_list_includes.rb
+++ b/api/app/controllers/concerns/spree/api/v2/product_list_includes.rb
@@ -3,22 +3,28 @@ module Spree
module V2
module ProductListIncludes
def product_list_includes
- {
- product_properties: [],
- option_types: [],
- variant_images: [],
- master: product_variant_includes,
- variants: product_variant_includes,
- translations: []
+ @product_list_includes ||= {
+ taggings: [:tag],
+ variants: [],
+ master: [:prices]
}
+
+ @product_list_includes[:variant_images] = [] if params[:include]&.match('images')
+ @product_list_includes[:option_types] = [] if params[:include]&.match('option_types')
+ @product_list_includes[:product_properties] = [:property] if params[:include]&.match('product_properties')
+ @product_list_includes[:master] = variant_includes if params[:include]&.match(/master|default_variant/)
+ @product_list_includes[:variants] = variant_includes if params[:include]&.match(/variants|default_variant/)
+ @product_list_includes[:taxons] = [:taxonomy, :icon, :store, :rich_text_translations, image_attachment: :blob] if params[:include]&.match('taxons')
+ @product_list_includes
end
- def product_variant_includes
- {
+ def variant_includes
+ variant_includes = {
prices: [],
option_values: :option_type,
- images: []
}
+ variant_includes[:images] = [] if params[:include]&.match(/images/)
+ variant_includes
end
end
end
diff --git a/docs/api-reference/storefront.yaml b/docs/api-reference/storefront.yaml
index b7cf502745..2e1fd973db 100644
--- a/docs/api-reference/storefront.yaml
+++ b/docs/api-reference/storefront.yaml
@@ -4481,11 +4481,11 @@ components:
Specify what related resources (relationships) you would like to receive in the response body. Eg.
```
- default_variant,variants,option_types,product_properties,taxons,images,primary_variant,vendor
+ default_variant,variants,option_types,product_properties,taxons,images,primary_variant
```
[More information](https://jsonapi.org/format/#fetching-includes)
- example: 'default_variant,variants,option_types,product_properties,taxons,images,primary_variant,vendor'
+ example: 'default_variant,variants,option_types,product_properties,taxons,images,primary_variant'
StoreIncludeParam:
name: include
in: query
@@ -5666,7 +5666,7 @@ components:
total_count: 1
total_pages: 1
links:
- self: 'http://localhost:3000/api/v2/storefront/account/orders?include=line_items,variants,variants.images,billing_address,shipping_address,user,payments,shipments,promotions,vendors'
+ self: 'http://localhost:3000/api/v2/storefront/account/orders?include=line_items,variants,variants.images,billing_address,shipping_address,user,payments,shipments,promotions'
next: 'http://localhost:3000/api/v2/storefront/account/orders?include=line_items%2Cvariants%2Cvariants.images%2Cbilling_address%2Cshipping_address%2Cuser%2Cpayments%2Cshipments%2Cpromotions&page=1'
prev: 'http://localhost:3000/api/v2/storefront/account/orders?include=line_items%2Cvariants%2Cvariants.images%2Cbilling_address%2Cshipping_address%2Cuser%2Cpayments%2Cshipments%2Cpromotions&page=1'
last: 'http://localhost:3000/api/v2/storefront/account/orders?include=line_items%2Cvariants%2Cvariants.images%2Cbilling_address%2Cshipping_address%2Cuser%2Cpayments%2Cshipments%2Cpromotions&page=1'