You are here

class SchemaProvider in GraphQL 8

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

Generates a GraphQL Schema.

Hierarchy

Expanded class hierarchy of SchemaProvider

1 string reference to 'SchemaProvider'
graphql.services.yml in ./graphql.services.yml
graphql.services.yml
1 service uses SchemaProvider
graphql.schema_provider in ./graphql.services.yml
Drupal\graphql\SchemaProvider

File

src/SchemaProvider.php, line 8

Namespace

Drupal\graphql
View source
class SchemaProvider implements SchemaProviderInterface {

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

  /**
   * Sorted list of schema providers.
   *
   * @var array
   */
  protected $sortedProviders;

  /**
   * {@inheritdoc}
   */
  public function getQuerySchema() {
    return array_reduce($this
      ->getSortedProviders(), function ($carry, SchemaProviderInterface $provider) {
      return array_merge($carry, $provider
        ->getQuerySchema() ?: []);
    }, []);
  }

  /**
   * {@inheritdoc}
   */
  public function getMutationSchema() {
    return array_reduce($this
      ->getSortedProviders(), function ($carry, SchemaProviderInterface $provider) {
      return array_merge($carry, $provider
        ->getMutationSchema() ?: []);
    }, []);
  }

  /**
   * Adds a active theme negotiation service.
   *
   * @param \Drupal\graphql\SchemaProviderInterface $provider
   *   The schema provider to add.
   * @param int $priority
   *   Priority of the schema provider.
   */
  public function addSchemaProvider(SchemaProviderInterface $provider, $priority = 0) {
    $this->providers[$priority][] = $provider;
    $this->sortedProviders = NULL;
  }

  /**
   * Returns the sorted array of schema providers.
   *
   * @return \Drupal\graphql\SchemaProviderInterface[]
   *   An array of schema provider objects.
   */
  protected function getSortedProviders() {
    if (!isset($this->sortedProviders)) {
      krsort($this->providers);
      $this->sortedProviders = [];
      foreach ($this->providers as $providers) {
        $this->sortedProviders = array_merge($this->sortedProviders, $providers);
      }
    }
    return $this->sortedProviders;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SchemaProvider::$providers protected property Unsorted list of schema providers nested and keyed by priority.
SchemaProvider::$sortedProviders protected property Sorted list of schema providers.
SchemaProvider::addSchemaProvider public function Adds a active theme negotiation service.
SchemaProvider::getMutationSchema public function Overrides SchemaProviderInterface::getMutationSchema
SchemaProvider::getQuerySchema public function Overrides SchemaProviderInterface::getQuerySchema
SchemaProvider::getSortedProviders protected function Returns the sorted array of schema providers.