Core engine for Apollo GraphQL server providing a spec-compliant GraphQL runtime compatible with any GraphQL client
npx @tessl/cli install tessl/npm-apollo--server@5.0.0Apollo Server is a comprehensive, production-ready GraphQL server implementation for TypeScript and JavaScript that provides a spec-compliant GraphQL runtime compatible with any GraphQL client. It serves as the core engine for building scalable GraphQL APIs with multiple deployment options, extensive plugin system, and comprehensive tooling for GraphQL federation scenarios.
npm install @apollo/server graphqlimport { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";For CommonJS:
const { ApolloServer } = require("@apollo/server");
const { startStandaloneServer } = require("@apollo/server/standalone");import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
// Define your GraphQL schema
const typeDefs = `
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => "Hello world!",
},
};
// Create Apollo Server instance
const server = new ApolloServer({
typeDefs,
resolvers,
});
// Start standalone server
const { url } = await startStandaloneServer(server, {
listen: { port: 4000 },
});
console.log(`🚀 Server ready at: ${url}`);Apollo Server is built around several key components:
Core ApolloServer class providing initialization, startup, request execution, and shutdown functionality. Essential for all GraphQL server deployments.
class ApolloServer<TContext extends BaseContext = BaseContext> {
constructor(config: ApolloServerOptions<TContext>);
start(): Promise<void>;
stop(): Promise<void>;
executeOperation<TData, TVariables>(
request: GraphQLRequest<TVariables>,
options?: ExecuteOperationOptions<TContext>
): Promise<GraphQLResponse<TData>>;
}Comprehensive plugin architecture for extending Apollo Server functionality including caching, tracing, analytics, federation, and custom integrations.
interface ApolloServerPlugin<TContext extends BaseContext> {
serverWillStart?(service: GraphQLServerContext): Promise<GraphQLServerListener | void>;
requestDidStart?(requestContext: GraphQLRequestContext<TContext>): Promise<GraphQLRequestListener<TContext> | void>;
}
// Built-in plugins
function ApolloServerPluginCacheControl(options?: ApolloServerPluginCacheControlOptions): ApolloServerPlugin;
function ApolloServerPluginUsageReporting<TContext>(options?: ApolloServerPluginUsageReportingOptions<TContext>): ApolloServerPlugin<TContext>;HTTP request handling, standalone server creation, and framework integration capabilities. Supports both standalone deployment and integration with existing web frameworks.
function startStandaloneServer<TContext extends BaseContext>(
server: ApolloServer<TContext>,
options?: StartStandaloneServerOptions<TContext> & { listen?: ListenOptions }
): Promise<{ url: string }>;
class HeaderMap extends Map<string, string> {
set(key: string, value: string): this;
get(key: string): string | undefined;
}Comprehensive error management with standard error codes, custom error formatting, and error reporting utilities.
enum ApolloServerErrorCode {
INTERNAL_SERVER_ERROR = 'INTERNAL_SERVER_ERROR';
GRAPHQL_PARSE_FAILED = 'GRAPHQL_PARSE_FAILED';
GRAPHQL_VALIDATION_FAILED = 'GRAPHQL_VALIDATION_FAILED';
BAD_USER_INPUT = 'BAD_USER_INPUT';
}
function unwrapResolverError(error: unknown): unknown;Essential type definitions for server configuration, request/response handling, context management, and plugin development.
interface ApolloServerOptions<TContext extends BaseContext> {
typeDefs?: TypeSource;
resolvers?: IResolvers;
schema?: GraphQLSchema;
plugins?: ApolloServerPlugin<TContext>[];
formatError?: (formattedError: GraphQLFormattedError, error: unknown) => GraphQLFormattedError;
}
interface GraphQLRequest<TVariables = VariableValues> {
query?: string | DocumentNode;
operationName?: string;
variables?: TVariables;
extensions?: Record<string, any>;
}