GraphQL API with Apollo Server, Express.js, and Database Integration

GraphQL is a modern and powerful query language for APIs that allows clients to request exactly the data they need. When combined with Apollo Server, Express.js, and MongoDB, you build a flexible, performant, and secure backend for modern applications. Integrating Authentication and Role-Based Access Control (RBAC) ensures fine-grained access control, while DataLoader helps avoid the N+1 query problem common in GraphQL.

This guide walks you through setting up a robust GraphQL API using:

  • Apollo Server for building the GraphQL server, defining GraphQL schemas and resolvers
  • Express.js as the HTTP server for middleware and server handling
  • MongoDB for data persistence accessed via Mongoose
  • GraphQL Shield and JWT for secure access control: RBAC
  • DataLoader and Redis for batching and caching database queries efficiently
  • Prometheus and Grafana for API performance monitoring and observability
  • GraphiQL for API development, introspection, and documentation

Project Setup

  1. Initialize the Project
mkdir graphql-api
cd graphql-api
npm init -y
  1. Install Required Dependencies
  • Main dependencies:
npm install @apollo/server graphql express cors graphql-tag
  • Development dependencies:
npm install -D typescript @types/node @types/express @types/cors
  1. Create tsconfig.json
npx tsc --init

Then update the tsconfig.json (minimal config):

{
  "compilerOptions": {
    "target": "ES6",
    "module": "CommonJS",
    "rootDir": "src",
    "outDir": "dist",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true
  }
}
  1. Project Structure
graphql-api/
├── src/
│ ├── index.ts
│ ├── schema.ts
│ ├── typeDefs.ts
│ └── resolvers.ts
├── tsconfig.json
└── package.json
  1. Setup GraphQL Schema and Resolvers
  • src/typeDefs.ts
import { gql } from "graphql-tag";

export const typeDefs = gql`
  type Query {
    hello: String
  }
`;
  • src/resolvers.ts
export const resolvers = {
  Query: {
    hello: () => "Hello, GraphQL with TypeScript!",
  },
};
  1. Set Up Apollo Server with Express
  • src/index.ts
import express from "express";
import { ApolloServer } from "apollo-server-express";
import { typeDefs } from "./schema";
import { resolvers } from "./resolvers";

async function startServer() {
  const app = express();

  const server = new ApolloServer({
    typeDefs,
    resolvers,
  });

  await server.start();
  server.applyMiddleware({ app });

  const PORT = 4000;
  app.listen(PORT, () => {
    console.log(`🚀 Server ready at http://localhost:${PORT}`);
  });
}

startServer();
  1. Add scripts to package.json
"scripts": {
  "start": "node dist/index.js",
  "dev": "nodemon src/index.ts",
  "build": "tsc"
}
  1. Run Your Project
npm run dev
  1. Open http://localhost:4000/graphql and try:
query {
  hello
}