Two-way link for relationship


#1

Hi there!
I’m trying to understand how to use relationships and stuck with it.

For example, I have a group resource which contains users:

GET /groups/123

{
  "data": {"type": "group", "id": "123"},
  "relationships": {
    "users": {
      "data": [
        {"type": "users", "id": "1"},
        {"type": "users", "id": "2"},
      ],
      "links": {
        "self": "/groups/123/relationships/users",
        "related": "/groups/123/users"
      }
    }
  }
}

Then I fetch related link and get list of users in the group.

GET /groups/123/users

{
  "data": [
    {"type": "users", "id": "1"},
    {"type": "users", "id": "2"}
  ],
  "relationships": {
    "group": {
      "data": {"type": "groups", "id": "123"},
      "links": {
         "self": "?",
         "related": "/"
      }
    }
  }
}

Is it ok to use "/" as related link and how self should looks like in this case? Or I shouldn’t use member links in the list of users at all?


#2

I don’t believe your response is a valid representation - relationships are not allowed at the top-level. You could have groups relationship for each user, but this would show all the groups for the individual user, not the group for the collection of users.

I suppose you might be able to achieve your aim by doing something like this: /users?filter[id]=1,2&include=groups and modelling data as a single “search response” resource that contained an array of users, rather than modelling data as an array of users. This probably needs more thought though. (Note: JSON API has opinions about filtering but not about searching, and arguably they are subtley different things, so “search response” might not be the best name for the resource in the response.)