Skip to main content
GraphQL Fundamentals·Lesson 3 of 5

Building a GraphQL Server

Apollo Server is the most popular way to build a GraphQL API with Node.js. It handles parsing queries, validating against your schema, running resolvers, and formatting responses.

Setup

mkdir graphql-api && cd graphql-api
npm init -y
npm install @apollo/server graphql

Your First Server

Ctrl+Enter
HTML
CSS
JS
Preview

Run it: node --experimental-vm-modules index.js

Resolvers in Detail

Every field in your schema has a resolver function with this signature:

fieldName(parent, args, context, info) { ... }
ArgumentWhat it contains
parentThe resolved value of the parent object
argsArguments passed to this field in the query
contextShared data (auth user, database connection)
infoQuery AST and execution metadata

Context — Sharing Auth Across Resolvers

Pass shared data like the authenticated user through context:

Ctrl+Enter
HTML
CSS
JS
Preview
Ctrl+Enter
HTML
CSS
JS
Preview

Testing with GraphiQL

Apollo Server ships a built-in browser IDE at http://localhost:4000. You can write queries, run them, and explore the schema — no extra tooling needed during development.

Try running these against the server above:

query AllBooks {
  books {
    id
    title
    author
    year
  }
}

mutation AddBook {
  addBook(title: "Refactoring", author: "Martin Fowler", year: 2018) {
    id
    title
  }
}

Schema-First vs Code-First

ApproachDescriptionTooling
Schema-firstWrite SDL (.graphql files), generate resolversApollo Server, graphql-tools
Code-firstDefine schema in code, SDL auto-generatedTypeGraphQL, Pothos

Schema-first (what we've used here) is more explicit and makes the SDL the source of truth. Code-first is preferred in TypeScript projects where you want type safety across schema and resolvers.

Error Handling

Throw GraphQLError for user-facing errors with proper error codes:

Ctrl+Enter
HTML
CSS
JS
Preview