我使用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吗?
答案 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)