ash_json_api v0.5.0 AshJsonApi View Source
An ash extension for building a JSON:API with ash resources.
Usage
Assume you have already built a resource using Ash such as this Post resource:
defmodule Post do
use Ash.Resource,
data_layer: Ash.DataLayer.Postgres
actions do
read :default
create :default
end
attributes do
attribute :name, :string
end
relationships do
belongs_to :author, Author
end
end
As you can see, the resource takes care of interacting with the database, setting up attributes and relationships, as well as specifying actions (CRUD) that can be performed on the resource. What is now needed is to add a configuration for how this resource will interact with JSON:API
defmodule Post do
use Ash.Resource,
data_layer: AshPostgres,
extensions: [AshJsonApi.Resource]
...
json_api do
routes do
base "/posts"
# Add a `GET /posts/:id` route, that calls into the :read action called :default
get :default
# Add a `GET /posts` route, that calls into the :read action called :default
index :default
end
# Expose these attributes in the API
fields [:name]
end
...
Then, update your API with the API extension and configuration:
defmodule MyApp.Api do
use Ash.Api, extensions: [AshJsonApi.Api]
json_api do
...
end
end
See AshJsonApi.Api
and AshJsonApi.Resource
for the DSL documentation.