Skip to content

Commit

Permalink
[Mutation] Fix CreateAsset mutation not considering required field fi…
Browse files Browse the repository at this point in the history
…ename (#798)

* [Mutation] Fix CreateAsset mutation not considering required field filename - resolves #791

* [Mutation] Add assets docs

* [Mutation] Fix asset delete by id or fullpath

* Apply php-cs-fixer changes

---------

Co-authored-by: dvesh3 <dvesh3@users.noreply.github.com>
  • Loading branch information
dvesh3 and dvesh3 committed Sep 14, 2023
1 parent 9a06238 commit 2475eea
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 20 deletions.
36 changes: 33 additions & 3 deletions doc/10_GraphQL/07_Mutation/04_Asset_Mutations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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....."}) {
Expand All @@ -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
```
42 changes: 25 additions & 17 deletions src/GraphQL/Mutation/MutationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down Expand Up @@ -1029,7 +1031,7 @@ public function buildCreateAssetMutation(&$config, $context)
} catch (\Exception $e) {
return [
'success' => false,
'message' => 'saving failed'
'message' => 'saving failed: ' . $e->getMessage()
];
}

Expand Down Expand Up @@ -1380,31 +1382,37 @@ 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) {
],
'resolve' => static function ($value, $args) use ($type, $omitPermissionCheck, $me) {
try {
$id = $args['id'];
/** @var Configuration $configuration */
$configuration = $context['configuration'];
$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);

if (!$omitPermissionCheck && !WorkspaceHelper::checkPermission($element, 'delete')) {
return [
'success' => false,
'message' => 'delete ' . $type . ' permission denied.'
];
'success' => false,
'message' => 'delete ' . $type . ' permission denied.'
];
}
$result = ['success' => false];
$element->delete();

return [
'success' => true,
'message' => $type . ' ' . $id . ' deleted'
];
$result = [
'success' => true,
'message' => $type . ' ' . $idOrPath . ' deleted'
];
} catch (\Exception $e) {
return [
'success' => false,
'message' => $e->getMessage()
];
$result['message'] = $e->getMessage();
}

return $result;
}
];

Expand Down

0 comments on commit 2475eea

Please sign in to comment.