When it comes to web development, applications frequently share data with one another. This is where APIs (application programming interfaces) come in, APIs serve as middlemen, allowing programs to request and receive data in a structured format.
However, when it comes to developing APIs, developers have two options: REST APIs and GraphQL APIs.
According to Postman 2023 state of the API report, 86% of developers use REST, while 29% use GraphQL.
This article will analyse both approaches and help you decide which one is the best fit for your next project.
Understanding APIs
APIs serve as messengers between programs. They define a set of rules and functions that enable apps to communicate and exchange data.
Imagine you run a recipe website that needs to display customer reviews. The website cannot directly access a separate user review database; this is where the API comes in.
The recipe website would send a request to the user review database using the API, stating the data required. The API would then retrieve this information and return it to the website, allowing it to show the reviews.
What is REST?
REST (Representational State Transfer) is an architectural style that provides a simple and consistent interface for transferring resources between a client and a server. REST specifies operations that employ common HTTP methods to conduct CRUD (Create, Read, Update, and Delete) tasks on resources.
Each resource has a unique identifier (URI) and can be accessed through dedicated endpoints. For instance, to access a specific product in a REST API, you might have an endpoint like /products/123 (where 123 is the product ID).
What are the advantages of working with REST APIs?
- Ease of use: Developers that are already familiar with HTTP can quickly learn REST. It leverages common HTTP techniques and conventions, resulting in a highly familiar flow.
- Language agnostic: REST APIs can be implemented in any language that supports HTTP without the requirement for extra support or communication layers.
- Strong community and tooling: REST is well-known and has been around for over 30 years. It has a strong community backing, and several developers have dedicated time and resources to developing comprehensive infrastructure for working with REST APIs.
- Widely supported: Most development tools and frameworks have strong support for REST APIs.
What is GraphQL?
GraphQL is a modern API query language that takes a different approach. Instead of preset endpoints, GraphQL uses a single endpoint for all data queries.
Developers create queries specifying the exact data they require, and the GraphQL server retrieves and returns that data in a single response.
GraphQL uses HTTP as its transport protocol and allows you to conduct three types of operations: queries, mutations, and subscriptions.
- Queries: Queries enable the client to request specific data using a predefined data format. They can be used to retrieve data from numerous sources, including different APIs, without having to make several round-trip calls to the server.
- Mutations in GraphQL enable the client to modify data on the server. This involves adding and updating existing data items.
- Subscriptions provide bidirectional, real-time communication between the client and server. Clients can subscribe to events and receive updates as soon as they are published, or vice versa.
What are the advantages of GraphQL?
GraphQL was originally designed to address some of the drawbacks of REST APIs, such as over-fetching. It also offers a good method for requesting and manipulating data.
Some of the advantages of GraphQL are:
- Single endpoint: GraphQL has a single endpoint for every operation. This streamlines the API’s URL structure and eliminates the need for many paths or routes in an API.
- Flexible data retrieval: GraphQL allows clients to specify and receive the exact data they require. This decreases the danger of overfetching and underfetching data while also improving network request efficiency by transmitting only relevant data objects.
- Strongly typed schema: GraphQL requires a schema that defines the data types and the relationships between them. The schema provides clients with an overview of the data that is available to them.
What are the similarities between GraphQL and REST?
Despite having different design philosophies, GraphQL and REST share many features because they are both used to provide web service APIs. Some of this similarities are;
- Similar architectural principles: REST and GraphQL are similar in many ways. Both of them are client-server model compatible, stateless, and accessible to any client with HTTP understanding.
- HTTP as transport layer: Both REST and GraphQL use HTTP as their transport layer by sending and receiving data using the standard HTTP POST method.
- Data transmission formats: Standard data formats are used for communication between GraphQL and REST. The most often used format is JSON, but other formats like XML can also be used, particularly with REST.
- Middleware and extensions: To add features like logging, caching, and authentication, middleware can be added to both GraphQL and REST.
What are the major differences between GraphQL and REST?
GraphQL and REST have different design patterns and approaches to data modeling. Here are some of the differences;
1. Popularity
GraphQL was released in 2015 by Facebook. Since then, it has grown quickly and has become well-known in recent years.
REST has been used for several decades and is a well-established standard for building APIs, especially public ones.
2. Enforced schema
GraphQL requires the use of a schema, regardless of whether developers follow a schema-first or code-first strategy. The code-first method generates the schema from the resolvers.
REST does not need the use of a schema. Developers can use API specifications such as OpenAPI and AsyncAPI to take a schema-first approach with REST, or they can auto-generate schemas from code, but this is not required for the REST architecture to function.
3. HTTP status code
GraphQL assigns the 200 status code to all responses, even error responses. The error answer is usually contained in the response payload itself.
REST uses standard HTTP status codes to indicate the status of the response. This is really useful for error handling.
4. Versioning
GraphQL has a single versioned endpoint. It manages updates and deprecations by updating and extending the schema without explicitly versioning the API, resulting in a more seamless long-term evolution.
REST APIs must be versioned in order to safely manage modifications and deprecations. This is commonly done in the API URL, although there are other techniques to API versioning as well.
5. Performance
GraphQL gives customers a lot of flexibility when requesting data, making it more efficient. Clients can request and receive only the data they want. This ensures that no extraneous data is delivered, and clients do not need to send many requests to obtain the information they require.
REST APIs, depending on how they were constructed, have the potential to over-fetch or under-fetch data. Clients may need to send repeated inquiries to obtain the information they require.
6. Introspection
GraphQL enables introspection, allowing clients to quickly retrieve schema-related information. Introspection can assist developers in automatically generating queries and documentation, as well as experimenting with various queries, mutations, and subscriptions.
REST APIs do not often include built-in introspection capabilities. However, API specifications like OpenAPI and AsyncAPI enable introspection and provide the same benefits as a GraphQL schema.
Conclusion
Choosing between REST and GraphQL depends on your project’s needs. REST is a good fit for simple data structures, existing integrations, or projects prioritizing ease of development. GraphQL shines for complex data relationships, performance optimization, and projects benefiting from a single endpoint and schema-driven approach.
Consider these trade-offs between ease of use, flexibility, performance, and maintainability when making your decision.