iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up
« Previous

GraphQL HOME

GraphQL is a query language for APIs. Clients ask for exactly the shape they need; one endpoint serves them all.

GraphQL — homepage

EXAMPLE
# ===== The idea =====
# REST exposes resources; clients fetch fixed shapes per endpoint.
# GraphQL exposes a TYPE GRAPH; clients pick the fields they want.

# Single endpoint:  POST /graphql
# Single response:  exactly the shape the query asked for

# ===== Tiny schema + query =====
type User {
  id: ID!
  name: String!
  email: String!
  orders(limit: Int = 10): [Order!]!
}

type Order {
  id: ID!
  total: Int!
  status: String!
}

type Query {
  user(id: ID!): User
}

# Query
query {
  user(id: "u-1") {
    name
    orders(limit: 3) {
      total
      status
    }
  }
}

# Response
{
  "data": {
    "user": {
      "name": "Alex",
      "orders": [
        { "total": 4995, "status": "paid" },
        { "total": 1299, "status": "new" }
      ]
    }
  }
}

# ===== Three operation kinds =====
# query        read
# mutation     write
# subscription real-time push (websocket / SSE)

# ===== What GraphQL gives you =====
# - One round trip for nested data
# - Strong types: SDL is the contract; clients generate types
# - Self-documenting: GraphiQL / GraphQL Playground / Apollo Studio
# - Versionless evolution: add fields without breaking clients

# ===== What GraphQL adds =====
# - N+1 problem requires DataLoader-style batching
# - Resolver complexity grows with type graph
# - Caching different from REST (clients cache by type+id, not URL)
# - Schema design is real work; bad shapes hurt for a long time

# ===== Where in the stack =====
# Client: Apollo Client, urql, Relay, graphql-request
# Server: Apollo Server, GraphQL Yoga, Mercurius, Hot Chocolate (.NET)
# Tools:  GraphQL Code Generator, schema linting, persisted queries

# ===== Patterns to internalise =====
# - Design types around domain entities, not table rows
# - DataLoader for every leaf with a database lookup
# - Persisted queries in production (avoid arbitrary queries from clients)
# - Pagination with Relay-style edges + cursors

# ===== Pitfalls =====
# - One mega-Query type with everything attached -> hard to evolve
# - Exposing internal IDs and shapes that leak schema concerns
# - Public APIs without query depth/complexity limits -> DoS risk
# - Treating GraphQL as REST with extra steps; the wins come from shaping for the read

# ===== When to choose GraphQL =====
# - Many clients with different needs (web, mobile, partners)
# - Aggregation over multiple services
# - Strong typing across team boundaries

Why it matters

GraphQL is a contract-first approach where the schema is the API. Clients fetch the exact shape they need; the server resolves it through small typed functions. The win is over-fetching gone and types everywhere; the cost is schema design discipline plus N+1 mitigation. Worth it when many clients consume one graph.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
query { hello }
Try it Yourself »

Discussion

Loading…

« Previous