You are here

public function DeleteEntityBase::resolve in GraphQL 8.3

File

modules/graphql_core/src/Plugin/GraphQL/Mutations/Entity/DeleteEntityBase.php, line 72

Class

DeleteEntityBase

Namespace

Drupal\graphql_core\Plugin\GraphQL\Mutations\Entity

Code

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'];
    $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 entity could not be loaded.'),
      ]);
    }
    if (!$entity
      ->access('delete')) {
      return new EntityCrudOutputWrapper(NULL, NULL, [
        $this
          ->t('You do not have the necessary permissions to delete this entity.'),
      ]);
    }
    try {
      $entity
        ->delete();
    } catch (EntityStorageException $exception) {
      return new EntityCrudOutputWrapper(NULL, NULL, [
        $this
          ->t('Entity deletion failed with exception: @exception.', [
          '@exception' => $exception
            ->getMessage(),
        ]),
      ]);
    }
    return new EntityCrudOutputWrapper($entity);
  });
}