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
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 relationships
→ emotions
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.