Health Checks
Determining the health status of Apollo Server
Apollo Server 4 no longer supports built-in health checks . Instead, we recommend performing GraphQL-level health checks to ensure your server successfully serves traffic and performs GraphQL operations.
Load balancers often use health checks to determine if a server is available and ready to serve traffic.
GraphQL-level health checks
The easiest way to determine if your GraphQL server is healthy is to run a GraphQL operation.
Every GraphQL server supports a trivial query that requests the __typename
of the top-level Query
type. This means every GraphQL server can respond to a GET
request to a URL such as:
1https://your.server/graphql?query=%7B__typename%7D
Note that this health check will run an actual GraphQL operation. If your server requires special headers or cookies to run any query, you'll need to provide those with your request.
Sending an
apollo-require-preflight: true
header alongside your health check ensures that Apollo Server's CSRF prevention feature won't block it.
If you want to create a health check for your HTTP server that is unrelated to the health of the GraphQL execution engine (i.e., such as Apollo Server 3's health check feature ), you can add a GET
handler that always succeeds to your web framework.
Below is an example of an HTTP server health check with expressMiddleware
:
1// imports, etc.
2
3const app = express();
4const server = new ApolloServer({
5 typeDefs,
6 resolvers
7});
8
9await server.start();
10app.use('/graphql',
11 cors<cors.CorsRequest>(),
12 express.json(),
13 expressMiddleware(server)
14);
15await new Promise<void>(resolve => app.listen({ port: 4000 }, resolve));
16
17// Our GraphQL server is listening for GraphQL operations
18// on `http://localhost:4000/graphql`
19console.log(`🚀 Server ready at http://localhost:4000/graphql`);
20
21// Requests to `http://localhost:4000/health` now return "Okay!"
22app.get('/health', (req, res) => {
23 res.status(200).send('Okay!');
24});
If you are using
startStandaloneServer
, you must first swap to using theexpressMiddleware
function before creating an HTTP server health check.