-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement: add error handling tooling for custom queries
docs: add guide for generic actions
- Loading branch information
1 parent
6fedbd7
commit faff318
Showing
7 changed files
with
246 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Generic Actions | ||
|
||
Generic actions allow us to build any interface we want in Ash. AshGraphql | ||
has full support for generic actions, from type generation to data loading. | ||
|
||
This means that you can write actions that return records or lists of records | ||
and those will have all of their fields appropriately loadable, or you can have | ||
generic actions that return simple scalars, like integers or strings. | ||
|
||
## Examples | ||
|
||
Here we have a simple generic action returning a scalar value. | ||
|
||
```elixir | ||
graphql do | ||
queries do | ||
action :say_hello, :say_hello | ||
end | ||
end | ||
|
||
actions do | ||
action :say_hello, :string do | ||
argument :to, :string, allow_nil?: false | ||
|
||
run fn input, _ -> | ||
{:ok, "Hello, #{input.arguments.to}"} | ||
end | ||
end | ||
end | ||
``` | ||
|
||
And here we have a generic action returning a list of records. | ||
|
||
```elixir | ||
graphql do | ||
type :post | ||
|
||
queries do | ||
action :random_ten, :random_ten | ||
end | ||
end | ||
|
||
actions do | ||
action :random_ten, {:array, :struct} do | ||
constraints items: [instance_of: __MODULE__] | ||
|
||
run fn input, context -> | ||
# This is just an example, not an efficient way to get | ||
# ten random records | ||
with {:ok, records} <- Ash.read(__MODULE__) do | ||
{:ok, Enum.take_random(records, 10)} | ||
end | ||
end | ||
end | ||
end | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters