From 9e155e55c207921f6b312437e6c090274272d104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20M=C3=A4nnchen?= Date: Tue, 23 Jul 2024 23:40:53 +0200 Subject: [PATCH] docs: document how to call a Generic Action (#1334) --- .../topics/actions/generic-actions.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/documentation/topics/actions/generic-actions.md b/documentation/topics/actions/generic-actions.md index 0ea8867d..04917286 100644 --- a/documentation/topics/actions/generic-actions.md +++ b/documentation/topics/actions/generic-actions.md @@ -78,3 +78,34 @@ end > run # ... > end > ``` + +## Calling Generic Actions + +To execute a generic action in Ash, follow these steps: + +1. **Prepare the action input:** Use `Ash.ActionInput.for_action/4` to specify the resource, the action and its arguments. +2. **Run the action:** Use `Ash.run_action/2` to execute the action with the prepared input. + +### Example Usage + +Consider an `Ash.Resource` with the action `:say_hello`: + +```elixir +action :say_hello, :string do + argument :name, :string, allow_nil?: false + + run fn input, _ -> + {:ok, "Hello: #{input.arguments.name}"} + end +end +``` + +Call this action: + +```elixir +{:ok, greeting} = Resource +|> Ash.ActionInput.for_action(:say_hello, %{name: "Alice"}) +|> Ash.run_action() + +IO.puts(greeting) # Output: Hello: Alice +``` \ No newline at end of file