GraphQL vs REST API Apa Bedanya?

Here are some tips that we can share, if this information is useful don't forget to keep following

 API or Application Programming Interface is a protocol or technology used to handle a piece of software interacting with each other. One example is to get data from a database.

     GraphQL API providing ease of data capture and a faster development process. Even so, GraphQL is not a substitute for REST API. Each of these approaches has its own advantages and disadvantages according to our case or needs at the time of developing the system. 


Getting Data with REST API

Based on the scenario, we try to get the data with the REST API. To get detailed league data along with team and player data, we must access at least one endpoint. Of course we will get JSON response when we request data by accessing endpoint

The process of collecting data on REST API when more than one is needed request

          “idLeague”:”4328″,         “strSport”:”Soccer”,         “strLeague”:”English Premier League”,         “intFormedYear”:”1992″,         “strGender”:”Male”,         “strCountry”:”England”,         “strWebsite”:”www.premierleague.com”,         “strDescriptionEN”:”The Premier League (often referred to as the English Premier League (EPL) outside England),is the top level of the English football league system.”,         “strBadge”:”/i6o0kh1549879062.png”,         “strTrophy”:”/yrywtr1422246014.png”      }

We also need to access endpoint Here is a list of teams in the league. 

          “idTeam”:”133604″,         “strTeam”:”Arsenal”,         “intFormedYear”:”1892″,         “idLeague”:”4328″,         “strStadium”:”Emirates Stadium”,         “strBadge”:”/i6o0kh1549879062.png”,         “strDescription”: “Arsenal Football Club is a professional football club based in Holloway, London.”},         “idTeam”:”133601″,         “strTeam”:”Aston Villa”,         “intFormedYear”:”1874″,         “idLeague”:”4328″,         “strStadium”:”Villa Park”,         “strBadge”:”/i6o0kh1549879062.png”,         “strDescription”: “Aston Villa Football Club, also known as Villa, The Villa, The Villans,The Lions) are a professional football club based in Witton, Birmingham.”}
  

To get the player list data from these teams we also need to access one endpoint again as follows:

          “idPlayer”:”12345″,         “strPlayer”:”Ronaldo”,         “strPhoto”:”/i6o0kh1549879062.png”},         “idPlayer”:”54321″,         “strPlayer”:”Messi”,         “strPhoto”:”/i6o0kh1549879062.png”}
Approach GraphQL to get the data? 

We only need 1 (one) query that we have determined the need. Server will reply by providing JSON object where our needs have been adjusted. 

  • The process of collecting data on GraphQL API when more than one request is required
 query {    league(id: 4328) {        strLeague        strBadge        teams {            strTeam            strBadge            players {                strPlayer                strPhoto            }        }    }}

to JSON ResponeIt will look like this:

  “data”: {    “league”: {    “strLeague”: “English Premier League”,    “strBadge”: “/i6o0kh1549879062.png”,    “teams”: [        {         “strTeam”: “Arsenal”,        “strBadge”: “/i6o0kh1549879062.png”,        “players”: [            {             “strPlayer”: “Ronaldo”,            “strPhoto”: “/i6o0kh1549879062.png”        },        {             “strPlayer”: “Messi”,            “strPhoto”: “/i6o0kh1549879062.png”        }        ]    },    {         “strTeam”: “Aston Villa”,        “strBadge”: “/i6o0kh1549879062.png”,        “players”: [            {             “strPlayer”: “Ronaldo”,            “strPhoto”: “/i6o0kh1549879062.png”      },      {             “strPlayer”: “Messi”,            “strPhoto”: “/i6o0kh1549879062.png”      }      ]      }      ]    }}

     With the GraphQL approach we can set any conditions or data we need on a query. We can get all the data according to our needs without additional information that we don't need. How? It's more efficient and flexible if we get data with the GraphQL approach. And don't forget if this information is useful, keep following us to update information about other interesting technologies.

Berita Rekomendasi

SSDL: Membangun Perangkat Lunak Aman

12/11/2024

SSDL: Building Secure Software

Secure Software Development Lifecycle (SSDL) is a software development process designed to build secure and reliable software. SSDL integrates security practices into all stages of device development...

View
Strategi Employee Life-Cycle untuk HR

11/11/2024

Employee Life-Cycle Strategy for HR

There are a variety of topics that address topics around employees regarding improving specific processes such as hiring, payroll and benefits administration or performance management and hiring. However, a process that deserves attention is the management...

View
HR dan Sistem Kerja New Normal

11/11/2024

HR and the New Normal Work System

It does not feel like it has been going on for more than three months of the PSSB (Large-Scale Social Restrictions) Program in Indonesia due to the Covid-19 Pandemic. The community certainly hopes that the pandemic disaster will pass quickly. This pandemic...

View