Absinthe & Deleting

Hi Guys,

I have had a long standing question, and perhaps someone can clear it up…

I have a delete user mutation which looks like

field :delete_user, type: :user do
  arg(:id, non_null(:positive_int))
  fn info, args, %{context: context} ->
    UserResolver.delete ...
  end
  |> resolve
end

Main thing, it returns the User data which has been deleted.

Problem is, this User return type has associations, these associations don’t get populated.

From the logs I see the following Ecto/SQL calls during a deletion…

  1. Select from Users table
  2. Delete each related entry in each related table using associations
  3. Delete user entry
  4. Select each related entry in each table using associations

So basically the last select query should probably come /before/ the delete queries, in order to show the whole state of the User before deletion.

I am unsure whether this is somehow based on the Ecto lib or the Absinthe one…

Or perhaps I am doing something obviously wrong.

John

I don’t think that returning a :user type is the right thing to do from a deletion perspective. The subquery on a mutation always runs after the mutation, it’s entire purpose is to let you ask for information that reflects the update you’ve just made. If you’ve deleted the user, then it won’t have anything to return.

Most of our deletion stuff simply returns a confirmation.

4 Likes

Ahh… That is a v good point…

So in this case, you would just return the ID that was deleted or something?

I just want to understand what might be idiomatic…

1 Like

I don’t know if there’s a true idiom here, but in REST services I always return a 202 with a body of “OK” for deletes.

In GraphQL, I have an object type called operation_result that has two fields, status and message (the latter is optional for the field to fill in) and I just return OK in that status field. On the client I know to handle the deletion in whatever way makes sense (usually either refetch the query or remove the record from the cached data.)

4 Likes

Thanks!

I looked at what Github was doing,

and noticed that they returned

  1. information about the deleted object

  2. the parent object

So basically that’s what I have also done, which also is about as much information I can return while sticking with idiomatic Absinthe.

2 Likes