You are here

CommentSchemaExtension.php in Open Social 10.2.x

File

modules/social_features/social_comment/src/Plugin/GraphQL/SchemaExtension/CommentSchemaExtension.php
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;

/**
 * Adds comment data to the Open Social GraphQL API.
 *
 * @SchemaExtension(
 *   id = "social_comment_schema_extension",
 *   name = "Open Social - Comment Schema Extension",
 *   description = "GraphQL schema extension for Open Social comment data.",
 *   schema = "open_social"
 * )
 */
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')));
  }

}

Classes

Namesort descending Description
CommentSchemaExtension Adds comment data to the Open Social GraphQL API.