-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
30 lines (30 loc) · 766 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module.exports = function graphqlServerExpressUpload(options) {
function isUpload(req) {
return Boolean(
req.baseUrl === options.endpointURL &&
req.method === 'POST' &&
req.is('multipart/form-data')
);
}
return function(req, res, next) {
if (!isUpload(req)) {
return next();
}
var files = req.files;
var body = req.body;
var variables = JSON.parse(body.variables);
// append files to variables
files.forEach(file => {
if (!variables[file.fieldname]) {
variables[file.fieldname] = [];
}
variables[file.fieldname].push(file);
})
req.body = {
operationName: body.operationName,
query: body.query,
variables: variables
};
return next();
}
}