You are here

SchemaProvider.php in GraphQL 8

Same filename and directory in other branches
  1. 8.2 src/SchemaProvider.php

Namespace

Drupal\graphql

File

src/SchemaProvider.php
View source
<?php

namespace Drupal\graphql;


/**
 * Generates a GraphQL Schema.
 */
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;
  }

}

Classes

Namesort descending Description
SchemaProvider Generates a GraphQL Schema.