Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add DataLoader for Fulfillment warehouse resolver (#9740)
Krzysztof Kwaśniak9d14fadb2167
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.pyindex 1a10738dd8..c509bf7245 100644--- a/saleor/graphql/order/types.py+++ b/saleor/graphql/order/types.py@@ -1,6 +1,6 @@import loggingfrom decimal import Decimal-from typing import Optional+from typing import List, Optionalfrom uuid import UUIDimport graphene@@ -26,7 +26,7 @@ from ...core.tracing import traced_resolverfrom ...discount import OrderDiscountTypefrom ...graphql.checkout.types import DeliveryMethodfrom ...graphql.utils import get_user_or_app_from_context-from ...graphql.warehouse.dataloaders import WarehouseByIdLoader+from ...graphql.warehouse.dataloaders import StockByIdLoader, WarehouseByIdLoaderfrom ...order import OrderStatus, modelsfrom ...order.models import FulfillmentStatusfrom ...order.utils import (@@ -104,7 +104,7 @@ from ..shipping.dataloaders import (ShippingMethodChannelListingByShippingMethodIdAndChannelSlugLoader,)from ..shipping.types import ShippingMethod-from ..warehouse.types import Allocation, Warehouse+from ..warehouse.types import Allocation, Stock, Warehousefrom .dataloaders import (AllocationsByOrderLineIdLoader,FulfillmentLinesByFulfillmentIdLoader,@@ -490,9 +490,28 @@ class Fulfillment(ModelObjectType):return root.get_status_display()@staticmethod- def resolve_warehouse(root: models.Fulfillment, _info):- line = root.lines.first()- return line.stock.warehouse if line and line.stock else None+ def resolve_warehouse(root: models.Fulfillment, info):+ def _resolve_stock_warehouse(stock: Stock):+ return WarehouseByIdLoader(info.context).load(stock.warehouse_id)++ def _resolve_stock(fulfillment_lines: List[models.FulfillmentLine]):+ try:+ line = fulfillment_lines[0]+ except IndexError:+ return None++ if stock_id := line.stock_id:+ return (+ StockByIdLoader(info.context)+ .load(stock_id)+ .then(_resolve_stock_warehouse)+ )++ return (+ FulfillmentLinesByFulfillmentIdLoader(info.context)+ .load(root.id)+ .then(_resolve_stock)+ )class OrderLine(ModelObjectType):diff --git a/saleor/graphql/warehouse/dataloaders.py b/saleor/graphql/warehouse/dataloaders.pyindex 632f47bfe8..2a03d0baaf 100644--- a/saleor/graphql/warehouse/dataloaders.py+++ b/saleor/graphql/warehouse/dataloaders.py@@ -340,3 +340,10 @@ class WarehouseByIdLoader(DataLoader):keys)return [warehouses.get(warehouse_uuid) for warehouse_uuid in keys]+++class StockByIdLoader(DataLoader):+ context_key = "stock_by_id"++ def batch_load(self, keys):+ return Stock.objects.using(self.database_connection_name).in_bulk(keys).values()