DEV Community

Avinash Maurya
Avinash Maurya

Posted on

1

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.

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (1)

Collapse
 
mzakiullahusman profile image
Muhammad Zakiullah Usman

Nice work, chatgpt

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay