Docs
Launch GraphOS Studio

Health checks

Determining the health status of Apollo Server


Apollo Server 4 no longer

. Instead, we recommend performing
GraphQL-level health checks
to ensure your server successfully serves traffic and performs .

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 is healthy is to run a GraphQL .

Every supports a trivial that requests the

__typename
of the top-level Query type. This means every can respond to a GET request to a URL such as:

https://your.server/graphql?query=%7B__typename%7D

Note that this health check will run an actual . If your server requires special headers or cookies to run any , you'll need to provide those with your request.

Sending an apollo-require-preflight: true header alongside your health check ensures that 's

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 execution engine (i.e., such as

), you can add a GET handler that always succeeds to your web framework.

Below is an example of an HTTP server health check with

:

// imports, etc.
const app = express();
const server = new ApolloServer({
typeDefs,
resolvers,
});
await server.start();
app.use('/graphql', cors<cors.CorsRequest>(), express.json(), expressMiddleware(server));
await new Promise<void>((resolve) => app.listen({ port: 4000 }, resolve));
// Our GraphQL server is listening for GraphQL operations
// on `http://localhost:4000/graphql`
console.log(`🚀 Server ready at http://localhost:4000/graphql`);
// Requests to `http://localhost:4000/health` now return "Okay!"
app.get('/health', (req, res) => {
res.status(200).send('Okay!');
});
// imports, etc.
const app = express();
const server = new ApolloServer({
typeDefs,
resolvers,
});
await server.start();
app.use('/graphql', cors(), express.json(), expressMiddleware(server));
await new Promise((resolve) => app.listen({ port: 4000 }, resolve));
// Our GraphQL server is listening for GraphQL operations
// on `http://localhost:4000/graphql`
console.log(`🚀 Server ready at http://localhost:4000/graphql`);
// Requests to `http://localhost:4000/health` now return "Okay!"
app.get('/health', (req, res) => {
res.status(200).send('Okay!');
});

If you are using startStandaloneServer, you must first

before creating an HTTP server health check.

Previous
Metrics and logging
Next
ApolloServer
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company