GraphQL — Hands-on with next-generation API.

Samadhiwathsala
3 min readJul 10, 2022

If this is also an API then why do I need it when there is a REST API? 🤔🤔🤔

This is the first impression that you heard about the term GraphQL API. That’s why you need to know about GraphQL. To get a better understanding, first, we need to know about the difference between REST API and GraphQL API.

The major difference concept between those two APIs is that GraphQL API follows client-driven architecture whereas REST API follows server-driven architecture. This means the REST API is served the data based on the pre-structured data model and that is accessed through the API endpoints. To the GraphQL server, you can simply send the query with the concrete data requirement and then the server responds with a JSON object where these requirements are fulfilled.

Difference between REST API and GraphQL API

GraphQL🧐

A query language that can optimize the RESTful API calls. This is a server-side runtime for the application programming interface. Further, this helps to fetch and update the data through GraphQL schemas such as queries and mutations. Strongly typed GraphQL is ideal for reducing development time since it integrates with code generation tools, is self-documenting based on schema types and descriptions, and is strongly typed.

How it works 🚀

It simply said that GraphQL shows and provides the data as a graph and also it is a runtime therefore it allows defining the schema and querying the data as you prefer. It also aids in the definition of the links between these data objects, as well as extra iterations or variations of each entity.

Mainly there are three major types of GraphQL operations:

Query:

In working with GraphQL the queries are used to fetch the data. The most important thing is that the client can request the data by its own data structure.

{
students{
name
}
}

The return result of the above query would be like this:

{
"students": [
{ "name": "John" },
{ "name": "Jane" },
{ "name": "Alice" }
]
}

Mutation:

To do any kind of changes in the database that can be used Mutation operation. Generally, there are three main operations that can be done. That are written new data(Create), Update exist data(Update), Delete exist data(Delete)

mutation {
createStudent(name: "Anna", age: 12) {
name
age
}
}

The return result of the above mutation would be like this:

"createStudent": {
"name": "Anna",
"age": 12,
}

Subscription:

Through this subscription operation, clients can real-time continuously work with the server. A client will start and maintain a consistent connection to the server when it subscribes to an event. The server pushes the relevant data to the client whenever that specific event really occurs. Subscriptions represent a stream of data that is sent to the client as opposed to queries and mutations, which operate in a more traditional “request-response-cycle.”

--

--

Samadhiwathsala

Undergraduate in Sabaragamuwa University of Sri Lanaka