2

我使用 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...这是一个错误吗?

4

1 回答 1

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)
于 2016-11-05T22:40:32.650 回答