Ash FrameworkAF
Ash Framework8mo ago
67 replies
Steve

Trouble Understanding Reactor

I'm taking a stab at implementing a Reactor, but the documentation isn't very clear. I've read both the Reactor documentation and the AshReactor documentation for getting started, but I'm still getting tripped up. Here is a basic example:

defmodule MyApp.Accounts.Reactors.RegisterUser do
  @moduledoc false
  use Ash.Reactor

  alias MyApp.Accounts.User

  input :email
  input :name

  create :create_user, User do
    inputs %{name: input(:name), email: input(:email)}
  end
end


From what I gather, this will forward the inputs defined inside the create to the default create action of the User resource. Am I right? How would this differ if I had to call a different action that wasn't default.

Next, I call this by placing an action on the User resource like so:

action :register_user do
  argument :email, :ci_string, allow_nil?: false
  argument :name, :string, allow_nil?: false

  run RegisterUser
end


Calling User.register_user seems to run the RegisterUser reactor, which runs the User.create action like expected, but then I get this error:

** (Ash.Error.Framework.InvalidReturnType) Invalid return from generic action Elixir.MyApp.Accounts.User.register_user.

Expected :ok or {:error, error}, got:

%MyApp.Accounts.User{sessions: #Ash.NotLoaded<:relationship, field: :sessions>, __meta__: #Ecto.Schema.Metadata<:loaded, "users">, id: "01976743-40a1-719d-b070-519808c3e38e", inserted_at: ~U[2025-06-13 03:09:10.177172Z], updated_at: ~U[2025-06-13 03:09:10.177172Z], email: #Ash.CiString<"test@example.com">, name: "Test User", role: :user, status: :pending}

    (ash 3.5.12) lib/ash/actions/action.ex:211: Ash.Actions.Action.raise_invalid_generic_action_return!/2
    (ash 3.5.12) lib/ash/actions/action.ex:143: Ash.Actions.Action.run/3
    (ash 3.5.12) lib/ash.ex:1449: Ash.run_action/2
    iex:16: (file)


Ok, so I then change my file to return just an :ok to see if I can get it to work. It looks like this:

defmodule MyApp.Accounts.Reactors.RegisterUser do
  @moduledoc false
  use Ash.Reactor

  alias MyApp.Accounts.User

  input :email
  input :name

  create :create_user, User do
    inputs %{name: input(:name), email: input(:email)}
  end

  step :format_response do
    wait_for :create_user

    run fn _args, _context ->
      :ok
    end
  end

  return :format_response
end


I run User.register_user again and now I get this error (sorry for the formatting, this is how it's output):

POST IS TOO LONG FOR DISCORD, THE REST IS IN THE FIRST COMMENT
Was this page helpful?