relationship data
Hello everyone!
I'm trying to migrate the backend of an old ember app from rails/json-api-resources to Ash. The resources produced by rails contain relationship data even when not added in includes. I can't reproduce this with Ash, and would like to know if it's possible.
From what I've seen in the docs, it seems the only way to have this data attribute filled in with the linkage data is to add the relationship in includes. But I can't see what I should do when I want the linkage data but not the relationship data itself (because it is already loaded)
5 Replies
I think we'd need to add an option for this in the route definition
I.e
show_linkage: [:foo, ...]
So you confirm it's "missing" and I should dig in the code to see if I'm able to add it ?
Yes
Unfortunately 😢
I don't think it would be too difficult to add though.
Thank you very much! Knowing that it's possible is encouraging. I'll look into it as soon as I can.
I dug into this, and it turns out there are 3 different problems:
1) the current implementation does not put "data: null" when a to-one relation is empty. It leads to unnecessary queries. The spec says we should do it, so I'm going to open a PR for that.
2) in complex cases with includes, sometimes a record appears multiple times in the preload tree, because it has multiple paths. For to-one relations, it's not handled, and the record is ignored. It leads to missing includes, which also leads to extra requests. I will also open a PR for that.
3) The app I'm working on uses a param from json_api_resources, "always_include_linkage_data". With that option set on a relation, the linkage data of that relation will always be present on the resource. It works for primary resources AND for included resources. So it extends the preload scope, adds more linkage, but does NOT add more included records. I will also open a PR for that, but I'm not sure it should be added to ash_json_api, because I can't tell from the spec if it's compliant or not.
In fact, I think 2 only happens if 3 is implemented.
Will review your PRs, thanks!