I am curious how to best handle :belongs_to relationships over openai/Swagger.
I have created a read action on a resource, which has the necessary prepare commands and of course these work beautifully over the code_interface apis and within Elixir.
My question is how best to exercise them in a similar fashion over a JSONapi. I have discovered and used the “related” macro, but would like the index/read to automatically supply those linked attributes in the main request (if that makes any sense at all!).
Thanks again for a wonderful framework that created so many new possibilites.
Martin
if you want it to happen automatically things get a bit complex. Primarily because relationships don’t show up in the attributes
key on purpose.
The way it works out of the box is using includes
. You can read more about including data in the spec at https://jsonapi.org
To configure them in ash_json_api
, you express the allowed includes of a given resource, like so:
json_api do
....
includes [relationship: [:nested_relationship]]
end
Then, in the api, you use the include
parameter. For example: include=relationship
or include=relationship.nested_relationship
Multiple includes can be separated with commas, i.e include=foo,bar.baz
However, those related entities will come back in the included
field, separate from the data.
Is that sufficient for your needs? In general we suggest not preloading those things and allowing the caller to request what it needs.
Zach:
That makes a ton of sense (and I love learning how this all comes together).
Currently I followed the documented recommendation and created the base_routes in the domain module, so it has the index macro with the :read action and default_fields argument.
It seems that the includes macro (that you pointed me at) needs to be added to the json_api on the resource itself. So I added that and can now specify the include parameter in the openapi/swagger call - but…
I am not seeing any included field in the response? I tried removing the related macros without any effect.
These relationships are all simple “belongs_to” ones - basically a uuid pointing to the “one” for which I want the string name to display in some UI. I don’t have any nested relationships. So I added " includes([:client, :product, :visit_type, :accountable_party])" to the json_api in the resource.
Is there another step that I have missed (to create the included field)?
Thanks a million for being there and responding. I am sharing this with a colleague who is amazed at the level of enthusiasm and support around Ash. He is new to Elixir and Phoenix and Ash and is quickly becoming a convert.
Thanks again
Martin
interesting. Do you have authorization policies that may be filtering them? You wouldn’t see them in the attributes
, have to look lower in the included
property. If that isn’t working it could be a bug w/ the openapi implementation. Would want to see what the request its making on curl
and the response body.
And thank you for the kind words. I’m happy to help 
No - I haven’t yet added any authorization to this. I wasn’t looking in the attributes field. My read of the openapi link was that there would now be a new field (as a sibling) for “included”.
My data field (for the index read) has: id, attributes, links, meta, type, & relationships - for each returned item.
FWIW, the get URL request is of the form:
http://localhost:4000/api/json/library/surveys?include=product,client&fields[survey]=id,title,status,client_id,product_id,visit_type_id,accountable_party_id
Does that help at all? Is there any way I can help debug the library code?
Found the issue. I just realized (or I should say that my colleague discovered) that the included fields are in a completed separate part of the response body as a sibling to “data” and not to “attributes” - which makes sense as it caters for multiple use of the same “one” entity in the various “many” entities returned in the data field.
Many thanks for all of the help again. Now we have work to do in parsing of the json data!
Martin
1 Like
IIRC there are libraries out there that can help with that, if you are using js/ts. Like client libraries that get generated from open api specs, etc.
Thanks for the library heads-up. Much appreciated.