From 0acada198c61bbdff4a63448af4a6787f1b70aec Mon Sep 17 00:00:00 2001 From: Divesh Pahuja Date: Wed, 13 Sep 2023 15:05:40 +0200 Subject: [PATCH 1/4] [Mutation] Fix CreateAsset mutation not considering required field filename - resolves #791 --- src/GraphQL/Mutation/MutationType.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/GraphQL/Mutation/MutationType.php b/src/GraphQL/Mutation/MutationType.php index 570567d8..dab5a5d7 100644 --- a/src/GraphQL/Mutation/MutationType.php +++ b/src/GraphQL/Mutation/MutationType.php @@ -990,11 +990,13 @@ public function buildCreateAssetMutation(&$config, $context) } $type = $args['type']; + $filename = $args['filename']; $className = 'Pimcore\\Model\\Asset\\' . ucfirst($type); - /** @var Concrete $newInstance */ + /** @var Asset $newInstance */ $newInstance = new $className(); $newInstance->setParentId($parent->getId()); + $newInstance->setFilename($filename); $tags = []; if (isset($args['input'])) { @@ -1029,7 +1031,7 @@ public function buildCreateAssetMutation(&$config, $context) } catch (\Exception $e) { return [ 'success' => false, - 'message' => 'saving failed' + 'message' => 'saving failed: ' . $e->getMessage() ]; } From 7979d02835c2e812c208d192324ef0119aaaf5ea Mon Sep 17 00:00:00 2001 From: Divesh Pahuja Date: Wed, 13 Sep 2023 15:22:57 +0200 Subject: [PATCH 2/4] [Mutation] Add assets docs --- .../07_Mutation/04_Asset_Mutations.md | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/doc/10_GraphQL/07_Mutation/04_Asset_Mutations.md b/doc/10_GraphQL/07_Mutation/04_Asset_Mutations.md index cde0af27..4777be98 100644 --- a/doc/10_GraphQL/07_Mutation/04_Asset_Mutations.md +++ b/doc/10_GraphQL/07_Mutation/04_Asset_Mutations.md @@ -2,13 +2,27 @@ ## Create Asset -TODO add sample +This will create an Asset with uploading data provided: +```graphql +mutation { + createAsset( + parentId: 1, + filename: "foo.png", + type: "image", + input: { + data: "ewogICAgImZpZWxkY29sbGVjdGlvbiI6IFsKICAgICAgICB7CiAgICAgICAgICAgICJwYXJlbn...." + }) { + success + message + } +} +``` ## Update Asset This will rename the Asset and update the data. Request: -``` +```graphql mutation { updateAsset(id: 76, input: {filename:"newfilename", data:"iVBORw0KGgoAAAANSUhEUg....."}) { @@ -22,5 +36,21 @@ mutation { ``` ## Delete Asset +```graphql +mutation { + deleteAsset(id: 533) { + success + message + } +} + +or + +mutation { + deleteAsset(fullpath: "/Sample Content/Background Images/foo.png") { + success + message + } +} -TODO add sample +``` \ No newline at end of file From c70757cacb9dfb443b33bf38502ebc0e73f55062 Mon Sep 17 00:00:00 2001 From: Divesh Pahuja Date: Wed, 13 Sep 2023 15:23:38 +0200 Subject: [PATCH 3/4] [Mutation] Fix asset delete by id or fullpath --- src/GraphQL/Mutation/MutationType.php | 49 +++++++++++++++------------ 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/src/GraphQL/Mutation/MutationType.php b/src/GraphQL/Mutation/MutationType.php index dab5a5d7..1a7bdc83 100644 --- a/src/GraphQL/Mutation/MutationType.php +++ b/src/GraphQL/Mutation/MutationType.php @@ -1382,31 +1382,38 @@ public function buildDeleteElementMutation(&$config, $context, $type) 'args' => [ 'id' => ['type' => Type::int()], 'fullpath' => ['type' => Type::string()], - ], 'resolve' => static function ($value, $args, $context, ResolveInfo $info) use ($type, $omitPermissionCheck, $me) { - try { - $id = $args['id']; - /** @var Configuration $configuration */ - $configuration = $context['configuration']; - $element = $me->getElementByTypeAndIdOrPath($args, $type); + ], + 'resolve' => static function ($value, $args) + use ($type, $omitPermissionCheck, $me) { + try { + $idOrPath = $args['id'] ?? ($args['fullpath'] ?? null); + if (!$idOrPath) { + return [ + 'success' => false, + 'message' => 'Missing required field id or fullpath to delete the asset.' + ]; + } - if (!$omitPermissionCheck && !WorkspaceHelper::checkPermission($element, 'delete')) { - return [ - 'success' => false, - 'message' => 'delete ' . $type . ' permission denied.' + $element = $me->getElementByTypeAndIdOrPath($args, $type); + + if (!$omitPermissionCheck && !WorkspaceHelper::checkPermission($element, 'delete')) { + return [ + 'success' => false, + 'message' => 'delete ' . $type . ' permission denied.' + ]; + } + $result = ['success' => false]; + $element->delete(); + + $result = [ + 'success' => true, + 'message' => $type . ' ' . $idOrPath . ' deleted' ]; + } catch (\Exception $e) { + $result['message'] = $e->getMessage(); } - $element->delete(); - return [ - 'success' => true, - 'message' => $type . ' ' . $id . ' deleted' - ]; - } catch (\Exception $e) { - return [ - 'success' => false, - 'message' => $e->getMessage() - ]; - } + return $result; } ]; From f5044af4e1aa4826502c3f75eb931c26d1ecf97a Mon Sep 17 00:00:00 2001 From: dvesh3 Date: Wed, 13 Sep 2023 13:24:05 +0000 Subject: [PATCH 4/4] Apply php-cs-fixer changes --- src/GraphQL/Mutation/MutationType.php | 35 +++++++++++++-------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/GraphQL/Mutation/MutationType.php b/src/GraphQL/Mutation/MutationType.php index 1a7bdc83..bd919d0a 100644 --- a/src/GraphQL/Mutation/MutationType.php +++ b/src/GraphQL/Mutation/MutationType.php @@ -1383,37 +1383,36 @@ public function buildDeleteElementMutation(&$config, $context, $type) 'id' => ['type' => Type::int()], 'fullpath' => ['type' => Type::string()], ], - 'resolve' => static function ($value, $args) - use ($type, $omitPermissionCheck, $me) { - try { - $idOrPath = $args['id'] ?? ($args['fullpath'] ?? null); - if (!$idOrPath) { - return [ + 'resolve' => static function ($value, $args) use ($type, $omitPermissionCheck, $me) { + try { + $idOrPath = $args['id'] ?? ($args['fullpath'] ?? null); + if (!$idOrPath) { + return [ 'success' => false, 'message' => 'Missing required field id or fullpath to delete the asset.' ]; - } + } - $element = $me->getElementByTypeAndIdOrPath($args, $type); + $element = $me->getElementByTypeAndIdOrPath($args, $type); - if (!$omitPermissionCheck && !WorkspaceHelper::checkPermission($element, 'delete')) { - return [ + if (!$omitPermissionCheck && !WorkspaceHelper::checkPermission($element, 'delete')) { + return [ 'success' => false, 'message' => 'delete ' . $type . ' permission denied.' ]; - } - $result = ['success' => false]; - $element->delete(); + } + $result = ['success' => false]; + $element->delete(); - $result = [ + $result = [ 'success' => true, 'message' => $type . ' ' . $idOrPath . ' deleted' ]; - } catch (\Exception $e) { - $result['message'] = $e->getMessage(); - } + } catch (\Exception $e) { + $result['message'] = $e->getMessage(); + } - return $result; + return $result; } ];