Search code examples
ember.jsember-datajson-api

How do you handle large relationship data attributes and compound documents?


If an article has several comments (think thousands over time). Should data.relationships.comments return with a limit?

{
  "data": [
    {
      "type": "articles",
      "id": 1,
      "attributes": {
        "title": "Some title",
      },
      "relationships": {
        "comments": {
          "links": {
            "related": "https://www.foo.com/api/v1/articles/1/comments"
          },
          "data": [
            { "type": "comment", "id": "1" }
            ...
            { "type": "comment", "id": "2000" }
          ]
        }
      }
    }
  ],
  "included": [
    {
      "type": "comments",
      "id": 1,
      "attributes": {
        "body": "Lorem ipusm",
      }
    },
    .....
    {
      "type": "comments",
      "id": 2000,
      "attributes": {
        "body": "Lorem ipusm",
      }
    },
  ]
}

This starts to feel concerning, when you think of compound documents (http://jsonapi.org/format/#document-compound-documents). Which means, the included section will list all comments as well, making the JSON payload quite large.


Solution

  • If you want to limit the number of records you get at a time from a long list use pagination (JSON API spec).

    I would load the comments separately with store.query (ember docs), like so -

    store.query('comments', { author_id: <author_id>, page: 3 });
    

    which will return the relevant subset of comments.

    If you don't initially want to make two requests per author, you could include the first 'page' in the authors request as you're doing now.

    You may also want to look into an addon like Ember Infinity (untested), which will provide an infinite scrolling list and automatically make pagination requests.