Unlucky
Unlucky

Reputation: 447

How to handle GraphQL subscription errors in Apollo Server, similar to formatError for queries and mutations?

I'm trying to handle errors in GraphQL subscriptions in Apollo Server the same way I handle them for queries and mutations using formatError.

Here’s my setup:

export default async () => {
  const typeDefs = readFileSync(path.resolve(import.meta.dirname, './schema.graphql'), 'utf8');
  const schema = makeExecutableSchema({typeDefs, resolvers});
  const app = express();
  const httpServer = http.createServer(app);
  const wsServer = new WebSocketServer({
    server: httpServer,
    path: '/subscriptions',
  });
  const pubsub = new PubSub();
  const serverCleanup = useServer({
    schema,
    context: async () => (Promise.resolve({
      pubsub: pubsub,
    })),
    onError: (ctx, msg, errors) => {
      console.log('onError', ctx, msg, errors); // Doesn't print anything; server crashes instead
      return [];
    },
  }, wsServer);

  const server = new ApolloServer({
    schema,
    plugins: [
      ApolloServerPluginDrainHttpServer({httpServer}),
      {
        async serverWillStart() {
          return Promise.resolve({
            async drainServer() {
              await serverCleanup.dispose();
            },
          });
        },
      },
    ],
    formatError: (formattedError) => {
      switch (formattedError.extensions?.code) {
        case GRAPHQL_USER_FRIENDLY_ERROR:
          return {message: formattedError.message};
        case ApolloServerErrorCode.INTERNAL_SERVER_ERROR:
          console.error(formattedError);
          return {message: 'An unexpected error occurred.'};
        default:
          return formattedError;
      }
    },
  });

  await server.start();

  app.use(
    '/graphql',
    cors(),
    express.json(),
    expressMiddleware(server, {
      context: async () => (Promise.resolve({
        pubsub: pubsub,
      })),
    }),
  );

  httpServer.listen(4000, () => {
    console.log('Server is ready!');
  });
}

How can I catch and handle GraphQL subscription errors in Apollo Server (e.g., caused by one-time invalid data) to prevent server crashes, log the errors, or return custom error messages, similar to how formatError works for queries and mutations?

Any guidance, correctoins, best practices, or relevant sources would be greatly appreciated.

    "@apollo/server": "^4.11.2",
    "graphql-ws": "^5.16.0",
    "ws": "^8.18.0"

Upvotes: 1

Views: 38

Answers (0)

Related Questions