javascriptgqlgraphql-yogayoga-x1

Is there a similar method to the `didEncounterError` from ApolloServer that I can use in GraphQL Yoga?


I'm migrating from ApolloServer to GraphQL Yoga server, and in the plugins of ApolloServer, they have the didEncounterError method which gives you access to the context, and I couldn't find something similar in the documentation of Yoga, does anyone have an idea how can I access the context when an error occurs?

This is what I had on ApolloServer

const apolloServer = new ApolloServer({
...,
plugins: [
    {
      requestDidStart() {
        return {
          didEncounterErrors(context) {
            context.errors.forEach(error => {
              console.log(context.operationName);
              console.log(error);
             }
          });
        };
      },
    },
  ],
});

Thanks for your help!


Solution

  • You can use the useLogger plugin from graphql-yoga like this:

    plugins: [
      useLogger({
        logFn: (eventName, args) => {
          args.result.errors.forEach((error) => {
            console.log(args.operationName);
            console.log(error);
          });
        },
      }),
    ],