body-parser-graphql
Version:
GraphQL server middleware to support application/graphql requests
70 lines (53 loc) • 2.64 kB
Markdown
# body-parser-graphql [](https://www.npmjs.com/package/body-parser-graphql)
[](https://github.com/semantic-release/semantic-release)[](https://circleci.com/gh/supergraphql/body-parser-graphql)[](https://codeclimate.com/github/supergraphql/body-parser-graphql)[](https://coveralls.io/github/supergraphql/body-parser-graphql)[](https://renovateapp.com/)
Express body-parser that supports the `application/graphql` MIME type.
## How does it work?
`body-parser-graphql` checks the `Content-Type` header of the request. If the Content-Type is `application/graphql`, the request is transformed into a 'normal' `application/json` GraphQL request, and the `Content-Type` header is set to `application/json`.
Received request:
```graphql
{
posts {
id
title
}
}
```
`request.body` value after the middleware:
```js
{
query: {
posts {
id
title
}
}
}
```
If an `application/json` request is received, it applies the JSON body-parser.
## Installation
Install `body-parser-graphql` using your favorite package manager:
```shell
$ yarn add body-parser-graphql
$ npm install body-parser-graphql
```
## Usage
The `body-parser-graphql` can be used as a drop-in replacement for the normal `json` body-parser.
```diff
import * as express from 'express'
- import * as bodyParser from 'body-parser'
+ import * as bodyParser from 'body-parser-graphql'
const app = express()
- app.use(bodyParser.json())
+ app.use(bodyParser.graphql())
// Your express routes
app.listen(/* your configuration */)
```
Alternatively, you can also import the body-parser directly:
```typescript
import { bodyParserGraphQL } from 'body-parser-graphql'
app.use(bodyParserGraphQL())
```
<hr>
<p align="center">
<img src="https://img.shields.io/badge/built-with_%F0%9F%92%99-blue.svg?style=for-the-badge"/><a href="https://github.com/kbrandwijk" target="-_blank"><img src="https://img.shields.io/badge/by-kim_brandwijk-blue.svg?style=for-the-badge"/></a>
</p>