Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
Logging query string with express-bunyan-logger and graphql

I've made a short, self contained example of a problem I'm having with express-bunyan-logger.

I'm trying to add express-bunyan-logger as middleware, and utilize includesFn to log the query string and body as a custom fields.

const app = express();

const graphqlLogger = require('express-bunyan-logger')({
  name: 'graphql-logger',
  includesFn: (req, res) => {
    const includes = {};
    if (req.body) includes.req_body = JSON.stringify(req.body);
    if (req.query) includes.req_query = JSON.stringify(req.query);
    return includes;
  },
});

app.use(graphqlLogger);

Problem is, even for a URL of the form http://localhost:4000/graphql?query=query%20%7B%0A%20%20books%20%7B%0A%20%20%20%20title%0A%20%20%7D%0A%7D, the req_query and req_body fields are always empty. If I inspect the req object that's passed to includesFn in a debugger, it doesn't contain any values for req.query or req.params.

I'm sure I'm doing something wrong here; how do I get the includesFn to log the query string and body correctly?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

So, turns out the includesFn is being called twice, once per each request.

First, for the GET request, then you will get only the req.query object not empty.

The second request is the POST request that is being with JSON data, so in order to to get the req.body defined and not empty, you have to do the following:

  1. run npm install body-parser.
  2. include the following in your app.js file:

    const bodyParser = require('body-parser');
    
    ...
    
    app.use(bodyParser.urlencoded({
      extended: true
    }));
    
    app.use(bodyParser.json());
    
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!