blog posts

What is GraphQL ?

We all have different applications installed on our mobile phones. Programs that have special applications for us. Take the DJ Kala app, for example. This application provides us with all product information, user comments, and the possibility of online payment from banks. As you know, in the computer world, information is stored as data, and this data requires memory to store. The question that arises is how all this information, which requires several gigabytes of memory to be stored, is stored in software with a small volume of about 24 megabytes? The answer is a programming interface.

The application that we install on our mobile phone, using a programming interface, connects to the main website of Digi Kala, on which the information has been uploaded, and reads all the information from the website and displays it to us. This interface is called the Web API. The Web API is a subset of the API.

We need a structure or an architecture to implement the Web API. What is in a GraphQL article? We’re going to talk about one of these architectures called GraphQL, so stay tuned.

GraphQL

What is a Web API?

API stands for Application Programming Interface. Simply put, APIs are software interfaces that enable communication between different applications. API refers to a tool or library that helps developers write code that interacts with other software.

 

The API can also connect an application to a web server or two web servers. In this case, the technology used is called Web API. This type of API typically uses the HTTP protocol for implementation. As mentioned in the introduction, there are several ways to implement Web APIs, including REST and GraphQL. These architectures send a request to the server using HTTP. And receives the answer to this request as one of the two XML or JSON standards.

For example, when you try to register on the shopping server website, you will be presented with the option to sign in with a Google Account. Here, site developers have created a Web API bridge between the site registration system and Google’s Gmail service, implemented with one of the Web API architectures such as GraphQL or REST. In this process, the shopingserver website sends a request to Google via the HTTP protocol, such as the user name, user photo, and و, receives information in one of the two XML or JSON standards, and creates an account on its site.

What is GraphQL?

GraphQL is a query language for APIs and is considered architecture for APIs. GraphQL was first developed in the Facebook environment to address the internal flaws of mobile applications and has since been used specifically by social networks. Finally, in 2015, it was introduced to the developer community as open-source, and today it has become a new architecture for the API.

In this type of architecture, the user describes the type of data he wants, and the server returns only the same data.

What are the benefits of GraphQL?

GraphQL has features that set it apart from other APIs. We will describe these features below:

1. In GraphQL architecture, we are not limited to a lot of data:

A client is a software that sends a request to the server of another site or application. In the above example, the client can be considered the server of the shopingserver site. GraphQL has a client-centric event, which means that it allows the client to access exactly the information. Or a so-called response from a large amount of available data with a query or so-called query request. Receive itself without JSON standard data without any additional data. As a result, the developer is not confined to a large amount of data and obtains exactly the information he wants with one request. It can be concluded that GraphQL architecture uses less bandwidth.

 

2. GraphQL has high flexibility

Because the user describes exactly the data, he needs and receives the same data without any additional data, making the GraphQL API was developed with great flexibility.

3- In GraphQL architecture, we have nothing to do with API version

GraphQL is structured so that the API version does not matter. As a result, the API developed with GraphQL evolves without changing its version, and the developer no longer has the challenge of managing different versions of the API.

4. With GraphQL, development teams can work simultaneously

Because API versioning makes no sense in GraphQL, multiple teams can work on a project simultaneously. We will explain the process further with an example. Consider a situation where the front-end developer is waiting for the back-end developer to release the final version of the API. Because API versioning does not matter in GraphQL, the front-end developer can work with an initial version of the API. Test the code without waiting for the back-end developer to work.

5. The GraphQL structure prevents over-fetching of data

We have already said that in GraphQL, the request is sent as a query, and a specific answer is received without any additional data. The meaning of a special query is that it allows the user to specify what data and what kind of data he wants to receive. As a result, this structure prevents over-fetching of data and does not send any requests multiple times. Excessive fetching means that this feature prevents or minimizes the sending and receiving of data that the user does not need. In other words, the user accurately requests the required information, and the API provides the system with the same requested information in the form of JSON code.

6. Its structure is strongly typed

One of the advantages of this structure is that it is strongly typed. Being strongly typed means that there is a data type for all the objects used, and you can not define an object whose data type is not specified. This structure has the following advantages:

  • Predictable code
  •  Apply the same conditions to the client and server
  •  Independence of development teams and their simultaneous advancement
  • Quick debugging of errors

What are the disadvantages of GraphQL?

GraphQL, like any other service, has advantages and disadvantages. Here are some of the most important drawbacks:
GraphQL does not support cache, unlike the REST architecture. Of course, this can be covered using tools such as relay, but it is not as efficient as the rest standard. GraphQL implementation is also more complex than other API architectures. To implement GraphQL on the server side, we also need packages to make our job easier. To submit a request, since the user must accurately describe the command, they must manually write long, complex code, which increases the error rate.
We know that GraphQL uses queries to request, so we need to be familiar with data schema. By the time we make our API available to others, we should have developed this API strong and secure enough that the user can not easily access the data structure (Schema) while performing queries and Dos attacks.

What is meant by DOS attacks?

DOS stands for Denial-Of-Service. DOS is an attack in which an attacker sends too many requests to a server or computer, causing it to overuse resources such as the server processor, database, bandwidth, and so on, rendering the server inaccessible.

Conclusion:

The API is a technology that simplifies communication between applications, and one of its architectures is GraphQL. GraphQL is an emerging tool in API development, and in your first user experience with this structure, you may encounter many problems that may take you hours to solve. But it is always updating and fixing bugs that anyone who uses it may encounter. Finally, given the advantages and disadvantages of GraphQL, is it up to you to decide if the structure is suitable for developing your project API? Have you experienced using GraphQL? If you have experience, share it with other users in the comments section.