You are here

class Context in GraphQL 8.4

Resolves a context value with default value support.

Hierarchy

Expanded class hierarchy of Context

1 file declares its use of Context
ResolverBuilder.php in src/GraphQL/ResolverBuilder.php
1 string reference to 'Context'
ResolverBuilderTest::testFromContext in tests/src/Kernel/ResolverBuilderTest.php
@covers ::context @covers ::fromContext

File

src/GraphQL/Resolver/Context.php, line 13

Namespace

Drupal\graphql\GraphQL\Resolver
View source
class Context implements ResolverInterface {

  /**
   * Name of the context.
   *
   * @var mixed
   */
  protected $name;

  /**
   * An arbitrary default value in case the context is not set.
   *
   * @var mixed
   */
  protected $default;

  /**
   * Context constructor.
   *
   * @param string $name
   * @param mixed $default
   */
  public function __construct($name, $default = NULL) {
    $this->name = $name;
    $this->default = $default;
  }

  /**
   * {@inheritdoc}
   */
  public function resolve($value, $args, ResolveContext $context, ResolveInfo $info, FieldContext $field) {
    $output = $field
      ->getContextValue($this->name);
    if (!isset($output) && !$field
      ->hasContextValue($this->name)) {
      $output = $this->default;
    }
    if ($output instanceof CacheableDependencyInterface) {
      $context
        ->addCacheableDependency($output);
    }
    return $output;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Context::$default protected property An arbitrary default value in case the context is not set.
Context::$name protected property Name of the context.
Context::resolve public function Resolve values for the fields. Overrides ResolverInterface::resolve
Context::__construct public function Context constructor.