GraphQL Tutorial

Introduction to GraphQL

As the name suggests, GraphQL is a query language (QL means Query Language). It is an Open-Source data query language used for the APIs. Moreover, it is also a server-side technology.

In the year 2012, GraphQL was an internal project of Facebook. FACEBOOK developed the specifications of GraphQL in 2012, where the leading creator was Lee Byron, but later it became an open-source technology in 2015.  According to data provided, it is moved to the newly-established GraphQL foundation, which is hosted by the non-profitable Linux Foundation on 7 November 2018 from the Facebook.  

The important thing about GraphQL is that it is a specification in the query language, not implementation.

One of the excellent features of using GraphQL is that it isn't tied to any specific type of the database or the storage engine, and instead, it is backed up by user’s existing code and data.

Companies using GraphQL

The social-media giant hub Facebook’s mobile application is powered by GraphQL since 2012. Now, GraphQL is used in the productions by the many of the organizations of various sizes like GitHub, the New York times, Credit Karma, Intuit, PayPal and many more.

Features of GraphQL

Some of the popular features of GraphQL are given below:

  • GraphQL is an approach for developing web APIs. It allows the clients to define the structure, type of the data required, and the same type of data will return from the server. Therefore, it prevents the excessive amount of data from being returned.
  • The richness and flexibility of the GraphQL are beneficial in the query's complexity, which may not be performable for the simple APIs. GraphQL also has the implications for how effectively it catches the results of the query from the web.
  • It supports reading, writing (called 'Mutation') and changes to the data, which is the real-time updates. Unlike the REST APIs, the GraphQL provides a single endpoint for the query.
  • GraphQL always responds with the precise data that the client has asked for.

Operating-systems compatible with GraphQL

As GraphQL is a cross-platform, query and manipulation language for the APIs. Hence, it is compatible with all type of Operating Systems.

The servers for GraphQL are available for many languages like JavaScript, Ruby, Scala, Go, Java, C#, Elixir, R, PHP, Clojure, Haskell, Python, Erlang, and many more. It can be used with different types of programming languages and frameworks. GraphQL usually serves over HTTP via a single endpoint, which is used to express the full set of the services' capabilities. 

The client who sends queries to the server-side can be a CMS like Drupal, a mobile app, a single page application, or anything.

Why GraphQL?

We already had REST APIs for retrieving the data from the server and for data communication. The REST APIs follow a very well-structured and result-oriented approach. But REST has its limitation. REST APIs can provide structured access to data or stateless servers, but it can't handle the query's complexity. It is not so flexible with the changing environment and with changing requirements of technologies and clients.

In such scenarios, the data gets more complex, and the routes and endpoints become longer. Sometimes, it becomes a very challenging task to fetch the result in a single query.

Therefore, we need a technology that can fulfill the changing world of technology and fetch all the required data in a single request, and the that technology is GraphQL.

 Following are the reasons for using GraphQL: -

  1. GraphQL can retrieve data from many resources just by a single query: -

With a single query of GraphQL APIs, you can fetch the data from many resources, but you will need to load many resources from REST APIs. By using this feature, it can retrieve results rapidly and smoothly. Therefore, mobile applications using GraphQL APIs are very fast, even on a prolonged internet connection.

One more critical point in this to note that GraphQL queries are even significantly more straightforward than the REST APIs. REST APIs can have multiple endpoints, but GraphQL has only single endpoint. That's why GraphQL is faster as compared to REST APIs. By having a single endpoint, GraphQL can also work on complex queries. And therefore, GraphQL is comparable easy to use.

You can easily understand the above point from the following example:

Example: - Lets we search for the tutorials on a Youtube channel, and the result will show all the keywords matching the tutorials. Let us assume that we want a tutorials playlist and Title of the playlist. So, it will make the query a bit complex, but in GraphQL, it will give you the results of the desired queries in a single request.

If we make the following query, in the GraphQL: -

GraphQL Tutorial

GraphQL will give us the following data in the result from the server as we run the query.

GraphQL Tutorial

We can see that we got all the tutorial playlists in the result with each playlist's Title. Now, we have to make a single request from the server to fetch all the desired data in the GraphQL.

Hence, we can say that it is one of the significant advantages of using GraphQL over REST APIs.

  • GraphQL will give you more precise and accurate data.

When you start working with GraphQL, you will find that GraphQL provides only that data, which you have requested in your query. Also, you will notice that the data GraphQL has retrieved is very accurate and precise. Suppose you have a book id, and you want to know the author's name. In REST APIs, you will get the author's name with the author's surname and other creations. It means data comes in excess amount, and you have to look in between all the results to find the requisite data that you have asked for. But, In GraphQL, the data will be given very specifically for what you asked. As for the request mentioned above, GraphQL will provide you only the author's name of the book id.

Let’s understand the above point by the following example: -

Suppose, we have a playlist of tutorials on our YouTube channel, and we want the Title of all the tutorials that we have on our YouTube channel. So, we will enter the following query in the GraphQL,

GraphQL Tutorial

By Entering the above-specified request, we will get the following result.

GraphQL Tutorial

Here, GraphQL has only fetched the Title of the tutorials and not fetched other excess data from the server.

So, from the above example, we can understand that GraphQL provides you exact data, and it is very accurate and efficient. Therefore, we can say that the GraphQL query results are always predictable because it only gives you what you asked for.

  • The queries of the GraphQL are elementary, and any person can easily understand them.
  • Applications that use the GraphQL are stable and very fast.
  • Multiple data can be fetched from the server using a single API call.
  • GraphQL is open-source, so you can use it freely.
  • GraphQL is not bounded to a server, so that you can use it with the different kinds of servers of your choice.
  • GraphQL helps you to deal with multiple types of databases.
  • You will not get the issues like excess fetched data or under fetched data in GraphQL.
  • GraphQL is best suited to complex system requirements.

Apart from the above points, Key point to note that GraphQL can work correctly with both front-end(client-side) and backend(server-side) development.

Audience

Anyone who has prior knowledge of node.js can quickly learn GraphQL, but it isn't mandatory. People having a basic knowledge or familiarity with the concept of node.js and react.js can quickly grasp how GraphQL works.

GraphQL Tutorial Index

GraphQL Tutorial

Apollo Installation and Framework

  • Apollo and GraphQL
  •  Apollo Server setup
  • Apollo client Installation

Miscellaneous

  • Question asked in Interview related to GraphQL