batch_loader v0.1.0-beta.2 BatchLoader.Absinthe View Source

A module which integrates BatchLoader with Absinthe.

Link to this section Summary

Functions

Creates a BatchLoader struct and calls the BatchLoader.Absinthe.Middleware, which will batch all collected items.

Creates a BatchLoader struct and calls the BatchLoader.Absinthe.Middleware, which will preload an Ecto association.

Creates a resolve function, which creates a BatchLoader struct and calls the BatchLoader.Absinthe.Middleware, which return an Ecto association.

Link to this section Functions

Link to this function

for(item, batch, options \\ [default_value: {:ok, nil}])

View Source

Creates a BatchLoader struct and calls the BatchLoader.Absinthe.Middleware, which will batch all collected items.

Example

field :user, :user_type do
  resolve(fn post, _, _ ->
    BatchLoader.Absinthe.for(post.user_id, fn user_ids ->
      Repo.all(from u in User, where: u.id in ^user_ids)
      |> Enum.map(fn user -> {user.id, {:ok, user}} end)
    end)
  end)
end
Link to this function

preload_assoc(item, assoc, callback, options \\ [default_value: {:ok, nil}, repo: nil, preload_opts: []])

View Source

Creates a BatchLoader struct and calls the BatchLoader.Absinthe.Middleware, which will preload an Ecto association.

Example

field :title, :string do
  resolve(fn post, _, _ ->
    BatchLoader.Absinthe.preload_assoc(post, :user, fn post_with_user ->
      {:ok, "#{post_with_user.title} - #{post_with_user.user.name}"}
    end)
  end)
end
Link to this function

resolve_assoc(assoc, options \\ [default_value: {:ok, nil}, repo: nil, preload_opts: []])

View Source

Creates a resolve function, which creates a BatchLoader struct and calls the BatchLoader.Absinthe.Middleware, which return an Ecto association.

Example

field :user, :user_type, resolve: BatchLoader.Absinthe.resolve_assoc(:user)