Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Raw GraphQL request on req object #197

Closed
richardwillars opened this issue Jan 24, 2017 · 6 comments
Closed

Raw GraphQL request on req object #197

richardwillars opened this issue Jan 24, 2017 · 6 comments

Comments

@richardwillars
Copy link

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!

@nodkz
Copy link

nodkz commented Jan 24, 2017

Maybe this issue will be helpful for you graphql/graphql-js#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,)

@asiandrummer
Copy link
Contributor

@richardwillars Using one of the arguments (document), shouldn't you be able to do something like: print(document) and get the query string?

@richardwillars
Copy link
Author

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

@shtse8
Copy link

shtse8 commented Oct 23, 2017

any updates?

@robrichard
Copy link
Collaborator

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);
       }
    };
}));

@IvanGoncharov
Copy link
Member

@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);
       }
    };
}));

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants