You are here

class ValidationController in GraphQL 8.4

Controller for the GraphiQL resolver validation.

Hierarchy

Expanded class hierarchy of ValidationController

File

src/Controller/ValidationController.php, line 14

Namespace

Drupal\graphql\Controller
View source
class ValidationController implements ContainerInjectionInterface {
  use StringTranslationTrait;

  /**
   * The schema plugin manager.
   *
   * @var \Drupal\graphql\GraphQL\ValidatorInterface
   */
  protected $validator;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) : self {
    return new static($container
      ->get('graphql.validator'));
  }

  /**
   * ValidateResolverController constructor.
   *
   * @param \Drupal\graphql\GraphQL\ValidatorInterface $validator
   *   The GraphQL validator.
   */
  public function __construct(ValidatorInterface $validator) {
    $this->validator = $validator;
  }

  /**
   * Controller for the GraphiQL query builder IDE.
   *
   * @param \Drupal\graphql\Entity\ServerInterface $graphql_server
   *   The GraphQL server entity.
   *
   * @return array
   *   The render array.
   */
  public function report(ServerInterface $graphql_server) {
    $build = [
      'validation' => [
        '#type' => 'table',
        '#caption' => $this
          ->t("Validation errors"),
        '#header' => [
          $this
            ->t('Type'),
          $this
            ->t('Field'),
          $this
            ->t('Message'),
        ],
        '#empty' => $this
          ->t("No validation errors."),
      ],
      'orphaned' => [
        '#type' => 'table',
        '#caption' => $this
          ->t("Resolvers without schema"),
        '#header' => [
          $this
            ->t('Type'),
          $this
            ->t('Fields'),
        ],
        '#empty' => $this
          ->t("No orphaned resolvers."),
      ],
      'missing' => [
        '#type' => 'table',
        '#caption' => $this
          ->t("Fields without resolvers"),
        '#header' => [
          $this
            ->t('Type'),
          $this
            ->t('Fields'),
        ],
        '#empty' => $this
          ->t("No missing resolvers."),
      ],
    ];
    foreach ($this->validator
      ->validateSchema($graphql_server) as $error) {
      $type = '';
      if (isset($error->nodes[1]) && property_exists($error->nodes[1], 'name')) {
        $type = $error->nodes[1]->name->value;
      }
      $field = '';
      if (isset($error->nodes[0]) && property_exists($error->nodes[0], 'name')) {
        $field = $error->nodes[0]->name->value;
      }
      $build['validation'][] = [
        'type' => [
          '#plain_text' => $type,
        ],
        'field' => [
          '#plain_text' => $field,
        ],
        'message' => [
          '#plain_text' => $error
            ->getMessage(),
        ],
      ];
    }

    // @todo Ability to configure ignores here.
    $metrics = [
      'orphaned' => $this->validator
        ->getOrphanedResolvers($graphql_server),
      'missing' => $this->validator
        ->getMissingResolvers($graphql_server),
    ];
    foreach ($metrics as $metric_type => $data) {
      foreach ($data as $type => $fields) {
        $build[$metric_type][$type] = [
          'type' => [
            '#plain_text' => $type,
          ],
          'fields' => [
            '#theme' => 'item_list',
            '#items' => $fields,
          ],
        ];
      }
    }
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
ValidationController::$validator protected property The schema plugin manager.
ValidationController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
ValidationController::report public function Controller for the GraphiQL query builder IDE.
ValidationController::__construct public function ValidateResolverController constructor.