jkwchui
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 working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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.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
)