Ash Onboarding Feedback | The good, the bad and the ugly

Hey everyone!

We’ve got an influx of new people onboarding to Ash at the moment, and with our recent change over to ElixirForum, I wanted to provide a good place for new users to ask questions, vent frustrations, or point out issues with onboarding docs.

Give us the good, the bad and the ugly!

10 Likes

couldn’t spot it in the docs - are there code generators (like phx.gen.live) already? Thats my primary workflow for ordinary things:

  • generate live or just context at the correct location, tweak the underlying migration a bit for custom needs
  • customize the pregenerated liveview to suit my needs, and tweak the context functions for interactions
  • fix the tests and add a few special cases

… done :slight_smile:

declarative or not, nothing beats codegen for me :slight_smile:

Not yet, they will be coming with 3.0 We have one generator: mix ash_phoenix.gen.live but that expects an existing resource.

2 Likes

Another vote for generators! :wink:

I’ve been slowly chipping away at it but client work comes first.

2 Likes

First of all, thanks for asking :heart:

I created two tiny PRs for documentation changes for things I believe will help

The Auth0 quickstart guide seems like it could only be comfortably followed by someone that is already familiar with Ash. In my situation, I am not suing Ash at work, I am interested in it and want to play with it. When I am in that frame of mind, I want to play with the highest-level coolest thing that will give me the most leverage after I learn it, even if it requires more setup or learning time. So Auth0 felt like a natural choice for me, even after I got password auth working.

I suspect you might end up with other newbies following the same train of thought and starting with Auth0.

The most jarring piece of the process is that I had to delete the Token resource and it’s migration and all references to it after I set up Auth0 because the Token resource was only necessary for password resets, and Auth0 will handle those on their side.

2 Likes

Thank you so much for contributing back with your experience. That is invaluable! I definitely think something that we can do to clean things up is to make the auth0 getting started guide assume that you have not set up ash_authentication yet, and then along the way, it can add notes for “you may have already done this bit if you followed the initial guide”. I’ve opened an issue on ash_authentication to that effect. Rework the Auth0 Guide · Issue #443 · team-alembic/ash_authentication · GitHub

3 Likes

As a possible way to measure the readability of the documentation I wanted to see what ChatGPT could do with the documentation and several attempts at prompt engineering.

I put this prompt into ChatGPT:

"Your role is a developer.

The result required is to write an Ash Resource using the Ash DSL, Ash framework and elixir supporting code where required.

The Ash DSL is described at the following links:
DSL: Ash.Resource — ash v2.17.17
DSL: Ash.Api — ash v2.17.17
Get Started With Postgres — ash_postgres v1.3.65
DSL: AshPostgres.DataLayer — ash_postgres v1.3.65
Getting Started With Graphql — ash_graphql v0.26.8
DSL: AshGraphql.Resource — ash_graphql v0.26.8

The goal is to make a Customer resource as follows:

  • include an attributes block that defines typical customer schema fields and validation constraints.
  • use the postgres datalayer
  • define an actions block with a set of meaningful action names for managing the resource.
  • include an api block that uses the defined actions
  • has an Ash Api interface for the resource that uses the actions.
  • include an Ash graphql configuration block.

Output must describe the steps required to produce a valid elixir application using the Ash framework and Ash DSL. The output must include all supporting elixir configuration, code and mix tasks including ash database migration generators and ecto database migrations required to run the application."

From here down is what ChatGPT 3.5 produced, (it is not correct):

To create a Customer resource using the Ash framework and DSL in an Elixir application, we’ll need to follow a series of steps. I’ll outline the steps and provide code examples for each part of the process.

Please note that this is a high-level overview, and you may need to adapt it to your specific project structure and requirements.

Step 1: Set Up Your Elixir Project

Assuming you have Elixir and Mix installed, you can create a new Elixir project with the following command:

mix new my_customer_app

Change into the project directory:

cd my_customer_app

Step 2: Add Dependencies

In your mix.exs file, add the following dependencies:

defp deps do
  [
    {:ash, "~> 0.3"},
    {:ash_postgres, "~> 0.3"},
    {:ash_graphql, "~> 0.3"},
    {:ecto, "~> 3.5"},
    {:postgrex, "~> 0.15"},
    # Add other dependencies as needed
  ]
end

Then, run mix deps.get to fetch the dependencies.

Step 3: Define the Customer Resource

Create a new module for the Customer resource using Ash DSL. You can do this by creating a file customer_resource.ex in the lib/my_customer_app/resources directory (you may need to create the resources directory if it doesn’t exist):

defmodule MyApp.CustomerResource do
  use Ash.Resource,
    name: :customer,
    data_layer: Ash.Postgres.DataLayer

  attributes do
    field :name, :string
    field :email, :string
    # Add other fields and validation constraints as needed
  end

  actions do
    create
    read
    update
    delete
  end

  api do
    actions [:create, :read, :update, :delete]
  end

  graphql do
    type :customer do
      fields [:id, :name, :email]
    end
  end
end

Step 4: Create Ecto Migration for Customer

Generate an Ecto migration to create the Customer table in your database:

mix ecto.gen.migration create_customer_table

Open the generated migration file (in the priv/repo/migrations directory) and define the schema for the Customer table. For example:

defmodule MyCustomerApp.Repo.Migrations.CreateCustomerTable do
  use Ecto.Migration

  def change do
    create table(:customers) do
      add :name, :string
      add :email, :string

      timestamps()
    end
  end
end

Then, run the migration:

mix ecto.migrate

Step 5: Configure Ash

In your config/config.exs file, configure Ash with your database connection and resource:

config :my_customer_app, MyCustomerApp.Repo,
  adapter: Ecto.Adapters.Postgres,
  database: "my_customer_app_dev",
  username: "postgres",
  password: "password",
  hostname: "localhost"

config :my_customer_app, Ash,
  data_layers: [MyCustomerApp.Repo],
  resources: [MyApp.CustomerResource]

Step 6: Generate GraphQL Schema

Create a module to define your GraphQL schema in lib/my_customer_app_web/schema.ex:

defmodule MyCustomerAppWeb.Schema do
  use Ash.GraphQL.Schema

  query do
    field :customer, type: :customer
    # Add other queries as needed
  end

  mutation do
    field :createCustomer, type: :customer
    field :updateCustomer, type: :customer
    field :deleteCustomer, type: :customer
    # Add other mutations as needed
  end
end

Step 7: Mount the GraphQL Endpoint

In your Phoenix router (lib/my_customer_app_web/router.ex), mount the GraphQL endpoint:

forward "/graphql", Absinthe.Plug,
  schema: MyCustomerAppWeb.Schema,
  context: %{}

Step 8: Run the Application

Now you can start your Elixir application:

mix phx.server

Your Ash-based Customer resource is now integrated into your Elixir application. You can access the GraphQL endpoint to interact with the Customer resource.

Please note that this is a simplified example, and in a real-world application, you would likely need to add more features like authentication, authorization, and error handling based on your specific requirements. Additionally, you should configure your production and test environments accordingly.

Might be worth putting that in gist and linking to it, so that it isn’t potentially confused by someone who is just scanning as valid Ash code :laughing: Its got some bits right and many other bits wrong.

1 Like

Honestly I was surprised it produced what it did, but the approach could be a litmus test for measuring comprehension and structure of the documentation. i.e a test suite of scenario based prompts fed into ChatGPT actually produce valid Elixir Ash programs.

Caveat, I am no prompt engineer, but good comprehensibility should not require too much finessing with the prompt to get a sane answer.

UPDATED the post to flag it as not correct.

And also a GiST of a slightly better prompt that produces validations in the output. I had to update the GiST a few times as I discovered that with full stops in lists would cause ChatGPT to ignore some the other things I asked for.

This page on Relationships shows how to load and read related data, but it does not show how to create it.

I eventually figured it out from the Livebook guides.