Guy14

Guy14

How to properly dispay relationships in the response body with AshJsonApi

Hello!

Sorry for being so talkative these days!

I’m trying to send relationships in a AshJsonApi response body, but I probably missed something.

I’ve wrote the library documentation, and this post: How to use AshJSONAPI / OpenUI to fetch key relationships for index (:read action) - #8 by MartinVolerich

Both seems to explain that sending relationships in the API response is as easy as adding an includes field, followed by one or a list of these relations:

# User Resource

  json_api do
    type "user"
    includes :emotions
  end

(my routes are defined at Domain level), as this is the recommended design in the official documentation.

I’m pretty sure my :read action returns relationships, because I tried it within IEX.

# User Resource

  actions do
    […]

    read :read do
      prepare build(load: :emotions)
    end
  end

Then, my full API response looks like that:

{
  "data": [
    {
      "attributes": {
        "name": "Guillaume"
      },
      "id": "e8dd4eef-5890-4a6b-b8aa-385fbfaef146",
      "links": {},
      "meta": {},
      "type": "user",
      "relationships": {
        "emotions": {
          "links": {},
          "meta": {}
        }
      }
    },
    {
      "attributes": {
        "name": "Hervé"
      },
      "id": "fad98c0e-d9bf-4210-9358-4a3d8cd45448",
      "links": {},
      "meta": {},
      "type": "user",
      "relationships": {
        "emotions": {
          "links": {},
          "meta": {}
        }
      }
    }
  ],
  "links": {
    "self": "http://localhost:4000/api/v1/users?includes=emotions"
  },
  "meta": {},
  "jsonapi": {
    "version": "1.0"
  }
}

I can see the field relationships with the emotions relation inside it.
But it is empty (again, I’m sure some relationships are populated for my user “Hervé”).

If I remove the includes :emotions in my json_api definition at the Resource level, I have the same output, with the relationships field, and an empty emotions field inside.

The only way I currently was able to retrieve my User’s :emotions in the AshJsonApi response is by adding the :emotions as a default field:

User Resource

  json_api do
    type "user"

    default_fields [
      :id,
      :name,
      :emotions
    ]
  end

This way, I don’t even have to keep the includes :emotion declaration, and here is the output:

{
  "data": [
    {
      "attributes": {
        "name": "Guillaume",
        "emotions": []
      },
      "id": "e8dd4eef-5890-4a6b-b8aa-385fbfaef146",
      "links": {},
      "meta": {},
      "type": "user",
      "relationships": {
        "emotions": {
          "links": {},
          "meta": {}
        }
      }
    },
    {
      "attributes": {
        "name": "Hervé",
        "emotions": [
          {
            "id": "5361ed70-c94d-412c-912f-2654e9d47556",
            "name": "Fierté",
            "basic_emotion_id": "7399f301-7a58-4175-85ac-ccd40e44af4a"
          },
          {
            "id": "155e8f1a-ef53-4418-8cf7-6c2083e0776c",
            "name": "Contentement",
            "basic_emotion_id": "7399f301-7a58-4175-85ac-ccd40e44af4a"
          },
          {
            "id": "9654784d-5e1b-407d-8c5e-5309ed2381c1",
            "name": "Frustration",
            "basic_emotion_id": "d0952031-fd83-4717-bc8c-5b00f67997ea"
          }
        ]
      },
      "id": "fad98c0e-d9bf-4210-9358-4a3d8cd45448",
      "links": {},
      "meta": {},
      "type": "user",
      "relationships": {
        "emotions": {
          "links": {},
          "meta": {}
        }
      }
    }
  ],
  "links": {
    "self": "http://localhost:4000/api/v1/users"
  },
  "meta": {},
  "jsonapi": {
    "version": "1.0"
  }
}

As you can see, my User “Hervé” has some :emotions :slight_smile:

But I guess this is not the right way to get them in the JsonApi response, and that I should be able to retrieve them in the relationshipsemotions field of the response.

P.S. to be more exhaustive, here is my emotions relationship delacation:

# User Resource

  relationships do
    many_to_many :emotions, Emotion do
      public? true
      through UserEmotion
      source_attribute_on_join_resource :user_id
      destination_attribute_on_join_resource :emotion_id
    end
  end

And here is my /users’s index route declaration:

#Users Domain

      base_route "/users", User do
        index :read
      end

NOTE: I had the same output with a belongs_to relationship before trying this one.

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

Did you also include emotions in the includes list on the resource? You have to do both.

Also Liked

zachdaniel

zachdaniel

Creator of Ash

includes defines what includes may be requested. The API follows the jsonapi.org specification. Including related data is done via a query parameter, like so ?include="emotions"

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement