Why GraphQL

Ajay
Ajay
Nov 18, 2017 · 4 min read
Image for post
Image for post

GraphQL is a data query language developed by Facebook under MIT license. It is not tied with any specific database or specific programming language or storage.

It provides a way to define complete description of data in schema and allows client to ask for what they need, simply returns the response in JSON.

Why Facebook developed GraphQL ?

Back in 2012, Facebook was working on their mobile application for Android and iOS platform. They had to make changes in the rest services according to the mobile platform. To resolve this, either make a different rest API for the Android and iOS platform or do handling at multiple places in a rest service. Sometimes in large codebase it becomes very messy and frustrated to manage it and multiple versioning of the API’s.

I see that sometimes we have a lot of attributes in the response of the rest API’s which sometimes is not useful for the client always, so devices which are on low network bandwidth has to wait long to get big JSON.

Here, Facebook team started to work on GraphQL to solve these problems and rethink data fetching way for devices, specially which are on low network bandwidth. It moved focus on developers at client side to ask what they need.

Why you should use GraphQL ?

  • Versioning: To avoid multiple versioning of your rest API.
  • Ask for what you need: Client has a provision to ask only those fields which they needs. There would be no handling on server side specific to the platform.
  • Get many API’s response in single request: Client has to call one query to get data from multiple rest API’s.

Most important basic component of GraphQL is object types which defines kind of object client can fetch from the service. In GraphQL, it represents like this:

There are two special types in object types. Query and Mutation. Object types has a query type and may or may not has mutation type. They defines the entry point of every GraphQL query.

So, Query looks like this. Here you can ask what you need, I am trying to fetch the event details like title, description, url. You can ask for more details according to your need.

Here, you can also ask for event location and weather which is handled by different rest API. It helps to fetch data from multiple resources in single request.

For this, you have to implement schema stitching to establish a link between two schema. Now, your query should look like this:

Please, if you find something to improve or any suggestion, don’t hesitate to contact me, I’ll try to do my best to answer any question or improve this tutorial.

Happy Coding!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store