Health checks
Determining the health status of the Apollo Server
Health checks are often used by load balancers to determine if a server is available and ready to start serving traffic. By default, Apollo Server provides a health check endpoint at /.well-known/apollo/server-health
which returns a 200 status code if the server has started.
This basic health check may not be comprehensive enough for some applications and depending on individual circumstances, it may be beneficial to provide a more thorough implementation by defining an onHealthCheck
function to the ApolloServer
constructor options. If defined, this onHealthCheck
function should return a Promise
which rejects if there is an error, or resolves if the server is deemed ready. A Promise
rejection will result in an HTTP status code of 503, and a resolution will result in an HTTP status code of 200, which is generally desired by most health-check tooling (e.g. Kubernetes, AWS, etc.).
Note: Alternatively, the
onHealthCheck
can be defined as anasync
function whichthrow
s if it encounters an error and returns when conditions are considered normal.
1const { ApolloServer, gql } = require('apollo-server');
2
3// Undefined for brevity.
4const typeDefs = gql``;
5const resolvers = {};
6
7const server = new ApolloServer({
8 typeDefs,
9 resolvers,
10 onHealthCheck: () => {
11 return new Promise((resolve, reject) => {
12 // Replace the `true` in this conditional with more specific checks!
13 if (true) {
14 resolve();
15 } else {
16 reject();
17 }
18 });
19 },
20});
21
22server.listen().then(({ url }) => {
23 console.log(`🚀 Server ready at ${url}`);
24 console.log(
25 `Try your health check at: ${url}.well-known/apollo/server-health`,
26 );
27});