Docs
Launch GraphOS Studio
Apollo Server 3 is officially deprecated, with end-of-life scheduled for 22 October 2024. Learn more about upgrading to a supported Apollo Server version.

API Reference: Drain HTTP server plugin


Using the plugin

This API reference documents the ApolloServerPluginDrainHttpServer plugin.

This plugin is designed for use with apollo-server-express and other framework-specific packages which are built on top of Node http.Servers. It is highly recommended that you use this plugin with packages like apollo-server-express if you want your server to shut down gracefully.

You do not need to explicitly use this plugin with the batteries-included apollo-server package: that package automatically uses this plugin internally.

When you use this plugin, will drain your HTTP server when you call the stop() method (which is also called for you when the SIGTERM and SIGINT signals are received, unless disabled with the stopOnTerminationSignals constructor option). Specifically, it will:

  • Stop listening for new connections
  • Close idle connections (i.e., connections with no current HTTP request)
  • Close active connections whenever they become idle
  • Wait for all connections to be closed
  • After a grace period, if any connections remain active, forcefully close them.

This plugin is exported from the apollo-server-core package. It is tested with apollo-server-express, apollo-server-koa, and apollo-server-fastify. (If you're using Hapi, you should instead use the ApolloServerPluginStopHapiServer plugin exported from the apollo-server-hapi package.)

Here's a basic example of how to use it with Express. See the framework integrations docs for examples of how to use it with other frameworks.

index.ts
const express = require('express');
const { ApolloServer } = require('apollo-server-express');
const { ApolloServerPluginDrainHttpServer, ApolloServerPluginLandingPageLocalDefault } = require('apollo-server-core');
const { typeDefs, resolvers } = require('./schema');
const http = require('http');
async function startApolloServer() {
const app = express();
// Our httpServer handles incoming requests to our Express app.
// Below, we tell Apollo Server to "drain" this httpServer,
// enabling our servers to shut down gracefully.
const httpServer = http.createServer(app);
const server = new ApolloServer({
typeDefs,
resolvers,
csrfPrevention: true,
cache: 'bounded',
plugins: [ApolloServerPluginDrainHttpServer({ httpServer }), ApolloServerPluginLandingPageLocalDefault({ embed: true })],
});
await server.start();
// Mount Apollo middleware here.
server.applyMiddleware({ app });
await new Promise<void>((resolve) => httpServer.listen({ port: 4000 }, resolve));
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
return { server, app };
}
index.js
const express = require('express');
const { ApolloServer } = require('apollo-server-express');
const { ApolloServerPluginDrainHttpServer, ApolloServerPluginLandingPageLocalDefault } = require('apollo-server-core');
const { typeDefs, resolvers } = require('./schema');
const http = require('http');
async function startApolloServer() {
const app = express();
// Our httpServer handles incoming requests to our Express app.
// Below, we tell Apollo Server to "drain" this httpServer,
// enabling our servers to shut down gracefully.
const httpServer = http.createServer(app);
const server = new ApolloServer({
typeDefs,
resolvers,
csrfPrevention: true,
cache: 'bounded',
plugins: [ApolloServerPluginDrainHttpServer({ httpServer }), ApolloServerPluginLandingPageLocalDefault({ embed: true })],
});
await server.start();
// Mount Apollo middleware here.
server.applyMiddleware({ app });
await new Promise((resolve) => httpServer.listen({ port: 4000 }, resolve));
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
return { server, app };
}

Options

Name /
Type
Description
httpServer

http.Server

The server to drain; required.

stopGracePeriodMillis

number

How long to wait before forcefully closing non-idle connections. Defaults to 10_000 (ten seconds).

Previous
Inline trace
Next
Cache control
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company