View source
<?php
namespace Drupal\graphql\GraphQL\Resolver;
use Drupal\graphql\GraphQL\Execution\FieldContext;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql\GraphQL\Utility\DeferredUtility;
use GraphQL\Executor\Promise\Adapter\SyncPromise;
use GraphQL\Type\Definition\ResolveInfo;
class Condition implements ResolverInterface {
protected $branches;
public function __construct(array $branches) {
$this->branches = $branches;
}
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) {
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) {
return $resolver ? $resolver
->resolve($value, $args, $context, $info, $field) : $condition;
}
}
return NULL;
}
}