Skip to content

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

Allow to fetch collection background image with thumbnail

Artur Smęte76e635dd8f7
diff --git a/saleor/graphql/product/types.py b/saleor/graphql/product/types.py
index d67477c38c..62058608ed 100644
--- a/saleor/graphql/product/types.py
+++ b/saleor/graphql/product/types.py
@@ -216,10 +216,26 @@ class ProductType(CountableDjangoObjectType):
user=user).filter(product_type=self).distinct()
+class ImageWithThumbnail(graphene.ObjectType):
+ url = graphene.String(
+ required=True,
+ description='The URL of the image.',
+ size=graphene.Int(description='Size of the image'))
+
+ class Meta:
+ description = 'Represents an image.'
+
+ def resolve_url(self, info, size=None):
+ if size:
+ return get_thumbnail(self, size)
+ return self.url
+
+
class Collection(CountableDjangoObjectType):
products = DjangoFilterConnectionField(
Product, filterset_class=ProductFilterSet,
description='List of collection products.')
+ background_image = graphene.Field(ImageWithThumbnail)
class Meta:
description = "Represents a collection of products."
@@ -235,21 +251,6 @@ class Collection(CountableDjangoObjectType):
user=user).filter(collections=self).distinct()
-class ImageWithThumbnail(graphene.ObjectType):
- url = graphene.String(
- required=True,
- description='The URL of the image.',
- size=graphene.Int(description='Size of the image'))
-
- class Meta:
- description = 'Represents an image.'
-
- def resolve_url(self, info, size=None):
- if size:
- return get_thumbnail(self, size)
- return self.url
-
-
class Category(CountableDjangoObjectType):
products = DjangoFilterConnectionField(
Product,
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 1890c83995..19c49a7c6a 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -1298,7 +1298,6 @@ def test_category_image_query(user_api_client, non_default_category):
query = """
query fetchCategory($id: ID!){
category(id: $id) {
- id
backgroundImage {
url(size: 100)
}
@@ -1313,3 +1312,27 @@ def test_category_image_query(user_api_client, non_default_category):
data = content['data']['category']
thumbnail_url = category.background_image.crop['100x100'].url
assert data['backgroundImage']['url'] == thumbnail_url
+
+
+def test_collection_image_query(user_api_client, collection):
+ image_file, image_name = create_image()
+ collection.background_image = image_file
+ collection.save()
+ collection_id = graphene.Node.to_global_id('Collection', collection.pk)
+ query = """
+ query fetchCollection($id: ID!){
+ collection(id: $id) {
+ backgroundImage {
+ url(size: 100)
+ }
+ }
+ }
+ """
+ variables = json.dumps({'id': collection_id})
+ response = user_api_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ data = content['data']['collection']
+ thumbnail_url = collection.background_image.crop['100x100'].url
+ assert data['backgroundImage']['url'] == thumbnail_url