Is it possible to do post-processing on an entity after creating while using Phoenix/Absinthe?

Hi, all.

I am currently evaluating GraphQL for a project. While I know this is possible, I want to verify how this works.

Is it possible to do post-processing on an entity after creating while using Phoenix/Absinthe? A simple example:

If you were writing a ToDo list application, you might want to update a user’s task count and send a notification to their supervisor when a task is added to their list.

This behavior should happen in the GraphQL server so that any other users of the API don’t have to know that this exists (and implement it).

Is this possible?

Thanks!

The solution would be 100% unrelated to your GraphQL schema. You can do whatever you want in the mutation resolver.

The simplest example:

field :add_todo_item, :todo_list do
	arg :text, :string
	arg :list_id, :int
	resolve fn %{list_id: id, text: text}, %{context: %{user: user}} -> 
		with {:ok, _} <- ToDos.add(id, text) do
			Users.increment_some_count(user)
			Managers.notify_something_happened(:todo_item_added, user)
			{:ok, ToDos.get_list(id)}
		end
	end
end

Ideally, you wouldn’t do all this in the resolver.

1 Like

oh yes. this totally makes sense now.

Thanks!