Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Omit `variables` from log if it is an empty object

### Fixed

### Removed
Expand Down
16 changes: 11 additions & 5 deletions lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const util = require('util')

const IS_PRODUCTION = process.env.NODE_ENV === 'production'

const _isEmptyObject = o => Object.keys(o).length === 0

const queryLogger = (req, _, next) => {
let query = req.body?.query || (req.query.query && decodeURIComponent(req.query.query))
// Only log requests that contain a query
Expand All @@ -25,13 +27,17 @@ const queryLogger = (req, _, next) => {
// Ignore parsing errors, handled by GraphQL server
}
}
if (IS_PRODUCTION && variables) variables = '***'
if (IS_PRODUCTION && variables && !_isEmptyObject(variables)) variables = '***'

// Only add properties to object that aren't undefined
const queryInfo = Object.fromEntries(Object.entries({ operationName, variables }).filter(([, v]) => v))
// Only add properties to object that aren't undefined or empty
const queryInfo = {
...(operationName && { operationName }),
...(variables && !_isEmptyObject(variables) && { variables })
}
// Only format queryInfo if it contains properties
const formattedQueryInfo =
Object.keys(queryInfo).length > 0 ? util.formatWithOptions({ colors: false, depth: null }, queryInfo) : undefined
const formattedQueryInfo = _isEmptyObject(queryInfo)
? undefined
: util.formatWithOptions({ colors: false, depth: null }, queryInfo)

// If query is multiline string, add newline padding to front
let formattedQuery = query.includes('\n') ? `\n${query}` : query
Expand Down