public function UpdateEntityBase::resolve in GraphQL 8.3
File
- modules/
graphql_core/ src/ Plugin/ GraphQL/ Mutations/ Entity/ UpdateEntityBase.php, line 71
Class
Namespace
Drupal\graphql_core\Plugin\GraphQL\Mutations\EntityCode
public function resolve($value, array $args, ResolveContext $context, ResolveInfo $info) {
// There are cases where the Drupal entity API calls emit the cache metadata
// in the current render context. In such cases
// EarlyRenderingControllerWrapperSubscriber throws the leaked cache
// metadata exception. To avoid this, wrap the execution in its own render
// context.
return $this->renderer
->executeInRenderContext(new RenderContext(), function () use ($value, $args, $context, $info) {
$entityTypeId = $this->pluginDefinition['entity_type'];
$bundleName = $this->pluginDefinition['entity_bundle'];
$storage = $this->entityTypeManager
->getStorage($entityTypeId);
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
if (!($entity = $storage
->load($args['id']))) {
return new EntityCrudOutputWrapper(NULL, NULL, [
$this
->t('The requested @bundle could not be loaded.', [
'@bundle' => $bundleName,
]),
]);
}
if (!$entity
->bundle() === $bundleName) {
return new EntityCrudOutputWrapper(NULL, NULL, [
$this
->t('The requested entity is not of the expected type @bundle.', [
'@bundle' => $bundleName,
]),
]);
}
if (!$entity
->access('update')) {
return new EntityCrudOutputWrapper(NULL, NULL, [
$this
->t('You do not have the necessary permissions to update this @bundle.', [
'@bundle' => $bundleName,
]),
]);
}
// The raw input needs to be converted to use the proper field and property
// keys because we usually convert them to camel case when adding them to
// the schema. Allow the other implementations to control this easily.
$input = $this
->extractEntityInput($value, $args, $context, $info);
try {
foreach ($input as $key => $value) {
$entity
->get($key)
->setValue($value);
}
} catch (\InvalidArgumentException $exception) {
return new EntityCrudOutputWrapper(NULL, NULL, [
$this
->t('The entity update failed with exception: @exception.', [
'@exception' => $exception
->getMessage(),
]),
]);
}
if (($violations = $entity
->validate()) && $violations
->count()) {
return new EntityCrudOutputWrapper(NULL, $violations);
}
if (($status = $entity
->save()) && $status === SAVED_UPDATED) {
return new EntityCrudOutputWrapper($entity);
}
return NULL;
});
}