Core engine for Apollo GraphQL server providing foundational GraphQL server functionality for Node.js
npx @tessl/cli install tessl/npm-apollo-server-core@3.13.0Apollo Server Core is the foundational library that powers Apollo Server, providing core GraphQL server functionality for Node.js. It implements the essential GraphQL server logic and exports the base ApolloServer class that serves as the foundation for all Apollo Server integrations including Express, Koa, Hapi, Lambda, and serverless environments.
npm install apollo-server-core graphqlimport { ApolloServerBase, gql } from "apollo-server-core";For CommonJS:
const { ApolloServerBase, gql } = require("apollo-server-core");import { ApolloServerBase, gql } from "apollo-server-core";
// Define your GraphQL schema
const typeDefs = gql`
type Book {
title: String
author: String
}
type Query {
books: [Book]
}
`;
// Define resolvers
const resolvers = {
Query: {
books: () => [
{ title: 'The Awakening', author: 'Kate Chopin' },
{ title: 'City of Glass', author: 'Paul Auster' },
],
},
};
// Create server instance
const server = new ApolloServerBase({
typeDefs,
resolvers,
});
// Start the server
await server.start();
// Execute a GraphQL operation
const response = await server.executeOperation({
query: '{ books { title author } }',
});Apollo Server Core is built around several key components:
The main ApolloServerBase class providing GraphQL server functionality with plugin support and lifecycle management.
class ApolloServerBase {
constructor(config: Config);
start(): Promise<void>;
stop(): Promise<void>;
executeOperation(
request: GraphQLRequest,
contextValue?: Context
): Promise<GraphQLResponse>;
}Configuration options for GraphQL execution including schema definition, resolvers, context, and validation rules.
interface Config<ContextFunctionParams = any> {
schema?: GraphQLSchema;
typeDefs?: TypeSource;
resolvers?: IResolvers;
context?: Context | ContextFunction<ContextFunctionParams>;
plugins?: PluginDefinition[];
introspection?: boolean;
csrfPrevention?: CSRFPreventionOptions | boolean;
cache?: KeyValueCache | 'bounded';
}Collection of built-in plugins for usage reporting, caching, landing pages, and server management.
function ApolloServerPluginUsageReporting<TContext>(
options?: ApolloServerPluginUsageReportingOptions<TContext>
): ApolloServerPlugin;
function ApolloServerPluginCacheControl(
options?: ApolloServerPluginCacheControlOptions
): ApolloServerPlugin;
function ApolloServerPluginLandingPageLocalDefault(
options?: ApolloServerPluginLandingPageLocalDefaultOptions
): ApolloServerPlugin;Functions for processing HTTP requests containing GraphQL queries and converting them to GraphQL operations.
function runHttpQuery(request: HttpQueryRequest): Promise<HttpQueryResponse>;
interface HttpQueryRequest {
method: string;
query: Record<string, any> | Array<Record<string, any>>;
options: GraphQLOptions | ((...args: Array<any>) => ValueOrPromise<GraphQLOptions>);
request: Pick<Request, 'url' | 'method' | 'headers'>;
}Comprehensive error handling system with structured error types and formatting utilities.
class ApolloError extends Error {
constructor(message: string, code?: string, extensions?: Record<string, any>);
}
class AuthenticationError extends ApolloError {}
class ForbiddenError extends ApolloError {}
class UserInputError extends ApolloError {}
function formatApolloErrors(errors: readonly GraphQLError[]): GraphQLFormattedError[];Utilities for GraphQL schema processing, query parsing, and template tag support.
const gql: (
template: TemplateStringsArray | string,
...substitutions: any[]
) => DocumentNode;
function resolveGraphqlOptions(
options: GraphQLOptions | ((...args: Array<any>) => ValueOrPromise<GraphQLOptions>),
...args: Array<any>
): Promise<GraphQLOptions>;type Context<T = object> = T;
type ContextFunction<FunctionParams = any, ProducedContext = object> = (
context: FunctionParams,
) => ValueOrPromise<Context<ProducedContext>>;
type PluginDefinition = ApolloServerPlugin | (() => ApolloServerPlugin);
interface GraphQLResponse {
data?: Record<string, any> | null;
errors?: readonly GraphQLFormattedError[];
extensions?: Record<string, any>;
http?: Pick<Response, 'body' | 'headers' | 'status'>;
}
interface GraphQLRequest {
query: string;
operationName?: string;
variables?: VariableValues;
extensions?: Record<string, any>;
http?: Pick<Request, 'url' | 'method' | 'headers' | 'body'>;
}
interface CSRFPreventionOptions {
requestHeaders?: string[];
}