Express GraphQL Request Body Parser
raw JSON →body-parser-graphql is an Express.js middleware designed to process incoming HTTP requests with the `Content-Type` header set to `application/graphql`. Upon receipt, it transparently transforms the raw GraphQL query string from the request body into a standard `application/json` format, typically an object with a `query` property containing the GraphQL operation. This allows existing GraphQL server implementations, which usually expect `application/json` payloads, to seamlessly consume queries sent with the `application/graphql` Content-Type. The current stable version is 1.1.0, released in April 2018. However, the package has been archived and is no longer actively maintained as of March 2023, meaning users should consider alternatives or built-in solutions provided by modern GraphQL server libraries.
Common errors
error TypeError: app.use() requires a middleware function but got a undefined ↓
app.use(bodyParserGraphQL()) or app.use(bodyParser.graphql()). error POST body missing. Did you forget use body-parser middleware? ↓
Content-Type: application/graphql and the GraphQL query string directly in the body. Ensure app.use(bodyParserGraphQL()) is called early in your middleware stack. error Error: request entity too large ↓
app.use(bodyParserGraphQL({ limit: '5mb' })). Warnings
breaking The `body-parser-graphql` package is no longer actively maintained. Its GitHub repository was archived on March 20, 2023, and is now read-only. This means there will be no further updates, bug fixes, or security patches. ↓
gotcha This package internally relies on `body-parser` functionality. While `body-parser` itself is actively maintained, this wrapper may use an older, fixed version of `body-parser` or have compatibility issues with newer versions of Express.js or Node.js without updates. ↓
gotcha The middleware expects a raw GraphQL query string in the request body when `Content-Type` is `application/graphql`. If the client sends a JSON object (e.g., `{ query: "..." }`) with `Content-Type: application/graphql`, it might not be parsed as expected, or could lead to errors. It converts `application/graphql` to `application/json` with the query string as the value of the `query` property. ↓
Install
npm install body-parser-graphql yarn add body-parser-graphql pnpm add body-parser-graphql Imports
- bodyParserGraphQL wrong
import bodyParserGraphQL from 'body-parser-graphql'correctimport { bodyParserGraphQL } from 'body-parser-graphql' - * as bodyParser wrong
const bodyParser = require('body-parser-graphql')correctimport * as bodyParser from 'body-parser-graphql' - CommonJS require wrong
const bodyParserGraphQL = require('body-parser-graphql').defaultcorrectconst { bodyParserGraphQL } = require('body-parser-graphql')
Quickstart
import * as express from 'express';
import { bodyParserGraphQL } from 'body-parser-graphql';
const app = express();
// Integrate the GraphQL body parser middleware
app.use(bodyParserGraphQL());
// Example GraphQL endpoint (assuming a GraphQL server handler)
app.post('/graphql', (req, res) => {
// req.body will now contain the parsed GraphQL query as a JSON object
// e.g., { query: "{ posts { id title } }" }
console.log('Received GraphQL query:', req.body.query);
res.json({
data: {
message: 'Hello from GraphQL endpoint!',
receivedQuery: req.body.query
}
});
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
console.log('Try sending a POST request to /graphql with Content-Type: application/graphql');
console.log('Body: { posts { id title } }');
});