What is GraphQL

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL is all about data communication. You have a client and a server and both of them need to talk with each other. The client needs to tell the server what data it needs, and the server needs to fulfill this client’s data requirement with actual data.

In a simple word GraphQL  is a syntax that describes how to ask for data. It has three most important features:

  • It lets the client specify exactly what data it needs
  • It makes it easier to aggregate data from multiple sources.
  • It uses a type system to describe data.

 

Why GraphQL

The 3 most important problems that GraphQL solves beautifully are:

  • The need to do multiple round trips to get/fetch data required : With GraphQL, you can generally get all the data required by a view with a solitary  single round-trip to the server. To do likewise with a REST API, we have to present unstructured parameters and conditions that are difficult to scale.
  • Clients dependency on servers: GraphQL, speaks a request language which eliminates the need for the server to hardcode the shape or size of the data.
  • It provide good frontend Experience : With GraphQL, developers express the data requirements of their user interfaces using a declarative dialect. They express what they need exactly, not how to make it available.

 

We must understand this by using this simple example suppose we have to launch a rocket to the mars and there are different scientist working together from different country like US, Russia and India and they have different sets of knowledge. We can only send our rocket by combining those knowledge. We must have  some Middle Man who can work as a knowledge translator for combining those knowledge given by the multiple scientist. GraphQL runtime do exactly the same thing it fetch the data from the different source and combine it together to form one complete data required to perform task.

Problem with REST APIs?

REST APIs are usually a collection of endpoints, where each endpoint represents a resource. So when a client needs data from multiple resources, it needs to perform multiple round-trips to a REST API to put together the data it needs.Clients do not have control over what data the server will return. There is no language through which they can do so.

or example, the READ REST API endpoints are either:

  • GET /orders – to get a list of all the records , or

  • GET /Order/order_id – to get the single record identified by that ID.

 

 

One other big problem with REST APIs is versioning. If you need to support multiple versions, that usually means new endpoints. This leads to more problems while using and maintaining those endpoints and it might be the cause of code duplication on the server

GraphQL API  has three main building blocks: the schema, queries, and resolvers.

Query :

This is something when the question asked from the client for eg:

{

“data”: {

  “person”: {

    “name”: “Darth Vader”,

    “birthYear”: “41.9BBY”,

    “planet”: {

      “name”: “Tatooine”

    },

    “films”: [

      { “title”: “A New Hope” },

      { “title”: “The Empire Strikes Back” },

      { “title”: “Return of the Jedi” },

      { “title”: “Revenge of the Sith” }

    ]

  }

}

 

Schema

 

Schema is declared by using GraphQL schema system

 

For learning in  more depth , visit to the GraphQL documentation.

Resolver

GraphQL server doesn’t knows how to work with an query until or unless you tell it using a resolver.

A resolver respond  GraphQL to how and from where to fetch the data particular given field. For example, here’s what a resolver for the post field above could look like

Query: {

post(root, args) {

  return Posts.find({ id: args.id });

}

}

Graph QL with Node

GraphQL goes excellent with node js. We can implement graphQL with by using express-graphQL, GraphQL ,and Lodash.

Categories: GraphQL