Skip to content

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

Return null when background_image is not set

Marcin Gębala5d948d85baff
diff --git a/saleor/graphql/product/types.py b/saleor/graphql/product/types.py
index c607d53c41..f74c80d701 100644
--- a/saleor/graphql/product/types.py
+++ b/saleor/graphql/product/types.py
@@ -296,6 +296,9 @@ class Collection(CountableDjangoObjectType):
interfaces = [relay.Node]
model = models.Collection
+ def resolve_background_image(self, info, **kwargs):
+ return self.background_image or None
+
def resolve_products(self, info, **kwargs):
user = info.context.user
return products_with_details(
@@ -331,6 +334,9 @@ class Category(CountableDjangoObjectType):
def resolve_ancestors(self, info, **kwargs):
return self.get_ancestors().distinct()
+ def resolve_background_image(self, info, **kwargs):
+ return self.background_image or None
+
def resolve_children(self, info, **kwargs):
return self.children.distinct()
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 1ae0d5d0e8..49a08ecf58 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -316,24 +316,24 @@ type CollectionCreate {
collection: Collection
}
-type CollectionDelete {
- errors: [Error]
- collection: Collection
-}
-
-input CollectionInput {
+input CollectionCreateInput {
isPublished: Boolean
name: String
slug: String
backgroundImage: Upload
seo: SeoInput
+ products: [ID]
}
-input CollectionCreateInput {
+type CollectionDelete {
+ errors: [Error]
+ collection: Collection
+}
+
+input CollectionInput {
isPublished: Boolean
name: String
slug: String
- products: [ID]
backgroundImage: Upload
seo: SeoInput
}
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 18fd845004..c6161eedc4 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -1319,6 +1319,28 @@ def test_category_image_query(user_api_client, non_default_category):
assert data['backgroundImage']['url'] == thumbnail_url
+def test_category_image_query_without_associated_file(
+ user_api_client, non_default_category):
+ category = non_default_category
+ category_id = graphene.Node.to_global_id('Category', category.pk)
+ query = """
+ query fetchCategory($id: ID!){
+ category(id: $id) {
+ name
+ backgroundImage {
+ url
+ }
+ }
+ }
+ """
+ variables = {'id': category_id}
+ response = user_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content['data']['category']
+ assert data['name'] == category.name
+ assert data['backgroundImage'] is None
+
+
def test_collection_image_query(user_api_client, collection):
image_file, image_name = create_image()
collection.background_image = image_file
@@ -1341,6 +1363,27 @@ def test_collection_image_query(user_api_client, collection):
assert data['backgroundImage']['url'] == thumbnail_url
+def test_collection_image_query_without_associated_file(
+ user_api_client, collection):
+ collection_id = graphene.Node.to_global_id('Collection', collection.pk)
+ query = """
+ query fetchCollection($id: ID!){
+ collection(id: $id) {
+ name
+ backgroundImage {
+ url
+ }
+ }
+ }
+ """
+ variables = {'id': collection_id}
+ response = user_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content['data']['collection']
+ assert data['name'] == collection.name
+ assert data['backgroundImage'] is None
+
+
@pytest.mark.parametrize('product_price, variant_override, api_variant_price', [
(100, None, 100),
(100, 200, 200),