Apollo Server
Apollo Server is a popular GraphQL server with extensive features and ecosystem.
Installation
npm install @apollo/serverpnpm add @apollo/serveryarn add @apollo/serverPrerequisites
Create an executable schema following the Getting Started guide.
Basic Server
Using the standalone server:
// src/server.ts
import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
import { schema } from "./schema";
const server = new ApolloServer({ schema });
const { url } = await startStandaloneServer(server, {
listen: { port: 4000 },
});
console.log(`Server is running on ${url}`);With Context
If your resolvers use a context type, first set up context and resolver factories, then provide a context factory to your server:
// src/server.ts
import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
import { schema } from "./schema";
import type { Context } from "./gqlkit/context";
const server = new ApolloServer<Context>({ schema });
const { url } = await startStandaloneServer(server, {
listen: { port: 4000 },
context: async ({ req }) => {
const user = await getUserFromRequest(req);
return {
currentUser: user,
db: database,
};
},
});
console.log(`Server is running on ${url}`);Further Reading
Last updated on