Stand up your own GraphQL API with Apollo Server in Node — define a schema, wire resolvers, and serve it over HTTP with a built-in sandbox to query it.
Why: a GraphQL server in JavaScript needs two pieces — a schema (typeDefs) and resolvers (the functions that produce data). Apollo Server is the most common implementation. Start with an empty folder. Note: GraphQL Yoga and mercurius are popular alternatives with the same shape.
mkdir graphql-apicd graphql-apiWhy: make it an ES-modules Node project, then add Apollo Server and the graphql package — pick your package manager.
$ pnpm init$ npm pkg set type=module$ pnpm add @apollo/server graphqlWhy: keep the SDL in its own .graphql file — it stays readable, gets editor highlighting, and is the contract you share. This tiny schema exposes one query that returns a list of books.
# schema.graphql
type Book {
id: ID!
title: String!
author: String!
}
type Query {
books: [Book!]!
}Why: resolvers are an object that mirrors the schema — Query.books is the function that returns the books. ApolloServer ties typeDefs and resolvers together, and startStandaloneServer serves it over HTTP with a built-in sandbox. Run it and open the printed URL.
import { ApolloServer } from '@apollo/server'
import { startStandaloneServer } from '@apollo/server/standalone'
import { readFileSync } from 'node:fs'
const typeDefs = readFileSync('./schema.graphql', 'utf8')
const books = [
{ id: '1', title: 'Dune', author: 'Herbert' },
{ id: '2', title: 'Neuromancer', author: 'Gibson' },
]
const resolvers = {
Query: {
books: () => books,
},
}
const server = new ApolloServer({ typeDefs, resolvers })
const { url } = await startStandaloneServer(server, { listen: { port: 4000 } })
console.log('Ready at ' + url)Why: start the server and open http://localhost:4000 — Apollo serves a sandbox where you can run the books query against YOUR schema. You now have a working GraphQL API; the next lessons make the resolvers do real work.
node index.jsopen http://localhost:4000 and run: query { books { title author } }