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

Commit ca5de49

Browse files
committed
Fuller example using multer
Closes #9
1 parent b8737ad commit ca5de49

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ If not found in the query-string, it will look in the POST request body.
6060
If a previous middleware has already parsed the POST body, the `request.body`
6161
value will be used. Use [`multer`][] or a similar middleware to add support
6262
for `multipart/form-data` content, which may be useful for GraphQL mutations
63-
involving uploading files.
63+
involving uploading files. See an [example using multer](https://github.com/graphql/express-graphql/blob/master/src/__tests__/http-test.js#L462).
6464

6565
If the POST body has not yet been parsed, graphql-express will interpret it
6666
depending on the provided *Content-Type* header.

src/__tests__/http-test.js

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,28 +460,70 @@ describe('test harness', () => {
460460
});
461461

462462
it('allows for pre-parsed POST bodies', async () => {
463+
// Note: this is not the only way to handle file uploads with GraphQL,
464+
// but it is terse and illustrative of using express-graphql and multer
465+
// together.
466+
467+
// A simple schema which includes a mutation.
468+
var UploadedFileType = new GraphQLObjectType({
469+
name: 'UploadedFile',
470+
fields: {
471+
originalname: { type: GraphQLString },
472+
mimetype: { type: GraphQLString }
473+
}
474+
});
475+
476+
var TestMutationSchema = new GraphQLSchema({
477+
query: new GraphQLObjectType({
478+
name: 'QueryRoot',
479+
fields: {
480+
test: { type: GraphQLString }
481+
}
482+
}),
483+
mutation: new GraphQLObjectType({
484+
name: 'MutationRoot',
485+
fields: {
486+
uploadFile: {
487+
type: UploadedFileType,
488+
resolve(rootValue) {
489+
// For this test demo, we're just returning the uploaded
490+
// file directly, but presumably you might return a Promise
491+
// to go store the file somewhere first.
492+
return rootValue.request.file;
493+
}
494+
}
495+
}
496+
})
497+
});
498+
463499
var app = express();
464500

465501
// Multer provides multipart form data parsing.
466502
var storage = multer.memoryStorage();
467503
app.use(urlString(), multer({ storage }).single('file'));
468504

505+
// Providing the request as part of `rootValue` allows it to
506+
// be accessible from within Schema resolve functions.
469507
app.use(urlString(), graphqlHTTP(req => {
470-
expect(req.file.originalname).to.equal('http-test.js');
471508
return {
472-
schema: TestSchema,
473-
rootObject: { request: req }
509+
schema: TestMutationSchema,
510+
rootValue: { request: req }
474511
};
475512
}));
476513

477514
var response = await request(app)
478515
.post(urlString())
479-
.field('query', '{ test(who: "World") }')
516+
.field('query', `mutation TestMutation {
517+
uploadFile { originalname, mimetype }
518+
}`)
480519
.attach('file', __filename);
481520

482521
expect(JSON.parse(response.text)).to.deep.equal({
483522
data: {
484-
test: 'Hello World'
523+
uploadFile: {
524+
originalname: 'http-test.js',
525+
mimetype: 'application/javascript'
526+
}
485527
}
486528
});
487529
});

0 commit comments

Comments
 (0)