Skip to content

Commit bc4b990

Browse files
committed
Do not run query complexity validation if there were other validation errors (as it will throw and mess up previous validation results), see #125
1 parent bc6c0e2 commit bc4b990

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

src/Validator/Rules/QueryComplexity.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,14 @@ public function __invoke(ValidationContext $context)
9494
},
9595
NodeKind::OPERATION_DEFINITION => [
9696
'leave' => function (OperationDefinitionNode $operationDefinition) use ($context, &$complexity) {
97-
$complexity = $this->fieldComplexity($operationDefinition, $complexity);
98-
99-
if ($complexity > $this->getMaxQueryComplexity()) {
100-
$context->reportError(
101-
new Error($this->maxQueryComplexityErrorMessage($this->getMaxQueryComplexity(), $complexity))
102-
);
97+
if (empty($context->getErrors())) {
98+
$complexity = $this->fieldComplexity($operationDefinition, $complexity);
99+
100+
if ($complexity > $this->getMaxQueryComplexity()) {
101+
$context->reportError(
102+
new Error($this->maxQueryComplexityErrorMessage($this->getMaxQueryComplexity(), $complexity))
103+
);
104+
}
103105
}
104106
},
105107
],

tests/Validator/QueryComplexityTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<?php
22
namespace GraphQL\Tests\Validator;
33

4+
use GraphQL\Error\Error;
5+
use GraphQL\Language\AST\NodeKind;
6+
use GraphQL\Language\Parser;
7+
use GraphQL\Validator\DocumentValidator;
48
use GraphQL\Validator\Rules\QueryComplexity;
9+
use GraphQL\Validator\ValidationContext;
510

611
class QueryComplexityTest extends AbstractQuerySecurityTest
712
{
@@ -149,6 +154,38 @@ public function testTypeNameMetaFieldQuery()
149154
$this->assertTypeNameMetaFieldQuery(3);
150155
}
151156

157+
public function testSkippedWhenThereAreOtherValidationErrors()
158+
{
159+
$query = 'query MyQuery { human(name: INVALID_VALUE) { dogs {name} } }';
160+
161+
$reportedError = new Error("OtherValidatorError");
162+
$otherRule = function(ValidationContext $context) use ($reportedError) {
163+
return [
164+
NodeKind::OPERATION_DEFINITION => [
165+
'leave' => function() use ($context, $reportedError) {
166+
$context->reportError($reportedError);
167+
}
168+
]
169+
];
170+
};
171+
172+
$errors = DocumentValidator::validate(
173+
QuerySecuritySchema::buildSchema(),
174+
Parser::parse($query),
175+
[$otherRule, $this->getRule(1)]
176+
);
177+
178+
$this->assertEquals(1, count($errors));
179+
$this->assertSame($reportedError, $errors[0]);
180+
181+
$this->setExpectedException('GraphQL\Error\Error');
182+
DocumentValidator::validate(
183+
QuerySecuritySchema::buildSchema(),
184+
Parser::parse($query),
185+
[$this->getRule(1)]
186+
);
187+
}
188+
152189
private function assertDocumentValidators($query, $queryComplexity, $startComplexity)
153190
{
154191
for ($maxComplexity = $startComplexity; $maxComplexity >= 0; --$maxComplexity) {

0 commit comments

Comments
 (0)