DEV Community

Avinash Maurya
Avinash Maurya

Posted on

RESTAPI vs GraphQL

Certainly! Let's compare GraphQL, a modern approach to API development, with a traditional approach, using REST, and showcase how Postman can be utilized for both.

Traditional Approach (REST API):

Example REST Endpoint:

Assume you have a RESTful API for managing a list of books:

  • Endpoint to Get a Book:
  GET /api/books/123
Enter fullscreen mode Exit fullscreen mode
  • Example Response:
  {
    "id": 123,
    "title": "The Great Gatsby",
    "author": "F. Scott Fitzgerald",
    "year": 1925
  }
Enter fullscreen mode Exit fullscreen mode

Usage in Postman (REST):

  1. Open Postman.
  2. Create a new request.
  3. Set the request method to GET.
  4. Enter the URL: https://example.com/api/books/123.
  5. Send the request.
  6. Receive the response.

GraphQL Approach:

Example GraphQL Query:

Assume you have a GraphQL API for the same book service:

  • GraphQL Query:
  query {
    book(id: 123) {
      title
      author
      year
    }
  }
Enter fullscreen mode Exit fullscreen mode

Usage in Postman (GraphQL):

  1. Open Postman.
  2. Create a new request.
  3. Set the request method to POST.
  4. Enter the URL: https://example.com/graphql.
  5. In the request body, enter the GraphQL query.
  6. Send the request.
  7. Receive the response.

Comparison:

  1. Data Retrieval:

    • REST: You get a predefined set of data in the response.
    • GraphQL: You request only the data you need, reducing over-fetching.
  2. Request Format:

    • REST: Multiple endpoints for different resources and actions (GET, POST, PUT, DELETE).
    • GraphQL: A single endpoint (/graphql) and a flexible query language.
  3. Response Format:

    • REST: The server dictates the response structure.
    • GraphQL: The client specifies the shape and structure of the response.
  4. Efficiency:

    • REST: May suffer from over-fetching or under-fetching of data.
    • GraphQL: Provides a more efficient way to retrieve precisely the needed data.
  5. Evolution:

    • REST: Versioning may be necessary to introduce changes.
    • GraphQL: Allows gradual changes without versioning, reducing the need for multiple endpoints.
  6. Postman Usage:

    • REST: Standard HTTP methods in Postman.
    • GraphQL: Use the Postman GraphQL client to send queries and visualize the schema.

In summary, GraphQL offers more flexibility in data retrieval, reduced over-fetching, and a single endpoint for various operations. Postman provides a user-friendly interface for both REST and GraphQL, allowing developers to interact with APIs efficiently, regardless of the chosen approach.

Top comments (1)

Collapse
 
mzakiullahusman profile image
Muhammad Zakiullah Usman

Nice work, chatgpt