Express-graphql: Allow customize body size limit

Created on 13 Jul 2017  路  15Comments  路  Source: graphql/express-graphql

Currently due to line parseBody.js#L112, the body of request limits to 100Kb.

I'm making an api that needs a bit more large body for some requests, for example 1Mb, can customization be added without the need to fork and modify directly?

enhancement

Most helpful comment

@luchillo17

You can enable a custom limit by using body-parseras middleware before express-graphql:

app.use(bodyParser.json({limit: '1mb'}));
app.use(
  `/graphql`,
  graphqlHTTP(req => ({
    schema
  }))
);

All 15 comments

Ping @wincent, what do you think about this feat? also my current fix (hack may be a better way to put it) is this:

  • Tell my Webpack config to not use express-graphql as an external, but to bundle it to the server.bundle.js output file.
  • Tell the Webpack config to parse parseBody.js#L112 and make string replacement on that limit variable like this:
export default {
  module: {
    rules: [
        {
          test: /parseBody\.js$/,
          use: [
            {
              loader: 'string-replace-loader',
              query: {
                search: 'var limit = 100 * 1024',
                replace: `var limit = '5mb'`,
                strict: true,
              },
            },
          ],
        },
    ]
  }
}

However the fact that i bundle that file generates certain error in my tests (and only in my tests, if i run the server it works ok):

FetchError{name: 'FetchError', message: 'request to http://localhost:50006/test failed, reason: socket hang up', type: 'system', errno: 'ECONNRESET', code: 'ECONNRESET'}

The hard-coded 100kb body limit does feel arbitrary. I'm not averse to making it configurable.

I'm already hitting the limit in my app in 2 different routes, using my workaround seems to break when used in tests and PM2, at this point i'll have to link it as submodule and edit such line.

Ok so i'm trying with SubModules and having issues with it in my tests as stated above, it's driving me crazy.

@wincent Man sorry about the ranting, i've tested a bit more and my issues have nothing to do with SubModules, most likely it has to do with latest changes from version 0.6.6 in registry, i've installed from the git repo and this issue happens:
image

Would need more info than is visible in that screenshot to know what's going on, and perhaps it should be a separate issue, to keep this issue for the feature request.

I've found that apollographql/apollo-server doesn't parse the request itself, but let it to the standard body-parser, which would allow the dev to set the limit in a single place, i think that's a better approach.

Why does this repo implements that by itself? any custom features that body-parser doesn't allow?

@luchillo17

You can enable a custom limit by using body-parseras middleware before express-graphql:

app.use(bodyParser.json({limit: '1mb'}));
app.use(
  `/graphql`,
  graphqlHTTP(req => ({
    schema
  }))
);

@excitement-engineer Yes but that's just the express limit, express-graphql has it's own internal limit, that's what this issue is about.

If the contents of the body has already been parsed before entering express-graphql then express-graphql will not use the internal json parser contained in the library. See https://github.com/graphql/express-graphql/blob/master/src/parseBody.js#L27-L30.

I am using this in my backend and I can confirm that the method I show above works.

Ok now i see it, if it's already parsed it will just return, if not it will try to parse according to the headers content type, i guess i should have looked at it, this is effectively the right way, closing now.

Using the same example as show by @excitement-engineer works but the error is not formatted by GraphQL. By default the error is wrapped in JSON property named errors

{"errors":[{"message":"Invalid body: request entity too large."}]}

When fixing a custom limit the response format changes:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>PayloadTooLargeError: request entity too large<br> &nbsp; &nbsp;at readStream ...</pre>
</body>
</html>

How to keep the original error format?

@gdrouet I'm having a similar issue. Did you find a solution to keep the original JSON formatting in the case your describe?

For me this simple solution worked:

// Add this before the GraphQL middleware
app.use(bodyParser({ limit: '1mb' }));

body-parser docs:
https://github.com/expressjs/body-parser/tree/1.16.0#limit

(I'm using express-graphq ^0.7.1.)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dhurlburtusa picture dhurlburtusa  路  4Comments

dmytro-kerest picture dmytro-kerest  路  3Comments

arealmaas picture arealmaas  路  4Comments

justinmchase picture justinmchase  路  4Comments

ivanhdzd picture ivanhdzd  路  3Comments