【发布时间】:2020-01-10 13:57:26
【问题描述】:
我正在使用 graphql ApolloServer 并为 Apolloserver 使用以下内容
server.applyMiddleware({ app, path: '/graphql' });
我需要在响应头中传递解析器返回的错误。
我阅读了文档,但看起来我们无法在上述中间件之后添加另一个中间件。
我还尝试在初始化服务器时添加formatResponse,但这里的对象不是我可以更改错误标头的实际 http 响应。
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;
}
});
是否有任何选项可以做到这一点?
【问题讨论】:
标签: express graphql apollo-server