What does GraphQL do that REST can’t do?
There are some key differences that make GraphQL more flexible in certain situations. Let me explain with an example:
REST:
Imagine you want to fetch information about a book from a library’s API. In a REST API, you might have endpoints like these:
To get the book’s title: GET /books/1/title
To get the book’s author: GET /books/1/author
To get the book’s publication date: GET /books/1/publication_date
Each of these endpoints corresponds to a specific piece of information about the book. If you need all this information, you would need to make three separate requests to the server. This can be inefficient, especially if you want more or less data or need data from multiple related resources.
GraphQL:
In GraphQL, you send a single query to the server specifying exactly what data you want, and you get back just that data. Here’s how it would look:
query { book(id: 1) {
title
author
publication_date } }
In this single query, you ask for the book’s title, author, and publication date. The server then responds with all the requested data in a single response.
Read more here: Source link
