jkwchui

jkwchui

What is the right way to setup AshJsonApi?

I have an sqlite database (Chinook, iTunes-like data), for which I have setup a simple Phoenix project.

The birds eye view:

  • Repo: Music.Repo
  • Api: Music
  • Router: TutorialWeb.Music.Router
  • Resources: Music.Album, Music.Artist

I setup a JsonApi for Music.Artist. The path api/json/music/artists works, but api/json/music/artists/:id errors (KeyError at GET /api/json/music/artists/5 key :arguments not found in: nil).

In IEx, Music.Artist.by_id!/1 works.

And so here are the questions…

  1. how to set this up (api to get by primary key) correctly?
  2. how to get by other attributes? (e.g., api/json/music/artists/name/Nirvana or api/json/music/artists?name=Nirvana)
  3. where to specify the load of calculations/relationships?
  4. are there public examples of AshJsonApi that I can read/learn from?

Truncated code follows:

# Api
defmodule Music do
  use Ash.Api,
    extensions: [AshJsonApi.Api]

  resources do
    resource Music.Album
    resource Music.Artist
end
# Router
defmodule TutorialWeb.Music.Router do
  use AshJsonApi.Api.Router,
    apis: [Music]
end
# Artist resource
defmodule Music.Artist do
  use Ash.Resource,
    data_layer: AshSqlite.DataLayer,
    extensions: [AshJsonApi.Resource]

  sqlite do
    repo Music.Repo
    table "Artist"
  end

  json_api do
    type "artist"

    routes do
      base "/artists"

      get :by_id
      index :read
      # post :create
      # ...
    end
  end

  attributes do
    attribute :ArtistId, :integer do # the unusual upcase attribute name comes from source db
      primary_key? true
      allow_nil? false
    end

    attribute :Name, :string
  end

  code_interface do
    define_for Music

    define :create
    define :read

    define :by_id,
      get_by: [:ArtistId],
      action: :read

    define :by_name,
      get_by: [:Name],
      action: :read

    define :alphabetical

    define :update
    define :destroy
  end

    actions do
      defaults [:create, :read, :update, :destroy]
  end
end

Most Liked

jkwchui

jkwchui

Thank you @dblack and @zachdaniel. I started with :read but didn’t realize the error was api/json/music/artist/5 looking specifically for :id (and not the primary key). Using source to map :ArtistId to :id and this is working.

And thank you for pointing to the tests — these are terse and to the point, super clear and easy to learn from.

(4) public example: I now have ducks-in-a-row for a 2nd Primer tutorial. This starts from an sqlite c/ Livebook, passes through identities, validation, pagination, telemetry, bulk create, mermaid diagrams, and end with a minimal Phoenix app showing a json api. The Mix/Phoenix part will probably be a Github repo with sequential commits.

(This would set the stage for the last Primer, that uses AirTable-sync’ed postgres, and have auth / policies / pubsub. The trilogy should give someone new to Ash a three-hour straight path through the most important features, and let y’all answer me once instead of answering the same questions repeatedly :stuck_out_tongue: )

dblack

dblack

I think you should be able to use get :read to use the default :read action and then request an artist by the primary key with api/json/music/artists/:id

I’m not sure about your other questions but the tests have some pretty good examples for loading related stuff and filtering.

Also, if you would like to abstract away the funky database column naming, you can specify the source key Ash.Resource — ash v3.29.3

attribute :id, :integer do
  primary_key? true
  allow_nil? false
  source :ArtistId
end
zachdaniel

zachdaniel

Creator of Ash
  1. there error is bad here, but :by_id in get :by_id is not pointing at an action on the resource. You want get :read. :by_id is the name of your code interface function that also points at :read. @dblack is correct.
  2. you can pass a fiiter statement ?filter[name]=foobar
  3. You don’t specify loads in the DSL/actions, you allow the client to specify them using the include and the fields parameters. You do need to configure in the resource what relationships can be included. For example:
json_api do
  includes [
    friends: [
      :comments
    ],
    comments: []
  ]
end

Would allow using ?include=friends.comments,comments. See the JSON:API spec for more information on how fields and includes are meant to work. https://jsonapi.org/

  1. I don’t know of any open source implementations of ash_json_api unfortunately.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

We're in Beta

About us Mission Statement