You are here

class Condition in GraphQL 8.4

Conditional resolver.

From given set of conditions and their respective resolvers it resolves the one whose condition is evaluated with non empty value.

Hierarchy

Expanded class hierarchy of Condition

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

File

src/GraphQL/Resolver/Condition.php, line 17

Namespace

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

  /**
   * List of condition and their corresponding resolvers.
   *
   * @var array
   */
  protected $branches;

  /**
   * Condition constructor.
   *
   * @param array $branches
   */
  public function __construct(array $branches) {
    $this->branches = $branches;
  }

  /**
   * {@inheritdoc}
   */
  public function resolve($value, $args, ResolveContext $context, ResolveInfo $info, FieldContext $field) {
    $branches = $this->branches;
    while (list($condition, $resolver) = array_pad(array_shift($branches), 2, NULL)) {
      if ($condition instanceof ResolverInterface) {
        if (($condition = $condition
          ->resolve($value, $args, $context, $info, $field)) === NULL) {

          // Bail out early if a resolver returns NULL.
          continue;
        }
      }
      if ($condition instanceof SyncPromise) {
        return DeferredUtility::returnFinally($condition, function ($cond) use ($branches, $resolver, $value, $args, $context, $info, $field) {
          array_unshift($branches, [
            $cond,
            $resolver,
          ]);
          return (new Condition($branches))
            ->resolve($value, $args, $context, $info, $field);
        });
      }
      if ((bool) $condition) {

        /** @var \Drupal\graphql\GraphQL\Resolver\ResolverInterface|null $resolver */
        return $resolver ? $resolver
          ->resolve($value, $args, $context, $info, $field) : $condition;
      }
    }

    // Functional languages throw exceptions here. Should we just return NULL?
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Condition::$branches protected property List of condition and their corresponding resolvers.
Condition::resolve public function Resolve values for the fields. Overrides ResolverInterface::resolve
Condition::__construct public function Condition constructor.