UNPKG

2.64 kBMarkdownView Raw
1# body-parser-graphql [![npm](https://img.shields.io/npm/v/body-parser-graphql.svg?style=for-the-badge)](https://www.npmjs.com/package/body-parser-graphql)
2[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=for-the-badge)](https://github.com/semantic-release/semantic-release)[![CircleCI](https://img.shields.io/circleci/project/github/supergraphql/body-parser-graphql.svg?style=for-the-badge)](https://circleci.com/gh/supergraphql/body-parser-graphql)[![Code Climate](https://img.shields.io/codeclimate/maintainability/supergraphql/body-parser-graphql.svg?style=for-the-badge&label=code%20quality)](https://codeclimate.com/github/supergraphql/body-parser-graphql)[![Coveralls](https://img.shields.io/coveralls/github/supergraphql/body-parser-graphql.svg?style=for-the-badge)](https://coveralls.io/github/supergraphql/body-parser-graphql)[![Renovate badge](https://img.shields.io/badge/renovate-enabled-e10079.svg?style=for-the-badge)](https://renovateapp.com/)
3Express body-parser that supports the `application/graphql` MIME type.
4
5## How does it work?
6`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`.
7
8Received request:
9```graphql
10{
11 posts {
12 id
13 title
14 }
15}
16```
17`request.body` value after the middleware:
18```js
19{
20 query: {
21 posts {
22 id
23 title
24 }
25 }
26}
27```
28
29If an `application/json` request is received, it applies the JSON body-parser.
30
31## Installation
32
33Install `body-parser-graphql` using your favorite package manager:
34```shell
35$ yarn add body-parser-graphql
36$ npm install body-parser-graphql
37```
38
39## Usage
40
41The `body-parser-graphql` can be used as a drop-in replacement for the normal `json` body-parser.
42
43```diff
44import * as express from 'express'
45- import * as bodyParser from 'body-parser'
46+ import * as bodyParser from 'body-parser-graphql'
47
48const app = express()
49
50- app.use(bodyParser.json())
51+ app.use(bodyParser.graphql())
52
53// Your express routes
54
55app.listen(/* your configuration */)
56```
57
58Alternatively, you can also import the body-parser directly:
59
60```typescript
61import { bodyParserGraphQL } from 'body-parser-graphql'
62
63app.use(bodyParserGraphQL())
64```
65
66<hr>
67<p align="center">
68 <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>
69</p>