Rest vs Qraphql - AdarshTheki/mern-stack-learn GitHub Wiki

REST APIs ( Representational State Transfer)

REST API is an architectural style for designing networked applications. It's a lightweight, flexible, and scalable way to communicate between systems, allowing data exchange and manipulation.

  • HTTP Methods:

    • GET (Retrieve resource)
    • POST (Create resource)
    • PUT (Update resource)
    • DELETE (Delete resource)
    • PATCH (Partial update resource)
  • REST API Components:

    • Resource: Identified by URI (Uniform Resource Identifier)
    • Request: Client sends HTTP request to server
    • Response: Server sends HTTP response to client
    • Header: Metadata about request/response (e.g., authentication, content type)
    • Body: Payload of request/response (e.g., JSON data)
  • REST API Status Codes:

    • 200 OK (successful request)
    • 404 Not Found (resource not found)
    • 401 Unauthorized (authentication failed)
    • 500 Internal Server Error (server error)

GraphQL (Graph Query Language)

GraphQL is a query language for APIs (Application Programming Interfaces) that allows clients to specify exactly what data they need, reducing overhead and improving performance.

  • Key Features:

    • Declarative queries
    • Strong typing
    • Schema-driven development
    • Query optimization
    • Real-time updates
  • GraphQL Query Components:

    • Fields (specific data)
    • Types (data types)
    • Resolvers (functions retrieving data)
    • Queries (fetching data)
    • Mutations (modifying data)
    • Subscriptions (real-time updates)

GraphQL vs. REST:

GraphQL REST
Query structure Declarative Imperative
Data retrieval Specific fields Entire resource
API endpoints Single endpoint Multiple endpoints
Data types Strongly typed Weakly typed
Real-time updates Native support WebSockets or polling
Performance Fast Multiple network calls take up more time
Web caching Uses web library Inbuilt
File uploading No Yes