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

How to restore LiveView state when using infinite scroll when browser back button is clicked?

I have a UX problem I'm trying to solve and not entirely sure how to go about solving it. User is on LiveView 1 that is a list view using infinite pagination + sort params + filter params User clicks on a record that navigates them to the record details LiveView 2 When the user clicks the browser back button, they expect to be taken back to the list view in the same state with the scroll position restored showing the record they clicked on....

Getting Started with API Extension Building

I've been using AshJsonApi pretty much since I started with Ash. My goal was to make some improvements to it and continue on. Long story short, I'm integrating with a... let's just call it "legacy" system and for a number of reasons—some sensible, some not—I'm going to need APIs that better resemble the existing infrastructure. I can't even really call these a "standard" so much as... I don't even know. Additionally, there's a team here looking to standardize internal service communication using gRPC....

Custom auth pages

Is there a simple example of custom auth pages for the AshAuth package. It's very hard trying to figure it out from the deps folder as those pages are components and configs upon components and configs. What i specifically want is to understand how can i control the magic link page to show a "Your email has been sent" message instead of the form. But at the moment it returns to the home page "/" ...
Solution:
It does submit using trigger_submit
No description

Ash query with 1 filter performs worse than the same Ecto query

We've noticed quite bad performance when querying data produced by paper_trail using Ash. The same kind of queries made with regular Ecto.Query perform much better. The tables involved are huge (it's paper trail after all), but the column by which we filter is indexed. Upon examining the difference between the produced queries, the only difference I see is in the WHERE clause: ...

Can reads return anything?

I have a search action that needs to return an array of tuples consisting of a resource reference and an atom that conveys why the resource was found by the search. There is only one type of resource involved so the action fits nicely in the resource. Can this be a read action or should it be generic?
Solution:
read actions always return lists of records pertaining to the resource

AshAuthentication with an already existing auth flow

I'm integrating Ash with a system that already has an authentication workflow. It's a passwordless flow that sends an OTP to the user's email and then generates a token for them, assuming everything checks out. I see that many Ash libraries integrate nicely with AshAuthentication. I noticed in the documentation that there's a method to integrate custom strategies. However, as I'm new to this, I'm having trouble fully understanding the documentation. The link can be found here: https://hexdocs.pm/ash_authentication/custom-strategy.html Can I integrate our existing auth flow into AshAuthentication using the custom strategy model? These OTPs are used for registration, verifying the user before updating certain information (e.g., updating their email), verifying their new email, and logging in. If it's possible, I'd like to integrate it to benefit from using it. I'll just have to buckle down and do my best to learn. If there's an easier way to integrate while maintaining compatibility with other Ash libraries, please let me know. Ultimately, I want my authentication system to work seamlessly with all the resources and tools I add....
Solution:
Honestly, there isn't much need to integrate it as a custom strategy

Weird policy behavior

Given the following policy: ``` policy action_type(:read) do forbid_if expr(is_nil(^actor(:organization_id))) authorize_if expr(id == ^actor(:organization_id))...
Solution:
No, the way you're doing it makes sense.

Error Loading 3 `no_attributes?: true` nested relationships

I'm running into an error that's very similar to one I encountered before. The structure is Grandpappy > has_many > Parent > has_many > Child > has_many Toy Grandparent also has_many :grandchild_toys via a no_attributes?: true relationship:...

Single File Ash Authentication

https://gist.github.com/TwistingTwists/7e17076db32e22b4e46e8fa15e3ae64c This is an attempt to have a single file AshAuthentication working with password strategy. however, on the localhost:4000/register route i cannot see any form. Should I build one?...

AshGraphQL bulk operation

How do I make graphql perform bulk upsert? I couldn't find anything in ash graphql docs, but I saw ash supported bulk actions...

ash.codegen --dev & ash_ai / vector attr type

Not sure if this is an ash_ai thing or an ash.codegen --dev with vectors thing, (I'm guessing the latter) but my project is deadlocked now since it wants to add this: ```use Ecto.Migration def up do...
Solution:
fix released

AshGraphQL mutation with metadata

I want to make a File resource, which will have a path to S3, a filename etc. And I want to define a :create action :sign_upload_url that will return a metadata :upload_url, however I can't make it return the metadata from mutation
Solution:
```elixir create :create do metadata :foo, :string ......

Testing liveviews requiring users to be logged in.

I'm only using the magic link strategy and I'm following the test setup from tunez to configure my tests. However, I'm getting this error - ** (KeyError) key :token not found in: %{selected: [:id, :role, :email, :updated_at, :inserted_at, :organization_id]} when I try to create a user and sign it in for the test. this is the test: ...
Solution:
Solved the issue. registration is disabled, so I had do create a user with a regular create action. then to sign in I had to perform the following incantation: ``` strategy = AshAuthentication.Info.strategy!(MyApp.Accounts.User, :magic_link) {:ok, token} = AshAuthentication.Strategy.MagicLink.request_token_for(strategy, user)...

Zipper replace and Rewrite

I'm playing around with igniter trying to figure this out. Not sure if on the right track. :thinkies: Let's say I got to a change block and I Sourceror.Zipper.replace replaced the third argument with a module that I will also create. I was thinking of doing like a git diff, which I think diff_and_yes? function does. But I can't figure out how does this relate to rewrite/source. ...
Solution:
You can use Igniter.do_or_dry_run

before_action in bulk_create

I'm implementing some logic that would bulk_destroy existing records and bulk_create in a transaction ``` create :replace_signatures do...

Invalid default values

One resource runs into an issue that I haven't had before and cannot solve. I've already updated all dependencies and removed the _build directory without success. The resource has some attributes ```elixir attribute :prefix, :string...
No description

Shared Validations

Where are shared validations typically stored? For example, say I have a User resource that has an email that has format and length requirements. Say there was a second resource where I wanted to share those same validations. Where would I store that? Normally I would put it in the MyApp.Accounts.User.Validations.Email module, but I'm not sure if that's appropriate to share across resources as a validation. The only alternative I can think of is having something like `MyApp.Accounts.Shared...
Solution:
There's no hard rule, but for me it's typically just MyApp.Domain.Validations...

Best way to run bulk ops in a transaction

I’m new to Ash, and I have a fairly simple code problem. I need to delete all the records for a resource for a given user and then bulk create new ones of the same resource for the same user. I want these two bulk operations to be in a transaction so they’re rolled back on error. I’ve been able to do this in a generic action by using bulk_destroy followed by bulk_create. However, I’ve not been able to figure out how to do this in a multi-step way by using callbacks (to delete first) which seems like it would be the Ash way. Am I missing something or is a generic action the way to go?...

Does `mix ash.codegen` generate drop migrations for deleted resources

I'm removing a couple resources and after deleting them mix ash.codegen says everything is up-to-date
Solution:
it does not, you'll have to do your own migrations for that currently

The domain Foo.Bar requires that an actor is provided at all times and none was provided.

Noticed this when running tests against latest main. We have our domains set up like: ```elixir authorization do...
Solution:
just pushed to main