We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Bug: wrong usage in a dataloader
### What are you trying to achieve?

Please take a look at this code:
https://github.com/saleor/saleor/blob/e3078c188b1ff270f533a71d49ce7d178cda79e4/saleor/graphql/shipping/dataloaders.py#L99

The keys parameter of batch_load method has type of:

```python
List[Tuple[str, str]]
```

So we must extract shipping method ids from the `keys` before using them to filter `ShippingMethod`
I think it's wrong to filter by the whole `keys` param here.

If I was wrong, please kindly let me know and I will close this issue.

Thank you for reaching out. Mr. @patrys 


### Steps to reproduce the problem

Just look at the code and you will understand right away.

### What did you expect to happen?

I think we must extract the `keys` param properly first

### Logs

_No response_

### Environment

Saleor version: Main branch
OS and version: anything


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `saleor/graphql/shipping/dataloaders.py`
Content:
```
1 from collections import defaultdict
2 
3 from django.db.models import Exists, F, OuterRef
4 
5 from ...channel.models import Channel
6 from ...shipping.models import (
7     ShippingMethod,
8     ShippingMethodChannelListing,
9     ShippingMethodPostalCodeRule,
10     ShippingZone,
11 )
12 from ..core.dataloaders import DataLoader
13 
14 
15 class ShippingMethodByIdLoader(DataLoader):
16     context_key = "shippingmethod_by_id"
17 
18     def batch_load(self, keys):
19         shipping_methods = ShippingMethod.objects.using(
20             self.database_connection_name
21         ).in_bulk(keys)
22         return [shipping_methods.get(shipping_method_id) for shipping_method_id in keys]
23 
24 
25 class ShippingZoneByIdLoader(DataLoader):
26     context_key = "shippingzone_by_id"
27 
28     def batch_load(self, keys):
29         shipping_zones = ShippingZone.objects.using(
30             self.database_connection_name
31         ).in_bulk(keys)
32         return [shipping_zones.get(shipping_zone_id) for shipping_zone_id in keys]
33 
34 
35 class ShippingZonesByChannelIdLoader(DataLoader):
36     context_key = "shippingzone_by_channel_id"
37 
38     def batch_load(self, keys):
39         shipping_zones_channel = ShippingZone.channels.through.objects.using(
40             self.database_connection_name
41         ).filter(channel_id__in=keys)
42         shipping_zones_map = (
43             ShippingZone.objects.using(self.database_connection_name)
44             .filter(
45                 Exists(shipping_zones_channel.filter(shippingzone_id=OuterRef("pk")))
46             )
47             .in_bulk()
48         )
49 
50         shipping_zones_by_channel_map = defaultdict(list)
51         for shipping_zone_id, channel_id in shipping_zones_channel.values_list(
52             "shippingzone_id", "channel_id"
53         ):
54             shipping_zones_by_channel_map[channel_id].append(
55                 shipping_zones_map[shipping_zone_id]
56             )
57         return [
58             shipping_zones_by_channel_map.get(channel_id, []) for channel_id in keys
59         ]
60 
61 
62 class ShippingMethodsByShippingZoneIdLoader(DataLoader):
63     context_key = "shippingmethod_by_shippingzone"
64 
65     def batch_load(self, keys):
66         shipping_methods = ShippingMethod.objects.using(
67             self.database_connection_name
68         ).filter(shipping_zone_id__in=keys)
69         shipping_methods_by_shipping_zone_map = defaultdict(list)
70         for shipping_method in shipping_methods:
71             shipping_methods_by_shipping_zone_map[
72                 shipping_method.shipping_zone_id
73             ].append(shipping_method)
74         return [
75             shipping_methods_by_shipping_zone_map.get(shipping_zone_id, [])
76             for shipping_zone_id in keys
77         ]
78 
79 
80 class PostalCodeRulesByShippingMethodIdLoader(DataLoader):
81     context_key = "postal_code_rules_by_shipping_method"
82 
83     def batch_load(self, keys):
84         postal_code_rules = (
85             ShippingMethodPostalCodeRule.objects.using(self.database_connection_name)
86             .filter(shipping_method_id__in=keys)
87             .order_by("id")
88         )
89 
90         postal_code_rules_map = defaultdict(list)
91         for postal_code in postal_code_rules:
92             postal_code_rules_map[postal_code.shipping_method_id].append(postal_code)
93         return [
94             postal_code_rules_map.get(shipping_method_id, [])
95             for shipping_method_id in keys
96         ]
97 
98 
99 class ShippingMethodsByShippingZoneIdAndChannelSlugLoader(DataLoader):
100     context_key = "shippingmethod_by_shippingzone_and_channel"
101 
102     def batch_load(self, keys):
103         shipping_methods = (
104             ShippingMethod.objects.using(self.database_connection_name)
105             .filter(shipping_zone_id__in=keys)
106             .annotate(channel_slug=F("channel_listings__channel__slug"))
107         )
108 
109         shipping_methods_by_shipping_zone_and_channel_map = defaultdict(list)
110         for shipping_method in shipping_methods:
111             key = (
112                 shipping_method.shipping_zone_id,
113                 getattr(shipping_method, "channel_slug"),  # annotation
114             )
115             shipping_methods_by_shipping_zone_and_channel_map[key].append(
116                 shipping_method
117             )
118         return [
119             shipping_methods_by_shipping_zone_and_channel_map.get(key, [])
120             for key in keys
121         ]
122 
123 
124 class ShippingMethodChannelListingByShippingMethodIdLoader(DataLoader):
125     context_key = "shippingmethodchannellisting_by_shippingmethod"
126 
127     def batch_load(self, keys):
128         shipping_method_channel_listings = ShippingMethodChannelListing.objects.using(
129             self.database_connection_name
130         ).filter(shipping_method_id__in=keys)
131         shipping_method_channel_listings_by_shipping_method_map = defaultdict(list)
132         for shipping_method_channel_listing in shipping_method_channel_listings:
133             shipping_method_channel_listings_by_shipping_method_map[
134                 shipping_method_channel_listing.shipping_method_id
135             ].append(shipping_method_channel_listing)
136         return [
137             shipping_method_channel_listings_by_shipping_method_map.get(
138                 shipping_method_id, []
139             )
140             for shipping_method_id in keys
141         ]
142 
143 
144 class ShippingMethodChannelListingByChannelSlugLoader(DataLoader):
145     context_key = "shippingmethodchannellisting_by_channel"
146 
147     def batch_load(self, keys):
148         shipping_method_channel_listings = (
149             ShippingMethodChannelListing.objects.using(self.database_connection_name)
150             .filter(channel__slug__in=keys)
151             .annotate(channel_slug=F("channel__slug"))
152         )
153         shipping_method_channel_listings_by_channel_slug = defaultdict(list)
154         for shipping_method_channel_listing in shipping_method_channel_listings:
155             shipping_method_channel_listings_by_channel_slug[
156                 shipping_method_channel_listing.channel_slug
157             ].append(shipping_method_channel_listing)
158         return [
159             shipping_method_channel_listings_by_channel_slug.get(channel_slug, [])
160             for channel_slug in keys
161         ]
162 
163 
164 class ShippingMethodChannelListingByShippingMethodIdAndChannelSlugLoader(DataLoader):
165     context_key = "shippingmethodchannellisting_by_shippingmethod_and_channel"
166 
167     def batch_load(self, keys):
168         shipping_method_ids = [key[0] for key in keys]
169         channel_slugs = [key[1] for key in keys]
170 
171         def _find_listing_by_shipping_method_id(listings_by_channel):
172             listings_by_method = []
173             for method_id, listings in zip(shipping_method_ids, listings_by_channel):
174                 for listing in listings:
175                     if method_id == listing.shipping_method_id:
176                         listings_by_method.append(listing)
177                         break
178                 else:
179                     listings_by_method.append(None)
180 
181             return listings_by_method
182 
183         return (
184             ShippingMethodChannelListingByChannelSlugLoader(self.context)
185             .load_many(channel_slugs)
186             .then(_find_listing_by_shipping_method_id)
187         )
188 
189 
190 class ChannelsByShippingZoneIdLoader(DataLoader):
191     context_key = "channels_by_shippingzone"
192 
193     def batch_load(self, keys):
194         from ..channel.dataloaders import ChannelByIdLoader
195 
196         channel_and_zone_is_pairs = (
197             Channel.objects.using(self.database_connection_name)
198             .filter(shipping_zones__id__in=keys)
199             .values_list("pk", "shipping_zones__id")
200         )
201         shipping_zone_channel_map = defaultdict(list)
202         for channel_id, zone_id in channel_and_zone_is_pairs:
203             shipping_zone_channel_map[zone_id].append(channel_id)
204 
205         def map_channels(channels):
206             channel_map = {channel.pk: channel for channel in channels}
207             return [
208                 [
209                     channel_map[channel_id]
210                     for channel_id in shipping_zone_channel_map.get(zone_id, [])
211                 ]
212                 for zone_id in keys
213             ]
214 
215         return (
216             ChannelByIdLoader(self.context)
217             .load_many({pk for pk, _ in channel_and_zone_is_pairs})
218             .then(map_channels)
219         )
220
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/saleor/graphql/shipping/dataloaders.py b/saleor/graphql/shipping/dataloaders.py
--- a/saleor/graphql/shipping/dataloaders.py
+++ b/saleor/graphql/shipping/dataloaders.py
@@ -100,9 +100,10 @@
     context_key = "shippingmethod_by_shippingzone_and_channel"
 
     def batch_load(self, keys):
+        shipping_zone_ids = [zone_id for (zone_id, _) in keys]
         shipping_methods = (
             ShippingMethod.objects.using(self.database_connection_name)
-            .filter(shipping_zone_id__in=keys)
+            .filter(shipping_zone_id__in=shipping_zone_ids)
             .annotate(channel_slug=F("channel_listings__channel__slug"))
         )

{"golden_diff": "diff --git a/saleor/graphql/shipping/dataloaders.py b/saleor/graphql/shipping/dataloaders.py\n--- a/saleor/graphql/shipping/dataloaders.py\n+++ b/saleor/graphql/shipping/dataloaders.py\n@@ -100,9 +100,10 @@\n     context_key = \"shippingmethod_by_shippingzone_and_channel\"\n \n     def batch_load(self, keys):\n+        shipping_zone_ids = [zone_id for (zone_id, _) in keys]\n         shipping_methods = (\n             ShippingMethod.objects.using(self.database_connection_name)\n-            .filter(shipping_zone_id__in=keys)\n+            .filter(shipping_zone_id__in=shipping_zone_ids)\n             .annotate(channel_slug=F(\"channel_listings__channel__slug\"))\n         )\n", "issue": "Bug: wrong usage in a dataloader\n### What are you trying to achieve?\r\n\r\nPlease take a look at this code:\r\nhttps://github.com/saleor/saleor/blob/e3078c188b1ff270f533a71d49ce7d178cda79e4/saleor/graphql/shipping/dataloaders.py#L99\r\n\r\nThe keys parameter of batch_load method has type of:\r\n\r\n```python\r\nList[Tuple[str, str]]\r\n```\r\n\r\nSo we must extract shipping method ids from the `keys` before using them to filter `ShippingMethod`\r\nI think it's wrong to filter by the whole `keys` param here.\r\n\r\nIf I was wrong, please kindly let me know and I will close this issue.\r\n\r\nThank you for reaching out. Mr. @patrys \r\n\r\n\r\n### Steps to reproduce the problem\r\n\r\nJust look at the code and you will understand right away.\r\n\r\n### What did you expect to happen?\r\n\r\nI think we must extract the `keys` param properly first\r\n\r\n### Logs\r\n\r\n_No response_\r\n\r\n### Environment\r\n\r\nSaleor version: Main branch\r\nOS and version: anything\r\n\n", "before_files": [{"content": "from collections import defaultdict\n\nfrom django.db.models import Exists, F, OuterRef\n\nfrom ...channel.models import Channel\nfrom ...shipping.models import (\n    ShippingMethod,\n    ShippingMethodChannelListing,\n    ShippingMethodPostalCodeRule,\n    ShippingZone,\n)\nfrom ..core.dataloaders import DataLoader\n\n\nclass ShippingMethodByIdLoader(DataLoader):\n    context_key = \"shippingmethod_by_id\"\n\n    def batch_load(self, keys):\n        shipping_methods = ShippingMethod.objects.using(\n            self.database_connection_name\n        ).in_bulk(keys)\n        return [shipping_methods.get(shipping_method_id) for shipping_method_id in keys]\n\n\nclass ShippingZoneByIdLoader(DataLoader):\n    context_key = \"shippingzone_by_id\"\n\n    def batch_load(self, keys):\n        shipping_zones = ShippingZone.objects.using(\n            self.database_connection_name\n        ).in_bulk(keys)\n        return [shipping_zones.get(shipping_zone_id) for shipping_zone_id in keys]\n\n\nclass ShippingZonesByChannelIdLoader(DataLoader):\n    context_key = \"shippingzone_by_channel_id\"\n\n    def batch_load(self, keys):\n        shipping_zones_channel = ShippingZone.channels.through.objects.using(\n            self.database_connection_name\n        ).filter(channel_id__in=keys)\n        shipping_zones_map = (\n            ShippingZone.objects.using(self.database_connection_name)\n            .filter(\n                Exists(shipping_zones_channel.filter(shippingzone_id=OuterRef(\"pk\")))\n            )\n            .in_bulk()\n        )\n\n        shipping_zones_by_channel_map = defaultdict(list)\n        for shipping_zone_id, channel_id in shipping_zones_channel.values_list(\n            \"shippingzone_id\", \"channel_id\"\n        ):\n            shipping_zones_by_channel_map[channel_id].append(\n                shipping_zones_map[shipping_zone_id]\n            )\n        return [\n            shipping_zones_by_channel_map.get(channel_id, []) for channel_id in keys\n        ]\n\n\nclass ShippingMethodsByShippingZoneIdLoader(DataLoader):\n    context_key = \"shippingmethod_by_shippingzone\"\n\n    def batch_load(self, keys):\n        shipping_methods = ShippingMethod.objects.using(\n            self.database_connection_name\n        ).filter(shipping_zone_id__in=keys)\n        shipping_methods_by_shipping_zone_map = defaultdict(list)\n        for shipping_method in shipping_methods:\n            shipping_methods_by_shipping_zone_map[\n                shipping_method.shipping_zone_id\n            ].append(shipping_method)\n        return [\n            shipping_methods_by_shipping_zone_map.get(shipping_zone_id, [])\n            for shipping_zone_id in keys\n        ]\n\n\nclass PostalCodeRulesByShippingMethodIdLoader(DataLoader):\n    context_key = \"postal_code_rules_by_shipping_method\"\n\n    def batch_load(self, keys):\n        postal_code_rules = (\n            ShippingMethodPostalCodeRule.objects.using(self.database_connection_name)\n            .filter(shipping_method_id__in=keys)\n            .order_by(\"id\")\n        )\n\n        postal_code_rules_map = defaultdict(list)\n        for postal_code in postal_code_rules:\n            postal_code_rules_map[postal_code.shipping_method_id].append(postal_code)\n        return [\n            postal_code_rules_map.get(shipping_method_id, [])\n            for shipping_method_id in keys\n        ]\n\n\nclass ShippingMethodsByShippingZoneIdAndChannelSlugLoader(DataLoader):\n    context_key = \"shippingmethod_by_shippingzone_and_channel\"\n\n    def batch_load(self, keys):\n        shipping_methods = (\n            ShippingMethod.objects.using(self.database_connection_name)\n            .filter(shipping_zone_id__in=keys)\n            .annotate(channel_slug=F(\"channel_listings__channel__slug\"))\n        )\n\n        shipping_methods_by_shipping_zone_and_channel_map = defaultdict(list)\n        for shipping_method in shipping_methods:\n            key = (\n                shipping_method.shipping_zone_id,\n                getattr(shipping_method, \"channel_slug\"),  # annotation\n            )\n            shipping_methods_by_shipping_zone_and_channel_map[key].append(\n                shipping_method\n            )\n        return [\n            shipping_methods_by_shipping_zone_and_channel_map.get(key, [])\n            for key in keys\n        ]\n\n\nclass ShippingMethodChannelListingByShippingMethodIdLoader(DataLoader):\n    context_key = \"shippingmethodchannellisting_by_shippingmethod\"\n\n    def batch_load(self, keys):\n        shipping_method_channel_listings = ShippingMethodChannelListing.objects.using(\n            self.database_connection_name\n        ).filter(shipping_method_id__in=keys)\n        shipping_method_channel_listings_by_shipping_method_map = defaultdict(list)\n        for shipping_method_channel_listing in shipping_method_channel_listings:\n            shipping_method_channel_listings_by_shipping_method_map[\n                shipping_method_channel_listing.shipping_method_id\n            ].append(shipping_method_channel_listing)\n        return [\n            shipping_method_channel_listings_by_shipping_method_map.get(\n                shipping_method_id, []\n            )\n            for shipping_method_id in keys\n        ]\n\n\nclass ShippingMethodChannelListingByChannelSlugLoader(DataLoader):\n    context_key = \"shippingmethodchannellisting_by_channel\"\n\n    def batch_load(self, keys):\n        shipping_method_channel_listings = (\n            ShippingMethodChannelListing.objects.using(self.database_connection_name)\n            .filter(channel__slug__in=keys)\n            .annotate(channel_slug=F(\"channel__slug\"))\n        )\n        shipping_method_channel_listings_by_channel_slug = defaultdict(list)\n        for shipping_method_channel_listing in shipping_method_channel_listings:\n            shipping_method_channel_listings_by_channel_slug[\n                shipping_method_channel_listing.channel_slug\n            ].append(shipping_method_channel_listing)\n        return [\n            shipping_method_channel_listings_by_channel_slug.get(channel_slug, [])\n            for channel_slug in keys\n        ]\n\n\nclass ShippingMethodChannelListingByShippingMethodIdAndChannelSlugLoader(DataLoader):\n    context_key = \"shippingmethodchannellisting_by_shippingmethod_and_channel\"\n\n    def batch_load(self, keys):\n        shipping_method_ids = [key[0] for key in keys]\n        channel_slugs = [key[1] for key in keys]\n\n        def _find_listing_by_shipping_method_id(listings_by_channel):\n            listings_by_method = []\n            for method_id, listings in zip(shipping_method_ids, listings_by_channel):\n                for listing in listings:\n                    if method_id == listing.shipping_method_id:\n                        listings_by_method.append(listing)\n                        break\n                else:\n                    listings_by_method.append(None)\n\n            return listings_by_method\n\n        return (\n            ShippingMethodChannelListingByChannelSlugLoader(self.context)\n            .load_many(channel_slugs)\n            .then(_find_listing_by_shipping_method_id)\n        )\n\n\nclass ChannelsByShippingZoneIdLoader(DataLoader):\n    context_key = \"channels_by_shippingzone\"\n\n    def batch_load(self, keys):\n        from ..channel.dataloaders import ChannelByIdLoader\n\n        channel_and_zone_is_pairs = (\n            Channel.objects.using(self.database_connection_name)\n            .filter(shipping_zones__id__in=keys)\n            .values_list(\"pk\", \"shipping_zones__id\")\n        )\n        shipping_zone_channel_map = defaultdict(list)\n        for channel_id, zone_id in channel_and_zone_is_pairs:\n            shipping_zone_channel_map[zone_id].append(channel_id)\n\n        def map_channels(channels):\n            channel_map = {channel.pk: channel for channel in channels}\n            return [\n                [\n                    channel_map[channel_id]\n                    for channel_id in shipping_zone_channel_map.get(zone_id, [])\n                ]\n                for zone_id in keys\n            ]\n\n        return (\n            ChannelByIdLoader(self.context)\n            .load_many({pk for pk, _ in channel_and_zone_is_pairs})\n            .then(map_channels)\n        )\n", "path": "saleor/graphql/shipping/dataloaders.py"}], "after_files": [{"content": "from collections import defaultdict\n\nfrom django.db.models import Exists, F, OuterRef\n\nfrom ...channel.models import Channel\nfrom ...shipping.models import (\n    ShippingMethod,\n    ShippingMethodChannelListing,\n    ShippingMethodPostalCodeRule,\n    ShippingZone,\n)\nfrom ..core.dataloaders import DataLoader\n\n\nclass ShippingMethodByIdLoader(DataLoader):\n    context_key = \"shippingmethod_by_id\"\n\n    def batch_load(self, keys):\n        shipping_methods = ShippingMethod.objects.using(\n            self.database_connection_name\n        ).in_bulk(keys)\n        return [shipping_methods.get(shipping_method_id) for shipping_method_id in keys]\n\n\nclass ShippingZoneByIdLoader(DataLoader):\n    context_key = \"shippingzone_by_id\"\n\n    def batch_load(self, keys):\n        shipping_zones = ShippingZone.objects.using(\n            self.database_connection_name\n        ).in_bulk(keys)\n        return [shipping_zones.get(shipping_zone_id) for shipping_zone_id in keys]\n\n\nclass ShippingZonesByChannelIdLoader(DataLoader):\n    context_key = \"shippingzone_by_channel_id\"\n\n    def batch_load(self, keys):\n        shipping_zones_channel = ShippingZone.channels.through.objects.using(\n            self.database_connection_name\n        ).filter(channel_id__in=keys)\n        shipping_zones_map = (\n            ShippingZone.objects.using(self.database_connection_name)\n            .filter(\n                Exists(shipping_zones_channel.filter(shippingzone_id=OuterRef(\"pk\")))\n            )\n            .in_bulk()\n        )\n\n        shipping_zones_by_channel_map = defaultdict(list)\n        for shipping_zone_id, channel_id in shipping_zones_channel.values_list(\n            \"shippingzone_id\", \"channel_id\"\n        ):\n            shipping_zones_by_channel_map[channel_id].append(\n                shipping_zones_map[shipping_zone_id]\n            )\n        return [\n            shipping_zones_by_channel_map.get(channel_id, []) for channel_id in keys\n        ]\n\n\nclass ShippingMethodsByShippingZoneIdLoader(DataLoader):\n    context_key = \"shippingmethod_by_shippingzone\"\n\n    def batch_load(self, keys):\n        shipping_methods = ShippingMethod.objects.using(\n            self.database_connection_name\n        ).filter(shipping_zone_id__in=keys)\n        shipping_methods_by_shipping_zone_map = defaultdict(list)\n        for shipping_method in shipping_methods:\n            shipping_methods_by_shipping_zone_map[\n                shipping_method.shipping_zone_id\n            ].append(shipping_method)\n        return [\n            shipping_methods_by_shipping_zone_map.get(shipping_zone_id, [])\n            for shipping_zone_id in keys\n        ]\n\n\nclass PostalCodeRulesByShippingMethodIdLoader(DataLoader):\n    context_key = \"postal_code_rules_by_shipping_method\"\n\n    def batch_load(self, keys):\n        postal_code_rules = (\n            ShippingMethodPostalCodeRule.objects.using(self.database_connection_name)\n            .filter(shipping_method_id__in=keys)\n            .order_by(\"id\")\n        )\n\n        postal_code_rules_map = defaultdict(list)\n        for postal_code in postal_code_rules:\n            postal_code_rules_map[postal_code.shipping_method_id].append(postal_code)\n        return [\n            postal_code_rules_map.get(shipping_method_id, [])\n            for shipping_method_id in keys\n        ]\n\n\nclass ShippingMethodsByShippingZoneIdAndChannelSlugLoader(DataLoader):\n    context_key = \"shippingmethod_by_shippingzone_and_channel\"\n\n    def batch_load(self, keys):\n        shipping_zone_ids = [zone_id for (zone_id, _) in keys]\n        shipping_methods = (\n            ShippingMethod.objects.using(self.database_connection_name)\n            .filter(shipping_zone_id__in=shipping_zone_ids)\n            .annotate(channel_slug=F(\"channel_listings__channel__slug\"))\n        )\n\n        shipping_methods_by_shipping_zone_and_channel_map = defaultdict(list)\n        for shipping_method in shipping_methods:\n            key = (\n                shipping_method.shipping_zone_id,\n                getattr(shipping_method, \"channel_slug\"),  # annotation\n            )\n            shipping_methods_by_shipping_zone_and_channel_map[key].append(\n                shipping_method\n            )\n        return [\n            shipping_methods_by_shipping_zone_and_channel_map.get(key, [])\n            for key in keys\n        ]\n\n\nclass ShippingMethodChannelListingByShippingMethodIdLoader(DataLoader):\n    context_key = \"shippingmethodchannellisting_by_shippingmethod\"\n\n    def batch_load(self, keys):\n        shipping_method_channel_listings = ShippingMethodChannelListing.objects.using(\n            self.database_connection_name\n        ).filter(shipping_method_id__in=keys)\n        shipping_method_channel_listings_by_shipping_method_map = defaultdict(list)\n        for shipping_method_channel_listing in shipping_method_channel_listings:\n            shipping_method_channel_listings_by_shipping_method_map[\n                shipping_method_channel_listing.shipping_method_id\n            ].append(shipping_method_channel_listing)\n        return [\n            shipping_method_channel_listings_by_shipping_method_map.get(\n                shipping_method_id, []\n            )\n            for shipping_method_id in keys\n        ]\n\n\nclass ShippingMethodChannelListingByChannelSlugLoader(DataLoader):\n    context_key = \"shippingmethodchannellisting_by_channel\"\n\n    def batch_load(self, keys):\n        shipping_method_channel_listings = (\n            ShippingMethodChannelListing.objects.using(self.database_connection_name)\n            .filter(channel__slug__in=keys)\n            .annotate(channel_slug=F(\"channel__slug\"))\n        )\n        shipping_method_channel_listings_by_channel_slug = defaultdict(list)\n        for shipping_method_channel_listing in shipping_method_channel_listings:\n            shipping_method_channel_listings_by_channel_slug[\n                shipping_method_channel_listing.channel_slug\n            ].append(shipping_method_channel_listing)\n        return [\n            shipping_method_channel_listings_by_channel_slug.get(channel_slug, [])\n            for channel_slug in keys\n        ]\n\n\nclass ShippingMethodChannelListingByShippingMethodIdAndChannelSlugLoader(DataLoader):\n    context_key = \"shippingmethodchannellisting_by_shippingmethod_and_channel\"\n\n    def batch_load(self, keys):\n        shipping_method_ids = [key[0] for key in keys]\n        channel_slugs = [key[1] for key in keys]\n\n        def _find_listing_by_shipping_method_id(listings_by_channel):\n            listings_by_method = []\n            for method_id, listings in zip(shipping_method_ids, listings_by_channel):\n                for listing in listings:\n                    if method_id == listing.shipping_method_id:\n                        listings_by_method.append(listing)\n                        break\n                else:\n                    listings_by_method.append(None)\n\n            return listings_by_method\n\n        return (\n            ShippingMethodChannelListingByChannelSlugLoader(self.context)\n            .load_many(channel_slugs)\n            .then(_find_listing_by_shipping_method_id)\n        )\n\n\nclass ChannelsByShippingZoneIdLoader(DataLoader):\n    context_key = \"channels_by_shippingzone\"\n\n    def batch_load(self, keys):\n        from ..channel.dataloaders import ChannelByIdLoader\n\n        channel_and_zone_is_pairs = (\n            Channel.objects.using(self.database_connection_name)\n            .filter(shipping_zones__id__in=keys)\n            .values_list(\"pk\", \"shipping_zones__id\")\n        )\n        shipping_zone_channel_map = defaultdict(list)\n        for channel_id, zone_id in channel_and_zone_is_pairs:\n            shipping_zone_channel_map[zone_id].append(channel_id)\n\n        def map_channels(channels):\n            channel_map = {channel.pk: channel for channel in channels}\n            return [\n                [\n                    channel_map[channel_id]\n                    for channel_id in shipping_zone_channel_map.get(zone_id, [])\n                ]\n                for zone_id in keys\n            ]\n\n        return (\n            ChannelByIdLoader(self.context)\n            .load_many({pk for pk, _ in channel_and_zone_is_pairs})\n            .then(map_channels)\n        )\n", "path": "saleor/graphql/shipping/dataloaders.py"}]}