diff --git a/.env.sample b/.env.sample index 83a927c3..4f556659 100644 --- a/.env.sample +++ b/.env.sample @@ -104,5 +104,5 @@ GCS_MEDIA_FOLDER=media/ # OPENAI_API_KEY= -# When LOG_REQUESTS exists, it also shows incoming GraphQL request, variables, and resolved user info +# When LOG_REQUESTS exists, it also shows incoming GraphQL operation, variables, and resolved user info LOG_REQUESTS= diff --git a/src/index.js b/src/index.js index 84f593a1..3feca635 100644 --- a/src/index.js +++ b/src/index.js @@ -84,6 +84,17 @@ router.get('/', ctx => { app.use(koaStatic(path.join(__dirname, '../static/'))); app.use(router.routes(), router.allowedMethods()); +const LOG_STR_LENGTH = 200; +/** + * For JSON.stringify in GraphQL logger + */ +/* istanbul ignore next */ +function truncatingLogReplacer(key, value) { + if (typeof value !== 'string' || value.length < LOG_STR_LENGTH) return value; + + return value.slice(0, LOG_STR_LENGTH) + '...'; +} + export const apolloServer = new ApolloServer({ schema, introspection: true, // Allow introspection in production as well @@ -128,18 +139,29 @@ export const apolloServer = new ApolloServer({ : [ { /* istanbul ignore next */ - requestDidStart({ - request: { query, variables, operationName }, - context: { appId, userId, appUserId }, - }) { - console.log({ - query, - variables, - operationName, - appId, - appUserId, - userId, - }); + requestDidStart(...args) { + const [ + { + context: { userId, appUserId, appId }, + request: { operationName, query, variables, http }, + }, + ] = args; + + console.log( + JSON.stringify( + { + msg: 'GraphQL request did start', + operationName, + query, + variables, + userId, + appUserId, + appId, + headers: Object.fromEntries(http.headers.entries()), + }, + truncatingLogReplacer + ) + ); }, }, ],