How to add or modify a response header in ApolloServer?
See original GitHub issueI am using graphql ApolloServer and using following for Apolloserver
server.applyMiddleware({ app, path: '/graphql' });
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.
const server = new ApolloServer({
schema,
validationRules: [depthLimit(7)],
playground: process.env.NODE_ENV !== 'production',
debug: process.env.NODE_ENV !== 'production',
formatError: err => {
// Don't give the specific errors to the client.
if (err.message.startsWith('Database Error: ') || err.message.startsWith('connect')) {
return new Error('Internal server error');
}
// Otherwise return the original error. The error can also
// be manipulated in other ways, so long as it's returned.
return err;
},
formatResponse: (res:any,options:any) => {
// can't set headers here as it is not the http response object.
return res;
}
});
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.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
How to add a response header in ApolloServer?
new ApolloServer({ context: ({res}) => { res.header('key', 'value') } }).
Read more >API Reference: ApolloServer - Apollo GraphQL Docs
This article documents the ApolloServer class from the @apollo/server package. You can use the ApolloServer class to create an instance of Apollo Server...
Read more >Accessing Authorization headers in Apollo graphql client
We can use HttpLink to send custom headers to in our requests (which we will do to authorize via 'Authorization' header), but there...
Read more >API Reference: @apollo/gateway - Apollo Server - Netlify
The example below demonstrates adding an x-user-id HTTP header to every request the gateway sends to an implementing service:.
Read more >apollo-server-plugin-http-headers - npm package - Snyk
In the past month we didn't find any pull request activity or change in issues status has been detected for the GitHub repository....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub 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:
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
Aha! Just what I was looking for. I want to set security headers in Vanilla Apollo Server