docs: add code interface to generic actions guide

This commit is contained in:
Zach Daniel 2024-07-23 17:42:32 -04:00
parent 9e155e55c2
commit 0301009073

View file

@ -71,6 +71,7 @@ end
> instance or instances of the resource. It's natural to assume that you can
> set your action's return type to the name of your resource. This won't work
> as resources do not define a type, unless they are embedded. In embedded resources, this won't work because the module is still being compiled, so referencing yourself as a type causes a compile error. Instead, use the `:struct` type and the `instance_of` constraint, like so:
>
> ```elixir
> action :get, :struct do
> constraints instance_of: __MODULE__
@ -108,4 +109,19 @@ Call this action:
|> Ash.run_action()
IO.puts(greeting) # Output: Hello: Alice
```
```
### Using Code Interface
You can also use [Code Interfaces](documentation/topics/resources/code-interfaces.md) to call actions:
Given a definition like:
```elixir
define :say_hello, args: [:name]
```
```elixir
{:ok, greeting} = Resource.say_hello("Alice")
greeting = Resource.say_hello!("Alice")
```