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

GraphQL: Added liveblogPosts to NodeLiveblog #108

Open
wants to merge 1 commit into
base: 8.x-1.x
Choose a base branch
from
Open
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
72 changes: 72 additions & 0 deletions src/Plugin/GraphQL/Fields/LiveblogPosts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
namespace Drupal\liveblog\Plugin\GraphQL\Fields;

use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\graphql\Annotation\GraphQLField;
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
use Drupal\node\Entity\Node;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Youshido\GraphQL\Execution\ResolveInfo;

/**
* Class LiveblogPosts
*
* @GraphQLField(
* id = "liveblog_posts",
* type = "LiveblogPost",
* name = "liveblogPosts",
* nullable = true,
* multi = true,
* parents = {"NodeLiveblog"}
* )
*/
class LiveblogPosts extends FieldPluginBase implements ContainerFactoryPluginInterface {
use DependencySerializationTrait;

/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
parent::__construct($configuration, $pluginId, $pluginDefinition);
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
return new static(
$configuration,
$pluginId,
$pluginDefinition,
$container->get('entity_type.manager')
);
}

/**
* {@inheritdoc}
*/
protected function resolveValues($value, array $args, ResolveInfo $info) {
if ($value instanceof Node && $value->bundle() == 'liveblog') {
$storage = $this->entityTypeManager->getStorage('liveblog_post');

$liveblog_posts = $storage->loadByProperties([
'liveblog' => $value->id(),
]);

foreach ($liveblog_posts as $post) {
yield $post;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for yield

}
}
}

}