在执行更新操作时,mutate函数需要很多参数。根据官方的石墨烯文档,我可以使用( parent , root , obj )对象访问我的类对象。
我尝试使用父级,根级和obj访问变量数据,但这没有用。
class UpdateEvent(graphene.Mutation):
ok = graphene.Boolean()
class Arguments:
id = graphene.ID()
name = graphene.String()
event_start_date = graphene.types.datetime.Date()
event_finish_date = graphene.types.datetime.Date()
event_mark_as_finished = graphene.Boolean()
event_description = graphene.String()
event_location = graphene.String()
event_price = graphene.Float()
event_price_unit = graphene.String()
def mutate(root, info):
event = Events.objects.get(pk=root.id)
event.name = root.name
event.event_start_date = root.event_start_date
event.event_finish_date= root.event_finish_date
event.event_mark_as_finished = root.event_mark_as_finished
event.event_description = root.event_description
event.event_location = root.event_location
event.event_price = root.event_price
event.event_price_unit = root.event_price_unit
event.save()
# This class registered to the Mutation class***
"errors": [{ "message": "mutate() got an unexpected keyword argument 'event_description'"}]
将所有变量放入mutate函数中可以很好地工作,但这是一种丑陋的方式。 我该如何处理?
答案 0 :(得分:0)
用一种输入类型将它们包装起来:
class UpdateEventMutationInput(graphene.InputObjectType):
id = graphene.ID()
name = graphene.String()
event_start_date = graphene.types.datetime.Date()
event_finish_date = graphene.types.datetime.Date()
event_mark_as_finished = graphene.Boolean()
event_description = graphene.String()
event_location = graphene.String()
event_price = graphene.Float()
event_price_unit = graphene.String()
并将其用于突变:
class UpdateEvent(graphene.Mutation):
ok = graphene.Boolean()
class Arguments:
data = UpdateEventMutationInput(required=True)
def mutate(root, info, data):
event = Events.objects.get(pk=data.id)
event.name = data.name
event.event_start_date = data.event_start_date
event.event_finish_date= data.event_finish_date
event.event_mark_as_finished = data.event_mark_as_finished
event.event_description = data.event_description
event.event_location = data.event_location
event.event_price = data.event_price
event.event_price_unit = data.event_price_unit
event.save()
它还具有以下优点:
$data: UpdateEventMutationInput!
变量)