What Is GraphQL ?
In a nutshell, GraphQL is a Query Language for APIs. It's syntax that describes how to ask for data, and is generally used to load data from a server to a client. It provides developer friendly syntax. It offers features to the user that they can fetch data exactly what they need. Created by Facebook in 2012, GraphQL was used internally for their mobile applications to reduce network usage by means of its specific data-fetching capabilities. Since then GraphQL specifications and reference implementation in JavaScript were open-sourced in 2015. Today major programming languages now support it, including Python, Java, C#, Node.js, and more. The GraphQL ecosystem is expanding with libraries and powerful tools like Apollo, GraphiQL, and GraphQL Explorer.. Here is the syntax of simple GraphQL Query.
Query =>
{
user (id:5) {
name
city
state
}
}
Output =>
{
"data":{
"user": {
"name": "Luke Skywalker",
"city":Raigarh,
"state":"Chhattishgarh"
}
}
}
In Above example if you need only some specific data of a user then you have to create syntax like that. It is a little bit similar to syntax like json it only contains keys not the value. This will fetch only name, city and state from the user table stored in the database and will not fetch other information.
Why Should We Use GraphQL ?
If you want an application that works perfectly and utilizes less loading time then you should use GraphQL for web application. This technique can help you to increase the app performance and will also prevent your app from crashing. Below are a few pointers on the benefits of using GraphQL.
1. No Over-Fetching or Under-Fetching − It does not fetch extra data which you don't need and it also does not fetch less data, it pulls exactly the amount of data you need.
2. Saves Time and Bandwidth - GraphQL allows making multiple resources request in a single query call, which saves a lot of time and bandwidth by reducing the number of network round trips to the server.
3. Schema Stitching for Combining Schemas - Schema stitching allows combining multiple, different schemas into a single schema.
4. Hierarchical-One of the important aspects of GraphQL is it is hierarchical in nature. GraphQL naturally follows relationships between objects, where a RESTful service may require multiple round-trips or a complex join statement in SQL. This data hierarchy pairs well with graph-structured data stores and ultimately with the hierarchical user interfaces it's used within.
5. Strongly typed − GraphQL type Structure is strongly typed which ensures syntactical correction and validates that data are of the same type before execution.
6. Defines a data shape - The first thing you will notice is that GraphQL queries mirror their response. This makes it easy to predict the shape of the data returned from a query, as well as to write a query if you know the data your app needs. More importantly, this makes GraphQL really easy to learn and use.
7. Easy to Deal with Database - it is very easy to deal with many databases.
8. Detailed error Information- In REST, we check the HTTP headers for the status of a response based on which we can determine what went wrong and how to handle it. And if an error comes while processing GraphQL queries then the backend will provide a detailed error message including all the resolvers and highlight the exact query part where the fault has come.
9. It Will help you to create the schema in the Right format.
10. You can choose which functions to expose and how they work.
Although Graph QL may sound very intimidating at first, since it's a technology that reaches across many areas of modern development. However, in the past couple of years, more and more companies, frameworks and large enterprises are adopting it because of its immense list of benefits.
Hit me with any query you have on how to go about learning GraphQL.