What is a REST API?
Let’s take an analogy from a real world scenario:
In a typical library,
- Students would go up to the librarian and make a request to borrow a book or return an already issued book etc.
- Next, the librarian would review the student request and decide whether or not it can fulfill the request (ex- validate student credentials, check book availability etc).
- Librarian would then fetch the requested physical book from the library database and issue it to the student.
In a very similar fashion,
- The REST API (i.e. librarian) sits in the middle between the web clients ex — desktop, mobile, smartwatch (i.e. students) and the database server (i.e. library database) and serves web requests.
In what scenarios, GraphQL would be a better fit?
- What if student requests books that are available across multiple libraries in the city, REST API would require the student to visit each library separately.
- What if a student only needs a paragraph on a specific page in a given book, REST API would still require the entire book to be issued.
Is GraphQL awesome or what…
Credit to Morten Rand-Hendriksen for this simple REST API explanation. Happy learning!
Bonus q: What’s the difference between REST and RESTful API?