0

I'm coming from ActiveModelSerializer and trying to grasp the JSON API spec with the fast_jsonapi gem. One thing I've been wrestling with for a little while now is how to handle associations, or relationships in this particular case. I understand that with relationships, fast_jsonapi returns a hash only consisting of the id and type of a child. With that in mind, how do I get the data I need without creating a compound document?

Someone had recommended using links in the relationship object to reduce the # of queries but this confuses me as well. The way I'm seeing this is that I'm potentially making multiple api calls for one object (at the very least). Can someone break this down for me?

Carl Edwards
  • 13,826
  • 11
  • 57
  • 119
  • 1
    What's wrong w/ a compound document? That's how you load related records via a single request (via the [includes](https://github.com/Netflix/fast_jsonapi#compound-document) here) You'll still get id/type values back in a relationship subkey for each record, but your response will also have a top-level `included` key which will have all the records for everything you specified in the `include=foo` param – Jay Dorsey May 29 '19 at 02:51
  • @JayDorsey the one main concern with compound documents is the convolusion involved. From what I've seen so far the objects are no longer grouped. – Carl Edwards May 29 '19 at 03:00
  • 1
    That's correct, the objects are no longer grouped. This creates (in certain cases) a smaller response size such as when multiple records map back to the same relationship. You use the id/type in the relationships subkey to lookup the data in the included key. There should be some frontend libraries that handle mapping everything (I know one for ember exists, at least). The alternative is, as you noted, to use the relationships links and make multiple API calls. – Jay Dorsey May 29 '19 at 12:18
  • I guess now I'm wondering what type of performance hit you take if the compound object has potentially hundreds of objects. How could pagination be factored into this? – Carl Edwards May 31 '19 at 13:42
  • That's actually a great, valid question. The spec has something to say about [include pagination](https://jsonapi.org/format/#fetching-pagination) "To paginate an included collection returned in a compound document, supply pagination links in the corresponding links object." I'm not sure if that's supported by fastjson_api. The jsonapi-resources gem provides a relationships link you can use to find relationships for an object; you use pagination as normal after following that link. It's an extra API call (to get the index of that child/related items) but I don't think unreasonable – Jay Dorsey May 31 '19 at 14:04

0 Answers0