GraphQL is a query language for APIs. Unlike REST, where the server decides the shape of the response, GraphQL lets the client ask for exactly what it needs.
REST vs GraphQL
With REST you might hit three endpoints to build a profile page:
GET /users/1
GET /users/1/posts
GET /users/1/followers
With GraphQL, one query does it all:
query {
user(id: "1") {
name
posts {
title
}
followers {
name
}
}
}Setting up a simple server
Using graphql-yoga — minimal and modern:
npm install graphql-yoga graphqlimport { createServer } from "node:http";
import { createYoga, createSchema } from "graphql-yoga";
const schema = createSchema({
typeDefs: `
type Query {
hello: String
}
`,
resolvers: {
Query: {
hello: () => "Hello from GraphQL!",
},
},
});
const yoga = createYoga({ schema });
createServer(yoga).listen(4000, () => {
console.log("Running at http://localhost:4000/graphql");
});Mutations
Reading data uses query, writing uses mutation:
mutation {
createPost(title: "GraphQL is great", body: "...") {
id
title
}
}Tips for beginners
- Start with queries before mutations.
- Use GraphQL Playground (built into most servers) to explore the schema interactively.
- Keep resolvers thin — business logic belongs in a service layer.
- Learn about
DataLoaderearly to avoid the N+1 query problem.
GraphQL shines when you have multiple clients (web, mobile) with different data needs. It's worth learning even if REST covers most of your use cases.