🔍GraphQL – In-Depth Study Sheet


🧠 What is GraphQL?

GraphQL is a query language for APIs and a runtime for executing those queries by using a type system you define for your data.

  • Developed by: Facebook (2012), Open-sourced in 2015.
  • Transport protocol: Typically uses HTTP POST, but works over any protocol.
  • Main Idea: Let the client ask for exactly the data it needs — no more, no less.

🏗️ How It Works

  • Schema-Driven:
    • Everything in GraphQL revolves around a strongly typed schema.
    • Defines queries, mutations, and types.
  • Query Structure:
query {
  customer(id: 3) {
    name
    orders {
      id
      total
      products {
        name
        price
      }
    }
  }
}

Returns a nested JSON response with just the requested fields.

  • Mutations: For creating/updating/deleting data.
mutation {
  createOrder(customerId: 3, items: [{ productId: 1, quantity: 2 }]) {
    orderId
    status
  }
}

Advantages of GraphQL

BenefitExplanation
🧩 Client-defined QueriesClients specify the exact data structure they need.
🔄 Single Request, Multiple ResourcesCombine what would be many REST calls into one GraphQL query.
🚫 No Overfetching/UnderfetchingUnlike REST, only needed fields are retrieved.
🕵️‍♀️ No Versioning NeededSchema evolves without breaking old queries.
🧱 Strongly Typed SchemaAPI structure is well-documented and type-safe.
🧪 Great for Rapid DevelopmentUI teams can iterate without backend changes.

⚠️ Challenges / Trade-offs

ChallengeDetails
⚙️ Complex Server LogicResolvers can become deeply nested and hard to manage.
🧠 Learning CurveNew paradigm vs. traditional REST.
🚫 Caching DifficultyHTTP caching is less effective since all queries often use POST.
📉 Performance PitfallsNaïve queries can result in N+1 problems without data loaders.
🛡️ Security & Rate LimitingMore complex than REST. Query depth and cost control needed.
🔐 Access Control ComplexityGranular access control must be enforced at the field level.

🔄 GraphQL vs REST vs gRPC

FeatureGraphQLRESTgRPC
ProtocolHTTP (POST)HTTP (GET, POST, etc.)HTTP/2
Payload FormatJSONJSON/XMLProtocol Buffers (binary)
Client ControlHigh (asks for exact fields)Low (fixed responses)Medium (contract-defined)
Schema TypeStrongly typed via SDLAd-hoc or OpenAPIStrongly typed via Protobuf
BatchingYes (manual or tools)NoYes
StreamingNot nativeNoYes (bi-directional)
CachingHardEasyHard
ToolingApollo, GraphiQL, RelayPostman, Swaggergrpcurl, Postman (limited)
Use CaseComplex front-ends, mobile appsSimple APIs, third-party accessInternal comms, low-latency ops

🧪 When to Use GraphQL

Use CaseSuitability
Modern web/mobile apps needing flexibility✅ Great fit
Aggregating multiple microservices✅ Excellent
Replacing REST APIs in monoliths⚠️ Requires planning
High-performance internal communication❌ gRPC is better

🧰 GraphQL Tooling Ecosystem

ToolPurpose
Apollo Server/ClientFull-featured GraphQL server & client ecosystem
GraphiQLIn-browser IDE for testing GraphQL queries
RelayFacebook’s GraphQL client for React
HasuraAuto-generates GraphQL over Postgres DB
PrismaORM and GraphQL schema generator