forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphqlUpload.js
More file actions
51 lines (44 loc) · 1.63 KB
/
Copy pathgraphqlUpload.js
File metadata and controls
51 lines (44 loc) · 1.63 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Cache the dynamic imports so the ESM-only graphql-upload modules are
// resolved once and then reused by both schema loading and request handling.
let graphqlUploadModulesPromise;
const loadGraphQLUploadModules = async () => {
if (!graphqlUploadModulesPromise) {
graphqlUploadModulesPromise = Promise.all([
import('graphql-upload/GraphQLUpload.mjs'),
import('graphql-upload/processRequest.mjs'),
]).then(([{ default: GraphQLUpload }, { default: processRequest }]) => ({
GraphQLUpload,
processRequest,
}));
}
return graphqlUploadModulesPromise;
};
// Expose the Upload scalar lazily so the rest of Parse Server can stay on the
// current module system while graphql-upload is now ESM-only.
const getGraphQLUpload = async () => {
const { GraphQLUpload } = await loadGraphQLUploadModules();
return GraphQLUpload;
};
const createGraphQLUploadMiddleware = options => {
const uploadOptions = {
...options,
// Decode multipart filename parameters as UTF-8 so filenames like
// "cafe.txt" with accents don't arrive as mojibake.
defParamCharset: 'utf8',
};
return async (req, res, next) => {
if (!req.is || !req.is('multipart/form-data')) {
return next();
}
try {
const { processRequest } = await loadGraphQLUploadModules();
// graphql-upload parses the multipart body and populates req.body with
// Upload promises before Apollo handles the GraphQL operation.
req.body = await processRequest(req, res, uploadOptions);
return next();
} catch (error) {
return next(error);
}
};
};
export { createGraphQLUploadMiddleware, getGraphQLUpload };