Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit e6fb666

Browse files
committed
Merge branch 'Aweary-master'
2 parents c4b8040 + 3069a66 commit e6fb666

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"babel-core": "5.8.22",
5959
"babel-eslint": "4.0.10",
6060
"babel-runtime": "5.8.20",
61+
"body-parser": "^1.14.0",
6162
"chai": "3.2.0",
6263
"coveralls": "2.11.4",
6364
"eslint": "1.1.0",

src/__tests__/http-test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { describe, it } from 'mocha';
1616
import { stringify } from 'querystring';
1717
import zlib from 'zlib';
1818
import multer from 'multer';
19+
import bodyParser from 'body-parser';
1920
import request from 'supertest-as-promised';
2021
import express4 from 'express'; // modern
2122
import express3 from 'express3'; // old but commonly still used
@@ -485,6 +486,42 @@ describe('test harness', () => {
485486
});
486487
});
487488

489+
it('allows for pre-parsed POST using application/graphql', async () => {
490+
var app = express();
491+
app.use(bodyParser.text({ type: 'application/graphql' }));
492+
493+
app.use(urlString(), graphqlHTTP({ schema: TestSchema }));
494+
495+
var req = request(app)
496+
.post(urlString())
497+
.set('Content-Type', 'application/graphql');
498+
req.write(new Buffer('{ test(who: "World") }'));
499+
var response = await req;
500+
501+
expect(JSON.parse(response.text)).to.deep.equal({
502+
data: {
503+
test: 'Hello World'
504+
}
505+
});
506+
});
507+
508+
it('does not accept unknown pre-parsed POST string', async () => {
509+
var app = express();
510+
app.use(bodyParser.text({ type: '*/*' }));
511+
512+
app.use(urlString(), graphqlHTTP({ schema: TestSchema }));
513+
514+
var req = request(app)
515+
.post(urlString());
516+
req.write(new Buffer('{ test(who: "World") }'));
517+
var error = await catchError(req);
518+
519+
expect(error.response.status).to.equal(400);
520+
expect(JSON.parse(error.response.text)).to.deep.equal({
521+
errors: [ { message: 'Must provide query string.' } ]
522+
});
523+
});
524+
488525
});
489526

490527
describe('Pretty printing', () => {

src/parseBody.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type { Request } from 'express';
1818

1919
export function parseBody(req: Request, next: NodeCallback): void {
2020
try {
21-
// If express has already parsed a body, use it.
21+
// If express has already parsed a body as an object, use it.
2222
if (typeof req.body === 'object') {
2323
return next(null, req.body);
2424
}
@@ -30,6 +30,18 @@ export function parseBody(req: Request, next: NodeCallback): void {
3030

3131
var typeInfo = contentType.parse(req);
3232

33+
// If express has already parsed a body as a string, and the content-type
34+
// was application/graphql, parse the string body.
35+
if (typeof req.body === 'string' &&
36+
typeInfo.type === 'application/graphql') {
37+
return next(null, graphqlParser(req.body));
38+
}
39+
40+
// Already parsed body we didn't recognise? Parse nothing.
41+
if (req.body) {
42+
return next();
43+
}
44+
3345
// Use the correct body parser based on Content-Type header.
3446
switch (typeInfo.type) {
3547
case 'application/graphql':

0 commit comments

Comments
 (0)