Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mutation] Fix CreateAsset mutation not considering required field fiename #798

Merged
merged 4 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading