GraphQL VS REST APIs


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.

  1. - 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.

  1. - 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 the name of a user but the endpoint returns the entire user object with name, email, address, etc., that's over-fetching and opposite for under-fecthing.

- Requires multiple requests for related data.

GraphQL Query :-

- Like ordering a customizable meal.

- Single endpoint for all data.

  1. - With GraphQL, there's only one endpoint (/graphql). You specify the exact data you need in your query, and GraphQL returns it all in a single response.

- Requests exactly what you need.

  1. 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.