import { performance } from 'perf_hooks' import * as Auth from '../utils/auth' import { logger } from '../utils/logger' export const logInput = (req, res, next) => { const { variables, query, operationName } = req.body const obj: any = {} const startTime = performance.now() res.on('finish', async () => { try { obj.userId = await Auth.getUserIdRest(req) } catch {} obj.time = new Date().toISOString() obj.operationName = operationName obj.variableValues = variables obj.query = query obj.req = { headers: req.headers, host: req.headers['x-forwarded-for'] || req.connection.remoteAddress, body: req.body, } obj.hasError = false obj.hasTimedOut = false obj.statusCode = res.statusCode if (res.statusCode >= 400) { obj.hasError = true } obj.responseTime = Math.floor(performance.now() - startTime) if (process.env.NODE_ENV === 'production') { // pino escapes the GraphQL query \n -> \\n so console is used instead console.log(JSON.stringify(obj)) } else { // Logging IntrospectionQuery when in dev is really annoying if (obj?.operationName !== 'IntrospectionQuery') { logger.info( `INCOMING --> ${obj.operationName} (Status: ${obj.statusCode}) executed in ${obj.responseTime}ms` ) } } }) next() }