You are here

class CommentSchemaExtension in Open Social 10.2.x

Same name and namespace in other branches
  1. 10.3.x modules/social_features/social_comment/src/Plugin/GraphQL/SchemaExtension/CommentSchemaExtension.php \Drupal\social_comment\Plugin\GraphQL\SchemaExtension\CommentSchemaExtension

Adds comment data to the Open Social GraphQL API.

Plugin annotation


@SchemaExtension(
  id = "social_comment_schema_extension",
  name = "Open Social - Comment Schema Extension",
  description = "GraphQL schema extension for Open Social comment data.",
  schema = "open_social"
)

Hierarchy

Expanded class hierarchy of CommentSchemaExtension

File

modules/social_features/social_comment/src/Plugin/GraphQL/SchemaExtension/CommentSchemaExtension.php, line 19

Namespace

Drupal\social_comment\Plugin\GraphQL\SchemaExtension
View source
class CommentSchemaExtension extends SchemaExtensionPluginBase {

  /**
   * {@inheritdoc}
   */
  public function registerResolvers(ResolverRegistryInterface $registry) {
    $builder = new ResolverBuilder();
    $this
      ->addQueryFields($registry, $builder);
    $this
      ->addCommentFields($registry, $builder);
  }

  /**
   * Registers type and field resolvers in the shared registry.
   *
   * @param \Drupal\graphql\GraphQL\ResolverRegistryInterface $registry
   *   The resolver registry.
   * @param \Drupal\graphql\GraphQL\ResolverBuilder $builder
   *   The resolver 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'));
  }

  /**
   * Registers type and field resolvers in the query type.
   *
   * @param \Drupal\graphql\GraphQL\ResolverRegistryInterface $registry
   *   The resolver registry.
   * @param \Drupal\graphql\GraphQL\ResolverBuilder $builder
   *   The resolver builder.
   */
  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')));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommentSchemaExtension::addCommentFields protected function Registers type and field resolvers in the shared registry.
CommentSchemaExtension::addQueryFields protected function Registers type and field resolvers in the query type.
CommentSchemaExtension::registerResolvers public function
StandardisedMutationSchemaTrait::registerMutationResolver protected function Add a field resolver for a mutation field.