【发布时间】:2017-04-29 18:36:50
【问题描述】:
我设法设置日志记录 graphQL 错误:
app.use('/graphql', graphqlHTTP(request => {
return {
schema,
rootValue: {
request
},
formatError: error => {
const params = {
message : error.message,
locations: error.locations,
stack : error.stack
};
winston.error(`message: "${error.message}", QUERY: "${request.body.query}"`);
// Optional ${request.body.operationName} ${request.body.variables}
return (params);
}
}
}));
如何设置一个通用函数,即使没有错误也可以访问请求和响应?
编辑:我已设法通过以下方式记录所有请求:
function loggingMiddleware(req, res, next) {
if (req.url.startsWith('/graphql')) {
winston.debug('REQUEST: ', req.body);
}
next();
}
app.use(loggingMiddleware);
在我打电话给app.use('/graphql') 之前,但仍然不知道如何运行“post graphql 处理”处理程序来记录响应。
【问题讨论】:
-
感谢您发布有关您如何管理日志记录的信息。我发现
req.body在当前版本的express-graphql中未定义(0.9.0)。
标签: javascript node.js express graphql