Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Update ProductCreate mutation.
IKarbowiak4d5af132cf7e
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.pyindex 6a869ce218..174cac8311 100644--- a/saleor/graphql/product/mutations/products.py+++ b/saleor/graphql/product/mutations/products.py@@ -525,11 +525,22 @@ class ProductInput(graphene.InputObjectType):"a product doesn't use variants."))+ stocks = graphene.List(+ graphene.NonNull(StockInput),+ description=(+ "Stocks of a product available for sale. Note: this field is "+ "only used if a product doesn't use variants."+ ),+ required=False,+ )quantity = graphene.Int(description=("The total quantity of a product available for sale. Note: this field is ""only used if a product doesn't use variants."- )+ ),+ deprecation_reason=(+ "DEPRECATED: Will be removed in 2.11. Use stocks input field instead."+ ),)track_inventory = graphene.Boolean(description=(@@ -911,14 +922,37 @@ class ProductCreate(ModelMutation):variant = models.ProductVariant.objects.create(product=instance, track_inventory=track_inventory, sku=sku)- quantity = cleaned_input.get("quantity")- if quantity is not None:- set_stock_quantity(variant, info.context.country, quantity)+ stocks = cleaned_input.get("stocks")+ if stocks:+ cls.create_stocks(variant, stocks)attributes = cleaned_input.get("attributes")if attributes:AttributeAssignmentMixin.save(instance, attributes)+ @classmethod+ @transaction.atomic+ def create_stocks(cls, variant, stocks):+ warehouse_ids = [stock["warehouse"] for stock in stocks]+ warehouses = cls.get_nodes_or_error(+ warehouse_ids, "warehouse", only_type=Warehouse+ )+ for stock_data, warehouse in zip(stocks, warehouses):+ try:+ Stock.objects.create(+ product_variant=variant,+ warehouse=warehouse,+ quantity=stock_data["quantity"],+ )+ except IntegrityError:+ msg = (+ "Stock for warehouse with id: {} already exists "+ "for this product variant.".format(stock_data["warehouse"])+ )+ raise ValidationError(+ {"warehouse": ValidationError(msg, code=StockErorrCode.UNIQUE,)}+ )+@classmethoddef _save_m2m(cls, info, instance, cleaned_data):collections = cleaned_data.get("collections", None)diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 3198854ce4..14f16a46ea 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -3214,6 +3214,7 @@ input ProductCreateInput {seo: SeoInputweight: WeightScalarsku: String+ stocks: [StockInput!]quantity: InttrackInventory: BooleanproductType: ID!@@ -3326,6 +3327,7 @@ input ProductInput {seo: SeoInputweight: WeightScalarsku: String+ stocks: [StockInput!]quantity: InttrackInventory: Boolean}diff --git a/tests/api/test_product.py b/tests/api/test_product.pyindex bee78d6c54..8edefe3728 100644--- a/tests/api/test_product.py+++ b/tests/api/test_product.py@@ -3574,12 +3574,13 @@ def test_product_filter_by_attribute_values(]-MUTATION_CREATE_PRODUCT_QUANTITY = """+MUTATION_CREATE_PRODUCT_WITH_STOCKS = """mutation createProduct($productType: ID!,$category: ID!$name: String!,$sku: String,+ $stocks: [StockInput!],$quantity: Int,$basePrice: Decimal!$trackInventory: Boolean)@@ -3590,6 +3591,7 @@ mutation createProduct(productType: $productType,name: $name,sku: $sku,+ stocks: $stocks,trackInventory: $trackInventory,quantity: $quantity,basePrice: $basePrice,@@ -3605,16 +3607,17 @@ mutation createProduct(quantity}}- errors {+ productErrors {messagefield+ code}}}"""-def test_create_product_without_variant_creates_stock(+def test_create_product_without_variant_creates_stocks(staff_api_client,category,permission_manage_products,@@ -3625,41 +3628,55 @@ def test_create_product_without_variant_creates_stock(product_type_id = graphene.Node.to_global_id("ProductType", product_type_without_variant.pk)+ stocks = [+ {+ "warehouse": graphene.Node.to_global_id("Warehouse", warehouse.pk),+ "quantity": 20,+ }+ ]variables = {"category": category_id,"productType": product_type_id,"name": "Test","quantity": 8,+ "stocks": stocks,"sku": "23434","trackInventory": True,"basePrice": Decimal("19"),}response = staff_api_client.post_graphql(- MUTATION_CREATE_PRODUCT_QUANTITY,+ MUTATION_CREATE_PRODUCT_WITH_STOCKS,variables,permissions=[permission_manage_products],)content = get_graphql_content(response)quantity = content["data"]["productCreate"]["product"]["variants"][0]["quantity"]- assert quantity == 8+ assert quantity == 20def test_create_product_with_variants_does_not_create_stock(- staff_api_client, category, product_type, permission_manage_products+ staff_api_client, category, product_type, permission_manage_products, warehouse):category_id = graphene.Node.to_global_id("Category", category.pk)product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)+ stocks = [+ {+ "warehouse": graphene.Node.to_global_id("Warehouse", warehouse.pk),+ "quantity": 20,+ }+ ]variables = {"category": category_id,"productType": product_type_id,"name": "Test","quantity": 8,+ "stocks": stocks,"sku": "23434","trackInventory": True,"basePrice": Decimal("19"),}response = staff_api_client.post_graphql(- MUTATION_CREATE_PRODUCT_QUANTITY,+ MUTATION_CREATE_PRODUCT_WITH_STOCKS,variables,permissions=[permission_manage_products],)@@ -3669,33 +3686,6 @@ def test_create_product_with_variants_does_not_create_stock(assert not Stock.objects.exists()-def test_create_without_variants_failes_without_warehouses(- staff_api_client, category, product_type_without_variant, permission_manage_products-):- category_id = graphene.Node.to_global_id("Category", category.pk)- product_type_id = graphene.Node.to_global_id(- "ProductType", product_type_without_variant.pk- )- variables = {- "category": category_id,- "productType": product_type_id,- "name": "Test",- "quantity": 8,- "sku": "23434",- "trackInventory": True,- "basePrice": Decimal("19"),- }- response = staff_api_client.post_graphql(- MUTATION_CREATE_PRODUCT_QUANTITY,- variables,- permissions=[permission_manage_products],- )- content = get_graphql_content(response, ignore_errors=True)- errors = content["errors"]- assert len(errors) == 1- assert errors[0]["message"] == "Warehouse matching query does not exist."--MUTATION_UPDATE_PRODUCT_QUANTITY = """mutation updateProduct($productId: ID!,