Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Refactor channel by order/checkout line loaders. (#16145)

Piotr Zabieglikb84bf6595f49
diff --git a/saleor/graphql/channel/dataloaders.py b/saleor/graphql/channel/dataloaders.py
index 8fa985bee0..eaa7ea3ba8 100644
--- a/saleor/graphql/channel/dataloaders.py
+++ b/saleor/graphql/channel/dataloaders.py
@@ -3,7 +3,7 @@ from django.db.models import Exists, OuterRef
from ...channel.models import Channel
from ...order.models import Order
from ..core.dataloaders import DataLoader
-from ..order.dataloaders import OrderByIdLoader, OrderLineByIdLoader
+from ..order.dataloaders import OrderByIdLoader
class ChannelByIdLoader(DataLoader):
@@ -24,25 +24,26 @@ class ChannelBySlugLoader(DataLoader):
return [channels.get(slug) for slug in keys]
-class ChannelByOrderLineIdLoader(DataLoader):
- context_key = "channel_by_orderline"
+class ChannelByOrderIdLoader(DataLoader):
+ context_key = "channel_by_order"
def batch_load(self, keys):
- def channel_by_lines(order_lines):
- order_ids = [line.order_id for line in order_lines]
-
- def channels_by_checkout(orders):
- channel_ids = [order.channel_id for order in orders]
-
- return ChannelByIdLoader(self.context).load_many(channel_ids)
-
+ def with_orders(orders):
+ def with_channels(channels):
+ channel_map = {channel.id: channel for channel in channels}
+ return [
+ channel_map.get(order.channel_id) if order else None
+ for order in orders
+ ]
+
+ channel_ids = set(order.channel_id for order in orders if order)
return (
- OrderByIdLoader(self.context)
- .load_many(order_ids)
- .then(channels_by_checkout)
+ ChannelByIdLoader(self.context)
+ .load_many(channel_ids)
+ .then(with_channels)
)
- return OrderLineByIdLoader(self.context).load_many(keys).then(channel_by_lines)
+ return OrderByIdLoader(self.context).load_many(keys).then(with_orders)
class ChannelWithHasOrdersByIdLoader(DataLoader):
diff --git a/saleor/graphql/checkout/dataloaders.py b/saleor/graphql/checkout/dataloaders.py
index 7103d28998..92f3886be4 100644
--- a/saleor/graphql/checkout/dataloaders.py
+++ b/saleor/graphql/checkout/dataloaders.py
@@ -637,27 +637,26 @@ class CheckoutMetadataByCheckoutIdLoader(DataLoader[str, CheckoutMetadata]):
return [checkout_metadata.get(checkout_id) for checkout_id in keys]
-class ChannelByCheckoutLineIDLoader(DataLoader):
- context_key = "channel_by_checkout_line"
+class ChannelByCheckoutIDLoader(DataLoader):
+ context_key = "channel_by_checkout"
def batch_load(self, keys):
- def channel_by_lines(checkout_lines):
- checkout_ids = [line.checkout_id for line in checkout_lines]
-
- def channels_by_checkout(checkouts):
- channel_ids = [checkout.channel_id for checkout in checkouts]
-
- return ChannelByIdLoader(self.context).load_many(channel_ids)
+ def with_checkouts(checkouts):
+ def with_channels(channels):
+ channel_map = {channel.id: channel for channel in channels}
+ return [
+ channel_map.get(checkout.channel_id) if checkout else None
+ for checkout in checkouts
+ ]
+ channel_ids = set(checkout.channel_id for checkout in checkouts if checkout)
return (
- CheckoutByTokenLoader(self.context)
- .load_many(checkout_ids)
- .then(channels_by_checkout)
+ ChannelByIdLoader(self.context)
+ .load_many(channel_ids)
+ .then(with_channels)
)
- return (
- CheckoutLineByIdLoader(self.context).load_many(keys).then(channel_by_lines)
- )
+ return CheckoutByTokenLoader(self.context).load_many(keys).then(with_checkouts)
class CheckoutLinesProblemsByCheckoutIdLoader(
diff --git a/saleor/graphql/checkout/types.py b/saleor/graphql/checkout/types.py
index 58c9eee002..7c2d1099c4 100644
--- a/saleor/graphql/checkout/types.py
+++ b/saleor/graphql/checkout/types.py
@@ -34,7 +34,7 @@ from ..channel import ChannelContext
from ..channel.dataloaders import ChannelByIdLoader
from ..channel.types import Channel
from ..checkout.dataloaders import (
- ChannelByCheckoutLineIDLoader,
+ ChannelByCheckoutIDLoader,
CheckoutLinesProblemsByCheckoutIdLoader,
CheckoutProblemsByCheckoutIdDataloader,
)
@@ -271,7 +271,7 @@ class CheckoutLine(ModelObjectType[models.CheckoutLine]):
@staticmethod
def resolve_variant(root: models.CheckoutLine, info: ResolveInfo):
variant = ProductVariantByIdLoader(info.context).load(root.variant_id)
- channel = ChannelByCheckoutLineIDLoader(info.context).load(root.id)
+ channel = ChannelByCheckoutIDLoader(info.context).load(root.checkout_id)
return Promise.all([variant, channel]).then(
lambda data: ChannelContext(node=data[0], channel_slug=data[1].slug)
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
index 479956f36b..f6a388a9d8 100644
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -62,7 +62,7 @@ from ..account.utils import (
from ..app.dataloaders import AppByIdLoader
from ..app.types import App
from ..channel import ChannelContext
-from ..channel.dataloaders import ChannelByIdLoader, ChannelByOrderLineIdLoader
+from ..channel.dataloaders import ChannelByIdLoader, ChannelByOrderIdLoader
from ..channel.types import Channel
from ..checkout.utils import prevent_sync_event_circular_query
from ..core.connection import CountableConnection
@@ -1140,7 +1140,7 @@ class OrderLine(ModelObjectType[models.OrderLine]):
)
variant = ProductVariantByIdLoader(context).load(root.variant_id)
- channel = ChannelByOrderLineIdLoader(context).load(root.id)
+ channel = ChannelByOrderIdLoader(context).load(root.order_id)
return Promise.all([variant, channel]).then(requestor_has_access_to_variant)