Introduction to GraphQL #1

Table of contents
Reading Time: 2 minutes

GraphQL is a query language to retrieve data from a server created by Facebook in 2012 and was open-sourced in 2015. It is basically an alternative to REST or SOAP. It provides an understandable description of data and gives more power to the Client to ask for the exact data that they need. It leverages two types of requests.
1. Queries which simply retrieve data from the server
2. Mutations changing the data.

Let’s jump to the comparison of GraphQL with SOAP and REST. Although all three are used to create API architecture. But on the basis of their nature, they are quite different. SOAP is the protocol whereas REST is an architectural style and the GraphQL is a query language.

REST vs GraphQL

Consider a scenario where our application is using REST API and has an endpoint /student that fetches ball the student details(name, roll number, address, marks). As per the requirement, we need all the fields for the web application, but for mobile application, only name and roll number is required.

Solution 1: We can use /student endpoint for web and mobile applications both. But it will be waste to fetch all the non required data if the mobile application is using this endpoint.

Solution 2: What if we create two separate endpoints each for mobile and web? But does it make sense? Lots of effort. Isn’t it?

Here comes GraphQL as a rescue that allows us to make our response customized and yes with a single endpoint. Exposing a single endpoint will bring desirable students’ details. With GraphQL, a single query has sent to the GraphQL server which includes the concrete data requirements which return us back with JSON response.

With REST APIs we generally face under fetching(client downloads less information than actually needed) and over fetching(client downloads more information than actually needed) of data as every endpoint returns fixed data structure. So, to fulfill our need of getting data as per client results into so many REST APIs endpoints. So, no more under fetching and over fetching of data with GraphQL.
So, GraphQL is more flexible and efficient in client-server interactions as compare to Rest APIs.

REST API is schemaless while GraphQL uses a strong type schema system that defines different operations like queries, mutations, and subscriptions and acts as a strong contract specifying API’s capabilities. And even the developers don’t have to manually write API documentation,  it can be easily auto-generated based on the schema.

SOAP vs GRAPHQL

GraphQL and SOAP both use strong data typing which ensures better communication within the application.
They both use the single endpoint to access data, but SOAP is heavyweight and consumes more resources and bandwidth while GraphQL doesn’t put much strain on the network due to smaller payload size and considered as lightweight.

Conclusion

In this blog, we learned some basics about GraphQL and how it is different from SOAP and REST. We will look more about GraphQL in future blogs.
Hope this is helpful and gives you a basic understanding. Please feel free to provide your suggestions. 🙂

References:
https://da-14.com/blog/
https://www.howtographql.com
http://graphql.org/

blog-footer