Getting started with GraphQL

Dhaval Patel
Mindful Engineering
5 min readJun 4, 2021

When you haven’t used GraphQL and you hear the word “GraphQL”, Questions starts firing up in our mind, like what is difference between REST and GraphQL and why I shall use it when I am happy with REST?, How can I use it? Can I use it in the programming language I am using? and so many others questions as well. So you are at the right place to get all this answers my friend, lets understand about GraphQL.

What is GraphQL?

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. In simple words, It gives you all front end developers and designers powers using which one can retrieve only the fields that you need. Which gives you ability to manage API data and structure on your own, Once implemented there is not need to change in APIs for different requirement, developer can request stuff from APIs as per their requirements.

How it is different from REST API?

For calling REST API, we generally using HTTP methods like GET, POST, PUT and DELETE, where on the other hand GraphQL uses different approach, Named Query, Mutation and Subscription.

Query

In GraphQL , Query is the way using which you retrieve data, same as we do in REST using GET requests.

Let’s say you want to query amount from serverCounter table, then this is how you need to pass query(on left) and this is how you will get response(on right).

As you can see we used query keyword on querying result from serverCounter and got result in same structure as we query in Json data format.

Mutation

Mutation is way you do add or removal operations on table as same we do in REST using POST , PUT and DELETE.

Using mutation we can do add mutation which is used for adding entry into the table, i .e. We are adding a Post to table using addPost. Input variable allows us to pass data that we want to be added in our table, inside curly bracket we can define what response we want after adding a post.

Subscription

Subscription is a way to retrieve data just like query but the difference is that subscription continuously listen for updated values. for that it establishes long lasting connection with server.

Using subscription, you got the updated value whenever any mutation through values going to update of counter.

How to use GraphQL?

GraphQL used on both sides client and as well as server. On server side for writing schema and resolvers we will use GraphQL server for that different GraphQL server are available. like graphql.js, apollo-server, express-graphql, graphql-yoga.

Schema

Your GraphQL server uses a schema to describe the shape of your data graph. Schema give you idea about data which are store in database on server. schema also define which queries and mutation are available to frontend. The GraphQL specification includes an easy and readable schema definition language(SDL).

by looking above schema you can easily understand the post type which contains id ,title ,content and comments with data type of Int non-null , string non-null and array of comment type. Which help you to write query and mutation in different tools like graphiql by providing suggestion and documentation.

Resolvers

Resolver is function that is responsible for providing data to each field which are in schema. Resolver provide data to each field using database , third party apis or any other ways.

for every query , mutation add fields which can add resolvers which provide you result to frontend.

By using schema and resolvers, we can create GraphQL server. Now how to use in frontend. For frontend we have many client which we can use like apollo-client, relay, amplify-js, urql, graphqurl. using anyone use can fire query and mutation from frontend.

we retrieve posts with necessary data like title ,content and comments using useQuery hook.

In which programming language we can use GraphQL?

I can say almost in all famous programming language. see below supported language list from Official GraphQL site.

Which is better GraphQL or REST ?

GraphQL solved the issue of fetching data which are not necessary and also reduce multiple api call for many cases which GraphQL faster than REST API. Using GraphQL you can get structured data which you want. For Mobile app use cases where mobile data limitation come, there it is useful. On other default Http caching is not there in GraphQL, for that you need do it manually. Http Status code mechanism also is for error management and monitoring is not there. So Both GraphQL and REST has some flows. REST is old and well known by everyone.GraphQL is also very emerging and popular in recent times and continuously improving.

--

--