|
| 1 | +# Rules for working with AshJsonApi |
| 2 | + |
| 3 | +## Understanding AshJsonApi |
| 4 | + |
| 5 | +AshJsonApi is a package for integrating Ash Framework with the JSON:API specification. It provides tools for generating JSON:API compliant endpoints from your Ash resources. AshJsonApi allows you to expose your Ash resources through a standardized RESTful API, supporting all JSON:API features like filtering, sorting, pagination, includes, and relationships. |
| 6 | + |
| 7 | +## Domain Configuration |
| 8 | + |
| 9 | +AshJsonApi works by extending your Ash domains and resources with JSON:API capabilities. First, add the AshJsonApi extension to your domain. |
| 10 | + |
| 11 | +### Setting Up Your Domain |
| 12 | + |
| 13 | +```elixir |
| 14 | +defmodule MyApp.Blog do |
| 15 | + use Ash.Domain, |
| 16 | + extensions: [ |
| 17 | + AshJsonApi.Domain |
| 18 | + ] |
| 19 | + |
| 20 | + json_api do |
| 21 | + # Define JSON:API-specific settings for this domain |
| 22 | + authorize? true |
| 23 | + |
| 24 | + # You can define routes at the domain level |
| 25 | + routes do |
| 26 | + base_route "/posts", MyApp.Blog.Post do |
| 27 | + get :read |
| 28 | + index :read |
| 29 | + post :create |
| 30 | + patch :update |
| 31 | + delete :destroy |
| 32 | + end |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + resources do |
| 37 | + resource MyApp.Blog.Post |
| 38 | + resource MyApp.Blog.Comment |
| 39 | + end |
| 40 | +end |
| 41 | +``` |
| 42 | + |
| 43 | +## Resource Configuration |
| 44 | + |
| 45 | +Each resource that you want to expose via JSON:API needs to include the AshJsonApi.Resource extension. |
| 46 | + |
| 47 | +### Setting Up Resources |
| 48 | + |
| 49 | +```elixir |
| 50 | +defmodule MyApp.Blog.Post do |
| 51 | + use Ash.Resource, |
| 52 | + domain: MyApp.Blog, |
| 53 | + extensions: [AshJsonApi.Resource] |
| 54 | + |
| 55 | + attributes do |
| 56 | + uuid_primary_key :id |
| 57 | + attribute :title, :string |
| 58 | + attribute :body, :string |
| 59 | + attribute :published, :boolean |
| 60 | + end |
| 61 | + |
| 62 | + relationships do |
| 63 | + belongs_to :author, MyApp.Accounts.User |
| 64 | + has_many :comments, MyApp.Blog.Comment |
| 65 | + end |
| 66 | + |
| 67 | + json_api do |
| 68 | + # The JSON:API type name (required) |
| 69 | + type "post" |
| 70 | + end |
| 71 | + |
| 72 | + actions do |
| 73 | + defaults [:create, :read, :update, :destroy] |
| 74 | + |
| 75 | + read :list_published do |
| 76 | + filter expr(published == true) |
| 77 | + end |
| 78 | + |
| 79 | + update :publish do |
| 80 | + accept [] |
| 81 | + change set_attribute(:published, true) |
| 82 | + end |
| 83 | + end |
| 84 | +end |
| 85 | +``` |
| 86 | + |
| 87 | +## Route Types |
| 88 | + |
| 89 | +AshJsonApi supports various route types according to the JSON:API spec: |
| 90 | + |
| 91 | +- `get` - Fetch a single resource by ID |
| 92 | +- `index` - List resources, with support for filtering, sorting, and pagination |
| 93 | +- `post` - Create a new resource |
| 94 | +- `patch` - Update an existing resource |
| 95 | +- `delete` - Destroy an existing resource |
| 96 | +- `related` - Fetch related resources (e.g., `/posts/123/comments`) |
| 97 | +- `relationship` - Fetch relationship data (e.g., `/posts/123/relationships/comments`) |
| 98 | +- `post_to_relationship` - Add to a relationship |
| 99 | +- `patch_relationship` - Replace a relationship |
| 100 | +- `delete_from_relationship` - Remove from a relationship |
| 101 | + |
| 102 | +## JSON:API Pagination, Filtering, and Sorting |
| 103 | + |
| 104 | +AshJsonApi supports standard JSON:API query parameters: |
| 105 | + |
| 106 | +- Filter: `?filter[attribute]=value` |
| 107 | +- Sort: `?sort=attribute,-other_attribute` (descending with `-`) |
| 108 | +- Pagination: `?page[number]=2&page[size]=10` |
| 109 | +- Includes: `?include=author,comments.author` |
0 commit comments