Best way to do a delete operation with GraphQL + graphene
Asked Answered
S

1

6

I'm struggling to get my Delete operation working. My Create, Read and Update are working fine, but a Delete has nothing to return.

class DeleteEmployeeInput(graphene.InputObjectType):
    """Arguments to delete an employee."""
    id = graphene.ID(required=True, description="Global Id of the employee.")


class DeleteEmployee(graphene.Mutation):
    """Delete an employee."""
    employee = graphene.Field(
        lambda: Employee, description="Employee deleted by this mutation.")

    class Arguments:
        input = DeleteEmployeeInput(required=True)

    def mutate(self, info, input):
        data = utils.input_to_dictionary(input)
        #data['edited'] = datetime.utcnow()

        employee = db_session.query(
            EmployeeModel).filter_by(id=data['id'])
        employee.delete(data['id'])
        db_session.commit()
        #employee = db_session.query(
            #EmployeeModel).filter_by(id=data['id']).first()

        #return DeleteEmployee(employee=employee)

What is the best way to delete an entry? I assume I have to return an OK or an Error.

When I run my mutation:

mutation {
  deleteEmployee (input: {
       id: "RW1wbG95ZWU6MQ=="
  }) 
}

I get the error Field \"deleteEmployee\" of type \"DeleteEmployee\" must have a sub selection."

Note the commented out lines

Spellbinder answered 3/12, 2018 at 15:39 Comment(0)
E
7

Try replacing employee = graphene.Field... with ok = graphene.Boolean() and then make the last line of your mutate method return DeleteEmployee(ok=True)

Your mutate method will then look something like:

    def mutate(self, info, input):
        ... skipping deletion code ...
        db_session.commit()
        return DeleteEmployee(ok=True)
Englis answered 3/12, 2018 at 18:53 Comment(7)
That worked perfectly. I'm relatively new to python as you may have noticed. What exactly happens in the return line return DeleteEmployee(ok=True)? It returns an new instance of the same class DeleteEmployee? The ok=True is an kwarg that is passed to the constructor of the graphene.Mutation class?Spellbinder
The last line of the mutate method creates an object that is the output of the mutation. It would make more sense in the documentation if the mutate method was consistently shown a classmethod.Englis
I'm still a bit confused, but I guess I have to grab some 101 udemy courses. For example the line input = DeleteEmployeeInput(required=True) The class DeleteEmployeeInput itself doesn't have a (visible) variable required. It is placed in the super() class graphene.InputObjectType?Spellbinder
DeleteEmployeeInput is defining the shape of the inputs, whereas inside class Arguments you are defining the relationship between DeleteEmployee and its inputs, which is why required goes there.Englis
So required is a python convention and not a graphene variable? Because I don't see where the variable required is createdSpellbinder
required is an argument to method that is creating the graphene fieldEnglis
DeleteEmployeeInput is an instance of graphene.InputObjectType with an additional attribute id right? So to my understanding the class attribute required has to be created inside graphene.InputObjectType but I don't see one in there. I'm coming from PHP btw, maybe I'm missing something crucial here.Spellbinder

© 2022 - 2024 — McMap. All rights reserved.