or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

configuration.mderror-handling.mdgraphql-utilities.mdhttp-integration.mdindex.mdplugins.mdserver-core.md
tile.json

index.mddocs/

Apollo Server Core

Apollo 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.

Package Information

  • Package Name: apollo-server-core
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install apollo-server-core graphql

Core Imports

import { ApolloServerBase, gql } from "apollo-server-core";

For CommonJS:

const { ApolloServerBase, gql } = require("apollo-server-core");

Basic Usage

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 } }',
});

Architecture

Apollo Server Core is built around several key components:

  • ApolloServerBase Class: The main server class that orchestrates GraphQL execution
  • Request Pipeline: Processes incoming GraphQL requests through plugin hooks
  • Plugin System: Extensible architecture for adding functionality like caching, metrics, and authentication
  • Schema Management: Handles GraphQL schema processing and validation
  • Error Handling: Structured error system with formatting and categorization
  • HTTP Integration: Utilities for integrating with various HTTP frameworks

Capabilities

Server Core

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>;
}

Server Core

Configuration and Schema

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';
}

Configuration

Built-in Plugins

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;

Built-in Plugins

HTTP Query Processing

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'>;
}

HTTP Integration

Error Handling

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[];

Error Handling

GraphQL Utilities

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>;

GraphQL Utilities

Core Types

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[];
}