didEncounterErrors and willSendResponse are not called on AuthenticationError
See original GitHub issueUsing apollo-server-express
2.12.0
.
According to this lifecycle, willSendResponse
should always be called, and didEncounterErrors
should at least be called if there’s an error:
https://www.apollographql.com/docs/apollo-server/integrations/plugins/#request-lifecycle-event-flow
However, we’re only seeing requestDidStart
being called, and nothing more, if we have an authentication error. We’re using graphql-shield
7.2.6
for our authorization. The response is returned to the client, but willSendResponse
is never called (and nor is didEncounterErrors
).
Eg, given the following:
const server = new ApolloServer({
plugins: [
{
requestDidStart() {
console.log('requestDidStart')
},
parsingDidStart() {
console.log('parsingDidStart')
},
didEncounterErrors() {
console.log('didEncounterErrors')
},
willSendResponse() {
console.log('willSendResponse')
},
},
],
// ...
All we see output in our logs is:
requestDidStart
And we get a valid response with an errors
array with message, location, stacktrace, etc all looking as they should for an authentication error.
So neither parsingDidStart
nor willSendResponse
is invoked – somehow these are bypassed, which indicates there’s either a bug, or the lifecycle is not accurately documented.
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (1 by maintainers)
Top GitHub Comments
Hey @mhart, please take a closer look at the examples in the docs. You’ll notice the API for a plugin is slightly different from how you’ve used it in your snippet.
requestDidStart
is a function that returns an object of the other hooks you’re looking to use. This allows plugins to maintain a local context or scope during the course of a request that the other hooks can consume.I believe what you really want is this:
@trickleup does this apply to you as well or are you experiencing the issue differently? Please provide a reproduction if you think there’s a bug that needs to be addressed. Thanks!
Thanks @trevor-scheer! Yes, with your example the
didEncounterErrors
hook does fire for the persisted query exceptions. My bad, thanks for clarifying!