Quick answer
Apollo Server is the reference way to run a GraphQL API in Node.js: you describe the data in a schema, write a resolver for each field, and the server handles parsing, validation, and HTTP. This post builds an orders API for a shop in Karachi, with a query that filters by city and a mutation that adds an order.
A shop in Karachi takes orders over the phone and writes each one in a notebook. The owner wants a small API so a web page can list orders and add new ones, and the phone screen only needs the customer, the city, and the total. A REST version would mean one endpoint for the list, another for orders by city, and a third for adding an order. GraphQL offers a different deal: one endpoint, one schema, and each screen asks for exactly the fields it renders. That idea is explained in the GraphQL Explained: Ask for Exactly the Data You Need post, and this post is the server side of it.
What I would check before using it
My first check is how many shapes the clients need. If a web page and a phone app need different slices of the same orders, GraphQL earns its place, because one schema serves them all. If there is one client and one fixed shape, a REST API is simpler, and the Koa Tutorial: Build a Node.js Orders API From Scratch post builds exactly that kind of API.
My second check is whether the team already works in Node.js. Apollo Server runs on Node, and the schema and resolvers are plain JavaScript, so a team that already ships Node services can pick it up in an afternoon. A team in another language should look at that language's GraphQL server library first.
My third check is validation. Apollo Server checks every query against the schema before a resolver runs, so a typo in a field name fails fast with a clear message. Mutations still need their own checks: a negative total has to be rejected inside the resolver, because the schema alone cannot say that a number must be positive.
My fourth check is caching. GraphQL requests are POST requests with different bodies, so HTTP caching takes deliberate design. If the API mostly serves one screen with a fixed shape, that extra design work buys nothing.
My verdict, as judgement: use Apollo Server when several clients need different shapes of the same data and the team already works in Node.js. Keep REST for a single client with a fixed shape. The recommendation flips if the API has to be cached aggressively, or if the team has no Node.js experience.
Four orders, one query, and a mutation that gets rejected
Four orders belong to one small shop: Sana in Karachi, Rs 9,800; Daniyal in Lahore, Rs 13,400; Mahnoor in Islamabad, Rs 11,200; Hamza in Karachi, Rs 6,750.
First, install Apollo Server and its GraphQL library, then write the server: one Order type, two queries, and one mutation.
$ npm install @apollo/server graphql added 110 packages in 9s // Orders API for The Practical Developer (Apollo Server demo). // // A small shop in Karachi takes orders over the phone. This server // exposes them through GraphQL: one query lists every order, one // filters by city, and a mutation adds a new order. Run with: // node server.mjs // Then query it at http://localhost:4000/ with curl. import { ApolloServer } from '@apollo/server'; import { startStandaloneServer } from '@apollo/server/standalone'; const orders = [ { id: '101', customer: 'Sana', city: 'Karachi', items: ['kurta', 'dupatta'], total: 9800 }, { id: '102', customer: 'Daniyal', city: 'Lahore', items: ['sneakers'], total: 13400 }, { id: '103', customer: 'Mahnoor', city: 'Islamabad', items: ['handbag', 'wallet'], total: 11200 }, { id: '104', customer: 'Hamza', city: 'Karachi', items: ['jacket', 'shalwar kameez'], total: 6750 }, ]; const typeDefs = ` type Order { id: ID! customer: String! city: String! items: [String!]! total: Int! } type Query { orders: [Order!]! ordersByCity(city: String!): [Order!]! } type Mutation { addOrder(customer: String!, city: String!, items: [String!], total: Int!): Order! } `; const resolvers = { Query: { orders: () => orders, ordersByCity: (_parent, { city }) => orders.filter((o) => o.city === city), }, Mutation: { addOrder: (_parent, { customer, city, items, total }) => { if (total <= 0) { throw new Error('Order total must be greater than 0'); } const nextId = String(Math.max(...orders.map((o) => Number(o.id))) + 1); const order = { id: nextId, customer, city, items: items ?? [], total }; orders.push(order); return order; }, }, }; const server = new ApolloServer({ typeDefs, resolvers }); const { url } = await startStandaloneServer(server, { listen: { port: 4000 } }); console.log(`Server ready at: ${url}`); Starting the server prints one line.
$ node server.mjs Server ready at: http://localhost:4000/ The phone screen only needs the Karachi orders, so the query names exactly the fields it renders: two orders, Rs 9,800 and Rs 6,750, totalling Rs 16,550.
$ curl -s -X POST http://localhost:4000/ -H "Content-Type: application/json" \ -d '{"query": "{ ordersByCity(city: \"Karachi\") { id customer city total } }"}' {"data":{"ordersByCity":[{"id":"101","customer":"Sana","city":"Karachi","total":9800},{"id":"104","customer":"Hamza","city":"Karachi","total":6750}]}} Asking for every order returns all four, and the four totals add to Rs 41,150.
$ curl -s -X POST http://localhost:4000/ -H "Content-Type: application/json" \ -d '{"query": "{ orders { id customer city total } }"}' {"data":{"orders":[{"id":"101","customer":"Sana","city":"Karachi","total":9800},{"id":"102","customer":"Daniyal","city":"Lahore","total":13400},{"id":"103","customer":"Mahnoor","city":"Islamabad","total":11200},{"id":"104","customer":"Hamza","city":"Karachi","total":6750}]}} Adding an order is a mutation, and the server returns the new order with the id it assigned, 105.
$ curl -s -X POST http://localhost:4000/ -H "Content-Type: application/json" \ -d '{"query": "mutation { addOrder(customer: \"Zara\", city: \"Karachi\", items: [\"scarf\"], total: 5200) { id customer city total } }"}' {"data":{"addOrder":{"id":"105","customer":"Zara","city":"Karachi","total":5200}}} Now the part that goes wrong. A mutation with a total of -500 reaches the resolver, the check throws, and Apollo Server returns an error instead of writing the order. Nothing is added, and the response carries the message, the error code, and null data.
$ curl -s -X POST http://localhost:4000/ -H "Content-Type: application/json" \ -d '{"query": "mutation { addOrder(customer: \"Zara\", city: \"Karachi\", items: [\"scarf\"], total: -500) { id customer city total } }"}' {"errors":[{"message":"Order total must be greater than 0","extensions":{"code":"INTERNAL_SERVER_ERROR"}}],"data":null} The fix is to pass a positive total, and to check it in the client before sending the mutation, so a bad value never leaves the form. The schema alone cannot catch this, because Int! only says the total must be a number, not that it must be positive.
Common mistakes and how to fix them
First, a mutation that breaks a rule. The symptom is the error "Order total must be greater than 0", the code INTERNAL_SERVER_ERROR, and data null, and no order is written. The fix is to pass a valid value and to validate in the client as well as the resolver.
Second, asking for a field that is not in the schema. The symptom is the error "Cannot query field \"phone\" on type \"Order\".", with the code GRAPHQL_VALIDATION_FAILED, and the whole query is refused. The fix is to read the schema before writing the query, instead of guessing names from the REST version.
Third, forgetting a required argument. The symptom is the error "Field \"addOrder\" argument \"total\" of type \"Int!\" is required, but it was not provided.", also with the code GRAPHQL_VALIDATION_FAILED. The fix is to pass every argument marked with an exclamation mark, or drop the mark to make it optional.
Fourth, posting without a Content-Type header. The symptom is a CSRF blocked error with the code BAD_REQUEST, even though the query is fine. The fix is to send "Content-Type: application/json" with every request, as the demo does.
Frequently Asked Questions
Do I need Apollo Server to use GraphQL in Node.js? No. The graphql package on its own can run a schema, and Apollo Server adds the HTTP layer and validation around it.
Is Apollo Server the same as Apollo Client? No. Apollo Server runs on the back end and answers queries, while Apollo Client runs in the browser or app and sends them. They share the schema but live on opposite sides of the request.
Can I use Apollo Server with an existing Express app? Yes. Apollo Server ships a middleware mode that mounts the GraphQL endpoint on an Express app, so the API can sit next to existing REST routes.
How do resolvers get their data? Each resolver is a function that returns the value for one field, and it can read from anywhere: an array in memory, a database, or another service.
What to remember
- Apollo Server turns a schema and resolvers into a working GraphQL API, with parsing and validation handled.
- A query names exactly the fields it wants, and the response is shaped like the query.
- A mutation is how data changes, and it needs its own checks inside the resolver.
- An exclamation mark in the schema means required: a missing argument fails validation before any resolver runs.
- Every request needs the Content-Type: application/json header, or Apollo Server blocks it as a possible CSRF request.
Where this leaves you
Apollo Server turns the GraphQL idea into a running API with very little code: one schema, a handful of resolvers, and the server takes care of the rest. The demo proved it: a query returned exactly the two Karachi orders, a mutation added a real order with the next id, and a bad mutation was rejected with a clear message instead of writing broken data. Next, take one REST endpoint that returns too much for one of your screens and write the schema for it. If the GraphQL response is smaller, you have found a real use for it.
Further reading
- Apollo Server docs
- Learn GraphQL
- GraphQL Explained: Ask for Exactly the Data You Need
- Koa Tutorial: Build a Node.js Orders API From Scratch
- Fastify: A Fast Node API Without the Ceremony
- Gin Go Tutorial: Build Fast APIs With Very Little Code
- gRPC Explained: APIs That Speak in Binary
Waqas Shahid is a software engineer from Karachi with more than 12 years of experience, including 6+ years in engineering leadership. He writes The Practical Developer.


