Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Extend ShippingZone type with channels
IKarbowiak0207ac82e369
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex a4b4355855..0ce3484574 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -5007,7 +5007,8 @@ type ShippingZone implements Node & ObjectWithMetadata {priceRange: MoneyRangecountries: [CountryDisplay]shippingMethods: [ShippingMethod]- warehouses: [Warehouse]+ warehouses: [Warehouse!]!+ channels: [Channel!]!description: String}diff --git a/saleor/graphql/shipping/dataloaders.py b/saleor/graphql/shipping/dataloaders.pyindex 7c02ade60c..76403c8731 100644--- a/saleor/graphql/shipping/dataloaders.py+++ b/saleor/graphql/shipping/dataloaders.py@@ -2,6 +2,7 @@ from collections import defaultdictfrom django.db.models import F+from ...channel.models import Channelfrom ...shipping.models import (ShippingMethod,ShippingMethodChannelListing,@@ -116,3 +117,33 @@ class ShippingMethodChannelListingByShippingMethodIdAndChannelSlugLoader(DataLoashipping_method_channel_listings_by_shipping_method_and_channel_map[key]for key in keys]+++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.filter(+ shipping_zones__id__in=keys+ ).values_list("pk", "shipping_zones__id")+ shipping_zone_channel_map = defaultdict(list)+ for channel_id, zone_id in channel_and_zone_is_pairs:+ shipping_zone_channel_map[zone_id].append(channel_id)++ def map_channels(channels):+ channel_map = {channel.pk: channel for channel in channels}+ return [+ [+ channel_map[channel_id]+ for channel_id in shipping_zone_channel_map[zone_id]+ ]+ for zone_id in keys+ ]++ return (+ ChannelByIdLoader(self.context)+ .load_many({pk for pk, _ in channel_and_zone_is_pairs})+ .then(map_channels)+ )diff --git a/saleor/graphql/shipping/types.py b/saleor/graphql/shipping/types.pyindex 89f8e02048..1299dc71b4 100644--- a/saleor/graphql/shipping/types.py+++ b/saleor/graphql/shipping/types.py@@ -7,6 +7,7 @@ from ...shipping import modelsfrom ..channel import ChannelQsContextfrom ..channel.dataloaders import ChannelByIdLoaderfrom ..channel.types import (+ Channel,ChannelContext,ChannelContextType,ChannelContextTypeWithMetadata,@@ -21,6 +22,7 @@ from ..translations.fields import TranslationFieldfrom ..translations.types import ShippingMethodTranslationfrom ..warehouse.types import Warehousefrom .dataloaders import (+ ChannelsByShippingZoneIdLoader,PostalCodeRulesByShippingMethodIdLoader,ShippingMethodChannelListingByShippingMethodIdAndChannelSlugLoader,ShippingMethodChannelListingByShippingMethodIdLoader,@@ -212,7 +214,14 @@ class ShippingZone(ChannelContextTypeWithMetadata, CountableDjangoObjectType):),)warehouses = graphene.List(- Warehouse, description="List of warehouses for shipping zone."+ graphene.NonNull(Warehouse),+ description="List of warehouses for shipping zone.",+ required=True,+ )+ channels = graphene.List(+ graphene.NonNull(Channel),+ description="List of channels for shipping zone.",+ required=True,)description = graphene.String(description="Description of a shipping zone.")@@ -266,3 +275,7 @@ class ShippingZone(ChannelContextTypeWithMetadata, CountableDjangoObjectType):@staticmethoddef resolve_warehouses(root: ChannelContext[models.ShippingZone], *_args):return root.node.warehouses.all()++ @staticmethod+ def resolve_channels(root: ChannelContext[models.ShippingZone], info, **_kwargs):+ return ChannelsByShippingZoneIdLoader(info.context).load(root.node.id)