Built-in Plugin Batch

Learn another type of built-in plugin batch.

We'll cover the following...

Batch

The problem with async, of course, is that while it’s faster than serial database queries, we still do N number of them to get N number of categories when we could really do just one database query to get all the categories. We need a way to aggregate values, a function that can use those aggregated values to run an SQL query, and then get those values back into individual fields. Fortunately, the Absinthe.Middleware.Batch plugin has our back. Let’s see how it looks in our resolver:

Press + to interact
import Absinthe.Resolution.Helpers, only: [batch: 3]
# «Rest of file»
def category_for_item(menu_item, _, _) do
batch({PlateSlate.Menu, :categories_by_id}, menu_item.category_id, fn
categories ->
{:ok, Map.get(categories, menu_item.category_id)}
end) |> IO.inspect
end

Like before, we have an Absinthe helper function we import to provide a nice API and an |> IO.inspect at the end. Hence, we’ll be able to see in a second what this function returns. The ...