for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Drupal\graphql\GraphQL\Execution\QueryVisitor;
use Youshido\GraphQL\Exception\ResolveException;
use Youshido\GraphQL\Field\FieldInterface;
class MaxComplexityQueryVisitor implements QueryVisitorInterface {
/**
* The allowed maximum complexity;
*
* @var int
*/
protected $maxComplexity;
* MaxComplexityQueryVisitor constructor.
* @param int $maxComplexity
* The allowed maximum complexity.
public function __construct($maxComplexity) {
$this->maxComplexity = $maxComplexity;
}
* {@inheritdoc}
public function visit(array $args, FieldInterface $field, $child) {
/** @var \Youshido\GraphQL\Config\Field\FieldConfig $config */
$config = $field->getConfig();
$cost = $config->get('cost', NULL);
if (is_callable($cost)) {
$cost = $cost($args, $field, $child);
return $cost;
public function reduce($carry, $current) {
return !empty($current) ? $carry + $current : $carry;
public function initial() {
return 0;
public function finish($result) {
if ($result > $this->maxComplexity) {
throw new ResolveException('The query exceeds the allowed maximum complexity.');