Thuta Learning
GraphQL
AdvancedWeb Developmentbeginner

Setting Up Apollo Server

What you'll walk away with

  • Explain the core ideas behind Setting Up Apollo Server
  • Run the sample GraphQL query or code and verify its output
  • Apply the technique correctly to the Tutorial Platform and production scenarios

Build the mental model

Apollo Server is a Node.js library implementing the GraphQL spec; giving it `typeDefs` (an SDL string) and `resolvers` (an object map) is enough for a working server. The `startStandaloneServer` helper handles HTTP server setup, CORS, and body parsing with sensible defaults, making it the easiest entry point for getting started — production deployments more often integrate with Express or Fastify.

Connect it to a real scenario

Write the Tutorial Platform API with `typeDefs` (a Tutorial type and tutorials query) and `resolvers` (a function reading from an in-memory or database repository), and run it on port 4000. After `npm run dev`, open Apollo Sandbox in the browser to test the query live.

Try the working example

typescript
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';

const typeDefs = `#graphql
  type Tutorial {
    id: ID!
    title: String!
  }

  type Query {
    tutorials: [Tutorial!]!
  }
`;

const resolvers = {
  Query: {
    tutorials: () => tutorialRepository.findAll(),
  },
};

const server = new ApolloServer({ typeDefs, resolvers });
const { url } = await startStandaloneServer(server, { listen: { port: 4000 } });
console.log(`Tutorial Platform API ready at ${url}`);
You should see
You can run a local Apollo Server and issue a query from Apollo Sandbox.

5-minute try-it

Add a `tutorial(id: ID!): Tutorial` field to the `Query` type and write its resolver function.

One important caution

A typo mismatch between a type name in `typeDefs` (`Tutorial`) and the corresponding key in `resolvers` means the server silently falls back to a default resolver instead of calling yours — a hard-to-spot bug.

Apollo Server — Getting StartedGraphQL

Easy traps

  • A typo mismatch between a type name in `typeDefs` (`Tutorial`) and the corresponding key in `resolvers` means the server silently falls back to a default resolver instead of calling yours — a hard-to-spot bug.
  • Validate sample queries and mutations on a local or test server with recoverable data before applying them to production.

Exercise

Add a `tutorial(id: ID!): Tutorial` field to the `Query` type and write its resolver function.

You'll know it worked when: You can run a local Apollo Server and issue a query from Apollo Sandbox.

Setting Up Apollo Server | Thuta Learning