title: How do you validate a GraphQL mutation in Python
body: <p>I am new to GraphQL. I am using <a href="http://docs.graphene-python.org/projects/django/en/latest/" rel="noreferrer">Graphene-Django</a> and have a mutation called <code>CreateUser</code>. It takes three arguments <code>username</code>, <code>email</code>, <code>password</code>. </p>
  
  <p>How do I validate the data and return multiple errors back?</p>
  
  <p>I want something like this returned.</p>
  
  <pre><code>{  
     "name":[  
        "Ensure this field has at least 2 characters."
     ],
     "email":[  
        "This field may not be blank."
     ],
     "password":[  
        "This field may not be blank."
     ]
  }
  </code></pre>
  
  <p>So I can render the errors on the form like this:</p>
  
  <p><a href="https://i.stack.imgur.com/PGZjh.png" rel="noreferrer"><img src="https://i.stack.imgur.com/PGZjh.png" alt="enter image description here"></a></p>
  
  <p>My code so far:</p>
  
  <pre><code>from django.contrib.auth.models import User as UserModel
  from graphene_django import DjangoObjectType
  import graphene
  
  
  class User(DjangoObjectType):
      class Meta:
          model = UserModel
          only_fields = 'id', 'username', 'email'
  
  
  class Query(graphene.ObjectType):
      users = graphene.List(User)
      user = graphene.Field(User, id=graphene.Int())
  
      def resolve_users(self, info):
          return UserModel.objects.all()
  
      def resolve_user(self, info, **kwargs):
          try:
              return UserModel.objects.get(id=kwargs['id'])
          except (UserModel.DoesNotExist, KeyError):
              return None
  
  
  class CreateUser(graphene.Mutation):
  
      class Arguments:
          username = graphene.String()
          email = graphene.String()
          password = graphene.String()
  
      user = graphene.Field(User)
  
      def mutate(self, info, username, email, password):
          user = UserModel.objects.create_user(username=username, email=email, password=password)
          return CreateUser(user=user)
  
  
  class Mutation(graphene.ObjectType):
      create_user = CreateUser.Field()
  
  
  schema = graphene.Schema(query=Query, mutation=Mutation)
  </code></pre>