Batch GraphQL stock_status and stop reloading configurables for only_x_left_in_stock (#40709) - #41216
Open
lbajsarowicz wants to merge 1 commit into
Conversation
…x_left_in_stock (magento#40709) stock_status was a per-field resolver issuing one cataloginventory_stock_status query per product; it now runs as a BatchResolverInterface that preloads every product of a response through StockRegistryPreloader in a single query, while cart-item requests keep delegating to StockStatusProvider unchanged. OnlyXLeftInStockResolver checked the Only X Left threshold only after loading a configurable by SKU through the product repository; it now returns early when the threshold is 0 and reads the selected child from the simple_product custom option instead of reloading the product it already holds.
|
Hi @lbajsarowicz. Thank you for your contribution!
Allowed build names are:
You can find more information about the builds here For more details, review the Code Contributions documentation. |
Contributor
Author
|
@magento run all tests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description (*)
Two GraphQL resolvers in
CatalogInventoryGraphQlissue per-product queries for every item of aproductsresponse.stock_statusis a plain per-field resolver;StockStatusProvider::resolve()callsStockStatusRepository::get()for each product, which goes straight to the database and never consultsStockRegistryStorage. 24 items, 24 queries.only_x_left_in_stockreloads every configurable item throughProductRepositoryInterface::get($product->getSku())before it checks the "Only X left Threshold" setting. That reload was added for configurable cart items, whereConfigurable::getSku()returns the selected child's SKU through thesimple_productcustom option. In aproductsquery there is no such option, so the resolver reloads the product it already holds, at roughly 13 queries per configurable, and then returnsnullbecause the threshold is 0 by default.The fix:
Magento\CatalogInventoryGraphQl\Model\Resolver\StockStatusimplementsBatchResolverInterfaceand is now the@resolverofProductInterface.stock_status. It preloads the statuses of all plain product requests in one query through the existingStockRegistryPreloaderand maps them toIN_STOCK/OUT_OF_STOCKthe same way as before (a product without a stock status row staysOUT_OF_STOCK). Requests carrying acart_itemare delegated unchanged toStockStatusProvider::resolve(), so the bundle and configured-variant logic for cart items is untouched.StockStatusProvideritself is not modified.OnlyXLeftInStockResolverreads the threshold first and returnsnullbefore touching the registry or the repository. For a configurable it takes the selected child from thesimple_productcustom option when present, otherwise it uses the product it was given; the repository is only consulted if the reported SKU differs from the entity SKU.Performance impact
Measured on a vanilla 2.4-develop install (performance toolkit
smallprofile: 1200 products, legacy CatalogInventory, MSI disabled), request:{ products(filter:{}, pageSize: 24) { total_count items { sku stock_status only_x_left_in_stock } } }24 items, 16 of them configurable. Wall time is the median of 7 fresh requests without the profiler; call counts and memory are from one PHP SPX run of the same request. The response body is byte-identical before and after.
cataloginventory_stock_statusqueriescataloginventory_stock_itemqueriesFixed Issues (if relevant)
Manual testing scenarios (*)
bin/magento dev:query-log:enable) and send the GraphQL query above against a catalog with a few configurable products.cataloginventory_stock_statusqueries invar/debug/db.log: one per response instead of one per item, and no product loads by SKU.only_x_left_in_stockfor it: the remaining qty is returned as before.cart { items { product { stock_status only_x_left_in_stock } } }: values match the selected variant, as before.Questions or comments
Gates run locally: unit (16 tests, 98 assertions), PHPCS, PHPStan and the Static Tests
LiveCodeTeston the changed files are clean. The module has no integration suite. The new unit test forOnlyXLeftInStockResolverfails without the fix and passes with it.The Semantic Version Checker will report the new public class and the changed
@resolverclass onProductInterface.stock_status; the GraphQL schema surface is unchanged. Third-party plugins onStockStatusProvider::resolve()keep applying to cart items but no longer to plain product items, since those are resolved in the batch.Contribution checklist (*)