GraphQL — Next Level of API Language

Combine your multiple REST endpoint into One…

GraphQL is a new API standard that provides a more efficient, powerful and flexible alternative to REST Web services. It was developed and open-sourced by Facebook and is now maintained by a large community of companies and individuals from all over the world. It provides an alternative to REST and ad-hoc web service architectures.

It enables declarative data fetching where a user can specify exactly what type of data it needs from an API endpoint. Instead of multiple endpoints that return fixed data models, a GraphQL server only exposes a single endpoint and responds with precisely the data a client asked for. Simply says, You can get a bundle of data using only one GrpahQL API request instead of triggering so much of API endpoints.

Let Assume, there are some API endpoints to retrieve some useful information from the server.

  • Getting user information from an API endpoint. It will return the user id, name, address and birthday of the related user id.
  • Now you want to fetch some posts information of this respected user. So you need to trigger another API endpoint with the user id which was used for the last rest call.
  • Finally, you want to fetch some followers of the respected user to create some statistical report. So you need to trigger another endpoint to fetch this data.

So, If you want to analyze the user info, you want to trigger at least three API endpoints to retrieve the required info. The count of the triggers may differ about your API designs. It can be large or less than this count. Anyway there are one or more API triggers required to achieve some tasks.

GraphQL is one of the solutions which can reduce this amount of triggers to retrieve the response from the massive data collections. It based on some kind of querying for the data retrieval.

For the above example, You can retrieve the required data using one API trigger! How is this possible?

Let’s play with GraphQL,

Endpoint : ~/api/graphql
Request : query {
User(id : "er3tg439frjw") {
name
posts {
title
}
followers(last:3) {
name
}
}
}
Response : {
"data" : {
"User" : {
"name" : "Mary",
"posts" : [
{ "title" : "Learn GraphQL today" }
]
"followers" : [
{ "name" : "Jhon" },
{ "name" : "Alice" },
{ "name" : "Sarah" }
]
}
}

Have you seen the Response of the GraphQL request? It contains almost much required data for the request.
Yah, you can get it by only one API request!. That’s the very big advantage of the GraphQL based API endpoints.


GraphQL — A Query Language for APIs

Now a days, Most applications want to fetch data from a server where that data is stored in a database. It’s the responsibility of the API to provide an interface to the stored data that fits an application’s needs.

GraphQL is often confused with being a database technology. This is a misconception, GraphQL is a query language for APIs — not databases. In that sense it’s database agnostic and effectively can be used in any context where an API is used[1]

GraphQL is totally based on queries. It will fulfill the request queries with your existing/pre-defined data as the response.It provides a complete and understandable description of the data in your API and gives users the power to ask for exactly what they want over the huge data and nothing more, makes it very easier to evolve APIs over time, and enables powerful developer tools.

You can simply ask for what you need from the massive data, you will get exactly that one.

Just send a GraphQL query to your API endpoint and get exactly what you need from there, nothing more and nothing less-exactly what you want.

The response for the GraphQL queries always contains the predictable results. API endpoints using GraphQL are fast and stable because they control the data they get, not the server.

It will reduce the number of API calls for a scenario. GraphQL queries access not just the properties of one resource but also smoothly follow references between them. While typical REST APIs require loading from multiple endpoints, GraphQL APIs get all the data in a single request.

Just summary about the GraphQL,

  1. REST and GraphQL are totally different — Don’t put them into one bucket.
  2. You can definitely use both at the same time — There are no blockers or restrictions in the real time.
  3. GraphQL is dope if used for the right thing — Just read more about GraphQL

REST vs GraphQL

  • GraphQL reduces network requests by allowing users fetch or retrieve all the data they need in a single query using the single endpoint. There are no multiple requests to fetch the data in the real time.
  • Data fetching is one of the most significant improvements in theGraphQL. In a typical REST API, to fetch or retrieve data from a server, users might need to trigger multiple endpoints. But with GraphQL, users only have one endpoint with which they can access data on a server.
  • Typical REST APIs contains the versioning like v1, v2, v3 etc. which simply indicate the version of the REST API we are using. But with GraphQL, there is no need for versioning as users can easily add new fields and types to our GraphQL API without impacting existing queries.
  • GraphQL has no caching mechanism. Since HTTP already implements caching, and REST is implemented using HTTP, the client can use HTTP caching to avoid re-fetching resources.
  • GraphQL only responds with 200 OK response status. That means, If the query contains the required data or not, users will get the 200 OK response status. They need to check with the response to understand the success or error status. But Error handling in REST is pretty straightforward, We have different status codes to indelicate the response status.

Why is GitHub using GraphQL as their V4?

GitHub chose GraphQL for their API v4 because it offers significantly more flexibility for their integrators. The ability to define precisely the data you want — and only the data you want — is a powerful advantage over the REST API v3 endpoints. GraphQL lets you replace multiple REST requests with a single call to fetch the data you specify.


Like what you read? Give Kailayapathy Suthagar a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.