Skip to content

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

Extract channel dataloaders to module (#18169)

Lukasz Ostrowski2038eee80a00
diff --git a/saleor/graphql/account/types.py b/saleor/graphql/account/types.py
index b30031ad86..41d35fa640 100644
--- a/saleor/graphql/account/types.py
+++ b/saleor/graphql/account/types.py
@@ -24,7 +24,7 @@ from ...thumbnail.utils import (
from ..account.utils import check_is_owner_or_has_one_of_perms
from ..app.dataloaders import AppByIdLoader, get_app_promise
from ..app.types import App
-from ..channel.dataloaders import ChannelBySlugLoader
+from ..channel.dataloaders.by_self import ChannelBySlugLoader
from ..channel.types import Channel
from ..checkout.dataloaders import CheckoutByUserAndChannelLoader, CheckoutByUserLoader
from ..checkout.types import Checkout, CheckoutCountableConnection
diff --git a/saleor/graphql/channel/dataloaders/__init__.py b/saleor/graphql/channel/dataloaders/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/graphql/channel/dataloaders/by_checkout.py b/saleor/graphql/channel/dataloaders/by_checkout.py
new file mode 100644
index 0000000000..1e00707cdf
--- /dev/null
+++ b/saleor/graphql/channel/dataloaders/by_checkout.py
@@ -0,0 +1,28 @@
+from uuid import UUID
+
+from ....channel.models import Channel
+from ...checkout.dataloaders import CheckoutByTokenLoader
+from ...core.dataloaders import DataLoader
+from .by_self import ChannelByIdLoader
+
+
+class ChannelByCheckoutIDLoader(DataLoader[UUID, Channel]):
+ context_key = "channel_by_checkout"
+
+ def batch_load(self, keys):
+ 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 = {checkout.channel_id for checkout in checkouts if checkout}
+ return (
+ ChannelByIdLoader(self.context)
+ .load_many(channel_ids)
+ .then(with_channels)
+ )
+
+ return CheckoutByTokenLoader(self.context).load_many(keys).then(with_checkouts)
diff --git a/saleor/graphql/channel/dataloaders.py b/saleor/graphql/channel/dataloaders/by_order.py
similarity index 64%
rename from saleor/graphql/channel/dataloaders.py
rename to saleor/graphql/channel/dataloaders/by_order.py
index 1a2017f48d..26d443da2d 100644
--- a/saleor/graphql/channel/dataloaders.py
+++ b/saleor/graphql/channel/dataloaders/by_order.py
@@ -2,28 +2,11 @@ from uuid import UUID
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
-
-
-class ChannelByIdLoader(DataLoader[int, Channel]):
- context_key = "channel_by_id"
-
- def batch_load(self, keys):
- channels = Channel.objects.using(self.database_connection_name).in_bulk(keys)
- return [channels.get(channel_id) for channel_id in keys]
-
-
-class ChannelBySlugLoader(DataLoader[str, Channel]):
- context_key = "channel_by_slug"
-
- def batch_load(self, keys):
- channels = Channel.objects.using(self.database_connection_name).in_bulk(
- keys, field_name="slug"
- )
- return [channels.get(slug) for slug in keys]
+from ....channel.models import Channel
+from ....order.models import Order
+from ...core.dataloaders import DataLoader
+from ...order.dataloaders import OrderByIdLoader
+from .by_self import ChannelByIdLoader
class ChannelByOrderIdLoader(DataLoader[UUID, Channel]):
diff --git a/saleor/graphql/channel/dataloaders/by_self.py b/saleor/graphql/channel/dataloaders/by_self.py
new file mode 100644
index 0000000000..06ac56f4cf
--- /dev/null
+++ b/saleor/graphql/channel/dataloaders/by_self.py
@@ -0,0 +1,20 @@
+from ....channel.models import Channel
+from ...core.dataloaders import DataLoader
+
+
+class ChannelByIdLoader(DataLoader[int, Channel]):
+ context_key = "channel_by_id"
+
+ def batch_load(self, keys):
+ channels = Channel.objects.using(self.database_connection_name).in_bulk(keys)
+ return [channels.get(channel_id) for channel_id in keys]
+
+
+class ChannelBySlugLoader(DataLoader[str, Channel]):
+ context_key = "channel_by_slug"
+
+ def batch_load(self, keys):
+ channels = Channel.objects.using(self.database_connection_name).in_bulk(
+ keys, field_name="slug"
+ )
+ return [channels.get(slug) for slug in keys]
diff --git a/saleor/graphql/channel/dataloaders/by_transaction.py b/saleor/graphql/channel/dataloaders/by_transaction.py
new file mode 100644
index 0000000000..fe6be9f34a
--- /dev/null
+++ b/saleor/graphql/channel/dataloaders/by_transaction.py
@@ -0,0 +1,70 @@
+from collections.abc import Iterable
+
+from promise import Promise
+
+from ....channel.models import Channel
+from ....payment.models import TransactionItem
+from ...core.dataloaders import DataLoader
+from .by_checkout import ChannelByCheckoutIDLoader
+from .by_order import ChannelByOrderIdLoader
+
+
+class ChannelByTransactionIdLoader(DataLoader[int, Channel]):
+ context_key = "channel_by_transaction_id"
+
+ def batch_load(self, keys: Iterable[int]):
+ transaction_ids = list(keys)
+
+ transaction_items = (
+ TransactionItem.objects.using(self.database_connection_name)
+ .only("order_id", "checkout_id", "id")
+ .in_bulk(transaction_ids)
+ )
+
+ transaction_to_order = {
+ item.id: item.order_id
+ for item in transaction_items.values()
+ if item.order_id
+ }
+
+ transaction_to_checkout = {
+ item.id: item.checkout_id
+ for item in transaction_items.values()
+ if item.checkout_id
+ }
+
+ order_ids = [
+ order_id
+ for transaction_id in transaction_ids
+ if (order_id := transaction_to_order.get(transaction_id)) is not None
+ ]
+
+ checkout_ids = [
+ checkout_id
+ for transaction_id in transaction_ids
+ if (checkout_id := transaction_to_checkout.get(transaction_id)) is not None
+ ]
+
+ def resolve_channels(loaded_data):
+ checkout_channels, order_channels = loaded_data
+
+ order_to_channel = dict(zip(order_ids, order_channels, strict=False))
+ checkout_to_channel = dict(
+ zip(checkout_ids, checkout_channels, strict=False)
+ )
+
+ def get_channel_for_transaction(transaction_id):
+ if order_id := transaction_to_order.get(transaction_id):
+ return order_to_channel.get(order_id)
+ if checkout_id := transaction_to_checkout.get(transaction_id):
+ return checkout_to_channel.get(checkout_id)
+ return None
+
+ return [get_channel_for_transaction(tid) for tid in transaction_ids]
+
+ checkout_promise = ChannelByCheckoutIDLoader(self.context).load_many(
+ checkout_ids
+ )
+ order_promise = ChannelByOrderIdLoader(self.context).load_many(order_ids)
+
+ return Promise.all([checkout_promise, order_promise]).then(resolve_channels)
diff --git a/saleor/graphql/channel/tests/dataloaders/__init__.py b/saleor/graphql/channel/tests/dataloaders/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/graphql/channel/tests/dataloaders/test_by_transaction.py b/saleor/graphql/channel/tests/dataloaders/test_by_transaction.py
new file mode 100644
index 0000000000..9f051898a3
--- /dev/null
+++ b/saleor/graphql/channel/tests/dataloaders/test_by_transaction.py
@@ -0,0 +1,179 @@
+from .....channel.models import Channel
+from ....context import SaleorContext
+from ...dataloaders.by_transaction import ChannelByTransactionIdLoader
+
+
+def test_batch_load_with_order_transactions(
+ order_with_lines, transaction_item_generator
+):
+ # given
+ order = order_with_lines
+ transaction1 = transaction_item_generator(order_id=order.id, checkout_id=None)
+ transaction2 = transaction_item_generator(order_id=order.id, checkout_id=None)
+
+ # when
+ context = SaleorContext()
+ loader = ChannelByTransactionIdLoader(context)
+ channels = loader.batch_load([transaction1.id, transaction2.id]).get()
+
+ # then
+ assert len(channels) == 2
+ assert channels[0] == order.channel
+ assert channels[1] == order.channel
+ assert all(isinstance(channel, Channel) for channel in channels)
+
+
+def test_batch_load_with_checkout_transactions(checkout, transaction_item_generator):
+ # given
+ transaction1 = transaction_item_generator(checkout_id=checkout.token, order_id=None)
+ transaction2 = transaction_item_generator(checkout_id=checkout.token, order_id=None)
+
+ # when
+ context = SaleorContext()
+ loader = ChannelByTransactionIdLoader(context)
+ channels = loader.batch_load([transaction1.id, transaction2.id]).get()
+
+ # then
+ assert len(channels) == 2
+ assert channels[0] == checkout.channel
+ assert channels[1] == checkout.channel
+ assert all(isinstance(channel, Channel) for channel in channels)
+
+
+def test_batch_load_with_mixed_transactions(
+ order_with_lines, checkout, transaction_item_generator
+):
+ # given
+ order = order_with_lines
+ order_transaction = transaction_item_generator(order_id=order.id, checkout_id=None)
+ checkout_transaction = transaction_item_generator(
+ checkout_id=checkout.token, order_id=None
+ )
+
+ # when
+ context = SaleorContext()
+ loader = ChannelByTransactionIdLoader(context)
+ channels = loader.batch_load([order_transaction.id, checkout_transaction.id]).get()
+
+ # then
+ assert len(channels) == 2
+ assert channels[0] == order.channel
+ assert channels[1] == checkout.channel
+
+
+def test_batch_load_with_nonexistent_transaction_ids():
+ # given
+ nonexistent_ids = [99999, 88888]
+
+ # when
+ context = SaleorContext()
+ loader = ChannelByTransactionIdLoader(context)
+ channels = loader.batch_load(nonexistent_ids).get()
+
+ # then
+ assert len(channels) == 2
+ assert channels[0] is None
+ assert channels[1] is None
+
+
+def test_batch_load_with_transaction_without_order_or_checkout(
+ transaction_item_generator,
+):
+ # given - transaction with neither order_id nor checkout_id
+ transaction = transaction_item_generator(order_id=None, checkout_id=None)
+
+ # when
+ context = SaleorContext()
+ loader = ChannelByTransactionIdLoader(context)
+ channels = loader.batch_load([transaction.id]).get()
+
+ # then
+ assert len(channels) == 1
+ assert channels[0] is None
+
+
+def test_batch_load_empty_keys():
+ # when
+ context = SaleorContext()
+ loader = ChannelByTransactionIdLoader(context)
+ channels = loader.batch_load([]).get()
+
+ # then
+ assert channels == []
+
+
+def test_batch_load_with_deleted_order(order_with_lines, transaction_item_generator):
+ # given
+ order = order_with_lines
+ transaction = transaction_item_generator(order_id=order.id, checkout_id=None)
+ transaction_id = transaction.id
+ # Delete transaction first, then order to avoid protected foreign key constraint
+ transaction.delete()
+ order.delete()
+
+ # when
+ context = SaleorContext()
+ loader = ChannelByTransactionIdLoader(context)
+ channels = loader.batch_load([transaction_id]).get()
+
+ # then
+ assert len(channels) == 1
+ assert channels[0] is None
+
+
+def test_batch_load_with_deleted_checkout(checkout, transaction_item_generator):
+ # given
+ transaction = transaction_item_generator(checkout_id=checkout.token, order_id=None)
+ checkout.delete()
+
+ # when
+ context = SaleorContext()
+ loader = ChannelByTransactionIdLoader(context)
+ channels = loader.batch_load([transaction.id]).get()
+
+ # then
+ assert len(channels) == 1
+ assert channels[0] is None
+
+
+def test_batch_load_maintains_order(
+ order_with_lines, checkout, transaction_item_generator
+):
+ # given
+ order = order_with_lines
+ transaction1 = transaction_item_generator(order_id=order.id, checkout_id=None)
+ transaction2 = transaction_item_generator(checkout_id=checkout.token, order_id=None)
+ transaction3 = transaction_item_generator(order_id=order.id, checkout_id=None)
+
+ # when
+ context = SaleorContext()
+ loader = ChannelByTransactionIdLoader(context)
+ # Request in specific order
+ channels = loader.batch_load(
+ [transaction2.id, transaction1.id, transaction3.id]
+ ).get()
+
+ # then
+ assert len(channels) == 3
+ # Results should match the order of request
+ assert channels[0] == checkout.channel # transaction2 -> checkout
+ assert channels[1] == order.channel # transaction1 -> order
+ assert channels[2] == order.channel # transaction3 -> order
+
+
+def test_batch_load_handles_duplicate_transaction_ids(
+ order_with_lines, transaction_item_generator
+):
+ # given
+ order = order_with_lines
+ transaction = transaction_item_generator(order_id=order.id, checkout_id=None)
+
+ # when
+ context = SaleorContext()
+ loader = ChannelByTransactionIdLoader(context)
+ # Request same transaction ID multiple times
+ channels = loader.batch_load([transaction.id, transaction.id, transaction.id]).get()
+
+ # then
+ assert len(channels) == 3
+ assert all(channel == order.channel for channel in channels)
diff --git a/saleor/graphql/channel/types.py b/saleor/graphql/channel/types.py
index 5af1bb3bf4..f81736b0d7 100644
--- a/saleor/graphql/channel/types.py
+++ b/saleor/graphql/channel/types.py
@@ -38,7 +38,7 @@ from ..meta.types import ObjectWithMetadata
from ..tax.dataloaders import TaxConfigurationByChannelId
from ..warehouse.dataloaders import WarehousesByChannelIdLoader
from ..warehouse.types import Warehouse
-from .dataloaders import ChannelWithHasOrdersByIdLoader
+from .dataloaders.by_order import ChannelWithHasOrdersByIdLoader
from .enums import (
AllocationStrategyEnum,
MarkAsPaidStrategyEnum,
diff --git a/saleor/graphql/checkout/dataloaders/__init__.py b/saleor/graphql/checkout/dataloaders/__init__.py
index 822da82f67..b3bbb8c420 100644
--- a/saleor/graphql/checkout/dataloaders/__init__.py
+++ b/saleor/graphql/checkout/dataloaders/__init__.py
@@ -3,7 +3,6 @@ from .checkout_infos import (
CheckoutLinesInfoByCheckoutTokenLoader,
)
from .models import (
- ChannelByCheckoutIDLoader,
CheckoutByTokenLoader,
CheckoutByUserAndChannelLoader,
CheckoutByUserLoader,
@@ -19,7 +18,6 @@ from .problems import (
from .promotion_rule_infos import VariantPromotionRuleInfoByCheckoutLineIdLoader
__all__ = [
- "ChannelByCheckoutIDLoader",
"CheckoutByTokenLoader",
"CheckoutByUserAndChannelLoader",
"CheckoutByUserLoader",
diff --git a/saleor/graphql/checkout/dataloaders/checkout_infos.py b/saleor/graphql/checkout/dataloaders/checkout_infos.py
index 51fc6376b3..98b36f5c5e 100644
--- a/saleor/graphql/checkout/dataloaders/checkout_infos.py
+++ b/saleor/graphql/checkout/dataloaders/checkout_infos.py
@@ -8,7 +8,7 @@ from ....core.db.connection import allow_writer_in_context
from ....discount import VoucherType
from ....discount.utils.voucher import attach_voucher_to_line_info
from ...account.dataloaders import AddressByIdLoader, UserByUserIdLoader
-from ...channel.dataloaders import ChannelByIdLoader
+from ...channel.dataloaders.by_self import ChannelByIdLoader
from ...core.dataloaders import DataLoader
from ...discount.dataloaders import (
CheckoutDiscountByCheckoutIdLoader,
diff --git a/saleor/graphql/checkout/dataloaders/models.py b/saleor/graphql/checkout/dataloaders/models.py
index 61bd0de354..2af1f5083a 100644
--- a/saleor/graphql/checkout/dataloaders/models.py
+++ b/saleor/graphql/checkout/dataloaders/models.py
@@ -1,13 +1,10 @@
from collections import defaultdict
from collections.abc import Iterable
-from uuid import UUID
from django.db.models import F
-from ....channel.models import Channel
from ....checkout.models import Checkout, CheckoutLine, CheckoutMetadata
from ....payment.models import TransactionItem
-from ...channel.dataloaders import ChannelByIdLoader
from ...core.dataloaders import DataLoader
@@ -78,28 +75,6 @@ class CheckoutLinesByCheckoutTokenLoader(DataLoader[str, list[CheckoutLine]]):
return [line_map.get(checkout_id, []) for checkout_id in keys]
-class ChannelByCheckoutIDLoader(DataLoader[UUID, Channel]):
- context_key = "channel_by_checkout"
-
- def batch_load(self, keys):
- 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 = {checkout.channel_id for checkout in checkouts if checkout}
- return (
- ChannelByIdLoader(self.context)
- .load_many(channel_ids)
- .then(with_channels)
- )
-
- return CheckoutByTokenLoader(self.context).load_many(keys).then(with_checkouts)
-
-
class CheckoutMetadataByCheckoutIdLoader(DataLoader[str, CheckoutMetadata]):
context_key = "checkout_metadata_by_checkout_id"
diff --git a/saleor/graphql/checkout/resolvers.py b/saleor/graphql/checkout/resolvers.py
index c6daa56fc8..2befc2d49f 100644
--- a/saleor/graphql/checkout/resolvers.py
+++ b/saleor/graphql/checkout/resolvers.py
@@ -7,7 +7,7 @@ from ...permission.enums import (
CheckoutPermissions,
PaymentPermissions,
)
-from ..channel.dataloaders import ChannelByIdLoader
+from ..channel.dataloaders.by_self import ChannelByIdLoader
from ..core.context import SyncWebhookControlContext, get_database_connection_name
from ..core.tracing import traced_resolver
from ..core.utils import from_global_id_or_error
diff --git a/saleor/graphql/checkout/types.py b/saleor/graphql/checkout/types.py
index 30dce70b4f..2ddc5620bf 100644
--- a/saleor/graphql/checkout/types.py
+++ b/saleor/graphql/checkout/types.py
@@ -30,10 +30,10 @@ from ...warehouse.reservations import is_reservation_enabled
from ...webhook.event_types import WebhookEventSyncType
from ..account.dataloaders import AddressByIdLoader, UserByUserIdLoader
from ..account.utils import check_is_owner_or_has_one_of_perms
-from ..channel.dataloaders import ChannelByIdLoader
+from ..channel.dataloaders.by_checkout import ChannelByCheckoutIDLoader
+from ..channel.dataloaders.by_self import ChannelByIdLoader
from ..channel.types import Channel
from ..checkout.dataloaders import (
- ChannelByCheckoutIDLoader,
CheckoutLinesProblemsByCheckoutIdLoader,
CheckoutProblemsByCheckoutIdDataloader,
)
diff --git a/saleor/graphql/core/types/taxes.py b/saleor/graphql/core/types/taxes.py
index d150b5b968..4964c2afa3 100644
--- a/saleor/graphql/core/types/taxes.py
+++ b/saleor/graphql/core/types/taxes.py
@@ -17,7 +17,7 @@ from ....order.models import Order, OrderLine
from ....order.utils import get_order_country
from ....tax.utils import get_charge_taxes
from ...account.dataloaders import AddressByIdLoader
-from ...channel.dataloaders import ChannelByIdLoader
+from ...channel.dataloaders.by_self import ChannelByIdLoader
from ...channel.types import Channel
from ...checkout import types as checkout_types
from ...checkout.dataloaders import (
diff --git a/saleor/graphql/discount/dataloaders.py b/saleor/graphql/discount/dataloaders.py
index 2bb3dc1e22..91a1d8d612 100644
--- a/saleor/graphql/discount/dataloaders.py
+++ b/saleor/graphql/discount/dataloaders.py
@@ -21,7 +21,7 @@ from ...discount.models import (
VoucherCode,
)
from ...product.models import ProductVariant
-from ..channel.dataloaders import ChannelBySlugLoader
+from ..channel.dataloaders.by_self import ChannelBySlugLoader
from ..core.dataloaders import DataLoader
if TYPE_CHECKING:
diff --git a/saleor/graphql/discount/types/sales.py b/saleor/graphql/discount/types/sales.py
index 554ae44c4c..372c5c7733 100644
--- a/saleor/graphql/discount/types/sales.py
+++ b/saleor/graphql/discount/types/sales.py
@@ -4,7 +4,7 @@ from graphene import relay
from ....discount import DiscountValueType, models
from ....permission.enums import DiscountPermissions
from ....product.models import Category, Collection, Product, ProductVariant
-from ...channel.dataloaders import ChannelBySlugLoader
+from ...channel.dataloaders.by_self import ChannelBySlugLoader
from ...channel.types import Channel
from ...core import ResolveInfo
from ...core.connection import CountableConnection, create_connection_slice
diff --git a/saleor/graphql/discount/types/vouchers.py b/saleor/graphql/discount/types/vouchers.py
index 488fb63537..c6550f05ee 100644
--- a/saleor/graphql/discount/types/vouchers.py
+++ b/saleor/graphql/discount/types/vouchers.py
@@ -3,7 +3,7 @@ from graphene import relay
from ....discount import models
from ....permission.enums import DiscountPermissions
-from ...channel.dataloaders import ChannelByIdLoader
+from ...channel.dataloaders.by_self import ChannelByIdLoader
from ...channel.types import Channel
from ...core import ResolveInfo, types
from ...core.connection import CountableConnection, create_connection_slice
diff --git a/saleor/graphql/giftcard/types.py b/saleor/graphql/giftcard/types.py
index 8368be216b..c983d41574 100644
--- a/saleor/graphql/giftcard/types.py
+++ b/saleor/graphql/giftcard/types.py
@@ -16,7 +16,7 @@ from ..account.utils import (
)
from ..app.dataloaders import AppByIdLoader
from ..app.types import App
-from ..channel.dataloaders import ChannelByIdLoader
+from ..channel.dataloaders.by_self import ChannelByIdLoader
from ..core.connection import CountableConnection
from ..core.context import ChannelContext, get_database_connection_name
from ..core.descriptions import DEFAULT_DEPRECATION_REASON
diff --git a/saleor/graphql/menu/types.py b/saleor/graphql/menu/types.py
index ce076a3803..dabaaffd08 100644
--- a/saleor/graphql/menu/types.py
+++ b/saleor/graphql/menu/types.py
@@ -5,7 +5,7 @@ from ...menu import models
from ...permission.enums import PagePermissions
from ...permission.utils import has_one_of_permissions
from ...product.models import ALL_PRODUCTS_PERMISSIONS
-from ..channel.dataloaders import ChannelBySlugLoader
+from ..channel.dataloaders.by_self import ChannelBySlugLoader
from ..core import ResolveInfo
from ..core.connection import CountableConnection
from ..core.context import ChannelContext
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
index 5ed83938f7..09d1f911cb 100644
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -64,7 +64,8 @@ from ..account.utils import (
)
from ..app.dataloaders import AppByIdLoader
from ..app.types import App
-from ..channel.dataloaders import ChannelByIdLoader, ChannelByOrderIdLoader
+from ..channel.dataloaders.by_order import ChannelByOrderIdLoader
+from ..channel.dataloaders.by_self import ChannelByIdLoader
from ..channel.types import Channel
from ..checkout.utils import prevent_sync_event_circular_query
from ..core.connection import CountableConnection
diff --git a/saleor/graphql/page/schema.py b/saleor/graphql/page/schema.py
index 63a53dd5ff..534064e970 100644
--- a/saleor/graphql/page/schema.py
+++ b/saleor/graphql/page/schema.py
@@ -1,6 +1,6 @@
import graphene
-from ..channel.dataloaders import ChannelBySlugLoader
+from ..channel.dataloaders.by_self import ChannelBySlugLoader
from ..core import ResolveInfo
from ..core.connection import create_connection_slice, filter_connection_queryset
from ..core.context import ChannelContext
diff --git a/saleor/graphql/product/dataloaders/products.py b/saleor/graphql/product/dataloaders/products.py
index f69b4e69cb..24e0046a0b 100644
--- a/saleor/graphql/product/dataloaders/products.py
+++ b/saleor/graphql/product/dataloaders/products.py
@@ -19,7 +19,7 @@ from ....product.models import (
VariantChannelListingPromotionRule,
VariantMedia,
)
-from ...channel.dataloaders import ChannelBySlugLoader
+from ...channel.dataloaders.by_self import ChannelBySlugLoader
from ...core.dataloaders import BaseThumbnailBySizeAndFormatLoader, DataLoader
ProductIdAndChannelSlug = tuple[int, str]
diff --git a/saleor/graphql/product/schema.py b/saleor/graphql/product/schema.py
index 41323b9445..a4f24a4b41 100644
--- a/saleor/graphql/product/schema.py
+++ b/saleor/graphql/product/schema.py
@@ -7,7 +7,7 @@ from ...permission.utils import has_one_of_permissions
from ...product import models
from ...product.models import ALL_PRODUCTS_PERMISSIONS
from ...product.search import search_products
-from ..channel.dataloaders import ChannelBySlugLoader
+from ..channel.dataloaders.by_self import ChannelBySlugLoader
from ..channel.utils import get_default_channel_slug_or_graphql_error
from ..core import ResolveInfo
from ..core.connection import create_connection_slice, filter_connection_queryset
diff --git a/saleor/graphql/product/types/categories.py b/saleor/graphql/product/types/categories.py
index 2c6b105453..cc1f43a5c0 100644
--- a/saleor/graphql/product/types/categories.py
+++ b/saleor/graphql/product/types/categories.py
@@ -11,7 +11,7 @@ from ....thumbnail.utils import (
get_thumbnail_format,
get_thumbnail_size,
)
-from ...channel.dataloaders import ChannelBySlugLoader
+from ...channel.dataloaders.by_self import ChannelBySlugLoader
from ...channel.utils import get_default_channel_slug_or_graphql_error
from ...core.connection import (
CountableConnection,
diff --git a/saleor/graphql/product/types/channels.py b/saleor/graphql/product/types/channels.py
index 668a5c67e4..4e622c38e1 100644
--- a/saleor/graphql/product/types/channels.py
+++ b/saleor/graphql/product/types/channels.py
@@ -19,7 +19,7 @@ from ....tax.utils import (
get_tax_rate_for_country,
)
from ...account import types as account_types
-from ...channel.dataloaders import ChannelByIdLoader
+from ...channel.dataloaders.by_self import ChannelByIdLoader
from ...channel.types import Channel
from ...core.descriptions import ADDED_IN_321
from ...core.doc_category import DOC_CATEGORY_PRODUCTS
diff --git a/saleor/graphql/product/types/collections.py b/saleor/graphql/product/types/collections.py
index 824962fe76..0e30ca2a04 100644
--- a/saleor/graphql/product/types/collections.py
+++ b/saleor/graphql/product/types/collections.py
@@ -12,7 +12,7 @@ from ....thumbnail.utils import (
get_thumbnail_format,
get_thumbnail_size,
)
-from ...channel.dataloaders import ChannelBySlugLoader
+from ...channel.dataloaders.by_self import ChannelBySlugLoader
from ...core import ResolveInfo
from ...core.connection import (
CountableConnection,
diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.py
index 4fab5e9ba7..ab4cc1d1ca 100644
--- a/saleor/graphql/product/types/products.py
+++ b/saleor/graphql/product/types/products.py
@@ -53,7 +53,7 @@ from ...attribute.types import (
SelectedAttribute,
)
from ...attribute.utils.shared import AssignedAttributeData
-from ...channel.dataloaders import ChannelBySlugLoader
+from ...channel.dataloaders.by_self import ChannelBySlugLoader
from ...channel.utils import get_default_channel_slug_or_graphql_error
from ...core.connection import (
CountableConnection,
diff --git a/saleor/graphql/shipping/dataloaders.py b/saleor/graphql/shipping/dataloaders.py
index f029e59da0..9e68b32537 100644
--- a/saleor/graphql/shipping/dataloaders.py
+++ b/saleor/graphql/shipping/dataloaders.py
@@ -9,6 +9,7 @@ from ...shipping.models import (
ShippingMethodPostalCodeRule,
ShippingZone,
)
+from ..channel.dataloaders.by_self import ChannelByIdLoader
from ..core.dataloaders import DataLoader
@@ -196,8 +197,6 @@ class ChannelsByShippingZoneIdLoader(DataLoader):
context_key = "channels_by_shippingzone"
def batch_load(self, keys):
- from ..channel.dataloaders import ChannelByIdLoader
-
channel_and_zone_is_pairs = (
Channel.objects.using(self.database_connection_name)
.filter(shipping_zones__id__in=keys)
diff --git a/saleor/graphql/shipping/types.py b/saleor/graphql/shipping/types.py
index 5544a2fa17..6de34c6d42 100644
--- a/saleor/graphql/shipping/types.py
+++ b/saleor/graphql/shipping/types.py
@@ -9,7 +9,7 @@ from ...product import models as product_models
from ...shipping import models
from ...shipping.interface import ShippingMethodData
from ..account.enums import CountryCodeEnum
-from ..channel.dataloaders import ChannelByIdLoader
+from ..channel.dataloaders.by_self import ChannelByIdLoader
from ..channel.types import Channel
from ..core.connection import CountableConnection, create_connection_slice
from ..core.context import (
diff --git a/saleor/graphql/tax/types.py b/saleor/graphql/tax/types.py
index a8ef538557..2f94f7da2f 100644
--- a/saleor/graphql/tax/types.py
+++ b/saleor/graphql/tax/types.py
@@ -1,7 +1,7 @@
import graphene
from ...tax import models
-from ..channel.dataloaders import ChannelByIdLoader
+from ..channel.dataloaders.by_self import ChannelByIdLoader
from ..channel.types import Channel
from ..core import ResolveInfo
from ..core.connection import CountableConnection
diff --git a/saleor/graphql/warehouse/dataloaders.py b/saleor/graphql/warehouse/dataloaders.py
index 085333d98f..9160ea0713 100644
--- a/saleor/graphql/warehouse/dataloaders.py
+++ b/saleor/graphql/warehouse/dataloaders.py
@@ -24,7 +24,7 @@ from ...warehouse.models import (
Warehouse,
)
from ...warehouse.reservations import is_reservation_enabled
-from ..channel.dataloaders import ChannelBySlugLoader
+from ..channel.dataloaders.by_self import ChannelBySlugLoader
from ..core.dataloaders import DataLoader
from ..shipping.dataloaders import (
ShippingZonesByChannelIdLoader,
diff --git a/saleor/graphql/webhook/subscription_types.py b/saleor/graphql/webhook/subscription_types.py
index cb967306d9..9a4d74ebc3 100644
--- a/saleor/graphql/webhook/subscription_types.py
+++ b/saleor/graphql/webhook/subscription_types.py
@@ -38,7 +38,7 @@ from ...webhook.const import MAX_FILTERABLE_CHANNEL_SLUGS_LIMIT
from ...webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ..account.types import User as UserType
from ..app.types import App as AppType
-from ..channel.dataloaders import ChannelByIdLoader
+from ..channel.dataloaders.by_self import ChannelByIdLoader
from ..channel.enums import TransactionFlowStrategyEnum
from ..core import ResolveInfo
from ..core.context import (