What is GraphQL
We have heard a lot about SQL, Structured Query Language. But What is this GraphSQL? What does even it mean. Where can I use it? What benefits does it provide over SQL? How good is it as compared to regular REST API.
What is GraphQL?
GraphQL is a query language designed to build client application by providing an intuitive and flexible syntax for describing their data requirement.
Yep, that is the most perfect definition and little bit jargon too.
Let me explain that in better and easier syntax. The main goal of GraphQL is to make query smarter. Currently, if you want to ask questions like “Give me Names, Age and highest score of top 5 players who played in the final of 2015 world cup”. Surely It can be done. But present endpoints in REST API looks like this:
/players /players/:id /country
Often endpoints without IDs just return everything and end points with ID returns full information about 1 resource. The biggest issue is you are either under fetching or over fetching information. Imagine you are over fetching 100s of IDs and making 100 API calls and then filtering just 5 values form it. It is expensive on servers. Servers like cricket information and Facebook.
GraphQL makes these endpoints smarter so that it can pick selective data, making it cost effective and faster.
Comparing REST API and GraphQL would be comparing a self-serving restaurant and waiter served restaurant.
REST API aka self-serving and GraphQL aka waiter served.
In self-serving restaurant, you manually take what you need while in another case, you just ask a waiter to bring what you need from all tables.
Now, let’s move to some common questions about GraphQL.
Is GraphQL related to Graph databases?
Nope. I agree that name looks the same but it’s not like that. The ability of GraphQL to crawl across API gave the name graph and you know what QL means.
Should I stop using REST API now?
Nope. In fact, most application doesn’t need graphQL. Most applications are not that much filled with data that you need to implement GraphQL for your application. It won’t be making a night and day difference. If you have it, it’s awesome. can be a little advantage for developers but no need to panic. You are fine without it too.
What is the syntax difference between REST API and GraphQL?
Just 1, GraphQL can dig deep into API and can ask smarter questions directly.
So, What Do I need to implement GraphQL or to learn GraphQL
basically, you need 2 things:
- GraphQL Server – to serve your API
- GraphQL client – to connect your endpoints.
For server, you can use GraphQL-JS (Node) with express-graphQL to create servers and for client, you can use Relay. Relay was once an internal tool at Facebook, so mostly you will feel like it’s overwhelming for you.
But there are many other server and client resources that you can look around.