我正在尝试一个带有表单的石墨烯-django 示例。但我得到下一个错误:
`graphql.error.base.GraphQLError:无法为不可为空的字段 [MyMutationPayload.name] 返回 null。
我尝试在 perform_mutate 函数内的返回表达式中设置值。如果请求未执行所有变体,则它不起作用。
class MyForm(forms.Form):
name = forms.CharField(min_length=10)
age = forms.IntegerField(min_value=0)
birth_date = forms.DateField()
class MyMutation(DjangoFormMutation):
class Meta:
form_class = MyForm
@classmethod
def perform_mutate(cls, form, info):
print('ok')
return cls(errors=[], name=form.cleaned_data.get('name'), age=form.cleaned_data.get('age'), birth_date=form.cleaned_data.get('birth_date'))
class Mutations():
my_mutation = MyMutation.Field()
class Mutation(Mutations, ObjectType):
pass
ROOT_SCHEMA = Schema(mutation=Mutation)
询问
mutation customMutation($data: MyMutationInput!){
myMutation(input: $data){
name
age
birthDate
errors{
field
messages
}
clientMutationId
}
}
变量
{
"data": {
"name": "Cristhiam",
"age": "-29",
"birthDate": "1990-04-06"
}
}
回复
{
"errors": [
{
"message": "Cannot return null for non-nullable field MyMutationPayload.name.",
"locations": [
{
"line": 3,
"column": 5
}
],
"path": [
"myMutation",
"name"
]
}
],
"data": {
"myMutation": null
}
}
变异结果应显示所有错误或所有表单值。