Skip to content

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

Allow to fetch category background image with thumbnail

Artur Smętd25fab014727
diff --git a/saleor/graphql/product/types.py b/saleor/graphql/product/types.py
index 3b3f1c3fd1..d67477c38c 100644
--- a/saleor/graphql/product/types.py
+++ b/saleor/graphql/product/types.py
@@ -235,6 +235,21 @@ 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,
@@ -250,6 +265,7 @@ class Category(CountableDjangoObjectType):
lambda: Category,
filterset_class=DistinctFilterSet,
description='List of children of the category.')
+ background_image = graphene.Field(ImageWithThumbnail)
class Meta:
description = """Represents a single category of products. Categories
@@ -293,5 +309,5 @@ class ProductImage(CountableDjangoObjectType):
def resolve_url(self, info, *, size=None):
if size:
- return get_thumbnail(self.image, size, 'crop')
+ return get_thumbnail(self.image, size)
return self.image.url
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 2d1ecb8131..1890c83995 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -1287,3 +1287,29 @@ def test_product_variants_no_ids_list(user_api_client, variant):
assert 'errors' not in content
data = content['data']['productVariants']
assert len(data['edges']) == ProductVariant.objects.count()
+
+
+def test_category_image_query(user_api_client, non_default_category):
+ category = non_default_category
+ image_file, image_name = create_image()
+ category.background_image = image_file
+ category.save()
+ category_id = graphene.Node.to_global_id('Category', category.pk)
+ query = """
+ query fetchCategory($id: ID!){
+ category(id: $id) {
+ id
+ backgroundImage {
+ url(size: 100)
+ }
+ }
+ }
+ """
+ variables = json.dumps({'id': category_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']['category']
+ thumbnail_url = category.background_image.crop['100x100'].url
+ assert data['backgroundImage']['url'] == thumbnail_url