You are here

class ResolveContext in GraphQL 8.3

Same name and namespace in other branches
  1. 8.4 src/GraphQL/Execution/ResolveContext.php \Drupal\graphql\GraphQL\Execution\ResolveContext

Hierarchy

Expanded class hierarchy of ResolveContext

117 files declare their use of ResolveContext
Alias.php in modules/graphql_core/src/Plugin/GraphQL/Fields/Routing/InternalUrl/Alias.php
AvailableLanguages.php in modules/graphql_core/src/Plugin/GraphQL/Fields/Languages/AvailableLanguages.php
BackupCar.php in tests/modules/graphql_plugin_test/src/Plugin/GraphQL/Fields/BackupCar.php
Bike.php in tests/modules/graphql_plugin_test/src/Plugin/GraphQL/Types/Bike.php
BlocksByRegion.php in modules/graphql_core/src/Plugin/GraphQL/Fields/Blocks/BlocksByRegion.php

... See full list

File

src/GraphQL/Execution/ResolveContext.php, line 9

Namespace

Drupal\graphql\GraphQL\Execution
View source
class ResolveContext implements RefinableCacheableDependencyInterface {
  use RefinableCacheableDependencyTrait;

  /**
   * Read-only list of global values.
   *
   * @var array
   */
  protected $globals;

  /**
   * The context stack.
   *
   * @var array
   */
  protected $contexts = [];

  /**
   * Root context values that will apply if no more specific context is there.
   *
   * @var array
   */
  protected $rootContext = [];

  /**
   * ResolveContext constructor.
   *
   * @param array $globals
   *   List of global values to expose to field resolvers.
   * @param array $rootContext
   *   The root context values the query will be initialised with.
   */
  public function __construct(array $globals = [], $rootContext = []) {
    $this->globals = $globals;
    $this->rootContext = $rootContext;
  }

  /**
   * Get a contextual value for the current field.
   *
   * Allows field resolvers to inherit contextual values from their ancestors.
   *
   * @param string $name
   *   The name of the context.
   * @param \GraphQL\Type\Definition\ResolveInfo $info
   *   The resolve info object.
   * @param mixed $default
   *   An arbitrary default value in case the context is not set.
   *
   * @return mixed
   *   The current value of the given context or the given default value if the
   *   context wasn't set.
   */
  public function getContext($name, ResolveInfo $info, $default = NULL) {
    $operation = isset($info->operation->name->value) ? $info->operation->name->value : $info->operation->operation;
    $path = $info->path;
    do {
      $key = implode('.', $path);
      if (isset($this->contexts[$operation][$key]) && array_key_exists($name, $this->contexts[$operation][$key])) {
        return $this->contexts[$operation][$key][$name];
      }
      array_pop($path);
    } while (count($path));
    if (isset($this->rootContext[$name])) {
      return $this->rootContext[$name];
    }
    return $default;
  }

  /**
   * Sets a contextual value for the current field and its decendents.
   *
   * Allows field resolvers to set contextual values which can be inherited by
   * their descendents.
   *
   * @param string $name
   *   The name of the context.
   * @param $value
   *   The value of the context.
   * @param \GraphQL\Type\Definition\ResolveInfo $info
   *   The resolve info object.
   *
   * @return $this
   */
  public function setContext($name, $value, ResolveInfo $info) {
    $operation = isset($info->operation->name->value) ? $info->operation->name->value : $info->operation->operation;
    $key = implode('.', $info->path);
    $this->contexts[$operation][$key][$name] = $value;
    return $this;
  }

  /**
   * Retrieve a global/static parameter value.
   *
   * @param string $name
   *   The name of the global parameter.
   * @param mixed $default
   *   An arbitrary default value in case the context is not set.
   *
   * @return mixed|null
   *   The requested global parameter value or the given default value if the
   *   parameter is not set.
   */
  public function getGlobal($name, $default = NULL) {
    if (isset($this->globals[$name])) {
      return $this->globals[$name];
    }
    return $default;
  }

}

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.
RefinableCacheableDependencyTrait::addCacheableDependency public function 1
RefinableCacheableDependencyTrait::addCacheContexts public function
RefinableCacheableDependencyTrait::addCacheTags public function
RefinableCacheableDependencyTrait::mergeCacheMaxAge public function
ResolveContext::$contexts protected property The context stack.
ResolveContext::$globals protected property Read-only list of global values.
ResolveContext::$rootContext protected property Root context values that will apply if no more specific context is there.
ResolveContext::getContext public function Get a contextual value for the current field.
ResolveContext::getGlobal public function Retrieve a global/static parameter value.
ResolveContext::setContext public function Sets a contextual value for the current field and its decendents.
ResolveContext::__construct public function ResolveContext constructor.