Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONAPI - Difference between self and related in a links resource

Tags:

json-api

Why is the self and related references different in the below JSONAPI resource? Aren't they pointing to the same resource? What is the difference between going to /articles/1/relationships/tags and /articles/1/tags?

{
  "links": {
    "self": "/articles/1/relationships/tags",
    "related": "/articles/1/tags"
  },
  "data": [
    { "type": "tags", "id": "2" },
    { "type": "tags", "id": "3" }
  ]
}
like image 320
jax Avatar asked Sep 11 '15 11:09

jax


People also ask

What is Self in API?

Overview. The “Self” API returns detailed information about the logged-in user.

What is JSON:API format?

What Is JSON API (JSONAPI.org)? JSON API is a format that works with HTTP. It delineates how clients should request or edit data from a server, and how the server should respond to said requests. A main goal of the specification (now at a stable v1.

What is link relation in API?

A link relation is a descriptive attribute attached to a hyperlink in order to define the type of the link, or the relationship between the source and destination resources. The attribute can be used by automated systems, or can be presented to a user in a different way.

Is JSON a type of API?

JSON:API is a specification for how a client should request that resources be fetched or modified, and how a server should respond to those requests. JSON:API is designed to minimize both the number of requests and the amount of data transmitted between clients and servers.


2 Answers

You can read about that here: https://github.com/json-api/json-api/issues/508.

Basically, with /articles/1/relationships/tags response will be object which represents relationship between articles and tags. The response could be something like this (what you put in your question):

{
  "links": {
    "self": "/articles/1/relationships/tags",
    "related": "/articles/1/tags"
  },
  "data": [
    { "type": "tags", "id": "2" },
    { "type": "tags", "id": "3" }
  ]
}

This response gives only the necessary data (in primary data attribute - data) to manipulate the relationship and not resources connected with relationship. That being said, you'll call /articles/1/relationships/tags if you want to create new relationship, add a new tag (basically updating relationship) to article, read which tags belong to article (you only need identity to search them on server) or delete article tags.

On the other hand, calling /articles/1/tags will respond with tags as primary data with all the other properties that they have (articles, relationships, links, and other top-level attributes such include, emphasized text, links and/or jsonapi).

like image 182
Tommz Avatar answered Oct 16 '22 00:10

Tommz


They are different. Here is an example from my project.

Try Get http://localhost:3000/phone-numbers/1/relationships/contact you will get response like this:

{
  "links": {
    "self": "http://localhost:3000/phone-numbers/1/relationships/contact",
    "related": "http://localhost:3000/phone-numbers/1/contact"
  },
  "data": {
    "type": "contacts",
    "id": "1"
  }
}

You didn't get the attributes and relationships which is probably you want to retrieve.

Then Try Get http://localhost:3000/phone-numbers/1/contact you will get response like this:

{
  "data": {
    "id": "1",
    "type": "contacts",
    "links": {
      "self": "http://localhost:3000/contacts/1"
    },
    "attributes": {
      "name-first": "John",
      "name-last": "Doe",
      "email": "[email protected]",
      "twitter": null
    },
    "relationships": {
      "phone-numbers": {
        "links": {
          "self": "http://localhost:3000/contacts/1/relationships/phone-numbers",
          "related": "http://localhost:3000/contacts/1/phone-numbers"
        }
      }
    }
  }
}

You can see you retrieved all the information you want, including the attributes and relationships.

But you should know that relationships can be used for some purpose. Please read http://jsonapi.org/format/#crud-updating-to-one-relationships as a sample.

like image 24
Lane Avatar answered Oct 16 '22 02:10

Lane