Welcome to Ed2Ti Blog.

My journey on IT is here.

RESTful API vs GraphQL

- Posted in Trebas College by

When it comes to building modern web applications, there are two popular approaches for designing and consuming APIs: RESTful APIs and GraphQL. Both have their advantages and disadvantages, and it's important to understand the differences between the two before deciding which one to use.

REST (Representational State Transfer) is a set of guidelines for building web services that use HTTP methods such as GET, POST, PUT, and DELETE to manipulate resources. RESTful APIs are designed around resources and their representations, and they typically use JSON or XML to transfer data between the client and server.

GraphQL, on the other hand, is a query language for APIs that was developed by Facebook. It allows clients to request specific data from the server using a single endpoint, and the server responds with only the requested data. GraphQL uses a type system to define the data that can be queried, and it supports real-time updates and subscriptions.

So, which approach is better? Let's take a closer look at the pros and cons of each.

RESTful APIs

Pros:

Widely adopted: RESTful APIs are a well-established standard and are used by many companies, including Twitter, Google, and Amazon. Caching: Because RESTful APIs use HTTP caching, they can improve performance by reducing the number of requests to the server. Simplicity: RESTful APIs are simple and easy to understand. They use HTTP methods and URLs to manipulate resources, which makes them easy to work with.

Cons:

Overfetching and underfetching: Because RESTful APIs return the entire resource by default, clients may end up receiving more data than they need (overfetching). On the other hand, if a client needs data from multiple resources, it may need to make multiple requests to retrieve all the necessary data (underfetching). Versioning: If the API changes, clients may need to be updated to work with the new version. This can be challenging if the API has a large number of clients. Complexity with hierarchical data: If the data is hierarchical, such as a tree structure, clients may need to make multiple requests to retrieve all the necessary data.

GraphQL

Pros:

Flexibility: Because clients can request only the data they need, GraphQL can reduce the amount of data transferred over the network, resulting in improved performance. Strongly typed: GraphQL's type system makes it easy to understand the structure of the data being queried and reduces the risk of errors. Real-time updates: GraphQL supports real-time updates and subscriptions, which makes it ideal for building real-time applications.

Cons:

Learning curve: Because GraphQL is a new technology, it may take some time to learn and understand. Caching: Because GraphQL queries can be very specific, caching can be more difficult to implement than with RESTful APIs. Server complexity: Because the server needs to process and validate queries, it can be more complex to implement than a RESTful API.

Conclusion

Both RESTful APIs and GraphQL have their advantages and disadvantages, and the choice between them will depend on the specific requirements of the project. RESTful APIs are a well-established standard and are simple to work with, but they can be limited in terms of flexibility and performance. GraphQL, on the other hand, offers greater flexibility and can improve performance by reducing the amount of data transferred over the network, but it requires more effort to implement and may not be suitable for all use cases.

Ultimately, the choice between RESTful APIs and GraphQL will depend on factors such as the size and complexity of the application, the types of data being queried, and the performance requirements of the project.

College: Trebas Institute
Professor: Iyad Koteich

It's impressive how fast and easy you can start your won API solution using Flask and Python.

In this post, I want to share with you my experience using tools and concepts to create a simple API solution.

Tools:

  • Phyton (https://www.python.org/)
  • Flask (https://flask.palletsprojects.com/en/2.2.x/)
  • SQLite (https://www.sqlite.org/)
  • Postman (https://web.postman.co/)

In this restful API I'm using the verbs:
* POST - To insert information into a database
* GET - To recover information from a database.
* PUT (I'm not using) - To update information into a database.
* DELETE (I'm not using) - To delete information from a database.

The next objective is to create a full CRUD API.
All the codes are here on my Git repository: https://github.com/ed2ti/python_flask_db