-
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
How to add or modify a response header in ApolloServer? #3264
Comments
Hey @SAGARACH65, great question! Our plugin API supports this, it just isn't documented yet. The interfaces are a great place to look for now if you'd like to know what various things a plugin is capable of. A small example might look something like: const server = new ApolloServer({
// ...,
plugins: [
{
requestDidStart() {
return {
didEncounterErrors(requestContext) {
requestContext.response.http.headers.set('Has-Errors', '1');
}
};
}
}
]
}); Hope this is helpful, if you have any further questions please let me know - and keep an eye out for some fresh docs in the future 👀 #2008 |
Thanks, @trevor-scheer. It pretty much solves my problem. |
Aha! Just what I was looking for. I want to set security headers in Vanilla Apollo Server |
An update for anyone who happens upon this issue, the plugin docs have been up for a bit now. Thanks to @abernix for all of his hard work on these 👏 |
@trevor-scheer : thank you for linking the doc! I am able to use plugins , however if the upstream (whatever apollo connects to get a response) , has a http header, I am unable to see that header value in the response object (inside the requestContext). So for example, if the backend returned a |
Thanks @trevor-scheer |
see https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching#dont_cache for info on no-cache see these links for info on setting http headers in Apollo Server: apollographql/apollo-server#3264 https://www.apollographql.com/docs/apollo-server/migration/#formatresponse-hook https://www.apollographql.com/docs/apollo-server/performance/caching/#caching-with-a-cdn
I am using graphql ApolloServer and using following for Apolloserver
And I need to pass the error returned from the resolvers in the response header.
I read through the docs but looks like we cannot add another middleware after the above-mentioned middleware.
I also tried adding a
formatResponse
while initializing the server but the object here is not the actual http response where i can change the error header.I need this because I have an Axios interceptor set up in the front end and it checks the header of the response to function and currently Apollo server returns 200 even when an error occurs.
The text was updated successfully, but these errors were encountered: