dongwooklee96

dongwooklee96

GET Requests in AshJsonApi with has_many resources - how to add link prefix and nested JSON in attributes field?

There are two resources in an 1: N relationship: diary and photo.
I want to load photos from an API into the diary.
In the router, there is a prefix called “/v1/api/json,” but it doesn’t seem to appear in the related link.

diary - resource

json_api do
    type "dental_diary"

    routes do
      base("/diaries")
      post(:create)
      get(:read)
      index(:list)
      patch(:update)
      delete(:destroy)

      related :photos, :read do
        primary? true
        route("/:id/photos")
      end
    end
  end

...

relationships do
    has_many :photos, Dentallog.DentalDiary.DiaryPhoto
  end

photo - resource

...
relationships do
    belongs_to :diary, Dentallog.DentalDiary.Diary do
      attribute_writable? true
    end
  end
  1. The first question is how to add the “/v1/api/json” prefix in the related link.
  2. The second question is whether there is a way to load photos as nested JSON in attributes field.

response

{
    "data": {
        "attributes": {
            "title": "1",
            "date_of_record": "2024-02-07",
            "date_of_visited": "2024-02-07",
            "pain_of_scale": 5,
            "memo": null,
            "doctor_opinion": null
        },
        "id": "fed42b30-92ee-4557-bf66-7cd38c83443b",
        "links": {},
        "meta": {},
        "type": "dental_diary",
        "relationships": {
            "photos": {
                "links": {
                    "related": "http://localhost:4000/diaries/fed42b30-92ee-4557-bf66-7cd38c83443b/photos"
                },
                "meta": {}
            },
            "paper_trail_versions": {
                "links": {},
                "meta": {}
            }
        }
    },
    "links": {
        "self": "http://localhost:4000"
    },
    "jsonapi": {
        "version": "1.0"
    }
}

Marked As Solved

barnabasJ

barnabasJ

Ash Core Team

First question:

I think the prefix has to match up with how you added the AshJsonApi.Router to your Phoenix Router

scope "/v1/api/json" do
  pipe_through(:api)

  forward "/", YourRouter
end

Second Question:

You can add the include query parameter, you can find more information about it here

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
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

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31013 112
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement