Here's my simple scenario. I have a model that has two mandatory fields name and age:
class Person(models.Model):
name = models.CharField(max_length=256)
age = models.PositiveIntegerField()
I'm using Django model form with Graphene
class PersonForm(forms.ModelForm):
class Meta:
model = Person
fields = ('name', 'age')
class PersonType(DjangoObjectType):
class Meta:
model = Person
class PersonMutation(DjangoModelFormMutation):
class Meta:
form_class = PersonForm
class Mutation(graphene.ObjectType):
person_mutation = PersonMutation.Field()
Lets say a person fills age field but not name field. I can then send a mutation query
mutation {
personMutation(input: {age: 25, name: ""}) {
errors {
field
messages
}
}
}
I get the following response, which is exactly what I want. This response is easy to go through and I get name field validation message.
{
"data": {
"personMutation": {
"errors": [
{
"field": "name",
"messages": [
"This field is required."
]
}
]
}
}
}
But what if the user fills name but not age? What kind of mutation query should I make?
If I make
mutation {
personMutation(input: {name: "my name"}) {
errors {
field
messages
}
}
}
I get the response below. That is a horrible message, I can't show that to user. Also response json format is different than before.
{
"errors": [
{
"message": "Argument \"input\" has invalid value {name: \"my name\"}.\nIn field \"age\": Expected \"Int!\", found null.",
"locations": [
{
"line": 2,
"column": 25
}
]
}
]
}
If I try to put age: null
or age: ""
as input parameter it's not any better. So how can I get Django's "This field is required" validation message when age is not set?
Graphene gives an instant error message because in the model both fields are blank=False
(default) that makes the field in the form required=True
(see doc).
I guess if you query with only age and no name at all not even an empty string you would get a comparable error message.
Some ideas on how to avoid this problem:
Make the fields blank=True
in your model. Then you need to check in your backend if the values are set and generate a custom error message if not.
Make the fields blank=False
(default) in your model and leave it to the frontend to check if the fields are set. E.g. make the "send" button disabled if not all required values are set. Which is the nicer way I think.