Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix order type resolvers performance (#9723)
Tomasz Szymański13b5a95e70da
diff --git a/saleor/graphql/giftcard/dataloaders.py b/saleor/graphql/giftcard/dataloaders.pyindex d9918a6acc..86dc2e0087 100644--- a/saleor/graphql/giftcard/dataloaders.py+++ b/saleor/graphql/giftcard/dataloaders.py@@ -1,6 +1,7 @@from collections import defaultdictfrom ...giftcard.models import GiftCard, GiftCardEvent, GiftCardTag+from ...order.models import Orderfrom ..core.dataloaders import DataLoader@@ -49,3 +50,21 @@ class GiftCardTagsByGiftCardIdLoader(DataLoader):tags[gift_card_tag.giftcardtag_id])return [tags_map.get(gift_card_id, []) for gift_card_id in keys]+++class GiftCardsByOrderIdLoader(DataLoader):+ context_key = "gift_cards_by_order_id"++ def batch_load(self, keys):+ gift_card_orders = Order.gift_cards.through.objects.using(+ self.database_connection_name+ ).filter(order_id__in=keys)+ gift_cards = GiftCard.objects.using(self.database_connection_name).in_bulk(+ [order.giftcard_id for order in gift_card_orders]+ )+ cards_map = defaultdict(list)+ for gift_card_order in gift_card_orders:+ cards_map[gift_card_order.order_id].append(+ gift_cards[gift_card_order.giftcard_id]+ )+ return [cards_map.get(order_id, []) for order_id in keys]diff --git a/saleor/graphql/invoice/dataloaders.py b/saleor/graphql/invoice/dataloaders.pynew file mode 100644index 0000000000..712206e9b6--- /dev/null+++ b/saleor/graphql/invoice/dataloaders.py@@ -0,0 +1,17 @@+from collections import defaultdict++from ...invoice.models import Invoice+from ..core.dataloaders import DataLoader+++class InvoicesByOrderIdLoader(DataLoader):+ context_key = "invoices_by_order_id"++ def batch_load(self, keys):+ invoices = Invoice.objects.using(self.database_connection_name).filter(+ order_id__in=keys+ )+ invoices_by_order_map = defaultdict(list)+ for invoice in invoices:+ invoices_by_order_map[invoice.order_id].append(invoice)+ return [invoices_by_order_map.get(order_id, []) for order_id in keys]diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.pyindex 37d25fdba0..b8996297f5 100644--- a/saleor/graphql/order/types.py+++ b/saleor/graphql/order/types.py@@ -83,7 +83,9 @@ from ..decorators import one_of_permissions_requiredfrom ..discount.dataloaders import OrderDiscountsByOrderIDLoader, VoucherByIdLoaderfrom ..discount.enums import DiscountValueTypeEnumfrom ..discount.types import Voucher+from ..giftcard.dataloaders import GiftCardsByOrderIdLoaderfrom ..giftcard.types import GiftCard+from ..invoice.dataloaders import InvoicesByOrderIdLoaderfrom ..invoice.types import Invoicefrom ..meta.types import ObjectWithMetadatafrom ..payment.enums import OrderAction, TransactionStatusEnum@@ -1221,8 +1223,8 @@ class Order(ModelObjectType):return Promise.all([transactions, payments]).then(_resolve_payment_status)@staticmethod- def resolve_payments(root: models.Order, _info):- return root.payments.all()+ def resolve_payments(root: models.Order, info):+ return PaymentsByOrderIdLoader(info.context).load(root.id)@staticmethod@one_of_permissions_required(@@ -1375,15 +1377,15 @@ class Order(ModelObjectType):check_is_owner_or_has_one_of_perms(requester, root.user, OrderPermissions.MANAGE_ORDERS)- return root.invoices.all()+ return InvoicesByOrderIdLoader(info.context).load(root.id)@staticmethoddef resolve_is_shipping_required(root: models.Order, _info):return root.is_shipping_required()@staticmethod- def resolve_gift_cards(root: models.Order, _info):- return root.gift_cards.all()+ def resolve_gift_cards(root: models.Order, info):+ return GiftCardsByOrderIdLoader(info.context).load(root.id)@staticmethoddef resolve_voucher(root: models.Order, info):