What does GraphQL do that REST can’t do?

What does GraphQL do that REST can’t do?


-3

I’ve been seeing that GraphQL allows the user to limit the data they receive, but that can be quite easily done on a REST API.

You can have a, for example, /users endpoint that takes a query array in the request body, that limits the columns returned from the server.

Or is there something else I’m missing?

EDIT: AS a disclaimer, I’m not asking for an opinion on which one is better. I’m asking for concrete things GraphQL does that REST cannot.

1

  • Is this really opinion based?

    – UndercoverCoder

    4 mins ago

1 Answer
1


1

You are correct that it is possible to limit the data returned by a REST API by including a query parameter in the request. However, GraphQL offers a number of advantages
Here is an example of a GraphQL query to fetch a user’s name and email address:

query {   user(id: 1) {
name
email   } }

Here is an example of a REST API request to fetch the same data:

GET /users/1?fields=name,email

As you can see, the GraphQL query is more concise and easier to read. It is also more flexible, since you could easily add additional fields to the query without having to modify the request.

I hope I have answered your question properly



Leave a Reply

Your email address will not be published. Required fields are marked *