When Facebook built GraphQL, they needed a powerful data capture API that could handle all of Facebook’s tasks, but was easy for its product developers to learn and use. GraphQL aims to recreate Facebook’s native mobile application. GraphQL is a query language. It provides the query as a string sent to the server. The server interprets the query and returns the results to the client in JSON format.

The advantages of GraphQL

GraphQL fast

GraphQL is much faster than other communication apis because it helps you reduce requested queries by selecting only specific fields to query.

Best suited for complex systems and microservices

We can integrate multiple systems behind the GraphQL API. It unifies them and hides their complexity. The GraphQL server is also used to fetch data from existing systems and package it into GraphQL response format. This is most beneficial for legacy infrastructure or third-party apis that are large and difficult to maintain and process.

When we have to move from a monolithic back-end application to a microservice architecture, the GraphQL API can help us handle the communication between multiple microservices by merging them into a single GraphQL schema.

There is no over-acquisition and under-acquisition

GraphQl’s main advantage over REST is that REST responses contain too much or sometimes not enough data, which creates the need for another request. GraphQL solves this problem by retrieving only accurate and specific data in a single request.

Simple and clear hierarchy

GraphQL follows a hierarchical structure, where relationships between objects are defined in a graphical structure. Here, each object type represents a component, and each relational field from one object type to another represents one component wrapping another.

Custom data shapes

When we ask the server for a GraphQL query, the server returns the response in a simple, safe, and predictable form. As such, it helps you write specific queries based on your requirements. This makes GraphQL very easy to learn and use.

Code sharing

The GraphQL fields used in multiple queries can be shared at a higher component level for reuse. This feature, called fragmentation, allows you to retrieve different data while retaining the same schema fields.

Strongly typed

GraphQL is a strongly typed language, where each level of a GraphQL query corresponds to a specific type, and each type describes a set of available fields. As such, it is similar to SQL and provides descriptive error messages before the query is executed.

Protocol, not storage

Any function supports the GraphQL field on the server. They do not specify or provide any backup storage. Instead, GraphQL draws on your existing code.

introspection

We can query the GraphQL server for supported types. It creates a powerful platform for tools and client software such as application frameworks, ides such as Relay or GraphiQL. GraphiQL helps developers quickly learn and explore apis.

The latest version is not required

In GraphQL, the result set or returned data is very specific based on the client query, so; It’s very simple and easy for the server to generalize. When we add new product features and additional fields to the server, they do not affect existing clients. You can use the old server with no qualms, because the server fields may be deprecated but still run. This compatibility process does not require an incrementing version number. You can see that Facebook uses the same version of the GraphQL API in their application.

The disadvantage of GraphQL

While GraphQL’s disadvantages are negligible compared to its advantages, we present some disadvantages here. Here’s a list of GraphQL’s downsides:

GraphQL query complexity

Don’t mistake GraphQL for a server-side database replacement. It’s just a simple query language. When a query is requested, the server performs database access. When we have to access multiple fields in a query, whether requested in a RESTful architecture or GraphQL, we still need to retrieve various resources and fields from the data source. Therefore, when a client requests too many nested field data at once, it displays the same problem. Therefore, there must be a mechanism, such as maximum query depth, query complexity weighting, and avoidance of recursive or persistent queries, to prevent inefficient requests from clients.

GraphQL Caching

Implementing simplified caching with GraphQL is more complex than implementing it in REST. In REST apis, we access resources through urls, so we can cache at the resource level because we use resource urls as identifiers. In GraphQL, on the other hand, it’s very complicated because every query can be different, even though it operates on the same entity. But most libraries built on top of GraphQL provide an efficient caching mechanism.

GraphQL rate limit

Another problem with GraphQL is the rate limit. In the REST API, you can simply specify that we only allow this number of requests a day “, but in GraphQL, it’s hard to specify this type of statement.

In this paper, original English from: www.javatpoint.com/graphql-adv…