You are here

class ExampleSchema in GraphQL 8.4

Plugin annotation


@Schema(
  id = "example",
  name = "Example schema"
)

Hierarchy

Expanded class hierarchy of ExampleSchema

File

examples/graphql_example/src/Plugin/GraphQL/Schema/ExampleSchema.php, line 16

Namespace

Drupal\graphql_examples\Plugin\GraphQL\Schema
View source
class ExampleSchema extends SdlSchemaPluginBase {

  /**
   * {@inheritdoc}
   */
  public function getResolverRegistry() {
    $builder = new ResolverBuilder();
    $registry = new ResolverRegistry();
    $this
      ->addQueryFields($registry, $builder);
    $this
      ->addArticleFields($registry, $builder);

    // Re-usable connection type fields.
    $this
      ->addConnectionFields('ArticleConnection', $registry, $builder);
    return $registry;
  }

  /**
   * @param \Drupal\graphql\GraphQL\ResolverRegistry $registry
   * @param \Drupal\graphql\GraphQL\ResolverBuilder $builder
   */
  protected function addArticleFields(ResolverRegistry $registry, ResolverBuilder $builder) : void {
    $registry
      ->addFieldResolver('Article', 'id', $builder
      ->produce('entity_id')
      ->map('entity', $builder
      ->fromParent()));
    $registry
      ->addFieldResolver('Article', 'title', $builder
      ->compose($builder
      ->produce('entity_label')
      ->map('entity', $builder
      ->fromParent()), $builder
      ->produce('uppercase')
      ->map('string', $builder
      ->fromParent())));
    $registry
      ->addFieldResolver('Article', 'author', $builder
      ->compose($builder
      ->produce('entity_owner')
      ->map('entity', $builder
      ->fromParent()), $builder
      ->produce('entity_label')
      ->map('entity', $builder
      ->fromParent())));
  }

  /**
   * @param \Drupal\graphql\GraphQL\ResolverRegistry $registry
   * @param \Drupal\graphql\GraphQL\ResolverBuilder $builder
   */
  protected function addQueryFields(ResolverRegistry $registry, ResolverBuilder $builder) : void {
    $registry
      ->addFieldResolver('Query', 'article', $builder
      ->produce('entity_load')
      ->map('type', $builder
      ->fromValue('node'))
      ->map('bundles', $builder
      ->fromValue([
      'article',
    ]))
      ->map('id', $builder
      ->fromArgument('id')));
    $registry
      ->addFieldResolver('Query', 'articles', $builder
      ->produce('query_articles')
      ->map('offset', $builder
      ->fromArgument('offset'))
      ->map('limit', $builder
      ->fromArgument('limit')));
  }

  /**
   * @param string $type
   * @param \Drupal\graphql\GraphQL\ResolverRegistry $registry
   * @param \Drupal\graphql\GraphQL\ResolverBuilder $builder
   */
  protected function addConnectionFields($type, ResolverRegistry $registry, ResolverBuilder $builder) : void {
    $registry
      ->addFieldResolver($type, 'total', $builder
      ->callback(function (QueryConnection $connection) {
      return $connection
        ->total();
    }));
    $registry
      ->addFieldResolver($type, 'items', $builder
      ->callback(function (QueryConnection $connection) {
      return $connection
        ->items();
    }));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableDependencyTrait::$cacheContexts protected property Cache contexts.
CacheableDependencyTrait::$cacheMaxAge protected property Cache max-age.
CacheableDependencyTrait::$cacheTags protected property Cache tags.
CacheableDependencyTrait::getCacheContexts public function 3
CacheableDependencyTrait::getCacheMaxAge public function 3
CacheableDependencyTrait::getCacheTags public function 3
CacheableDependencyTrait::setCacheability protected function Sets cacheability; useful for value object constructors.
ExampleSchema::addArticleFields protected function
ExampleSchema::addConnectionFields protected function
ExampleSchema::addQueryFields protected function
ExampleSchema::getResolverRegistry public function Retrieves the resolver registry. Overrides SchemaPluginInterface::getResolverRegistry
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
RefinableCacheableDependencyTrait::addCacheableDependency public function 1
RefinableCacheableDependencyTrait::addCacheContexts public function
RefinableCacheableDependencyTrait::addCacheTags public function
RefinableCacheableDependencyTrait::mergeCacheMaxAge public function
SdlSchemaPluginBase::$astCache protected property The cache bin for caching the parsed SDL.
SdlSchemaPluginBase::$extensionManager protected property The schema extension plugin manager.
SdlSchemaPluginBase::$inDevelopment protected property Whether the system is currently in development mode.
SdlSchemaPluginBase::$moduleHandler protected property The module handler service.
SdlSchemaPluginBase::create public static function @codeCoverageIgnore Overrides ContainerFactoryPluginInterface::create
SdlSchemaPluginBase::getExtensionDocument protected function Retrieves the parsed AST of the schema extension definitions.
SdlSchemaPluginBase::getExtensions protected function 1
SdlSchemaPluginBase::getSchema public function Overrides SchemaPluginInterface::getSchema
SdlSchemaPluginBase::getSchemaDefinition protected function Retrieves the raw schema definition string. 1
SdlSchemaPluginBase::getSchemaDocument protected function Retrieves the parsed AST of the schema definition.
SdlSchemaPluginBase::__construct public function SdlSchemaPluginBase constructor. Overrides PluginBase::__construct