From 645c0b8cf4564adc0a12b01873201901ac85fab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20de=20Metz?= Date: Mon, 5 Oct 2020 11:34:15 +0200 Subject: [PATCH] Implement deleteFile graphql mutation (#5292). (#7937) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Implement deleteFile graphql mutation (#5292). Signed-off-by: François de Metz * Test if the file exist before removing it. Signed-off-by: François de Metz * Fix a typo. Signed-off-by: François de Metz --- .../test/single-type.test.e2e.js | 2 +- .../config/schema.graphql.js | 12 +++- .../test/graphqlUpload.test.e2e.js | 60 +++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/packages/strapi-plugin-graphql/test/single-type.test.e2e.js b/packages/strapi-plugin-graphql/test/single-type.test.e2e.js index 5ed0d0d731..b219b45bec 100644 --- a/packages/strapi-plugin-graphql/test/single-type.test.e2e.js +++ b/packages/strapi-plugin-graphql/test/single-type.test.e2e.js @@ -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 */ ` { diff --git a/packages/strapi-plugin-upload/config/schema.graphql.js b/packages/strapi-plugin-upload/config/schema.graphql.js index 1d732f20e8..3d8794763e 100644 --- a/packages/strapi-plugin-upload/config/schema.graphql.js +++ b/packages/strapi-plugin-upload/config/schema.graphql.js @@ -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 }; + } + }, + }, }, }, }; diff --git a/packages/strapi-plugin-upload/test/graphqlUpload.test.e2e.js b/packages/strapi-plugin-upload/test/graphqlUpload.test.e2e.js index 32709115ec..3f3ad31147 100644 --- a/packages/strapi-plugin-upload/test/graphqlUpload.test.e2e.js +++ b/packages/strapi-plugin-upload/test/graphqlUpload.test.e2e.js @@ -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, + }, + }); + }); });