graphene.Mutation 错误?

Bug with graphene.Mutation?

我使用 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)

当我运行这个请求时,我有一个错误。

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

错误:

mutate() takes exactly 4 arguments (5 given)

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

从石墨烯 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)