Repository: saleor/saleor

Issue:
Remove redundant dataloader code
### What I'm trying to achieve
Please take a look at these 2 dataloaders:
https://github.com/saleor/saleor/blob/c800b979188e9dc237156f1083acef9b933343d1/saleor/graphql/product/dataloaders/products.py#L426
https://github.com/saleor/saleor/blob/c800b979188e9dc237156f1083acef9b933343d1/saleor/graphql/product/dataloaders/products.py#L156

These dataloaders do exactly same thing, only differ in names.

### Describe a proposed solution
We should get rid of one of them and update the related code that call them.

Thank you for reaching out, Ms. @IKarbowiak, Mr. @maarcingebala !



Discussion:

Resolving patch:
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -115,10 +115,10 @@
     plugin_manager_promise_callback,
 )
 from ..product.dataloaders import (
+    ImagesByProductIdLoader,
     MediaByProductVariantIdLoader,
     ProductByVariantIdLoader,
     ProductChannelListingByProductIdAndChannelSlugLoader,
-    ProductImageByProductIdLoader,
     ProductVariantByIdLoader,
     ThumbnailByProductMediaIdSizeAndFormatLoader,
 )
@@ -782,7 +782,7 @@ def _resolve_thumbnail(result):
 
             # we failed to get image from variant, lets use first from product
             return (
-                ProductImageByProductIdLoader(info.context)
+                ImagesByProductIdLoader(info.context)
                 .load(product.id)
                 .then(_get_first_product_image)
             )
diff --git a/saleor/graphql/product/dataloaders/__init__.py b/saleor/graphql/product/dataloaders/__init__.py
--- a/saleor/graphql/product/dataloaders/__init__.py
+++ b/saleor/graphql/product/dataloaders/__init__.py
@@ -23,7 +23,6 @@
     ProductChannelListingByIdLoader,
     ProductChannelListingByProductIdAndChannelSlugLoader,
     ProductChannelListingByProductIdLoader,
-    ProductImageByProductIdLoader,
     ProductMediaByIdLoader,
     ProductTypeByIdLoader,
     ProductTypeByProductIdLoader,
@@ -60,7 +59,6 @@
     "ProductTypeByVariantIdLoader",
     "ProductChannelListingByIdLoader",
     "ProductChannelListingByProductIdLoader",
-    "ProductImageByProductIdLoader",
     "ProductChannelListingByProductIdAndChannelSlugLoader",
     "ProductTypeByIdLoader",
     "ProductVariantByIdLoader",
diff --git a/saleor/graphql/product/dataloaders/products.py b/saleor/graphql/product/dataloaders/products.py
--- a/saleor/graphql/product/dataloaders/products.py
+++ b/saleor/graphql/product/dataloaders/products.py
@@ -422,20 +422,6 @@ def batch_load(self, keys):
         return [images.get(product_image_id) for product_image_id in keys]
 
 
-class ProductImageByProductIdLoader(DataLoader):
-    context_key = "product_image_by_product_id"
-
-    def batch_load(self, keys):
-        medias = ProductMedia.objects.using(self.database_connection_name).filter(
-            type=ProductMediaTypes.IMAGE,
-            product_id__in=keys,
-        )
-        product_id_medias_map = defaultdict(list)
-        for media in medias.iterator():
-            product_id_medias_map[media.product_id].append(media)
-        return [product_id_medias_map.get(product_id, []) for product_id in keys]
-
-
 class MediaByProductVariantIdLoader(DataLoader):
     context_key = "media_by_product_variant"
 


Test patch: