Wednesday, April 28, 2021

REST-API, JSON:API, GraphQL API whats the difference

 Hi All, 


After long time again writing some thing related to technical. So recently i saw there is a confusion which api specification to use. between REST, GraphQL, JSON API. 

So to first get a context list list down what all 3 api styles define and 


1) REST (Representational State Transfer) API. 

As this is the most popular api approach in current environment. 

REST was designed to provide interoperability between web resources by defining a uniform set of stateless operations. it uses http  verbs to act upon resources Get, Post, Put, Patch, Delete. 

Using stateless protocols and standard operations is meant to provide fast performance, reliable interactions, and extensibility through standardization.

As each request only responds with that which you asked for. This makes for a highly efficient, fast, and powerful approach.  But the same thing goes as a drawback of rest. When you need some group operation it leads to multiple round trips. and many times over fetching of data on client side which is not required. 


2) JSON:API

JSON:API sets a standard for communication; it expresses how requests to the server should be formatted, and then what the response to the client should be formatted as. Its goal is to optimize HTTP requests by reducing the number of requests needed as well as reducing the size of data packages exchanged between clients and servers.

To let know the server that api is containing data payload header used is 

Content-Type: application/vnd.api+json


A JSON object is at the root of requests, which must include either resource data, error, or meta information. Data, and also relationships to data can be fetched through GET calls like such

A typical JSON API request

GET /articles HTTP/1.1

Accept: application/vnd.api+json


// ...

{

  "type": "articles",

  "id": "1",

  "attributes": {

    "title": "My First Article"

  },

  "relationships": {

    "author": {

      "links": {

        "self": "/articles/1/relationships/author",

        "related": "/articles/1/author"

      },

      "data": { "type": "people", "id": "1" }

    }

  }

}

// ...


Advantages of using JSON API

Caching

Optimization Features (Sorting , pagination)

Compound Documents : Enables to add additional documents in the response.

GET https://api.example.com/posts?include=comments

 Sparse Field sets  :  Enable clients to only request data from specific fields.

GET /articles?include=author&;fields[articles]=title,body&;fields[people]=name 

HTTP/1.1

Accept: application/vnd.api+json

Optionality : Provides facility to turn on/off the features which are not required 


3) GraphQL

GraphQL is an application query language, It is primarily designed to create a predefined, understandable, and stable format for requests.

allowing the client to specifically state the structure of data and the data itself that should be returned from the server.

 GraphQL gives information as to related resources, allowing you to traverse from the first returned data to more related data content, forming a graph of related data and meta-relationships.


The provision of related resources also means that you can make more advanced queries and get all of that data in one fell swoop, whereas in other solutions like REST, you’d need to be make hugely complicated multi-endpoint series of calls to get everything you can in a single GraphQL request.


As sample GraphQL Request Body

{

    author {

        name,

        age

   }

}


this will return only author object with properties of name and age, other fields will not be send by server.




1 comment:

Jr0987 said...

Your technical work is absolutely commendable. Kudos to you !!
where can we get the latest M2 plugin.
thanks.