You are here

class SchemaLoader in GraphQL 8

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

Loads and caches a generated GraphQL schema.

Hierarchy

Expanded class hierarchy of SchemaLoader

1 file declares its use of SchemaLoader
RequestController.php in src/Controller/RequestController.php
1 string reference to 'SchemaLoader'
graphql.services.yml in ./graphql.services.yml
graphql.services.yml
1 service uses SchemaLoader
graphql.schema_loader in ./graphql.services.yml
Drupal\graphql\SchemaLoader

File

src/SchemaLoader.php, line 14

Namespace

Drupal\graphql
View source
class SchemaLoader {

  /**
   * The schema provider service.
   *
   * @var \Drupal\graphql\SchemaProviderInterface
   */
  protected $schemaProvider;

  /**
   * The schema cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $schemaCache;

  /**
   * Constructs a SchemaLoader object.
   *
   * @param \Drupal\graphql\SchemaProviderInterface $schema_provider
   *   The schema provider service.
   * @param \Drupal\Core\Cache\CacheBackendInterface $schema_cache
   *   The schema cache backend.
   */
  public function __construct(SchemaProviderInterface $schema_provider, CacheBackendInterface $schema_cache) {
    $this->schemaProvider = $schema_provider;
    $this->schemaCache = $schema_cache;
  }

  /**
   * Loads and caches the generated schema.
   *
   * @param \Drupal\Core\Language\LanguageInterface $language
   *   The language for which to fetch the schema.
   *
   * @return \Fubhy\GraphQL\Schema The generated GraphQL schema.
   *   The generated GraphQL schema.
   */
  public function loadSchema(LanguageInterface $language) {
    if ($schema = $this->schemaCache
      ->get($language
      ->getId())) {
      return $schema->data;
    }
    $query = new ObjectType('Root', $this->schemaProvider
      ->getQuerySchema());
    $mutation = $this->schemaProvider
      ->getMutationSchema();
    $mutation = $mutation ? new ObjectType('Mutation', $mutation) : NULL;
    $schema = new Schema($query, $mutation);

    // Resolve the type map to get rid of any closures before serialization.
    $schema
      ->getTypeMap();

    // Cache the generated schema in the configured cache backend.
    $expire = Cache::PERMANENT;
    $tags = [
      'views',
      'entity_field_info',
      'entity_bundles',
    ];
    $cid = $language
      ->getId();
    $this->schemaCache
      ->set($cid, $schema, $expire, $tags);
    return $schema;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SchemaLoader::$schemaCache protected property The schema cache backend.
SchemaLoader::$schemaProvider protected property The schema provider service.
SchemaLoader::loadSchema public function Loads and caches the generated schema.
SchemaLoader::__construct public function Constructs a SchemaLoader object.