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

Tips Tetap Produktif Selama bulan Ramadan

12/11/2024

Tips to Stay Productive During Ramadan

Perubahan ritme dalam beraktivitas pada bulan Ramadan membuat stamina tubuh berbeda dari hari-hari biasa. Tubuh akan terasa lemas karena asupan yang didapat hanya ketika sahur. Namun kamu masih dapat produktif…

View
7 Habits of Highly Effective Programmers

11/11/2024

7 Habits of Highly Effective Programmers

Habit menyangkup persimpangan pengetahuan, keterampilan dan keinginan Kebiasaan Sukses 1. Be Proactive     Kuncinya : ‘Saya bertanggung jawab atas perilaku saya dan pilihan yang saya buat dalam hidup’ Orang-orang…

View
Alternatif yang Lebih Aman dan Efisien Pengganti PHP

21/11/2024

Alternatif yang Lebih Aman dan Efisien Pengganti PHP

PHP telah menjadi tulang punggung banyak aplikasi web sejak diluncurkan. Meskipun demikian, meningkatnya kebutuhan akan keamanan, skalabilitas, dan kinerja yang lebih baik telah mendorong pengembang untuk mencari alternatif. Pengganti PHP,…

View