石墨烯的错误。突变?

时间:2016-11-04 16:12:02

标签: python django graphql graphene-python

我使用graphene-django来获得GrapQL API。 我在 schema.py

中创建了一个变异
class UpdateApplication(graphene.Mutation):
    class Input:
        id = graphene.String()
        name = graphene.String()

    application = graphene.Field(ApplicationNode)

    @classmethod
    def mutate(cls, instance, args, info):
        name = args.get('name')
        rid = from_global_id(args.get('id'))[1]
        update_application = Application.objects.filter(id=rid).update(name=name)

        return UpdateApplication(application=update_application)



class Mutation(ObjectType):
    update_application = UpdateApplication.Field()

schema = graphene.Schema(mutation=Mutation)

当我运行此resquest时,我有一个错误。

mutation update {
  updateApplication(id: "QXBwbGljYXRpb25Ob2RlOjE=", name: "foo") {
    application {
      name
    }
  }
}

错误:

mutate() takes exactly 4 arguments (5 given)

我在mutate()中放了4个参数而不是5个......这是一个bug吗?

1 个答案:

答案 0 :(得分:3)

从石墨烯1.0开始,上下文现在默认传递给变异和解析函数,而在以前的版本中需要@with_context: https://github.com/graphql-python/graphene/blob/master/UPGRADE-v1.0.md

所以你的mutate函数应该是这样的:

def mutate(self, args, context, info):
    name = args.get('name')
    rid = from_global_id(args.get('id'))[1]
    update_application = Application.objects.filter(id=rid).update(name=name)

    return UpdateApplication(application=update_application)