mirror of
https://github.com/retailcrm/graphql-php.git
synced 2025-02-06 07:49:24 +03:00
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:
parent
bc6c0e2eea
commit
bc4b990946
@ -94,6 +94,7 @@ class QueryComplexity extends AbstractQuerySecurity
|
|||||||
},
|
},
|
||||||
NodeKind::OPERATION_DEFINITION => [
|
NodeKind::OPERATION_DEFINITION => [
|
||||||
'leave' => function (OperationDefinitionNode $operationDefinition) use ($context, &$complexity) {
|
'leave' => function (OperationDefinitionNode $operationDefinition) use ($context, &$complexity) {
|
||||||
|
if (empty($context->getErrors())) {
|
||||||
$complexity = $this->fieldComplexity($operationDefinition, $complexity);
|
$complexity = $this->fieldComplexity($operationDefinition, $complexity);
|
||||||
|
|
||||||
if ($complexity > $this->getMaxQueryComplexity()) {
|
if ($complexity > $this->getMaxQueryComplexity()) {
|
||||||
@ -101,6 +102,7 @@ class QueryComplexity extends AbstractQuerySecurity
|
|||||||
new Error($this->maxQueryComplexityErrorMessage($this->getMaxQueryComplexity(), $complexity))
|
new Error($this->maxQueryComplexityErrorMessage($this->getMaxQueryComplexity(), $complexity))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
]
|
]
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace GraphQL\Tests\Validator;
|
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\Rules\QueryComplexity;
|
||||||
|
use GraphQL\Validator\ValidationContext;
|
||||||
|
|
||||||
class QueryComplexityTest extends AbstractQuerySecurityTest
|
class QueryComplexityTest extends AbstractQuerySecurityTest
|
||||||
{
|
{
|
||||||
@ -149,6 +154,38 @@ class QueryComplexityTest extends AbstractQuerySecurityTest
|
|||||||
$this->assertTypeNameMetaFieldQuery(3);
|
$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)
|
private function assertDocumentValidators($query, $queryComplexity, $startComplexity)
|
||||||
{
|
{
|
||||||
for ($maxComplexity = $startComplexity; $maxComplexity >= 0; --$maxComplexity) {
|
for ($maxComplexity = $startComplexity; $maxComplexity >= 0; --$maxComplexity) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user