[Answered ]-How to handle multiple records in graphql django update mutation

1πŸ‘

βœ…

To update multiple objects you need to define a new type and accordingly update your code like this:

types.py

class InputType(graphene.ObjectType):
    id = graphene.Int(required=True)
    first_name = graphene.String()
    last_name = graphene.String() 

mutation.py

class UpdateEmp(graphene.Mutation):
    emps = graphene.List(EmployeeType)

class Arguments:
    input_type = graphene.List(InputType)

@login_required
def mutate(self, info, objs):
    emp_list = [Employee.objects.filter(id=obj.pop('id')).update(**obj) for obj in objs]
    return UpdateEmp(emps=emp_list)

query:

mutation{
  uopdatemp(input_type: [{id: 1, firstName: "John", lastName: "Snow"}, {id: 2, firstName: "Tryrion", lastName: "Lannister"}])
  {
    Employee{
      id
      firstName,
      lastName
    }
    
  }

}
πŸ‘€Ahtisham

Leave a comment