fast_jsonapi icon indicating copy to clipboard operation
fast_jsonapi copied to clipboard

Exclude data from relationships (unless included)

Open ahx opened this issue 6 years ago • 16 comments

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 avatar Dec 03 '18 12:12 ahx

@ahx How are you handling this at the moment?

sumeetattree avatar Feb 14 '19 13:02 sumeetattree

@sumeetattree I am currently not using fast_jsonapi :/

ahx avatar Feb 14 '19 14:02 ahx

@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 avatar Feb 14 '19 14:02 sumeetattree

@sumeetattree https://github.com/fotinakis/jsonapi-serializers works fine

ahx avatar Feb 14 '19 14:02 ahx

Thank you. I'll take a look.

sumeetattree avatar Feb 14 '19 15:02 sumeetattree

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 avatar May 16 '19 03:05 dpikt

@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"
          }
        ]
      }
  }
}

raldred avatar May 20 '19 12:05 raldred

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.

dpikt avatar May 20 '19 19:05 dpikt

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 ?

raldred avatar May 20 '19 20:05 raldred

I'll take a stab at it this week!

dpikt avatar May 20 '19 20:05 dpikt

PR opened!

dpikt avatar Jun 10 '19 19:06 dpikt

@dpikt Is there any updates according your PR above?

okolomoets avatar Sep 03 '19 09:09 okolomoets

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.

dpikt avatar Sep 03 '19 15:09 dpikt

Eagerly looking forward for this fix

kapilnarula avatar Sep 12 '19 10:09 kapilnarula

PR ported to community fork here: https://github.com/fast-jsonapi/fast_jsonapi/pull/10

dpikt avatar Oct 04 '19 15:10 dpikt

Resolved on community fork.

dpikt avatar Oct 08 '19 16:10 dpikt