Skip to content

Commit 84770f3

Browse files
committed
feat: implemented graphql body-parser
1 parent e4f067a commit 84770f3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as bodyParser from 'body-parser'
2+
import { Request, Response, NextFunction, RequestHandler } from 'express'
3+
4+
export const bodyParserGraphQL: () => RequestHandler = () => (
5+
req: Request,
6+
res: Response,
7+
next: NextFunction
8+
) => {
9+
if (req.is('application/graphql')) {
10+
bodyParser.text({ type: 'application/graphql' })(req, res, () => {
11+
req.headers['content-type'] = 'application/json'
12+
req.body = {
13+
query: req.body
14+
}
15+
next()
16+
})
17+
} else {
18+
bodyParser.json()(req, res, next)
19+
}
20+
}
21+
22+
export const graphql = bodyParserGraphQL

0 commit comments

Comments
 (0)