AshJsonApi relationship fields

Hi. I’m using AshJsonApi with OpenAPI and I’m struggling to figure out how to specify which fields I would like to get from a relationship. For example, I have a hardware resource which has a belongs_to: :system relationship with a system resource which is on a different domain, and I would like to fetch the hardware with its system with specific fields. If I use the following value for the fields parameter, the attributes in the response include the system with the default_fields defined for that resource, which is expected:
{
"hardware": "id,created_on,system"
}
{
"hardware": "id,created_on,system"
}
If I try to use the following value for the fields parameter so that I can specify the fields for system, I get an invalid_type error for fields[system]:
{
"hardware": "id,created_on,system",
"system": "id,name"
}
{
"hardware": "id,created_on,system",
"system": "id,name"
}
I tried something similar using a different relationship with a resource that was on the same domain and, while I no longer get the invalid_type error, the fields returned for the relationship are the default_fields for that resource. Note that I've also tried using includes [:system] on the hardware resource and using the include parameter, but the data of that relationship only returns the id and type of the system resource, which is even less helpful. I would really appreciate an example of how to get this working correctly. Thanks!
3 Replies
barnabasj
barnabasj3w ago
https://jsonapi.org/format/#fetching-sparse-fieldsets According to this spec I would assume it should be something like this /hardware?include=system&fields[system]=id,type,name I'm not too familiar with the json:api spec myself, did more grapqhl in the past. When I had to do something with an ash_json_api I also just used the swagger ui that comes with it to click the query together there and just copy the url from there.
Noxis
NoxisOP3w ago
Thank you. I am using the Swagger UI and that is indeed how the query is being formed, but its either returning an invalid_type error (when the resource is in a different domain, apparently) or just not having any effect at all. I guess I still have a few more permutations to experiment with but I wanted to know if I was missing something obvious or if there was a working example I could reference.
barnabasj
barnabasj3w ago
If you can't get it working hit me up next week and we can look at it when "m back home from codebeam

Did you find this page helpful?