Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function to retrieve the query complexity #531

Merged
merged 1 commit into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased
- Add schema validation: Input Objects must not contain non-nullable circular references (#492)
- Added retrieving query complexity once query has been completed (#316)

#### v0.13.5
- Fix coroutine executor when using with promise (#486)
Expand Down
16 changes: 12 additions & 4 deletions src/Validator/Rules/QueryComplexity.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class QueryComplexity extends QuerySecurityRule
/** @var ValidationContext */
private $context;

/** @var int */
private $complexity;

public function __construct($maxQueryComplexity)
{
$this->setMaxQueryComplexity($maxQueryComplexity);
Expand All @@ -52,7 +55,7 @@ public function getVisitor(ValidationContext $context)

$this->variableDefs = new ArrayObject();
$this->fieldNodeAndDefs = new ArrayObject();
$complexity = 0;
$this->complexity = 0;

return $this->invokeIfNeeded(
$context,
Expand All @@ -79,16 +82,16 @@ public function getVisitor(ValidationContext $context)
return;
}

$complexity = $this->fieldComplexity($operationDefinition, $complexity);
$this->complexity = $this->fieldComplexity($operationDefinition, $complexity);

if ($complexity <= $this->getMaxQueryComplexity()) {
if ($this->getQueryComplexity() <= $this->getMaxQueryComplexity()) {
return;
}

$context->reportError(
new Error(self::maxQueryComplexityErrorMessage(
$this->getMaxQueryComplexity(),
$complexity
$this->getQueryComplexity()
))
);
},
Expand Down Expand Up @@ -259,6 +262,11 @@ static function ($error) {
return $args;
}

public function getQueryComplexity()
{
return $this->complexity;
}

public function getMaxQueryComplexity()
{
return $this->maxQueryComplexity;
Expand Down
15 changes: 15 additions & 0 deletions tests/Validator/QueryComplexityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ public function testSimpleQueries() : void
$this->assertDocumentValidators($query, 2, 3);
}

public function testGetQueryComplexity() : void
{
$query = 'query MyQuery { human { firstName } }';

$rule = $this->getRule(5);

DocumentValidator::validate(
QuerySecuritySchema::buildSchema(),
Parser::parse($query),
[$rule]
);

self::assertEquals(2, $rule->getQueryComplexity(), $query);
}

private function assertDocumentValidators($query, $queryComplexity, $startComplexity)
{
for ($maxComplexity = $startComplexity; $maxComplexity >= 0; --$maxComplexity) {
Expand Down