Skip to content

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

Make boolean fields required in input fields; allow null values in input fields

Marcin Gębaladb462160700b
diff --git a/saleor/graphql/core/mutations.py b/saleor/graphql/core/mutations.py
index 865121745a..e494fc0692 100644
--- a/saleor/graphql/core/mutations.py
+++ b/saleor/graphql/core/mutations.py
@@ -115,28 +115,27 @@ class ModelMutation(BaseMutation):
for field_name, field in InputCls._meta.fields.items():
if field_name in input:
value = input[field_name]
- if value is not None:
- # FIXME: maybe we could have custom input field type that takes
- # the type of IDs e.g. graphene.IdList(graphene.ID, type=Product).
-
- # handle list of IDs field
- if isinstance(field.type, graphene.List) and field.type.of_type == graphene.ID:
- instances = get_nodes(value) if value else []
- cleaned_input[field_name] = instances
-
- # handle ID field
- elif field.type == graphene.ID:
- instance = get_node(info, value)
- cleaned_input[field_name] = instance
-
- # handle uploaded files
- elif cls._check_type(field, Upload):
- value = info.context.FILES.get(value)
- cleaned_input[field_name] = value
-
- # handle other fields
- else:
- cleaned_input[field_name] = value
+ # FIXME: maybe we could have custom input field type that takes
+ # the type of IDs e.g. graphene.IdList(graphene.ID, type=Product).
+
+ # handle list of IDs field
+ if value is not None and isinstance(field.type, graphene.List) and field.type.of_type == graphene.ID:
+ instances = get_nodes(value) if value else []
+ cleaned_input[field_name] = instances
+
+ # handle ID field
+ elif value is not None and field.type == graphene.ID:
+ instance = get_node(info, value)
+ cleaned_input[field_name] = instance
+
+ # handle uploaded files
+ elif value is not None and cls._check_type(field, Upload):
+ value = info.context.FILES.get(value)
+ cleaned_input[field_name] = value
+
+ # handle other fields
+ else:
+ cleaned_input[field_name] = value
return cleaned_input
@classmethod
@@ -154,8 +153,6 @@ class ModelMutation(BaseMutation):
for f in opts.fields:
if not f.editable or isinstance(f, models.AutoField) or f.name not in cleaned_data:
continue
- if f.name in cleaned_data and cleaned_data[f.name] is None:
- continue
else:
f.save_form_data(instance, cleaned_data[f.name])
return instance
diff --git a/saleor/graphql/page/mutations.py b/saleor/graphql/page/mutations.py
index 90764157ed..d031c03846 100644
--- a/saleor/graphql/page/mutations.py
+++ b/saleor/graphql/page/mutations.py
@@ -8,7 +8,7 @@ class PageInput(graphene.InputObjectType):
slug = graphene.String()
title = graphene.String()
content = graphene.String()
- is_visible = graphene.Boolean()
+ is_visible = graphene.Boolean(required=True)
available_on = graphene.String()
diff --git a/saleor/graphql/product/mutations.py b/saleor/graphql/product/mutations.py
index 71b39f1f8c..88b40f44bf 100644
--- a/saleor/graphql/product/mutations.py
+++ b/saleor/graphql/product/mutations.py
@@ -6,7 +6,7 @@ from graphql_jwt.decorators import permission_required
from ...product import models
from ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
-from ..core.types import Error, Decimal, Money
+from ..core.types import Decimal, Error
from ..utils import get_attributes_dict_from_list, get_node, get_nodes
from .types import Collection, Product, ProductImage
@@ -74,7 +74,8 @@ class CategoryDelete(ModelDeleteMutation):
class CollectionInput(graphene.InputObjectType):
is_published = graphene.Boolean(
- description='Informs whether a collection is published.')
+ description='Informs whether a collection is published.',
+ required=True)
name = graphene.String(description='Name of the collection.')
slug = graphene.String(description='Slug of the collection.')
products = graphene.List(
@@ -184,12 +185,15 @@ class AttributeValueInput(InputObjectType):
class ProductInput(graphene.InputObjectType):
attributes = graphene.List(AttributeValueInput)
- available_on = graphene.String()
+ available_on = graphene.types.datetime.Date()
category = graphene.ID()
- charge_taxes = graphene.Boolean()
+ charge_taxes = graphene.Boolean(required=True)
+ collections = graphene.List(
+ graphene.ID,
+ description="List of IDs of collections that the product belongs to.")
description = graphene.String()
- is_published = graphene.Boolean()
- is_featured = graphene.Boolean()
+ is_published = graphene.Boolean(required=True)
+ is_featured = graphene.Boolean(required=True)
name = graphene.String()
product_type = graphene.ID()
price = Decimal()
@@ -208,7 +212,6 @@ class ProductCreateMutation(ModelMutation):
@classmethod
def clean_input(cls, info, instance, input, errors):
cleaned_input = super().clean_input(info, instance, input, errors)
-
# Attributes are provided as list of `AttributeValueInput` objects.
# We need to transform them into the format they're stored in the
# `Product` model, which is HStore field that maps attribute's PK to
@@ -227,6 +230,12 @@ class ProductCreateMutation(ModelMutation):
cleaned_input['attributes'] = attributes
return cleaned_input
+ @classmethod
+ def _save_m2m(cls, instance, cleaned_data):
+ collections = cleaned_data.get('collections', None)
+ if collections is not None:
+ instance.collections.set(collections)
+
@classmethod
def user_is_allowed(cls, user, input):
return user.has_perm('product.edit_product')
@@ -260,13 +269,12 @@ class ProductDeleteMutation(ModelDeleteMutation):
class ProductVariantInput(graphene.InputObjectType):
attributes = graphene.List(AttributeValueInput)
- cost_price = graphene.Float()
- name = graphene.String()
- price_override = graphene.Float()
+ cost_price = Decimal()
+ price_override = Decimal()
product = graphene.ID()
sku = graphene.String()
quantity = graphene.Int()
- track_inventory = graphene.Boolean()
+ track_inventory = graphene.Boolean(required=True)
class ProductVariantCreateMutation(ModelMutation):
@@ -334,10 +342,10 @@ class ProductVariantDeleteMutation(ModelDeleteMutation):
class ProductTypeInput(graphene.InputObjectType):
name = graphene.String()
- has_variants = graphene.Boolean()
+ has_variants = graphene.Boolean(required=True)
product_attributes = graphene.List(graphene.ID)
variant_attributes = graphene.List(graphene.ID)
- is_shipping_required = graphene.Boolean()
+ is_shipping_required = graphene.Boolean(required=True)
class ProductTypeCreateMutation(ModelMutation):