You are here

class DefaultValue in GraphQL 8.4

Same name in this branch
  1. 8.4 src/GraphQL/Resolver/DefaultValue.php \Drupal\graphql\GraphQL\Resolver\DefaultValue
  2. 8.4 src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/DefaultValue.php \Drupal\graphql\Plugin\GraphQL\DataProducer\EntityDefinition\Fields\DefaultValue

Default value resolver.

Allows to fall back if a value is NULL.

Hierarchy

Expanded class hierarchy of DefaultValue

1 file declares its use of DefaultValue
ResolverBuilder.php in src/GraphQL/ResolverBuilder.php

File

src/GraphQL/Resolver/DefaultValue.php, line 16

Namespace

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

  /**
   * The initial value.
   *
   * @var \Drupal\graphql\GraphQL\Resolver\ResolverInterface
   */
  protected $value;

  /**
   * The fallback value in case the initial value resolves to NULL.
   *
   * @var \Drupal\graphql\GraphQL\Resolver\ResolverInterface
   */
  protected $default;

  /**
   * DefaultValue constructor.
   *
   * @param \Drupal\graphql\GraphQL\Resolver\ResolverInterface $value
   *   The initial value to check.
   * @param \Drupal\graphql\GraphQL\Resolver\ResolverInterface $default
   *   The fallback value returned if the initial one resolves to NULL.
   */
  public function __construct(ResolverInterface $value, ResolverInterface $default) {
    $this->value = $value;
    $this->default = $default;
  }

  /**
   * {@inheritDoc}
   */
  public function resolve($value, $args, ResolveContext $context, ResolveInfo $info, FieldContext $field) {
    $result = $this->value
      ->resolve($value, $args, $context, $info, $field);
    if ($result === NULL) {
      return $this->default
        ->resolve($value, $args, $context, $info, $field);
    }
    if ($result instanceof SyncPromise) {
      return DeferredUtility::returnFinally($result, function ($current) use ($value, $args, $context, $info, $field) {
        if ($current === NULL) {
          return $this->default
            ->resolve($value, $args, $context, $info, $field);
        }
        return $current;
      });
    }
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultValue::$default protected property The fallback value in case the initial value resolves to NULL.
DefaultValue::$value protected property The initial value.
DefaultValue::resolve public function Resolve values for the fields. Overrides ResolverInterface::resolve
DefaultValue::__construct public function DefaultValue constructor.