You are here

class TypeResolver in GraphQL 8

Same name and namespace in other branches
  1. 8.2 src/TypeResolver.php \Drupal\graphql\TypeResolver

Attempts to GraphQL type definitions from arbitrary data types.

Hierarchy

Expanded class hierarchy of TypeResolver

1 string reference to 'TypeResolver'
graphql.services.yml in ./graphql.services.yml
graphql.services.yml
1 service uses TypeResolver
graphql.type_resolver in ./graphql.services.yml
Drupal\graphql\TypeResolver

File

src/TypeResolver.php, line 8

Namespace

Drupal\graphql
View source
class TypeResolver implements TypeResolverInterface {

  /**
   * Unsorted list of type resolvers nested and keyed by priority.
   *
   * @var array
   */
  protected $resolvers;

  /**
   * Sorted list of type resolvers.
   *
   * @var array
   */
  protected $sortedResolvers;

  /**
   * {@inheritdoc}
   */
  public function applies($type) {
    return TRUE;
  }

  /**
   * Adds a active theme negotiation service.
   *
   * @param \Drupal\graphql\TypeResolverInterface $resolver
   *   The type resolver to add.
   * @param int $priority
   *   Priority of the type resolver.
   */
  public function addTypeResolver(TypeResolverInterface $resolver, $priority = 0) {
    $this->resolvers[$priority][] = $resolver;
    $this->sortedResolvers = NULL;
  }

  /**
   * Returns the first applicable provider for the given type definition.
   *
   * @param mixed $type
   *
   * @return \Drupal\graphql\TypeResolverInterface|null
   */
  protected function getFirstApplicableTypeResolver($type) {
    foreach ($this
      ->getSortedResolvers() as $resolver) {
      if ($resolver
        ->applies($type)) {
        return $resolver;
      }
    }
    return NULL;
  }

  /**
   * Returns the sorted array of type resolvers.
   *
   * @return \Drupal\graphql\TypeResolverInterface[]
   *   An array of type resolver objects.
   */
  protected function getSortedResolvers() {
    if (!isset($this->sortedResolvers)) {
      krsort($this->resolvers);
      $this->sortedResolvers = [];
      foreach ($this->resolvers as $resolvers) {
        $this->sortedResolvers = array_merge($this->sortedResolvers, $resolvers);
      }
    }
    return $this->sortedResolvers;
  }

  /**
   * {@inheritdoc}
   */
  public function resolveRecursive($type) {
    if ($resolver = $this
      ->getFirstApplicableTypeResolver($type)) {
      return $resolver
        ->resolveRecursive($type);
    }
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TypeResolver::$resolvers protected property Unsorted list of type resolvers nested and keyed by priority.
TypeResolver::$sortedResolvers protected property Sorted list of type resolvers.
TypeResolver::addTypeResolver public function Adds a active theme negotiation service.
TypeResolver::applies public function Overrides TypeResolverInterface::applies
TypeResolver::getFirstApplicableTypeResolver protected function Returns the first applicable provider for the given type definition.
TypeResolver::getSortedResolvers protected function Returns the sorted array of type resolvers.
TypeResolver::resolveRecursive public function Overrides TypeResolverInterface::resolveRecursive