Ash Framework

AF

Ash Framework

The Elixir backend framework for unparalleled productivity. Declarative tools that let you stop wasting time. Use with Phoenix LiveView or build APIs in minutes for your front-end of choice.

Join

support

showcase

today-i-learned

AshRateLimiter - warning that unrelated update actions cannot be done atomically

I'm playing around with AshRateLimiter and have something like this: ```elixir rate_limit do hammer MyApp.Hammer...

Unable to create a resource with an array attribute

```elixir defmodule Galenic.Categorization.Request do use Ash.Resource, domain: Galenic.Categorization, data_layer: AshPostgres.DataLayer...
Solution:
your create action doesn't accept the columns attribute

Access calculation from relation caluclation.

I got a resource A the defines ``` calculate :access_count, :integer, Accounts.User.Calculations.AccessCount do public? true end...

Supports callback in Ash Validations

Is the supports/1 callback for the validation behaviour no longer available in Ash? Copied and pasted this email validation example from the Ash docs https://hexdocs.pm/ash/validations.html#supporting-queries-in-custom-validations ```elixir...
Solution:
Latest version of Ash only.

How to use one Enum value for REST/GraphQL, serialized as another to the database

Hello all, I'm still new to Elixir and the Ash framework. I'm trying to create a set of values to use for both the REST and GraphQL interfaces of my API to have values spelt out such as north, north_east, etc. spelt out. However when I read or write that field to the database, I'd like to have it serialized as string values such as N, NE, etc. Here's the code I have so far: ```elixir defmodule My.Direction do use Ash.Type.Enum values: [...
Solution:
You can override the cast_stored and dump_to_native callbacks on the type

Having trouble with AshGraphql subscriptions dsl

I created a subscription: ```elixir subscriptions do pubsub __.Endpoint subscribe :resource_name_updated do...

How to revalidate a nested AshPhoenix.Form update from an handle_event

I have this handle_event to update the value of a single nested for which has buttons to reset the input/set 50%/set100%: ```elixir def handle_event( "set_text", %{"form_path" => form_path, "field_name" => field_name, "text" => text},...
Solution:
That looks like how you'd do it currently to me

What should be the behavior of `after_action`?

The function in after_action is being called before prepare build(load: [:roles]). Is this normal behavior? ```elixir read :get_by_subject do description "Get a user by the subject claim in a JWT"...

Access magic link token as string for testing

I'm trying to write a test where a user first requests a magic link and then uses the token to log in. How can I get the generated JWT token in clear text? When doing something like this I get a token struct: ```elixir test "It is possible to sign in with token" do admin = generate(user(role: :admin))...
Solution:
I've used the test mailer for this from swoosh

Installer crash

I'm trying to install ash with some goodies but I hit an unrecoverable error and everything crashes. I'm using OTP 27 per Zach's recommendation relevant logs...

ash json patch with composite primary key

I have a resource with 2 primary keys ``` json_api do type "client_data" ...

Auxiliary Resources OK?

Hey guys, is this correct / good practice? I have 3 main categories of knowledge: Facts, Methods (or recipes, techniques), and Preferences I want to search across the three via a simple system of tags. I started with a free-form independent array of tags for each one of my main resources, but I ended up entangling myself, so I went ahead and created a Tags resource with many::many relationships in an attempt to be able to search all resources like so: ?filter[tags.name]=elixir....

How to paginate related data in json API?

This is just an example. Say I have Client and Data in many to many relationship. I have a json API to read the clients which defines a related route to read a client's related Data. Since the Data can be quite a lot, I want to force paginating it. I have a paginated read action defined in both Client and Data. However, when I call http://localhost:4000/api/v1/client/{id}/data the data is not paginated. Is this a bug or am I missing something? Here's the relevant code ```elixir...
Solution:
You can define a read action on the target resource

AshAI json schema usage

Any examples passing in json_schema to: ``` action :parse_raw, :map do argument :raw_content, :string, allow_nil?: false ...

Ash Ai connected with external MCP Servers

Hi 👋 is there an easy way to plug in mcp servers (over proxy or sse) into Ash AI?...
Solution:
You'd need to do something lower level w/ langchain.

the more tests I write, the more deadlocks I run into

The LLM wants to add max_cases to ExUnit.start(max_cases: 2) and shrink the config/test.ex App.Repo pool_size. I feel like these are bandaids and there's something fundamentally wrong with my test suite. Any tips/tricks for things to look for? Some anti-patterns in my generators.ex functions? ```bash...
Solution:
Like email: StreamData.repeatedly(fn -> "email#{System.unique_integer()}@example.com" end)

Use magic_link for both invitations and sign-in

I'd like to use the magic_link authentication strategy to both invite users, and to let existing users log in. My thinking is that in order to do that, I'll 1. Create a create action on my user resource which is limited to actor_attribute_equals(:role, :admin), and 2. Have a read action for the sign in logic. ...
Solution:
Okay so I think the way to do it would be to have two separate magic link strategies on the resource - one for sign in and one for invites. The invite one can have a longer token lifetime. When your admin is inviting someone you can trigger the request action for the invite in an after action hook.

Argument in many to many filter

I have a 2 resources Client and Data in a many to many relationship through a join resource client_data. The join resource has 2 UUID columns for the clients and data, but it also has a 3rd boolean column exposed?. When reading clients through a read action, I want to provide an arg that says to load only the exposed related Data of a Client or the non exposed or both. So I've set an argument on the read action, but now how do I access it in the filter expr on the many to many relationship?...
Solution:
```elixir prepare fn query, _ -> case query.arguments[:visibility] do :exposed -> Ash.Query.load(query, data: Ash.Query.filter(Data, parent(client_data.exposed?))) end...

Is it safe to log AuthController failures or can it leak private info?

When debugging auth problems it is helpful to log the errors in the failure function, can this lean any info or is it safe? ```elixir def failure(conn, activity, reason) do Logger.error( "Authentication activity #{inspect(activity)} failed with error: #{inspect(reason, pretty: true)}"...
Solution:
Any time you see inspect in a logger call you're likely risking leaking private data

How to get DataLayer transformer to run after optional Extension

Hi I’m working on an Ash DataLayer for Neo4j and I’ve noticed that the AshStateMachine has a transformer which adds the extended resource’s state_attribute to attributes so it introspects using Ash.Resource.Info, however this seems to only happen after my DataLayer transformer has already been run for the resource. How can I ensure in my DataLayer that my transformer runs after the optional Extension transformer?...
Solution:
You can use def after and def before to order transformers