I 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.
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 xyz : value header that should be returned to the client , I would like to pass this to the client:
willSendResponse(requestContext) {
console.log(requestContext.response.http.headers.get('xyz');//this is null
}
Thanks @trevor-scheer
Most helpful comment
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