Include Relationships of Relationships?


#1

This is exactly the example of the JsonApi hompage

   {
  "links": {...},
  "data": [{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON API paints my bikeshed!"
    },
    "relationships": {
      "author": {
        "links": {...},
        "data": { "type": "people", "id": "9" }
      },
      "comments": {
        "links": {...},
        "data": [
          { "type": "comments", "id": "5" },
          { "type": "comments", "id": "12" }
        ]
      }
    },
    "links": {...}
  }],
  "included": [{
    "type": "people",
    "id": "9",
    "attributes": {
      "first-name": "Dan",
      "last-name": "Gebhardt",
      "twitter": "dgeb"
    },
    "links": {...}
  }, {
    "type": "comments",
    "id": "5",
    "attributes": {
      "body": "First!"
    },
    "relationships": {
      "author": {
        "data": { "type": "people", "id": "2" }
      }
    },
    "links": {...}
  }, {
    "type": "comments",
    "id": "12",
    "attributes": {
      "body": "I like XML better"
    },
    "relationships": {
      "author": {
        "data": { "type": "people", "id": "9" }
      }
    },
    "links": {...}
  }]
}

I’m wondering why person with id 2 (which is author of comment 5) not appears in the “included” section. Would that be wrong?

What is best practice here? Include it or leave it out?


#2

Hi,

Person ID 9 is included because it is the author of the article, so this example is showing the direct relationships from the article itself.

It is perfectly valid to return the included relationships from the comments as well, but it would be up to the server implementation (most likely whether the server implementation supported the “include” parameter).

Thanks,
Cain