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

Memahami Scrum: Kerangka Kerja Agile

12/11/2024

Understanding Scrum: An Agile Framework

Scrum is one of the Agile Frameworks that is dedicated specifically to dealing with complex problems. Scrum is a simple framework that helps teams and organizations generate added value through adaptive solutions...

View
Pro Kontra Selama WFH

11/11/2024

Pros and Cons During WFH

Work From Home (WFH) is a work system where employees complete tasks from home or other locations outside the office, with the support of communication technology and digital collaboration. This system becomes...

View
GraphQL vs REST API Apa Bedanya?

11/11/2024

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 software interacting with each other. One example is to get data from a database. GraphQL API provides convenience in...

View