All posts

5 min read

REST developer learning GraphQL: what carries over, what's new, 30-day plan

Practical guide for seasoned REST developers transitioning to GraphQL, covering transferable concepts, new paradigms, pitfalls, and a 30‑day learning…

What carries over

REST conceptGraphQL equivalent / relevanceWhy it still matters
HTTP verbs (GET, POST, etc.)You still use HTTP, but most queries are POST (or GET for simple queries).The transport layer is unchanged; you still need to think about caching, authentication, and status codes.
StatelessnessEach GraphQL request is independent; the server does not store session state.Same scalability guarantees as REST; you still design idempotent operations.
Resource modelingTypes in the schema correspond to domain entities (User, Order, Product).Your mental model of “things” in the system carries over; you just express them as GraphQL types.
Versioning strategyInstead of versioned endpoints, you evolve the schema (add fields, deprecate).The principle of backward compatibility remains; you still need to communicate breaking changes.
Authentication & AuthorizationUse the same JWT, OAuth2, API keys, etc., passed via headers.Security layers are unchanged; you just plug them into the GraphQL context.
Error handlingHTTP status codes for transport errors; GraphQL errors array for application errors.You still need to distinguish network failures from business‑logic failures.
TestingUnit, integration, and contract tests still apply.Tools like Jest, Mocha, or Postman can be used with GraphQL queries.
Rate limiting & throttlingApply at the HTTP layer or per‑field resolver.Same concerns about abuse and resource consumption.

What's new

Side‑by‑side example

Task: Retrieve a list of published articles with their title, author name, and tags.

REST

GET /api/articles?status=published&fields=title,author,tags HTTP/1.1
Host: example.com
Authorization: Bearer <jwt>
[
  {
    "id": "a1",
    "title": "GraphQL Basics",
    "author": { "id": "u5", "name": "Alice" },
    "tags": ["graphql", "api"]
  },
  {
    "id": "a2",
    "title": "REST vs GraphQL",
    "author": { "id": "u7", "name": "Bob" },
    "tags": ["rest", "graphql"]
  }
]

GraphQL

query GetPublishedArticles {
  articles(status: PUBLISHED) {
    id
    title
    author {
      name
    }
    tags
  }
}
{
  "data": {
    "articles": [
      {
        "id": "a1",
        "title": "GraphQL Basics",
        "author": { "name": "Alice" },
        "tags": ["graphql", "api"]
      },
      {
        "id": "a2",
        "title": "REST vs GraphQL",
        "author": { "name": "Bob" },
        "tags": ["rest", "graphql"]
      }
    ]
  }
}

What changed and why

  • Single endpoint – All queries go to /graphql. No need to maintain a sprawling URL tree.
  • Declarative field selection – The client decides exactly which fields it needs; the server returns only those, eliminating over‑ and under‑fetching.
  • Strongly typed schema – The articles field, its arguments, and nested types are defined in a .graphql schema file, giving you compile‑time validation and IDE autocompletion.
  • Nested queries – You can fetch related objects (author) in the same request without extra round‑trips.
  • Versioning via schema evolution – Adding a new field (e.g., summary) is a non‑breaking change; you deprecate fields instead of creating a new endpoint.

Common pitfalls

  1. N+1 resolver problem – Naïve resolvers that fetch related data in a loop cause a separate DB query per parent. Mitigate with DataLoader or batch resolvers.
  2. Over‑exposing the schema – Adding every internal field to the public schema can leak implementation details and increase attack surface. Use @deprecated and limit exposure.
  3. Ignoring query complexity – Clients can request arbitrarily deep or wide queries. Implement depth limits, cost analysis, or persisted queries to protect the server.
  4. Treating GraphQL as a replacement for HTTP caching – Since most queries are POST, you lose default browser caching. Use Cache-Control headers, persisted queries, or a CDN that understands GraphQL.
  5. Mixing error handling styles – Returning HTTP 4xx/5xx for business errors breaks the GraphQL contract; instead, put errors in the errors array and keep the HTTP status 200 (unless the request itself fails).

Your 30‑day plan

Week 1 – Foundations

  1. Install the GraphQL reference server (npm i -g graphql-cli or brew install graphql-cli).
  2. Write a simple schema (schema.graphql) with type Query { hello: String }.
  3. Implement a resolver in Node.js using apollo-server (or your language of choice).
  4. Test queries with GraphQL Playground or Insomnia.
  5. Mini‑project: Build a “Hello, World” API that returns a greeting based on a name argument.

Week 2 – Data fetching & tooling

  1. Connect the resolver to a mock data source (JSON file or in‑memory array).
  2. Introduce a relational fetch: type Author { id: ID!, name: String! } and type Article { id: ID!, title: String!, author: Author! }.
  3. Add DataLoader to batch author lookups and eliminate N+1 queries.
  4. Explore schema‑first vs code‑first approaches (type-graphql or graphql-code-generator).
  5. Mini‑project: Create an Articles API with filtering (status) and nested author resolution.

Week 3 – Production concerns

  1. Add authentication middleware that injects a user object into the GraphQL context.
  2. Implement field‑level authorization (e.g., only the author can see draftContent).
  3. Set up query complexity limits using graphql-depth-limit or Apollo Server’s validationRules.
  4. Configure HTTP caching headers and experiment with persisted queries via Apollo Studio or graphql-cli.
  5. Mini‑project: Extend the Articles API to support a createArticle mutation with JWT‑based auth and input validation.

Week 4 – Ecosystem & best practices

  1. Generate TypeScript types from the schema with graphql-codegen.
  2. Write integration tests using supertest or apollo-server-testing.
  3. Add error‑formatting middleware to map domain errors to GraphQL errors entries.
  4. Document the schema with descriptions and deprecation notices; publish to a private GraphQL registry if needed.
  5. Capstone project: Build a small “Blog” service exposing queries (articles, article(id)) and mutations (createArticle, updateArticle) with proper auth, batching, and error handling. Deploy locally with Docker and run a smoke test suite.

FAQ

How do I handle pagination in GraphQL?

Use the Relay‑style Connection pattern (edges, node, pageInfo) or simple limit/offset arguments; both are supported by most client libraries.

Can I still use REST for file uploads?

Yes. You can keep a separate /upload endpoint or use GraphQL multipart request spec (apollo-upload-server) to send files alongside a mutation.

Do I need to learn a new query language if I already know SQL?

GraphQL is a query language for APIs, not databases. Its syntax is distinct from SQL, but the idea of selecting fields and filtering is familiar; learning the basics takes a few hours.

How does versioning work when I need to break a field?

Deprecate the field with @deprecated(reason: "use newField") in the schema, update clients, then remove it in a later release. This avoids creating a new endpoint version.

Keep going

#graphql #rest #api #learning