View source
<?php
namespace Drupal\social_comment\Plugin\GraphQL\SchemaExtension;
use Drupal\graphql\GraphQL\ResolverBuilder;
use Drupal\graphql\GraphQL\ResolverRegistryInterface;
use Drupal\social_graphql\Plugin\GraphQL\SchemaExtension\SchemaExtensionPluginBase;
class CommentSchemaExtension extends SchemaExtensionPluginBase {
public function registerResolvers(ResolverRegistryInterface $registry) {
$builder = new ResolverBuilder();
$this
->addQueryFields($registry, $builder);
$this
->addCommentFields($registry, $builder);
}
protected function addCommentFields(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
$registry
->addFieldResolver('Comment', 'id', $builder
->produce('entity_uuid')
->map('entity', $builder
->fromParent()));
$registry
->addFieldResolver('Comment', 'author', $builder
->produce('entity_owner')
->map('entity', $builder
->fromParent()));
$registry
->addFieldResolver('Comment', 'bodyHtml', $builder
->compose($builder
->produce('field')
->map('entity', $builder
->fromParent())
->map('field', $builder
->fromValue('field_comment_body')), $builder
->produce('field_renderer')
->map('field', $builder
->fromParent())));
$registry
->addFieldResolver('Comment', 'created', $builder
->fromPath('entity:comment', 'created.value'));
}
protected function addQueryFields(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
$registry
->addFieldResolver('Query', 'comments', $builder
->produce('query_comments')
->map('after', $builder
->fromArgument('after'))
->map('before', $builder
->fromArgument('before'))
->map('first', $builder
->fromArgument('first'))
->map('last', $builder
->fromArgument('last'))
->map('reverse', $builder
->fromArgument('reverse'))
->map('sortKey', $builder
->fromArgument('sortKey')));
$registry
->addFieldResolver('Query', 'comment', $builder
->produce('entity_load_by_uuid')
->map('type', $builder
->fromValue('comment'))
->map('bundles', $builder
->fromValue([
'comment',
]))
->map('uuid', $builder
->fromArgument('id')));
}
}