Skip to content

Commit 7a44e4e

Browse files
ruudkspawnia
authored andcommitted
Allow access to calculated query complexity
This allows you to read the calculated query complexity. A use case could be: add a rule that logs when queries are detected with a complexity higher than X.
1 parent a393385 commit 7a44e4e

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/Validator/Rules/QueryComplexity.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class QueryComplexity extends QuerySecurityRule
2727
{
2828
protected int $maxQueryComplexity;
2929

30+
protected int $queryComplexity;
31+
3032
/** @var array<string, mixed> */
3133
protected array $rawVariableValues = [];
3234

@@ -46,6 +48,7 @@ public function __construct(int $maxQueryComplexity)
4648

4749
public function getVisitor(QueryValidationContext $context): array
4850
{
51+
$this->queryComplexity = 0;
4952
$this->context = $context;
5053
$this->variableDefs = new NodeList([]);
5154
$this->fieldNodeAndDefs = new \ArrayObject();
@@ -79,16 +82,16 @@ public function getVisitor(QueryValidationContext $context): array
7982
return;
8083
}
8184

82-
$complexity = $this->fieldComplexity($operationDefinition->selectionSet);
85+
$this->queryComplexity = $this->fieldComplexity($operationDefinition->selectionSet);
8386

84-
if ($complexity <= $this->maxQueryComplexity) {
87+
if ($this->queryComplexity <= $this->maxQueryComplexity) {
8588
return;
8689
}
8790

8891
$context->reportError(
8992
new Error(static::maxQueryComplexityErrorMessage(
9093
$this->maxQueryComplexity,
91-
$complexity
94+
$this->queryComplexity
9295
))
9396
);
9497
},
@@ -263,6 +266,11 @@ public function getMaxQueryComplexity(): int
263266
return $this->maxQueryComplexity;
264267
}
265268

269+
public function getQueryComplexity(): int
270+
{
271+
return $this->queryComplexity;
272+
}
273+
266274
/**
267275
* Set max query complexity. If equal to 0 no check is done. Must be greater or equal to 0.
268276
*

0 commit comments

Comments
 (0)