无法创建没有错误的SerializerMutation,但创建了数据

时间:2018-04-11 13:46:18

标签: django graphql graphene-python

我不知道为什么但是当我尝试使用serializermutation创建新数据时,会创建数据,但graphiql会返回错误。这是我的代码。

石墨烯(2.1) _graphene_django(2.0.0)_

模型

class CompanyRole(models.Model):
    name = models.CharField(null=False, blank=False, max_length=20)

    def __str__(self):
        return '{}'.format(self.name)

    class Meta:
        verbose_name = _('Company role')
        verbose_name_plural = _('Company roles')

串行

class CompanyRoleSerializer(serializers.ModelSerializer):

    class Meta:
        model = CompanyRole
        fields = ('name',)

突变

class CreateCompanyRole(SerializerMutation):

    class Meta:
        serializer_class = CompanyRoleSerializer

class CompanyRoleMutation(AbstractType):
        create_company_role = CreateCompanyRole.Field()

模式

class Query(CompanyRoleQuery,
            CompanyQuery,
            graphene.ObjectType):
    pass


class Mutation(CompanyRoleMutation,
               graphene.ObjectType):
    pass


schema = graphene.Schema(query=Query, mutation=Mutation, types=[
    CompanyRoleType,
    CompanyType,
])

这是使用的查询

mutation {
  createCompanyRole(input:{name:"name"}){
    errors {
      field
      messages
    }
    name    
  }
}

,这是错误

{
  "data": {
    "createCompanyRole": null
  },
  "errors": [
    {
      "message": "SubclassWithMeta_Meta object argument after ** must be a mapping, not CompanyRole",
      "locations": [
        {
          "column": 3,
          "line": 2
        }
      ]
    }
  ]
}

但我可以从管理员访问新数据。有人可以帮帮我吗?

0 个答案:

没有答案