2018-09-10 16:05:00 +08:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
2018-04-10 12:56:13 +02:00
|
|
|
module.exports = {
|
2018-09-10 16:05:00 +08:00
|
|
|
mutation: `
|
|
|
|
upload(refId: ID, ref: String, source: String, file: Upload!): UploadFile!
|
|
|
|
`,
|
2018-04-10 12:56:13 +02:00
|
|
|
resolver: {
|
|
|
|
Query: {
|
2018-04-10 18:54:01 +02:00
|
|
|
file: false,
|
2018-04-10 12:56:13 +02:00
|
|
|
files: {
|
|
|
|
resolver: 'Upload.find'
|
|
|
|
}
|
2018-09-10 16:05:00 +08:00
|
|
|
},
|
|
|
|
Mutation: {
|
|
|
|
createFile: false,
|
|
|
|
updateFile: false,
|
|
|
|
deleteFile: false,
|
|
|
|
upload: {
|
|
|
|
description: 'Upload one or many files',
|
|
|
|
resolver: async (obj, { file, ...fields}, { context }) => {
|
|
|
|
// Construct context to fit with koa-parser guidelines
|
|
|
|
// and avoid to update our business logic too much.
|
|
|
|
context.request.body = {
|
|
|
|
files: {
|
|
|
|
files: await file
|
|
|
|
},
|
|
|
|
fields
|
|
|
|
};
|
|
|
|
|
|
|
|
// Call controller action.
|
|
|
|
await strapi.plugins.upload.controllers.upload.upload(context);
|
|
|
|
|
|
|
|
// Handle case when the user is uploading only one file.
|
|
|
|
if (_.isArray(context.body) && context.body.length === 1) {
|
|
|
|
return context.body[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return response.
|
|
|
|
return context.body;
|
|
|
|
}
|
|
|
|
}
|
2018-04-10 12:56:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|