Hello
I'd like to output the original GraphQL query (and any variables) to the console whenever an error is thrown in order to make debugging easier. At the moment I can't see any way of doing this as req.body isn't available as express-graphql is doing it's own body parsing, and the raw query doesn't seem to get passed anywhere.
The closest thing I've found so far is fieldNodes which is an object describing the query, but it's still quite difficult to reverse engineer the original query from this.
Could we get the raw query added to the express req object?
Thanks!
Maybe this issue will be helpful for you https://github.com/graphql/graphql-js/issues/662
and this custom error catching realization
Also graphql-express has extensions read here. It may be a function, which contains document argument. If you need a raw query try to open PR with adding query to args here (query is populated in upper scope, so it is easy to add just dropping a one line query,)
@richardwillars Using one of the arguments (document), shouldn't you be able to do something like: print(document) and get the query string?
If I was using GET I guess that'd work, though in my case I'm using POST. Don't think there's anything in there for that
any updates?
I think you can do this if you add your own body parser.
const graphqlHTTP = require('express-graphql');
const bodyParser = require('body-parser');
const {formatError} = require('graphql');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.use('/', graphqlHTTP(function (req) {
return {
schema: Schema,
graphiql: true,
rootValue: {},
context: buildContext(req),
formatError: e => {
const query = req.body.query;
const variables = req.body.variables;
console.log('Error!', query, JSON.stringify(variables, null, 2));
return formatError(e);
}
};
}));
@richardwillars Since 0.6.5 options callback receives the third argument which contains parsed request parameters so you can use @robrichard solution without external body parser:
const graphqlHTTP = require('express-graphql');
const {formatError} = require('graphql');
app.use('/', graphqlHTTP(function (req, res, params) {
return {
schema: Schema,
graphiql: true,
rootValue: {},
context: buildContext(req),
formatError: e => {
const query = params.query;
const variables = params.variables;
console.log('Error!', query, JSON.stringify(variables, null, 2));
return formatError(e);
}
};
}));
Most helpful comment
@richardwillars Since
0.6.5options callback receives the third argument which contains parsed request parameters so you can use @robrichard solution without external body parser: