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

Hashicorp Vault Teknologi Penyimpanan Secret

12/11/2024

Hashicorp Vault Secret Storage Technology

Currently, information security and sensitive data are a priority when developing applications. Especially in this case, the storage of secrets or important secrets such as database passwords, API keys, certificates, System...

View
Meningkatkan Kualitas Kode dan Efisiensi Development dengan Git Hooks

12/11/2024

Improve Code Quality and Development Efficiency with Git Hooks

Effective and efficient software development requires careful steps throughout the development cycle. One important aspect is ensuring that every commit that goes into the repository meets quality standards...

View
Flutter: Kerangka Kerja Aplikasi Mobile Multiplatform yang Efisien

12/11/2024

Flutter: An Efficient Multiplatform Mobile Application Framework

Flutter is a multiplatform mobile application framework created by Google. Flutter allows developers to build apps that can run on various platforms such as Android, iOS, websites, and desktops,...

View