Validations
Last updated
Was this helpful?
Was this helpful?
...
extend type Mutation {
createArticle(data: ArticleInput): ArticleResponse
}
...
<?php
declare(strict_types = 1);
namespace Drupal\graphql_composable\Wrappers\Response;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\graphql\GraphQL\Response\Response;
/**
* Type of response used when an article is returned.
*/
class ArticleResponse extends Response {
/**
* The article to be served.
*
* @var \Drupal\Core\Entity\ContentEntityInterface|null
*/
protected $article;
/**
* Sets the content.
*
* @param \Drupal\Core\Entity\ContentEntityInterface|null $article
* The article to be served.
*/
public function setArticle(?ContentEntityInterface $article): void {
$this->article = $article;
}
/**
* Gets the article to be served.
*
* @return \Drupal\Core\Entity\ContentEntityInterface|null
* The article to be served.
*/
public function article(): ?ContentEntityInterface {
return $this->article;
}
}<?php
namespace Drupal\graphql_composable\Plugin\GraphQL\DataProducer;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\graphql\Attribute\DataProducer;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
use Drupal\graphql_composable\GraphQL\Response\ArticleResponse;
use Drupal\node\Entity\Node;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Creates a new article entity.
*/
#[DataProducer(
id: 'graphql_docs_create_article',
name: new TranslatableMarkup('Create Article'),
description: new TranslatableMarkup('Creates a new article.'),
produces: new ContextDefinition(
data_type: 'any',
label: new TranslatableMarkup('Article'),
),
consumes: [
'data' => new ContextDefinition(
data_type: 'any',
label: new TranslatableMarkup('Article data'),
),
],
)]
class CreateArticle extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
/**
* The current user.
*/
protected AccountInterface $currentUser;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('current_user')
);
}
/**
* CreateArticle constructor.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*/
public function __construct(array $configuration, string $plugin_id, array $plugin_definition, AccountInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currentUser = $current_user;
}
/**
* Creates an article.
*
* @param array $data
* The title of the job.
*
* @return \Drupal\graphql_composable\GraphQL\Response\ArticleResponse
* The newly created article.
*
* @throws \Exception
*/
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;
}
}<?php
declare(strict_types=1);
namespace Drupal\graphql_composable\Plugin\GraphQL\DataProducer;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\graphql\Attribute\DataProducer;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
use Drupal\graphql_composable\GraphQL\Response\ArticleResponse;
/**
* Returns the article held on an ArticleResponse.
*/
#[DataProducer(
id: 'graphql_docs_article_response_article',
name: new TranslatableMarkup('Article Response Article'),
description: new TranslatableMarkup('Get the article from an ArticleResponse.'),
produces: new ContextDefinition(
data_type: 'any',
label: new TranslatableMarkup('Article'),
),
consumes: [
'response' => new ContextDefinition(
data_type: 'any',
label: new TranslatableMarkup('ArticleResponse'),
),
],
)]
class ArticleResponseArticle extends DataProducerPluginBase {
/**
* {@inheritdoc}
*/
public function resolve(ArticleResponse $response): ?ContentEntityInterface {
return $response->article();
}
}<?php
declare(strict_types=1);
namespace Drupal\graphql_composable\Plugin\GraphQL\DataProducer;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\graphql\Attribute\DataProducer;
use Drupal\graphql\GraphQL\Response\Response;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
/**
* Returns violation messages from a Response.
*/
#[DataProducer(
id: 'graphql_docs_response_violations',
name: new TranslatableMarkup('Response Violations'),
description: new TranslatableMarkup('Get the violations from a Response.'),
produces: new ContextDefinition(
data_type: 'any',
label: new TranslatableMarkup('Violations'),
),
consumes: [
'response' => new ContextDefinition(
data_type: 'any',
label: new TranslatableMarkup('Response'),
),
],
)]
class ResponseViolations extends DataProducerPluginBase {
/**
* {@inheritdoc}
*/
public function resolve(Response $response): array {
return $response->getViolations();
}
}/**
* {@inheritdoc}
*/
public function registerResolvers(ResolverRegistryInterface $registry) {
...
$registry->addFieldResolver('ArticleResponse', 'article',
$builder->produce('graphql_docs_article_response_article')
->map('response', $builder->fromParent())
);
$registry->addFieldResolver('ArticleResponse', 'errors',
$builder->produce('graphql_docs_response_violations')
->map('response', $builder->fromParent())
);
...
return $registry;
}{
"data": {
"createArticle": {
"errors": [
{
"message": "You do not have permissions to create articles."
}
],
"article": null
}
}
}