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…
- how to set this up (api to get by primary key) correctly?
- how to get by other attributes? (e.g.,
api/json/music/artists/name/Nirvanaorapi/json/music/artists?name=Nirvana) - where to specify the load of calculations/relationships?
- 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
Trending in Questions
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Hello !
We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security










Most Liked
jkwchui
Thank you @dblack and @zachdaniel. I started with
:readbut didn’t realize the error wasapi/json/music/artist/5looking specifically for:id(and not the primary key). Usingsourceto map:ArtistIdto:idand 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
)
dblack
I think you should be able to use
get :readto use the default:readaction and then request an artist by the primary key withapi/json/music/artists/:idI’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
sourcekey Ash.Resource — ash v3.29.3zachdaniel
:by_idinget :by_idis not pointing at an action on the resource. You wantget :read.:by_idis the name of your code interface function that also points at:read. @dblack is correct.?filter[name]=foobarincludeand thefieldsparameters. You do need to configure in the resource what relationships can be included. For example: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/ash_json_apiunfortunately.