What is GraphQL?

Hi Friends, In this tutorial we’ll learn What is GraphQL and what it is support and what it doesn’t support and how it looks like. What types of operations GraphQL will support and how we can retrieve the data by using GraphQL queries.

Let us start!

GraphQL is an open source query language for the APIs. Using this query language, we can redo the data or we can update the data on the server. It is a modern approach for client-server communication and aim to improve the developers build applications on the web.

 

How does it works?

Developers can make real-time updates to data through GraphQL open source capabilities for read processes, data mutation and monitoring.

Firstly, we have to design our GraphQL Schema. On the server, create a GraphQL schema based on the data.

Secondly, connect the resolvers to data sources. We have to write the resolver functions for connecting the data to our graph.

Finally, write our query specify what we do like to fetch.

 

GraphQL Operations:-

What is GraphQL Query?

Using this we can read/fetch the data from the server.

Query{

       Search(q: “name”){

          Title

          author

}

 

What is GraphQL Mutations?

Using this, we can write the data like create, modify and delete the data from the server.

Mutation{
       Create(title: “book”)
       id
}

Types of GraphQL Mutations-

Insert Mutation

Update Mutation

Delete Mutation

 

What is GraphQL Subscriptions?

Using this, we can get the response from update or delete something on the server.

Subscription {
    onCreate
     id
    title
}

All above operations, we can do with POST method.

You can take help of below site to write the GraphQL query.

https://hasura.io/learn/graphql/intro-graphql/graphql-subscriptions/

 

GraphQL Subscriptions with WebSockets-

GraphQL subscriptions are implemented using WebSocket protocol. Enable us to create a continuous connection  between server and client.

The WebSocket gets real-time update from the server.

 

GraphQL Queries :-

How to write simple and GraphQL queries:

There is a one good site from where you can take help to write the GraphQL queries.

Site – https://hasura.io/learn/graphql/graphiql

First, you have to Sign Up on this site and start writing the Queries. Before writing GraphQL queries first, we need to know GraphQL Schema and Types.

Schema – Structure of the elements. Let’s example below image is showing schema for which we will write the GraphQL queries.

Schema for GraphQL

 

GraphQL query language basically selecting fields on objects. Now from above image we will start writing the GraphQL queries. Below steps we need to follow –

  • We start with a root object.
  • Then we can select fields.

Let’s select ‘online_users’ as a root and fields are id and last_seen. With help of above site we can do it.

In below image you can see I have selected mentioned fields and after that click on Play button. It will create the Graphql queries in JSON format.

how to create GraphQL queries

 


GraphQL Vs Rest –

There are few differences between GraphQL and Rest.

Rest is a set of architectural constraints, not a protocol or a standards. API developers can implement REST.

A REST API (also known as Restful API) is an application programming interface that conforms to constraints of REST architectural style and allows for interaction with RESTful web services.

So common REST implementation utilize HTTP method GET, POST, PUT, PATCH and DELETE etc.

 

 

GraphQL is a Query language and server-side runtime (typically served over HTTP). It is a language for querying data. Unlike most query languages (such as SQL), you don’t use GraphQL to query a particular type of data store (such as a MySQL database). Instead, you use GraphQL to query data from any number of different sources.

Let’s see the differences between GraphQL and REST.

                                              GraphQL                                                REST
A Query language offering efficiency and flexibility for solving common problems when integrating APIsAn architectural style largely viewed as a conventional standard for designing APIs
Client-drivenServer-driven
Deployed over HTTP using a single endpoint that provides the full capabilities of the exposed serviceDeployed over a set of URLs where each of them exposes a single resource
Lacks automatic caching mechanismUses caching automatically
Fetching specific data with a single API callFetches fixed data with multiple APIs calls
Only a single tool is used predominantly for documentation: GraphiQLWide range of options for automated documentation, such as OpenAPI and API Blueprint.

 

Is GraphQL better than rest?

If you want to use modern design style that does not require making multiple round trips to fetch data, then GraphQL could be your best option.

If you intend to use a tried and proven technique that comes with robust native caching or authentication capabilities then REST could be your best option.

 

I hope you have enjoyed this tutorial. Happy Learning 🙂

 

 

Leave a Comment