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 concept | GraphQL equivalent / relevance | Why 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. |
| Statelessness | Each GraphQL request is independent; the server does not store session state. | Same scalability guarantees as REST; you still design idempotent operations. |
| Resource modeling | Types 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 strategy | Instead of versioned endpoints, you evolve the schema (add fields, deprecate). | The principle of backward compatibility remains; you still need to communicate breaking changes. |
| Authentication & Authorization | Use the same JWT, OAuth2, API keys, etc., passed via headers. | Security layers are unchanged; you just plug them into the GraphQL context. |
| Error handling | HTTP status codes for transport errors; GraphQL errors array for application errors. | You still need to distinguish network failures from business‑logic failures. |
| Testing | Unit, integration, and contract tests still apply. | Tools like Jest, Mocha, or Postman can be used with GraphQL queries. |
| Rate limiting & throttling | Apply 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
articlesfield, its arguments, and nested types are defined in a.graphqlschema 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
- 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.
- Over‑exposing the schema – Adding every internal field to the public schema can leak implementation details and increase attack surface. Use
@deprecatedand limit exposure. - Ignoring query complexity – Clients can request arbitrarily deep or wide queries. Implement depth limits, cost analysis, or persisted queries to protect the server.
- Treating GraphQL as a replacement for HTTP caching – Since most queries are POST, you lose default browser caching. Use
Cache-Controlheaders, persisted queries, or a CDN that understands GraphQL. - Mixing error handling styles – Returning HTTP 4xx/5xx for business errors breaks the GraphQL contract; instead, put errors in the
errorsarray and keep the HTTP status 200 (unless the request itself fails).
Your 30‑day plan
Week 1 – Foundations
- Install the GraphQL reference server (
npm i -g graphql-cliorbrew install graphql-cli). - Write a simple schema (
schema.graphql) withtype Query { hello: String }. - Implement a resolver in Node.js using
apollo-server(or your language of choice). - Test queries with GraphQL Playground or Insomnia.
- Mini‑project: Build a “Hello, World” API that returns a greeting based on a
nameargument.
Week 2 – Data fetching & tooling
- Connect the resolver to a mock data source (JSON file or in‑memory array).
- Introduce a relational fetch:
type Author { id: ID!, name: String! }andtype Article { id: ID!, title: String!, author: Author! }. - Add DataLoader to batch author lookups and eliminate N+1 queries.
- Explore schema‑first vs code‑first approaches (
type-graphqlorgraphql-code-generator). - Mini‑project: Create an Articles API with filtering (
status) and nested author resolution.
Week 3 – Production concerns
- Add authentication middleware that injects a
userobject into the GraphQL context. - Implement field‑level authorization (e.g., only the author can see
draftContent). - Set up query complexity limits using
graphql-depth-limitor Apollo Server’svalidationRules. - Configure HTTP caching headers and experiment with persisted queries via Apollo Studio or
graphql-cli. - Mini‑project: Extend the Articles API to support a
createArticlemutation with JWT‑based auth and input validation.
Week 4 – Ecosystem & best practices
- Generate TypeScript types from the schema with
graphql-codegen. - Write integration tests using
supertestorapollo-server-testing. - Add error‑formatting middleware to map domain errors to GraphQL
errorsentries. - Document the schema with descriptions and deprecation notices; publish to a private GraphQL registry if needed.
- 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