Do not run query complexity validation if there were other validation errors (as it will throw and mess up previous validation results), see #125

This commit is contained in:
Vladimir Razuvaev 2017-06-03 17:07:01 +07:00
parent bc6c0e2eea
commit bc4b990946
2 changed files with 44 additions and 5 deletions

View File

@ -94,12 +94,14 @@ class QueryComplexity extends AbstractQuerySecurity
},
NodeKind::OPERATION_DEFINITION => [
'leave' => function (OperationDefinitionNode $operationDefinition) use ($context, &$complexity) {
$complexity = $this->fieldComplexity($operationDefinition, $complexity);
if (empty($context->getErrors())) {
$complexity = $this->fieldComplexity($operationDefinition, $complexity);
if ($complexity > $this->getMaxQueryComplexity()) {
$context->reportError(
new Error($this->maxQueryComplexityErrorMessage($this->getMaxQueryComplexity(), $complexity))
);
if ($complexity > $this->getMaxQueryComplexity()) {
$context->reportError(
new Error($this->maxQueryComplexityErrorMessage($this->getMaxQueryComplexity(), $complexity))
);
}
}
},
],

View File

@ -1,7 +1,12 @@
<?php
namespace GraphQL\Tests\Validator;
use GraphQL\Error\Error;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\Parser;
use GraphQL\Validator\DocumentValidator;
use GraphQL\Validator\Rules\QueryComplexity;
use GraphQL\Validator\ValidationContext;
class QueryComplexityTest extends AbstractQuerySecurityTest
{
@ -149,6 +154,38 @@ class QueryComplexityTest extends AbstractQuerySecurityTest
$this->assertTypeNameMetaFieldQuery(3);
}
public function testSkippedWhenThereAreOtherValidationErrors()
{
$query = 'query MyQuery { human(name: INVALID_VALUE) { dogs {name} } }';
$reportedError = new Error("OtherValidatorError");
$otherRule = function(ValidationContext $context) use ($reportedError) {
return [
NodeKind::OPERATION_DEFINITION => [
'leave' => function() use ($context, $reportedError) {
$context->reportError($reportedError);
}
]
];
};
$errors = DocumentValidator::validate(
QuerySecuritySchema::buildSchema(),
Parser::parse($query),
[$otherRule, $this->getRule(1)]
);
$this->assertEquals(1, count($errors));
$this->assertSame($reportedError, $errors[0]);
$this->setExpectedException('GraphQL\Error\Error');
DocumentValidator::validate(
QuerySecuritySchema::buildSchema(),
Parser::parse($query),
[$this->getRule(1)]
);
}
private function assertDocumentValidators($query, $queryComplexity, $startComplexity)
{
for ($maxComplexity = $startComplexity; $maxComplexity >= 0; --$maxComplexity) {