Introduction to GraphQL

Uvindu Dharmawardana
interleap
Published in
3 min readFeb 5, 2020

This article is recommended for who are beginners to the GraphQL, this will help you to take a quick start on GraphQL

In this blog post, we will discuss the following topics

  • What is GraphQL…?
  • How does GraphQL work..?
  • What is the difference between REST vs GraphQL..?

What is GraphQL…?

  • REST and GraphQL are two ways to send data over HTTP. The REST-based approach is the traditional way of doing so and has gained a very high adoption rate in many application stacks in the last years.
  • GraphQL is developed by Facebook which is basically an open-source data query language for APIs. GarphQL is a stateless, client-independent API for exchanging data with higher query flexibility.
  • GraphQL pushes more power to the client which will help clients to decide what to fetch and not the server. Remember that GraphQL is not limited to react.js, you can use GraphQL any other front-end frameworks.

How does GraphQL work…?

In the above picture, you can see that there is a /POST request which is targets to an endpoint. In GraphQL you need to use only /POST requests even when you getting data, that is really important. You send the /POST request to one single endpoint. /POST request contains query expression which includes the definitions of the data that should be returned.

There are two types of GraphQL queries

  1. GraphQL document: A string written in the GraphQL language that defines one or more operations and fragments.
  2. Operation: A single query, mutation, or subscription that can be interpreted by a GraphQL execution engine.

Let’s look at a GraphQL document query example…..

What is the difference between REST and GraphQL…?

Well, this is the most interesting part of the story. both, REST and GraphQL, are API design architectures that can be used to build web services for data-driven applications, a major reason for GraphQL is better than REST API is that GraphQL has higher query flexibility for exchanging data. Let’s compare the operation types in both REST and GraphQL.

Getting Multiple Resources
Those are the operational level differences between REST and GraphQL. One big difference in GraphQL is that It can fetch multiple resources.RESTful API calls are chained on the client before the final representation can be formed for display. GraphQL can reduce this by enabling the server to aggregate the data for the client in a single query.

Reduce the number of endpoints

Like REST, you don’t need to define multiple endpoints. In GraphQL you can use one endpoint for several resources. This might be my favorite point. Having to design and build multiple REST API’s all with a multitude of endpoints, reducing this down to one seems like a glorious win for a developers time

Conclusion

In this article, we discussed the main difference between those two concepts. The RESTful approach is always limited to deal with single resources. If you need data that is coming from two or more resources (like posts and users) you need to do multi-round trips to the server. Furthermore, REST requests are always returning the full set of data that is available for a certain resource. There is no way to limit the request to only retrieve a subset of data fields.

The GraphQL approach is much more flexible. By using the GraphQL query language you can exactly describe what the response should look like. You are able to specify which fields should be included to limit the response to the data which is needed.

--

--

Uvindu Dharmawardana
interleap

B.Eng Software Engineering University of Westminster