Developer 101: An overview of REST and GraphQL API’s

Job Alex Muturi
4 min readSep 18, 2023
Photo by Serenity Mitchell on Unsplash

Introduction

In the current era software development, most software developers or engineers have from had to work with either of the two prominent approaches to building APIs that have continued to gain widespread attention: Representational State Transfer (REST) and GraphQL.

Each approach has its strengths and weaknesses, making it crucial and important for developers a good understanding of either or both. In this article, we’ll try and do overview of both REST and GraphQL, highlighting their key characteristics, advantages, and disadvantages.

REST API

What is REST?

REST, or Representational State Transfer, is an architectural style for designing networked applications. It defines a set of constraints and principles for building web services, with an emphasis on simplicity and scalability. RESTful APIs use HTTP methods (GET, POST, PUT, DELETE) to perform actions on resources represented by URLs.

Key Characteristics of REST API:

1. Stateless: REST APIs are stateless, meaning each request must contain all necessary information for processing, with no reliance on previous requests.

2. Resource-Based: Resources are identified by URLs, and standard HTTP methods operate on these resources.

3. Data Presentation: REST allows multiple representations of resources, such as JSON, XML, or HTML, based on client preferences.

4. Independence: Resources are independent, meaning each endpoint provides a specific set of functionalities.

Pros of REST API:

1. Simplicity: REST is easy to understand and implement, making it an excellent choice for straightforward projects.

2. Scalability: REST APIs can be scaled horizontally by adding more servers, and caching can improve performance.

3. Caching: REST supports server and client-side caching, reducing the load on the server.

4. Widespread Adoption: REST is widely adopted, and there is a rich ecosystem of tools and libraries available for it.

Cons of REST API:

1. Over-fetching and Under-fetching: A very common phenomenon, clients may receive more or less data than needed, leading to performance inefficiencies.

2. Versioning: Managing API versions can be challenging when backward-incompatible changes are required.

3. Chatty APIs: Under-fetching results in multiple requests being needed in order to gather related data, resulting in latency issues.

GraphQL

What is GraphQL?

GraphQL is a query language for APIs developed by Facebook. Unlike REST, which exposes fixed endpoints for resources, GraphQL allows clients to request precisely the data they need by specifying the fields and their relations in a query object, and nothing more. This fields, their relations, types, etc. are described or defined in a document referred to as a schema. GraphQL provides a single endpoint for querying and manipulating or mutating data.

Here is an example of a GraphQL Query.

export const FETCH_BOOKS = gql`
query FetchBooks(
$genre: String!
$page: Int!
$size: Int!
) {
getBooks(genre: $genre, page: $page, size: $size) {
code
message
data {
id
creationDate
title
isbn
description
author {
name
contact
}
}
}
`;

The above sample will somewhat look like an interface when used to describe a type or the shape of an object in Typescript.

Key Characteristics of GraphQL:

1. Query Language: Clients define the structure of the response, specifying the fields and relationships they want to retrieve. See sample above.

2. Strong Typing: GraphQL uses a strongly-typed schema to ensure data consistency and clarity.

3. Single Endpoint: GraphQL has a single endpoint for all operations i.e. querying and mutating the data, reducing the need for multiple API versions.

4. Real-time Updates: GraphQL can be integrated with real-time capabilities to receive updates instantly.

Pros of GraphQL:

1. Efficiency: GraphQL eliminates over-fetching and under-fetching, resulting in more efficient data retrieval.

2. Flexibility: Clients have fine-grained control over the data they receive, reducing unnecessary network traffic.

3. Single Endpoint: GraphQL simplifies the API structure, making it easier to maintain and reducing versioning concerns.

4. Introspection: GraphQL’s introspective nature allows clients to explore the schema and discover available operations.

Cons of GraphQL:

1. Complexity: The flexibility of GraphQL can lead to complex queries, requiring careful schema design and performance optimization.

2. Learning Curve: Developers need to learn the GraphQL query language and understand how to structure queries efficiently.

3. Security: Inadequate query validation can lead to security vulnerabilities, such as excessive data exposure (e.g., Denial-of-Service attacks).

Comparison: REST vs. GraphQL

REST is an excellent choice when simplicity and familiarity are paramount. It’s a solid choice for projects with clear data structures and straightforward requirements. However, REST can suffer from over-fetching and under-fetching issues, which may impact performance.

GraphQL, on the other hand, shines when flexibility and efficient data retrieval are crucial. It allows clients to request exactly what they need, reducing unnecessary data transfer. GraphQL is especially beneficial for complex applications with rapidly changing requirements.

Ultimately, the choice between REST and GraphQL depends on your project’s specific needs. Below are some factors you can consider when making your decision:

- Project Complexity: REST may be simpler for straightforward projects, while GraphQL excels in complex applications.

- Client Control: GraphQL provides more control to clients because they can describe the response object structure most reducing trips done to fetch the same, while REST follows a fixed server-defined structure or contract.

- Existing Ecosystem: Consider the tools, libraries, and expertise available for each technology in your development ecosystem.

- Performance: Assess whether the reduced data transfer of GraphQL outweighs the complexity of implementation.

- Real-time Needs: If your application requires real-time updates, GraphQL might be more suitable, though REST implementations can have Push capabilities, GraphQL offers more robust subscription options that can be baked right into the queries.

Conclusion

Both REST and GraphQL are valuable technologies with distinct advantages and disadvantages. REST offers simplicity and widespread adoption, while GraphQL provides fine-grained control and efficient data retrieval. Your choice should align with your project’s specific requirements, complexity, and performance considerations. Ultimately, understanding the strengths and weaknesses of each approach will empower you to make an informed decision and build robust APIs that meet your development goals.

--

--

Job Alex Muturi

Angular Developer | Angular Kenya Peer Mentor | Blogger | Tech Speaker