Crudry v0.3.1 Crudry.Resolver View Source

Generates CRUD functions to DRY Absinthe Resolvers.

Requires a previously generated Context.

Usage

To generate CRUD functions for a given schema resolver, simply do

defmodule MyApp.MyResolver do
  alias MyApp.Repo
  alias MyApp.MyContext
  alias MyApp.MySchema
  require Crudry.Resolver

  Crudry.Resolver.generate_functions MyContext, MySchema
end

And the resolver will become

defmodule MyApp.MyResolver do
  alias MyApp.Repo
  alias MyApp.MyContext
  alias MyApp.MySchema
  require Crudry.Resolver

  def get_my_schema(%{id: id}, _info) do
    MyContext.get_my_schema(id)
    |> nil_to_error(fn record -> {:ok, record} end)
  end

  def list_my_schemas(_args, _info) do
    MyContext.list_my_schemas()
  end

  def create_my_schema(%{my_schema: params}, _info) do
    MyContext.create_my_schema(params)
  end

  def update_my_schema(%{id: id, my_schema: params}, _info) do
    MyContext.get_my_schema(id)
    |> nil_to_error(fn record -> MyContext.update_my_schema(record, params) end)
  end

  def delete_my_schema(%{id: id}, _info) do
    MyContext.get_my_schema(id)
    |> nil_to_error(fn record -> MyContext.delete_my_schema(record) end)
  end

  # If `result` is `nil`, return an error. Otherwise, apply `func` to the `result`.
  def nil_to_error(result, func) do
    case result do
      nil -> {:error, "MySchema not found."}
      %{} = record -> func.(record)
    end
  end
end

Now, suppose the update resolver for our schema should update not only the schema but also some of its associations.

defmodule MyApp.Resolver do
  alias MyApp.Repo
  alias MyApp.MyContext
  alias MyApp.MySchema
  require Crudry.Resolver

  Crudry.Resolver.generate_functions MyContext, MySchema, except: [:update]

  def update_my_schema(%{id: id, my_schema: params}, _info) do
    MyContext.get_my_schema(id)
    |> nil_to_error(fn record -> MyContext.update_my_schema_with_assocs(record, params, [:assoc]) end)
  end
end

By using the nil_to_error function, we DRY the nil checking and also ensure the error message is the same as the other auto-generated functions.

Link to this section Summary

Functions

Sets default options for the resolver

Generates CRUD functions for the schema_module resolver

Link to this section Functions

Sets default options for the resolver.

Options

  • :only - list of functions to be generated. If not empty, functions not specified in this list are not generated. Default to [].

  • :except - list of functions to not be generated. If not empty, only functions not specified in this list will be generated. Default to [].

    The accepted values for :only and :except are: [:get, :list, :create, :update, :delete].

Examples

iex> Crudry.Resolver.default only: [:create, :list]
:ok

iex> Crudry.Resolver.default except: [:get!, :list, :delete]
:ok
Link to this macro generate_functions(context, schema_module, opts \\ []) View Source (macro)

Generates CRUD functions for the schema_module resolver.

Custom options can be given. To see the available options, refer to the documenation of Crudry.Resolver.default/1.

Examples

Suppose we want to implement basic CRUD functionality for a User resolver, assuming there is an Accounts context which already implements CRUD functions for User.

defmodule MyApp.AccountsResolver do
  require Crudry.Resolver

  Crudry.Resolver.generate_functions Accounts, Accounts.User
end

Now, all this functionality is available:

AccountsResolver.get_user(%{id: id}, info)
AccountsResolver.list_users(_args, info)
AccountsResolver.crete_user(%{user: params}, info)
AccountsResolver.update_user(%{id: id, user: params}, info)
AccountsResolver.delete_user(%{id: id}, info)
AccountsResolver.nil_to_error(result, func)