bgul
bgul

Reputation: 146

How to avoid using too much parameter in mutate() function?

Mutate function takes a lot of arguments while performing an update operation. According to the official graphene documentation, I could use (parent, root, obj) object to access my class objects.

I tried parent, root and obj to accessing variable data but it didn't work.

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'"}]

Putting all variables into the mutate function works perfectly but this is an ugly way. How can I handle this?

Upvotes: 1

Views: 96

Answers (1)

donnyyy
donnyyy

Reputation: 452

Wrap them with a single input type:

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()

And use it in mutation:

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()

It also brings such advantages:

  1. less code in mutation
  2. much easy to write a request for this mutation (it would contain a single $data: UpdateEventMutationInput! variable)
  3. less cross-version conflicts because you don't have to update the request as mutation input changes

Upvotes: 1

Related Questions