You are here

public function Validator::getOrphanedResolvers in GraphQL 8.4

Get a list of orphaned resolvers.

A resolver is considered orphaned if it's defined in the resolver registry for the server but the field does not occur in any SDL (.graphqls) files.

Parameters

\Drupal\graphql\Entity\ServerInterface $server: The server to validate.

array $ignore_types: Any types to ignore during validation.

Return value

array An array keyed by type containing arrays of field names.

Overrides ValidatorInterface::getOrphanedResolvers

File

src/GraphQL/Validator.php, line 119

Class

Validator
GraphQL validation service.

Namespace

Drupal\graphql\GraphQL

Code

public function getOrphanedResolvers(ServerInterface $server, array $ignore_types = []) : array {
  $plugin = $this
    ->getSchemaPlugin($server);
  $resolver_registry = $plugin
    ->getResolverRegistry();
  try {
    $schema = $plugin
      ->getSchema($resolver_registry);
  } catch (Error $e) {
    return [];
  }
  if (!method_exists($resolver_registry, "getAllFieldResolvers")) {
    $this->logger
      ->warning("Could not get orphaned resolvers for @server_name as its registry class (@klass) does not implement getAllFieldResolvers.", [
      '@server_name' => $server
        ->id(),
      '@klass' => get_class($resolver_registry),
    ]);
    return [];
  }
  $orphaned_resolvers = [];

  /**
   * @var string $type_name
   * @var array $fields
   */
  foreach ($resolver_registry
    ->getAllFieldResolvers() as $type_name => $fields) {
    if (in_array($type_name, $ignore_types, TRUE)) {
      continue;
    }
    try {
      $type = $schema
        ->getType($type_name);
    } catch (Error $_) {
      $type = NULL;
    }

    // If the type can't have any fields then our resolvers don't make sense.
    if (!$type instanceof InterfaceType && !$type instanceof ObjectType && !$type instanceof InputObjectType) {
      $orphaned_resolvers[$type_name] = array_keys($fields);
      continue;
    }
    foreach ($fields as $field_name => $resolver) {
      try {
        $type
          ->getField($field_name);
      } catch (InvariantViolation $_) {
        if (!isset($orphaned_resolvers[$type_name])) {
          $orphaned_resolvers[$type_name] = [];
        }
        $orphaned_resolvers[$type_name][] = $field_name;
      }
    }
  }
  return $orphaned_resolvers;
}