Express-graphql: Request fails when using application/graphql with body parser

Created on 9 Sep 2015  路  11Comments  路  Source: graphql/express-graphql

When sending a Content-Type : application/graphql with a query string the request fails.
If using body parser json or url encoded it says that i don't have a query string.
if used with text type application/graphql it gets stuck on parseBody.js in the line 119.

Most helpful comment

I solved my problem doing the following:

// configure app to use bodyParser()
// this will let us get the data from a POST
// exclusing the route to graphql
app.use(/\/((?!graphql).)*/, bodyParser.urlencoded({ extended: true }));
app.use(/\/((?!graphql).)*/, bodyParser.json());

All 11 comments

+1 I'm experiencing exactly the same issue as @AJ1310 describes

This is now fixed and will go out with the next release.

Are you sure this has been fixed? I'm running express-graphql 0.4.13 with body-parser 1.15.0 and doing a POST with headers set to Content-Type: application/graphql I get

{
  "errors": [
    {
      "message": "Must provide query string."
    }
  ]
} 

I solved my problem doing the following:

// configure app to use bodyParser()
// this will let us get the data from a POST
// exclusing the route to graphql
app.use(/\/((?!graphql).)*/, bodyParser.urlencoded({ extended: true }));
app.use(/\/((?!graphql).)*/, bodyParser.json());

Yes this is fixed. However if you're also using body-parser, you must still ensure the output of body-parser is compatible with the express-graphql plugin.

The recommended use is still to use the express-graphql middleware _before_ using any kind of body parser middleware you may be using for other routes.

I am experiencing the same issue using :

"body-parser": "^1.17.2",
"express": "^4.15.3",
"express-graphql": "^0.6.

My server:

const schema = require('./schema');
const express = require('express');
const graphqlHTTP = require('express-graphql');
const cors = require('cors')
const bodyParser = require('body-parser');


const app = express();
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use('/graphql', graphqlHTTP(req => {
  return ({
  schema,
  pretty: true,
  })
}));

const server = app.listen(9000, err => {
  if (err) { return err; }
  console.log(`GraphQL server running on http://localhost:${9000}/graphql`);
});

Request:

screen shot 2017-08-06 at 11 30 16

Any help?

@AvraamMavridis you got this to work? I got a similar issue

Btw, you're using content-type: text/plain it has to be application/json

@AvraamMavridis @ghost try adding

app.use(bodyParser.text({ type: 'application/graphql' }));

fixed it for me

@dh94 can you confirm that this need to be called after bodyParser.json() ? I had some issues with Apollo Server 2 in a Meteor app and writing

app.use(bodyParser.json())
app.use(bodyParser.text({ type: 'application/graphql' }));

seemed to fix it.

Edit: it was not sufficient, see body-parser-graphql package which works fine now

Was this page helpful?
0 / 5 - 0 ratings

Related issues

violet-day picture violet-day  路  3Comments

stevvvn picture stevvvn  路  3Comments

jamesmoriarty picture jamesmoriarty  路  4Comments

arealmaas picture arealmaas  路  4Comments

KieronWiltshire picture KieronWiltshire  路  3Comments