fast_jsonapi
fast_jsonapi copied to clipboard
Exclude data from relationships (unless included)
This is re-opening https://github.com/Netflix/fast_jsonapi/issues/252, because AFAIK fast_jsonapi does not support this in the current version.
In our scenario it is a bit costly to fetch the ids of related resources. So not returning "data" for specific relations by default, but just "links" would great. The goal is to return something like this:
"attributes": {
...
},
"relationships": {
"comments": {
"links": {
"related": "http://localhost/api/v1/users/1/comments"
}
},
Returning a link (URL) to the related records is possible with fast_jsonapi, but excluding "data" is AFAIK not suppordet and not possible via with sparse fieldsets as mentioned in #252.
jsonapi-serializers does this via has_many :comments, include_data: true
(false by default).
@ahx How are you handling this at the moment?
@sumeetattree I am currently not using fast_jsonapi :/
@ahx Ah, damn. I just came across this library and this looks amazing on paper. Trying to move away from active_model_serializers
. What are you using at the moment?
I am trying to rewrite the serializers without touching the old ones. So the idea is to run ams
and the new library side by side until it's all ported.
What would you recommend?
@sumeetattree https://github.com/fotinakis/jsonapi-serializers works fine
Thank you. I'll take a look.
It looks like this may have been addressed in v1.5 with the :lazy_load_data
option: https://github.com/Netflix/fast_jsonapi/commit/85b41c45d4c1f1328908811d353054cada306f2b
Does this cover your use case @ahx?
@dpikt This does work to remove the need to return all IDs even if the relationship is not included, however when including the the relationship, the relationships block remains empty.
EG. Here I have passed in include: [:packages] The includes are return but the relationship block remains empty
"relationships": {
"packages": {},
},
"included": [
{
"id": "1",
"type": "packages",
"attributes": {
"title": "Unfurnished",
"description": "I want it completely unfurnished, no extra fluff for me",
"price_in_pence": 305000,
"price_per_week_in_pence": 70400,
"promo_text": "",
"currency": "GBP"
}
}
]
I believe the spec requires the following, in addition to the "included" block https://jsonapi.org/format/1.0/#document-compound-documents
"relationships": {
"packages": {
"data": [
{
"type": "packages",
"id": "1"
}
]
}
}
}
Yeah I noticed that... I was considering opening a new issue for it but I suppose this issue could work as a stand-in. Are you working on a PR for that @raldred? If not I might spin one up.
I am not, only just arrived here due to coming across the issue. I will have a look into it further, unless you've made a start @dpikt ?
I'll take a stab at it this week!
PR opened!
@dpikt Is there any updates according your PR above?
As far as I know there hasn't been any action on the PR. The entire library seems to be in stasis at the moment (see #433), if things pick back up hopefully the PR will be included in that.
Eagerly looking forward for this fix
PR ported to community fork here: https://github.com/fast-jsonapi/fast_jsonapi/pull/10
Resolved on community fork.