The ApolloServerBase class is the main server class that orchestrates GraphQL execution, manages the server lifecycle, and provides the foundation for all Apollo Server integrations.
The main Apollo Server class providing GraphQL server functionality with plugin support and lifecycle management.
/**
* Base Apollo Server class that provides core GraphQL server functionality
*/
class ApolloServerBase {
/**
* Create a new Apollo Server instance
* @param config - Server configuration options
*/
constructor(config: Config);
/**
* Start the Apollo Server and initialize all plugins
* Must be called before processing any requests
* @returns Promise that resolves when server is ready
*/
start(): Promise<void>;
/**
* Stop the Apollo Server and cleanup resources
* @returns Promise that resolves when server is stopped
*/
stop(): Promise<void>;
/**
* Execute a GraphQL operation directly
* @param request - GraphQL request with optional query field
* @param integrationContextArgument - Optional context argument from integration
* @returns Promise resolving to GraphQL response
*/
executeOperation(
request: Omit<GraphQLRequest, 'query'> & {
query?: string | DocumentNode;
},
integrationContextArgument?: ContextFunctionParams
): Promise<GraphQLResponse>;
/**
* Get GraphQL server options for request processing (used by integrations)
* @param integrationContextArgument - Context from integration framework
* @returns Promise resolving to GraphQL execution options
*/
protected graphQLServerOptions(
integrationContextArgument?: ContextFunctionParams
): Promise<GraphQLServerOptions>;
}Usage Examples:
import { ApolloServerBase, gql } from "apollo-server-core";
// Basic server setup
const server = new ApolloServerBase({
typeDefs: gql`
type Query {
hello: String
}
`,
resolvers: {
Query: {
hello: () => "Hello world!",
},
},
});
// Start the server
await server.start();
// Execute operations directly
const response = await server.executeOperation({
query: "{ hello }",
});
console.log(response.data); // { hello: "Hello world!" }
// Stop the server when done
await server.stop();Apollo Server manages its internal state through a state machine with distinct phases.
type ServerState =
| { phase: 'initialized'; schemaManager: SchemaManager }
| { phase: 'starting'; barrier: Resolvable<void>; schemaManager: SchemaManager }
| { phase: 'failed to start'; barrier: Resolvable<void>; error: Error }
| { phase: 'started'; schemaDerivedData: SchemaDerivedData; schemaManager: SchemaManager }
| { phase: 'stopping'; barrier: Resolvable<void> }
| { phase: 'stopped' };
interface SchemaDerivedData {
schema: GraphQLSchema;
schemaHash: SchemaHash;
documentStore: DocumentStore | null;
}The server manages GraphQL schemas through an internal SchemaManager that handles schema updates and validation.
interface SchemaManager {
/**
* Get the current schema and derived data
*/
getSchemaDerivedData(): Promise<SchemaDerivedData>;
/**
* Subscribe to schema changes
*/
onSchemaChange(callback: SchemaChangeCallback): Unsubscriber;
}
type SchemaChangeCallback = (schema: GraphQLSchema) => void;
type Unsubscriber = () => void;Apollo Server provides lifecycle hooks that plugins can use to extend functionality.
interface ServerLifecycleHooks {
serverWillStart?: () => ValueOrPromise<GraphQLServerListener | void>;
}
interface GraphQLServerListener {
serverWillStop?: () => ValueOrPromise<void>;
}Advanced Usage:
import { ApolloServerBase } from "apollo-server-core";
// Server with plugins and lifecycle management
const server = new ApolloServerBase({
typeDefs,
resolvers,
plugins: [
{
serverWillStart() {
console.log('Server starting...');
return {
serverWillStop() {
console.log('Server stopping...');
},
};
},
},
],
context: ({ req }) => ({
user: req.user,
dataSources: {
userAPI: new UserAPI(),
},
}),
});
// Graceful shutdown handling
process.on('SIGTERM', async () => {
console.log('SIGTERM received, stopping server...');
await server.stop();
process.exit(0);
});