Implement deleteFile graphql mutation (#5292). (#7937)

* Implement deleteFile graphql mutation (#5292).

Signed-off-by: François de Metz <francois@2metz.fr>

* Test if the file exist before removing it.

Signed-off-by: François de Metz <francois@2metz.fr>

* Fix a typo.

Signed-off-by: François de Metz <francois@2metz.fr>
This commit is contained in:
François de Metz 2020-10-05 11:34:15 +02:00 committed by GitHub
parent 6bc8662ff6
commit 645c0b8cf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 2 deletions

View File

@ -58,7 +58,7 @@ describe('Single type Graphql support', () => {
afterAll(() => modelsUtils.deleteContentType('home-page'), 60000);
describe('Queries', () => {
test('No list avaialble', async () => {
test('No list available', async () => {
const res = await graphqlQuery({
query: /* GraphQL */ `
{

View File

@ -24,7 +24,6 @@ module.exports = {
Mutation: {
createFile: false,
updateFile: false,
deleteFile: false,
upload: {
description: 'Upload one file',
resolverOf: 'plugins::upload.upload.upload',
@ -57,6 +56,17 @@ module.exports = {
return await strapi.plugins.upload.services.upload.updateFileInfo(id, info);
},
},
deleteFile: {
description: 'Delete one file',
resolverOf: 'plugins::upload.upload.destroy',
resolver: async (obj, options, { context }) => {
const file = await strapi.plugins.upload.services.upload.fetch({ id: context.params.id });
if (file) {
const fileResult = await strapi.plugins.upload.services.upload.remove(file);
return { file: fileResult };
}
},
},
},
},
};

View File

@ -147,4 +147,64 @@ describe('Upload plugin end to end tests', () => {
},
});
});
test('Delete a file', async () => {
const res = await rq({
url: '/graphql',
method: 'POST',
body: {
query: /* GraphQL */ `
mutation removeFile($id: ID!) {
deleteFile(input: { where: { id: $id } }) {
file {
id
}
}
}
`,
variables: {
id: data.file.id,
},
},
});
expect(res.statusCode).toBe(200);
expect(res.body).toMatchObject({
data: {
deleteFile: {
file: {
id: data.file.id,
},
},
},
});
});
test('Delete a file that dont exist', async () => {
const res = await rq({
url: '/graphql',
method: 'POST',
body: {
query: /* GraphQL */ `
mutation removeFile($id: ID!) {
deleteFile(input: { where: { id: $id } }) {
file {
id
}
}
}
`,
variables: {
id: '404',
},
},
});
expect(res.statusCode).toBe(200);
expect(res.body).toMatchObject({
data: {
deleteFile: null,
},
});
});
});