Search code examples
ruby-on-railsjsonpaginationjson-api

Can a json response can be partially paginate?


I'm wondering if a json can be partially paginate.

For example

{
  "data": [{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON API paints my bikeshed!",
      "body": "The shortest article. Ever."
    }
  }],
  "included": [
    {
      "type": "people",
      "id": 42,
      "attributes": {
        "name": "John"
      }
    },
    {
      ...annnd 80000 others
      }
    }
  ]
}

Where included have soo many elements (80.000 for examples) than maybe we need pagination? But if it's paginate and we go on the next page only included elements will change, the json will still return the data.articles.

Is it a correct behavior ?

First proposal :

{
  "data": [{
    "type": "articles",
      "id": "1",
      "attributes": {
        "title": "JSON API paints my bikeshed!",
        "body": "The shortest article. Ever."
      },
      "relationships": {
        "users": {
          "link": "https://website.com/api/v1/articles/1/users.json"
        }
      }
  }]
}

Solution

  • To be compliant with the JSON API spec, your compound document must obey the full linkage requirement. Any included resources MUST be identified via relationship data.

    In your example, you could fulfill this by adding a data member under the users relationship. You could then link to every included person.

    If the relationship data is a partial set, you can use pagination links within the relationship object.