Making a GraphQL Server (with Apollo)
Set up a GraphQL server from scratch using Apollo Server and Node.js.
Building a GraphQL Server with Apollo
Now it's time to get hands-on! In this episode, we'll build a GraphQL server from scratch using Apollo Server — the most popular GraphQL server library for Node.js.
Project Setup
First, create a new project and install the dependencies:
mkdir graphql-server
cd graphql-server
npm init -y
npm install @apollo/server graphql
Update your package.json to use ES modules:
{
"type": "module"
}
Creating the Server
Create an index.js file with a basic Apollo Server:
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
// Type Definitions (Schema)
const typeDefs = `#graphql
type Game {
id: ID!
title: String!
platform: [String!]!
}
type Query {
games: [Game]
}
`;
// Sample data
let games = [
{ id: "1", title: "Zelda: Tears of the Kingdom", platform: ["Switch"] },
{ id: "2", title: "Final Fantasy XVI", platform: ["PS5", "PC"] },
{ id: "3", title: "Elden Ring", platform: ["PS5", "Xbox", "PC"] },
];
// Resolvers
const resolvers = {
Query: {
games() {
return games;
},
},
};
// Start the server
const server = new ApolloServer({
typeDefs,
resolvers,
});
const { url } = await startStandaloneServer(server, {
listen: { port: 4000 },
});
console.log(`🚀 Server ready at ${url}`);
Running the Server
node index.js
Visit http://localhost:4000 in your browser. You'll see the Apollo Sandbox — an interactive query editor. Try this query:
{
games {
id
title
platform
}
}
How It Works
The Apollo Server needs two main things:
- Type Definitions (Schema) — describes the shape of your data and what queries are available
- Resolvers — functions that return the actual data for each field in the schema
The flow is:
Client sends query → Apollo Server parses it → Matches to resolvers → Returns data
The Apollo Sandbox
Apollo Server v4 comes with a built-in sandbox at the server URL. Features include:
- Auto-complete — suggests fields as you type
- Schema explorer — browse your entire API schema
- Query history — saves your previous queries
- Response viewer — formatted JSON response
Project Structure
For a well-organized project, consider this structure:
graphql-server/
├── index.js # Server entry point
├── schema.js # Type definitions
├── resolvers.js # Resolver functions
├── data.js # Sample data / DB connection
└── package.json
Key Takeaways
- Apollo Server is the most popular GraphQL server for Node.js
- A GraphQL server needs type definitions (schema) and resolvers
- Apollo Sandbox provides an interactive query editor
- The server runs on a single endpoint
In the next episode, we'll dive deep into Schema & Types to understand the GraphQL type system.