|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\graphql\Controller; |
| 4 | + |
| 5 | +use Drupal\Core\DependencyInjection\ContainerInjectionInterface; |
| 6 | +use Drupal\Core\StringTranslation\StringTranslationTrait; |
| 7 | +use Drupal\graphql\Entity\ServerInterface; |
| 8 | +use Drupal\graphql\GraphQL\ValidatorInterface; |
| 9 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 10 | + |
| 11 | +/** |
| 12 | + * Controller for the GraphiQL resolver validation. |
| 13 | + */ |
| 14 | +class ValidationController implements ContainerInjectionInterface { |
| 15 | + use StringTranslationTrait; |
| 16 | + |
| 17 | + /** |
| 18 | + * The schema plugin manager. |
| 19 | + * |
| 20 | + * @var \Drupal\graphql\GraphQL\ValidatorInterface |
| 21 | + */ |
| 22 | + protected $validator; |
| 23 | + |
| 24 | + /** |
| 25 | + * {@inheritdoc} |
| 26 | + */ |
| 27 | + public static function create(ContainerInterface $container) : self { |
| 28 | + return new static( |
| 29 | + $container->get('graphql.validator') |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * ValidateResolverController constructor. |
| 35 | + * |
| 36 | + * @param \Drupal\graphql\GraphQL\ValidatorInterface $validator |
| 37 | + * The GraphQL validator. |
| 38 | + */ |
| 39 | + public function __construct(ValidatorInterface $validator) { |
| 40 | + $this->validator = $validator; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Controller for the GraphiQL query builder IDE. |
| 45 | + * |
| 46 | + * @param \Drupal\graphql\Entity\ServerInterface $graphql_server |
| 47 | + * The GraphQL server entity. |
| 48 | + * |
| 49 | + * @return array |
| 50 | + * The render array. |
| 51 | + */ |
| 52 | + public function report(ServerInterface $graphql_server) { |
| 53 | + $build = [ |
| 54 | + 'validation' => [ |
| 55 | + '#type' => 'table', |
| 56 | + '#caption' => $this->t("Validation errors"), |
| 57 | + '#header' => [$this->t('Type'), $this->t('Field'), $this->t('Message')], |
| 58 | + '#empty' => $this->t("No validation errors."), |
| 59 | + ], |
| 60 | + 'orphaned' => [ |
| 61 | + '#type' => 'table', |
| 62 | + '#caption' => $this->t("Resolvers without schema"), |
| 63 | + '#header' => [$this->t('Type'), $this->t('Fields')], |
| 64 | + '#empty' => $this->t("No orphaned resolvers."), |
| 65 | + ], |
| 66 | + 'missing' => [ |
| 67 | + '#type' => 'table', |
| 68 | + '#caption' => $this->t("Fields without resolvers"), |
| 69 | + '#header' => [$this->t('Type'), $this->t('Fields')], |
| 70 | + '#empty' => $this->t("No missing resolvers."), |
| 71 | + ], |
| 72 | + ]; |
| 73 | + |
| 74 | + foreach ($this->validator->validateSchema($graphql_server) as $error) { |
| 75 | + $type = ''; |
| 76 | + if (isset($error->nodes[1]) && property_exists($error->nodes[1], 'name')) { |
| 77 | + $type = $error->nodes[1]->name->value; |
| 78 | + } |
| 79 | + $field = ''; |
| 80 | + if (isset($error->nodes[0]) && property_exists($error->nodes[0], 'name')) { |
| 81 | + $field = $error->nodes[0]->name->value; |
| 82 | + } |
| 83 | + |
| 84 | + $build['validation'][] = [ |
| 85 | + 'type' => ['#plain_text' => $type], |
| 86 | + 'field' => ['#plain_text' => $field], |
| 87 | + 'message' => ['#plain_text' => $error->getMessage()], |
| 88 | + ]; |
| 89 | + } |
| 90 | + |
| 91 | + // @todo Ability to configure ignores here. |
| 92 | + $metrics = [ |
| 93 | + 'orphaned' => $this->validator->getOrphanedResolvers($graphql_server), |
| 94 | + 'missing' => $this->validator->getMissingResolvers($graphql_server), |
| 95 | + ]; |
| 96 | + |
| 97 | + foreach ($metrics as $metric_type => $data) { |
| 98 | + foreach ($data as $type => $fields) { |
| 99 | + $build[$metric_type][$type] = [ |
| 100 | + 'type' => ['#plain_text' => $type], |
| 101 | + 'fields' => [ |
| 102 | + '#theme' => 'item_list', |
| 103 | + '#items' => $fields, |
| 104 | + ], |
| 105 | + ]; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return $build; |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments