This is the simplest explanation can be written for GraphQL VS REST
REST API :-
- Like ordering from a fixed menu.
- Different endpoints for different data.
- - To get different types of data, you need to make requests to different endpoints. For example, to get user data and post data, you might need to make separate requests to
/users
and/posts
.
- Can over-fetch or under-fetch data.
- - When you make a request to a
REST API
, you might get back more data than you actually need(over-fetching)
or not enough data(under-fetching).
For example, if you only need thename
of auser
but the endpoint returns the entireuser object
withname, email, address,
etc., that'sover-fetching
and opposite forunder-fecthing
.
- Requires multiple requests for related data.
GraphQL Query :-
- Like ordering a customizable meal.
- Single endpoint for all data.
- - With GraphQL, there's only one endpoint
(/graphql)
. You specify the exact data you need in yourquery
, and GraphQL returns it all in a single response.
- Requests exactly what you need.
- You request only the specific fields you need, so there's no
over-fetching or under-fetching
. If you only ask for the name of a user, GraphQL will only return the name.
- Fetches related data in a single request.