Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to do a delete operation with GraphQL + graphene

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

like image 671
user3411864 Avatar asked Dec 03 '18 15:12

user3411864


People also ask

Can you delete with GraphQL?

Using the parse GraphQL, there are two different ways to delete an existing object in your database: Using generic mutation - this is the mutation that you can use to delete an object of any class. Using class mutation - this is the recommended mutation that you should use to delete an object of a specific class.

What is Django GraphQL?

GraphQL is an open-source data query and manipulation language for APIs, and a runtime for fulfilling queries with existing data. It was developed internally by Facebook in 2012 before being publicly released in 2015.


1 Answers

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)
like image 175
Mark Chackerian Avatar answered Sep 28 '22 14:09

Mark Chackerian