-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to set custom headers on errors #3062
Comments
Please try this with the new implementation provided in #2719 and let us know if it's not possible. Roughly, I think you'd want this: import { ApolloServer } from 'apollo-server';
const server = new ApolloServer({
typeDefs,
resolvers,
plugins: [
{
requestDidStart() {
return {
didEncounterErrors({ response, errors }) {
// You can inspect `errors`, if you'd like!
response.http.headers.set('Your-Own-Header', 'SEVERITY!');
}
}
},
},
],
}); |
Hmm, it seems that there are different categories of errors. I was already catching errors in resolvers and the new plugin also worked for errors in resolvers, however unfortunately that didn't do the trick for parsing or validation errors, e.g. when we send a malformed query from the client. When a validation error is detected (you ask for a field that is not in typedef), didEncounterErrors function is executed, however that response with headers is ignored, at least for the |
@abernix The proposed solution does not work. In my case, the error (PersistedQueryNotFound) is picked up by didEncounterErrors, but setting of the header is apparently ignored. At the moment, it appears impossible to customize the headers of (at least some) error responses. Is there a way to accomplish this that I am not aware of? If not, would you consider adding support for this? Either allowing it in didEncounterError, or maybe adding a new event willSendErrorResponse (similar to willSendResponse)? |
For anyone trying to resolve this, the problem seems to be that for some errors (namely APQ errors), apollo server forces some pre-defined headers. A temporary workaround is to manually throw an HttpQueryError with your own headers:
|
The headers for errors seem to be fixed, one cannot set any headers for errors via any other means (extensions etc).
See
apollo-server/packages/apollo-server-core/src/runHttpQuery.ts
Line 79 in f51aa07
The use case is explained in this issue: #3061
Note that I was able to send response headers for successful responses there, but not for errors.
The text was updated successfully, but these errors were encountered: