Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Performance fix for order type fulfillments (#7169)
Tomasz Szymański25e187f42faa
diff --git a/saleor/graphql/order/dataloaders.py b/saleor/graphql/order/dataloaders.pyindex 437f03dc64..749f4cba29 100644--- a/saleor/graphql/order/dataloaders.py+++ b/saleor/graphql/order/dataloaders.py@@ -2,7 +2,7 @@ from collections import defaultdictfrom django.db.models import F-from ...order.models import Order, OrderEvent, OrderLine+from ...order.models import Fulfillment, Order, OrderEvent, OrderLinefrom ...payment.models import Paymentfrom ...warehouse.models import Allocationfrom ..core.dataloaders import DataLoader@@ -85,3 +85,14 @@ class AllocationsByOrderLineIdLoader(DataLoader):order_lines_to_allocations[allocation.order_line_id].append(allocation)return [order_lines_to_allocations[order_line_id] for order_line_id in keys]+++class FulfillmentsByOrderIdLoader(DataLoader):+ context_key = "fulfillments_by_order"++ def batch_load(self, keys):+ fulfillments = Fulfillment.objects.filter(order_id__in=keys).order_by("pk")+ fulfillments_map = defaultdict(list)+ for fulfillment in fulfillments.iterator():+ fulfillments_map[fulfillment.order_id].append(fulfillment)+ return [fulfillments_map.get(order_id, []) for order_id in keys]diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.pyindex 23c2534db1..fb76611cc6 100644--- a/saleor/graphql/order/types.py+++ b/saleor/graphql/order/types.py@@ -51,6 +51,7 @@ from ..shipping.types import ShippingMethodfrom ..warehouse.types import Allocation, Warehousefrom .dataloaders import (AllocationsByOrderLineIdLoader,+ FulfillmentsByOrderIdLoader,OrderByIdLoader,OrderEventsByOrderIdLoader,OrderLineByIdLoader,@@ -816,12 +817,20 @@ class Order(CountableDjangoObjectType):@staticmethod@traced_resolverdef resolve_fulfillments(root: models.Order, info):- user = info.context.user- if user.is_staff:- qs = root.fulfillments.all()- else:- qs = root.fulfillments.exclude(status=FulfillmentStatus.CANCELED)- return qs.order_by("pk")+ def _resolve_fulfillments(fulfillments):+ user = info.context.user+ if user.is_staff:+ return fulfillments+ return filter(+ lambda fulfillment: fulfillment.status != FulfillmentStatus.CANCELED,+ fulfillments,+ )++ return (+ FulfillmentsByOrderIdLoader(info.context)+ .load(root.id)+ .then(_resolve_fulfillments)+ )@staticmethod@traced_resolver