mercurius-js / mercurius

Implement GraphQL servers and gateways with Fastify
https://mercurius.dev/
MIT License
2.33k stars 234 forks source link

Intercepting errors when resolving subscriptions #966

Closed LewisYearsley closed 1 year ago

LewisYearsley commented 1 year ago

Hey. We've been using errorFormatter Mercurius option to intercept and log errors. i.e.

app.register(mercurius, {
   // ...
    errorFormatter: (executionResult, context) => {
       // Send any errors to our logging service
    },
});

This has served us well for queries and mutations, but now we're using subscriptions.

In the case of subscriptions, we've noticed that the callback provided as errorFormatter doesn't seem to get run, is that expected?

I've come up with something that works, which is to move all of the logging logic to graphql hooks. i.e.

const handleErrorResponse = () => {
  // Do stuff with any errors
}

graphql.addHook("onResolution", handleErrorResponse);
graphql.addHook("onSubscriptionResolution", handleErrorResponse);

So to summarise:

Using Mercurius 11.5.0

mcollina commented 1 year ago

thanks for reporting! The answer is that I don't know, and it's a bit hard to verify without a reproduction script.

This might well be a missing feature in Mercurius. If so, would you like to send a Pull Request?

LewisYearsley commented 1 year ago

Thanks, @mcollina - I'll try to figure out what the behaviour should be and based on that I'll be happy to open a PR with the changes if required.

LewisYearsley commented 1 year ago

Thanks again @mcollina for getting my changes in - we have them out in prod now and already showing results. We're good to close.