Skip to content

Commit 6d593dd

Browse files
soyukamauriau
and
mauriau
authored
feat(graphql): allow to configure max query depth and max query complexity (#6880)
Co-authored-by: mauriau <m.auriau@toovalu.com>
1 parent 1dd9663 commit 6d593dd

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Executor.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use GraphQL\Type\Schema;
1919
use GraphQL\Validator\DocumentValidator;
2020
use GraphQL\Validator\Rules\DisableIntrospection;
21+
use GraphQL\Validator\Rules\QueryComplexity;
22+
use GraphQL\Validator\Rules\QueryDepth;
2123

2224
/**
2325
* Wrapper for the GraphQL facade.
@@ -26,13 +28,19 @@
2628
*/
2729
final class Executor implements ExecutorInterface
2830
{
29-
public function __construct(private readonly bool $graphQlIntrospectionEnabled = true)
31+
public function __construct(private readonly bool $graphQlIntrospectionEnabled = true, private readonly int $maxQueryComplexity = 500, private readonly int $maxQueryDepth = 20)
3032
{
3133
DocumentValidator::addRule(
3234
new DisableIntrospection(
3335
$this->graphQlIntrospectionEnabled ? DisableIntrospection::DISABLED : DisableIntrospection::ENABLED
3436
)
3537
);
38+
39+
$queryComplexity = new QueryComplexity($this->maxQueryComplexity);
40+
DocumentValidator::addRule($queryComplexity);
41+
42+
$queryDepth = new QueryDepth($this->maxQueryDepth);
43+
DocumentValidator::addRule($queryDepth);
3644
}
3745

3846
/**

Tests/ExecutorTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use ApiPlatform\GraphQl\Executor;
1717
use GraphQL\Validator\DocumentValidator;
1818
use GraphQL\Validator\Rules\DisableIntrospection;
19+
use GraphQL\Validator\Rules\QueryComplexity;
20+
use GraphQL\Validator\Rules\QueryDepth;
1921
use PHPUnit\Framework\TestCase;
2022

2123
/**
@@ -38,4 +40,20 @@ public function testDisableIntrospectionQuery(): void
3840
$expected = new DisableIntrospection(DisableIntrospection::ENABLED);
3941
$this->assertEquals($expected, DocumentValidator::getRule(DisableIntrospection::class));
4042
}
43+
44+
public function testChangeValueOfMaxQueryDepth(): void
45+
{
46+
$executor = new Executor(true, 20);
47+
48+
$expected = new QueryComplexity(20);
49+
$this->assertEquals($expected, DocumentValidator::getRule(QueryComplexity::class));
50+
}
51+
52+
public function testChangeValueOfMaxQueryComplexity(): void
53+
{
54+
$executor = new Executor(true, maxQueryDepth: 20);
55+
56+
$expected = new QueryDepth(20);
57+
$this->assertEquals($expected, DocumentValidator::getRule(QueryDepth::class));
58+
}
4159
}

0 commit comments

Comments
 (0)