使用django-graphene时出错

时间:2017-10-18 13:58:58

标签: python-3.x python-3.5 graphql

我正在尝试使用graphene-django创建graphql架构。 我使用python3.5和graphene == 1.4.1和graphene-django == 1.3

这是我的模块:

class category(models.Model):
    QUESTION = 'Q'
    SOLUTION = 'S'
    UNKNOWN = 'U'
    CATEGORY_CHOICE = (
        ('quiz', (
            (QUESTION, 'Question'),
            (SOLUTION, 'Solution'),
        )
         ),
        (UNKNOWN, 'Unknown'),
    )
    name = models.CharField(max_length=1, choices=CATEGORY_CHOICE)
    image = models.ImageField(upload_to='uploads/category/', default='anonymous.jpg')

这是我的架构:

class Query(graphene.AbstractType):
    category = graphene.Field(categoryType, id=graphene.Int())
    def resolve_category(self, info, **kwargs):
        id = kwargs.get('id')
        if id is not None:
            return models.category.objects.get(pk=id)
        return None

class Query(graphene.ObjectType, schema.Query):
    pass


schema = graphene.Schema(query=Query)

使用此查询:

query{
  category(id: 1){
    id
  }
}

我收到此错误:

{
  "errors": [
    {
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "message": "resolve_category() takes 2 positional arguments but 4 were given"
    }
  ],
  "data": {
    "category": null
  }
}

感谢您的帮助。 顺便说一句,我使用过这个教程: http://docs.graphene-python.org/projects/django/en/latest/tutorial-plain/#getting-single-objects

1 个答案:

答案 0 :(得分:1)

该文档引用了2.0版本的石墨烯,它取消了一些参数。 django软件包的主要版本也受到了冲击。升级两者:

pip install "graphene>=2.0.dev"
pip install "graphene-django>=2.0.dev"