我正在使用graphene-django
而不是rest api
(休息框架)。我正在处理用户注册。在其余框架中,验证是在序列化程序部分完成的,但是在使用时graphene
我如何验证和处理错误以将有意义的状态传递给客户端?
这是注册码
class Register(graphene.Mutation):
class Arguments:
email = graphene.String(required=True)
password = graphene.String(required=True)
password_repeat = graphene.String(required=True)
user = graphene.Field(UserQuery)
success = graphene.Boolean()
errors = graphene.List(graphene.String)
@staticmethod
def mutate(self, info, email, password, password_repeat):
if password == password_repeat:
try:
user = User.objects.create(email=email)
user.set_password(password)
user.is_active = False
user.full_clean()
user.save()
return Register(success=True, user=user)
except ValidationError as e:
import pdb
pdb.set_trace()
return Register(success=False, errors=[e])
return Register(success=False, errors=['password', 'password is not matching'])
一个示例可以是验证是否已存在具有电子邮件的用户