Skip to content

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

Simplify input of ProductImageUpdate mutation

Marcin Gębala65272dc2fa2d
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index b0a4de34b5..04e465fb65 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -428,15 +428,15 @@ class ProductTypeDelete(ModelDeleteMutation):
return user.has_perm('product.manage_products')
-class ProductImageInput(graphene.InputObjectType):
+class ProductImageCreateInput(graphene.InputObjectType):
alt = graphene.String(description='Alt text for an image.')
- image = Upload(required=True, description='Image file.')
+ image = Upload(required=True, description='Image field.')
product = graphene.ID(description='ID of an product.')
class ProductImageCreate(ModelMutation):
class Arguments:
- input = ProductImageInput(
+ input = ProductImageCreateInput(
required=True,
description='Fields required to create a product image.')
@@ -457,11 +457,15 @@ class ProductImageCreate(ModelMutation):
return user.has_perm('product.manage_products')
-class ProductImageUpdate(ProductImageCreate):
+class ProductImageUpdateInput(graphene.InputObjectType):
+ alt = graphene.String(description='Alt text for an image.')
+
+
+class ProductImageUpdate(ModelMutation):
class Arguments:
id = graphene.ID(
required=True, description='ID of a product image to update.')
- input = ProductImageInput(
+ input = ProductImageUpdateInput(
required=True,
description='Fields required to update a product image.')
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 2f14099d55..716fc39fe5 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -862,10 +862,9 @@ def test_invalid_product_image_create_mutation(admin_api_client, product):
def test_product_image_update_mutation(admin_api_client, product_with_image):
- product = product_with_image
query = """
- mutation updateProductImage($imageId: ID!, $image: Upload!, $alt: String, $product: ID!) {
- productImageUpdate(id: $imageId, input: {image: $image, alt: $alt, product: $product}) {
+ mutation updateProductImage($imageId: ID!, $alt: String) {
+ productImageUpdate(id: $imageId, input: {alt: $alt}) {
productImage {
alt
}
@@ -873,55 +872,15 @@ def test_product_image_update_mutation(admin_api_client, product_with_image):
}
"""
image_obj = product_with_image.images.first()
- image = image_obj.image
- assert not image_obj.alt
alt = 'damage alt'
- variables = {
- 'product': graphene.Node.to_global_id('Product', product.id),
- 'image': image.name, 'alt': alt,
- 'imageId': graphene.Node.to_global_id('ProductImage', image_obj.id)}
- body = get_multipart_request_body(query, variables, image.file, image.name)
- response = admin_api_client.post_multipart(reverse('api'), body)
+ variables = json.dumps({
+ 'alt': alt,
+ 'imageId': graphene.Node.to_global_id('ProductImage', image_obj.id)})
+ response = admin_api_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
content = get_graphql_content(response)
assert 'errors' not in content
- data = content['data']['productImageUpdate']
- assert data['productImage']['alt'] == alt
-
-
-def test_invalid_product_image_update_mutation(
- admin_api_client, product_with_image):
- product = product_with_image
- query = """
- mutation updateProductImage($image: Upload!, $alt: String, $product: ID!, $id: ID!) {
- productImageUpdate(id: $id, input: {image: $image, alt: $alt, product: $product}) {
- productImage {
- image
- }
- errors {
- field
- message
- }
- }
- }
- """
- image_obj = product_with_image.images.first()
- image = image_obj.image
- new_image_file, new_image_name = create_pdf_file_with_image_ext()
- variables = {
- 'product': graphene.Node.to_global_id('Product', product.id),
- 'image': new_image_name,
- 'id': graphene.Node.to_global_id('ProductImage', image_obj.id),
- }
- body = get_multipart_request_body(
- query, variables, new_image_file, new_image_name)
- response = admin_api_client.post_multipart(reverse('api'), body)
- content = get_graphql_content(response)
- assert content['data']['productImageUpdate']['errors'] == [{
- 'field': 'image',
- 'message': 'Invalid file type'}]
- product_with_image.refresh_from_db()
- assert product_with_image.images.count() == 1
- assert product_with_image.images.first().image == image
+ data = content['data']['productImageUpdate']['productImage']['alt'] == alt
def test_product_image_delete(admin_api_client, product_with_image):