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

7 Habits of Highly Effective Programmers

11/11/2024

7 Habits of Highly Effective Programmers

Habit involves the intersection of knowledge, skills and desires Success Habits 1. Be Proactive The Key: 'I'm responsible for my behavior and the choices I make in life' People...

View
API Testing untuk Aplikasi

03/09/2024

API Testing for Applications

In the software world, Application Programming Interfaces (APIs) connect application components, ensuring smooth communication between parts. API Testing for Applications ensures APIs function optimally and are error-free. FIRE...

View
Tools Analisis Data Optimalkan Keputusan Bisnis Anda

24/12/2024

Data Analytics Tools Optimize Your Business Decisions

Choosing the right Business Intelligence tool or data analysis tool can increase efficiency in analyzing data and generate valuable knowledge. In this discussion, we will provide a list of tools that...

View