GraphQL Vs REST API?

Sultan Butt
4 min readJan 22, 2023

What is REST API?

A REST API is an API that follows the design principles of the REST (or Representational State Transfer) architecture.

REST APIs communicate via HTTP requests to perform CRUD (or Create, Read, Update, and Delete) operations.

Pros and cons of REST API

REST offers great concepts such as stateless servers and structured access to resources. It provides a great deal of flexibility. Data is not tied to resources or methods, so REST can handle multiple types of calls, return different data formats and even change structurally with the correct implementation of hypermedia. This flexibility allows developers to build an API that meets your application needs while also meeting the diverse clients’ requirements.

Nevertheless, REST APIs have shown to be too inflexible to keep up with the rapidly changing requirements of the clients that access them. With REST, you have to make multiple requests to different endpoints to fetch the required data. You are also over-fetching since the endpoints return additional information that’s not needed.

What is GraphQL?

GraphQL is a query language for APIs. It gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

Facebook developed GraphQL in 2012 and it has continued to grow in popularity since it was first introduced. The biggest strengths of GraphQL include:

  • The ability to control exactly what information you receive from the server, and receive data in a predictable structure.
  • The ability to request data from multiple sources in a single request.

Pros and cons of GraphQL

GraphQL also offers some interesting concepts — specifically fragments: On the client-side, you can use GraphQL fragments to split a large query into smaller parts. The smaller parts can be used to define the query of the respective use case such as data requirements per UI component. Finally, a (large) query can then be assembled from the fragments, which contains all data requirements of all components and the required data can then be loaded from the server using a single request. Thus, GraphQL merges all the RESTful endpoints into one and uses less network traffic.

Having said that, the downside of GraphQL is that special attention must be given to performance on the server-side. Since more freedom is gained at the client-side with flexible data requirements, you have to invest more thought in avoid running into server performance issues.

How REST performance is different from GraphQL?

A major difference between the two standards is who defines the response — whether the client or server. In REST, the server defines the state of the response and in GraphQL, the client defines the shape of the response. Apart from this factor, there are many other notable differences that are summarized below:

  1. REST uses mostly HTTP protocol for data transfer. In GraphQL, it’s up to you as an implementer to decide the protocol for transmitter. Therefore, you can have HTTP, web sockets, MQTT, etc.
  2. REST needs one endpoint URL per resource, whereas GrahpQL usually requires only one endpoint per application.
  3. REST uses HTTP verb (GET, POST, DELETE, etc) for semantics and GraphQL uses queries and mutations for semantics
  4. REST maintains separate documentation for your endpoints and GraphQL is self-documented via schema introspection.

Conclusion

GraphQL and REST are both ways to build web APIs, but they have some key differences in how they work.

One of the main advantages of GraphQL is that it allows the client to specify exactly what data it needs, rather than having the server return a fixed set of data. This can lead to more efficient data transfer, as the client only receives the data it actually needs. Additionally, a single GraphQL endpoint can be used to retrieve data from multiple resources, making it easier to manage and maintain.

REST APIs, on the other hand, typically have a fixed structure, with a specific endpoint for each resource. This can make it more difficult to update or make changes to the API, as any change to the structure of the data will require updates to the client code.

So, whether GraphQL is better than REST API depend on the use case and the requirements of the specific application. GraphQL may be a better choice for situations where flexibility and efficient data transfer are important, while REST may be a better choice for simpler cases where a fixed structure is sufficient.

--

--

Sultan Butt

Full Stack Engineer (React, React Native, Node, GraphQL)