2021-09-10 15:54:19 +02:00
|
|
|
'use strict';
|
|
|
|
|
2022-05-13 16:10:18 +02:00
|
|
|
const { FILE_MODEL_UID } = require('./constants');
|
2021-09-10 15:54:19 +02:00
|
|
|
|
|
|
|
const FILE_INFO_INPUT_TYPE_NAME = 'FileInfoInput';
|
|
|
|
|
2021-09-29 10:47:10 +02:00
|
|
|
/**
|
|
|
|
* @param {{ strapi: import('@strapi/strapi').Strapi }}
|
|
|
|
*/
|
2021-09-10 15:54:19 +02:00
|
|
|
module.exports = ({ strapi }) => {
|
2021-09-22 18:17:51 +02:00
|
|
|
const { service: getGraphQLService, config: graphQLConfig } = strapi.plugin('graphql');
|
2021-09-10 15:54:19 +02:00
|
|
|
const { service: getUploadService } = strapi.plugin('upload');
|
|
|
|
|
2021-09-22 18:17:51 +02:00
|
|
|
const isShadowCRUDEnabled = graphQLConfig('shadowCRUD', true);
|
|
|
|
|
|
|
|
if (!isShadowCRUDEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-08 10:59:36 +01:00
|
|
|
getGraphQLService('extension').shadowCRUD('plugin::upload.folder').disable();
|
|
|
|
getGraphQLService('extension').shadowCRUD('plugin::upload.file').disableMutations();
|
|
|
|
|
|
|
|
const { getTypeName } = getGraphQLService('utils').naming;
|
2021-09-10 15:54:19 +02:00
|
|
|
|
2022-05-13 16:10:18 +02:00
|
|
|
const fileModel = strapi.getModel(FILE_MODEL_UID);
|
2021-09-10 15:54:19 +02:00
|
|
|
const fileTypeName = getTypeName(fileModel);
|
|
|
|
/**
|
|
|
|
* Register Upload's types, queries & mutations to the content API using the GraphQL extension API
|
|
|
|
*/
|
2021-09-15 15:44:42 +02:00
|
|
|
getGraphQLService('extension').use(({ nexus }) => {
|
2024-03-08 10:59:36 +01:00
|
|
|
const { inputObjectType, extendType, nonNull } = nexus;
|
2021-09-15 15:44:42 +02:00
|
|
|
|
|
|
|
// Represents the input data payload for the file's information
|
|
|
|
const fileInfoInputType = inputObjectType({
|
|
|
|
name: FILE_INFO_INPUT_TYPE_NAME,
|
|
|
|
|
|
|
|
definition(t) {
|
|
|
|
t.string('name');
|
|
|
|
t.string('alternativeText');
|
|
|
|
t.string('caption');
|
|
|
|
},
|
2021-09-10 15:54:19 +02:00
|
|
|
});
|
2021-09-15 15:44:42 +02:00
|
|
|
|
|
|
|
const mutations = extendType({
|
|
|
|
type: 'Mutation',
|
|
|
|
|
|
|
|
definition(t) {
|
|
|
|
/**
|
|
|
|
* Update some information for a given file
|
|
|
|
*/
|
2024-03-08 10:59:36 +01:00
|
|
|
t.field('updateUploadFile', {
|
|
|
|
type: nonNull(fileTypeName),
|
2021-09-15 15:44:42 +02:00
|
|
|
|
|
|
|
args: {
|
|
|
|
id: nonNull('ID'),
|
|
|
|
info: FILE_INFO_INPUT_TYPE_NAME,
|
|
|
|
},
|
|
|
|
|
|
|
|
async resolve(parent, args) {
|
|
|
|
const { id, info } = args;
|
|
|
|
|
2024-03-08 10:59:36 +01:00
|
|
|
return getUploadService('upload').updateFileInfo(id, info);
|
2021-09-15 15:44:42 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete & remove a given file
|
|
|
|
*/
|
2024-03-08 10:59:36 +01:00
|
|
|
t.field('deleteUploadFile', {
|
|
|
|
type: fileTypeName,
|
2021-09-15 15:44:42 +02:00
|
|
|
|
|
|
|
args: {
|
|
|
|
id: nonNull('ID'),
|
|
|
|
},
|
|
|
|
|
|
|
|
async resolve(parent, args) {
|
|
|
|
const { id } = args;
|
|
|
|
|
2021-09-28 11:11:03 +02:00
|
|
|
const file = await getUploadService('upload').findOne(id);
|
2021-09-15 15:44:42 +02:00
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-03-08 10:59:36 +01:00
|
|
|
return getUploadService('upload').remove(file);
|
2021-09-15 15:44:42 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-11-04 15:47:53 +01:00
|
|
|
return {
|
|
|
|
types: [fileInfoInputType, mutations],
|
|
|
|
resolversConfig: {
|
|
|
|
// Use custom scopes for the upload file CRUD operations
|
2022-08-08 15:50:34 +02:00
|
|
|
'Query.uploadFiles': { auth: { scope: 'plugin::upload.content-api.find' } },
|
2024-03-08 10:59:36 +01:00
|
|
|
'Query.uploadFiles_connection': { auth: { scope: 'plugin::upload.content-api.find' } },
|
2022-08-08 15:50:34 +02:00
|
|
|
'Query.uploadFile': { auth: { scope: 'plugin::upload.content-api.findOne' } },
|
|
|
|
'Mutation.updateUploadFile': { auth: { scope: 'plugin::upload.content-api.upload' } },
|
|
|
|
'Mutation.deleteUploadFile': { auth: { scope: 'plugin::upload.content-api.destroy' } },
|
2021-11-04 15:47:53 +01:00
|
|
|
},
|
|
|
|
};
|
2021-09-15 15:44:42 +02:00
|
|
|
});
|
2021-09-10 15:54:19 +02:00
|
|
|
};
|