You are here

public function Validator::getMissingResolvers in GraphQL 8.4

Get a list of missing resolvers.

A resolver is considered missing if a field definition exists in the SDL (.graphqls) files for this server but the type or any of its implemented interfaces do not have a registered resolver in the server's resolver registry for the field.

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::getMissingResolvers

File

src/GraphQL/Validator.php, line 61

Class

Validator
GraphQL validation service.

Namespace

Drupal\graphql\GraphQL

Code

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

    // We only care about concrete fieldable types. Resolvers may be defined
    // for interfaces to be available for all implementing types, but only the
    // actual resolved types need resolvers for their fields.
    if (!$type instanceof ObjectType) {
      continue;
    }

    // Skip hidden/internal/introspection types since they're handled by
    // GraphQL itself.
    if (strpos($type->name, "__") === 0) {
      continue;
    }
    if (in_array($type->name, $ignore_types, TRUE)) {
      continue;
    }
    foreach ($type
      ->getFields() as $field) {
      if ($resolver_registry
        ->getFieldResolverWithInheritance($type, $field->name) === NULL) {
        if (!isset($missing_resolvers[$type->name])) {
          $missing_resolvers[$type->name] = [];
        }
        $missing_resolvers[$type->name][] = $field->name;
      }
    }
  }
  return $missing_resolvers;
}