You are here

public function CreateArticle::resolve in GraphQL 8.4

Creates an article.

Parameters

array $data: The submitted values for the article.

Return value

\Drupal\graphql_composable\GraphQL\Response\ArticleResponse The newly created article.

Throws

\Exception

File

examples/graphql_composable/src/Plugin/GraphQL/DataProducer/CreateArticle.php, line 78

Class

CreateArticle
Creates a new article entity.

Namespace

Drupal\graphql_composable\Plugin\GraphQL\DataProducer

Code

public function resolve(array $data) {
  $response = new ArticleResponse();
  if ($this->currentUser
    ->hasPermission("create article content")) {
    $values = [
      'type' => 'article',
      'title' => $data['title'],
      'body' => $data['description'],
    ];
    $node = Node::create($values);
    $node
      ->save();
    $response
      ->setArticle($node);
  }
  else {
    $response
      ->addViolation($this
      ->t('You do not have permissions to create articles.'));
  }
  return $response;
}