Skip to content

Commit 18d7823

Browse files
authored
Ignore Introspection queries in QueryComplexity rule (#1788)
Fixes #1204
1 parent 61f7cf1 commit 18d7823

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Validator/Rules/QueryComplexity.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use GraphQL\Language\VisitorOperation;
1919
use GraphQL\Type\Definition\Directive;
2020
use GraphQL\Type\Definition\FieldDefinition;
21+
use GraphQL\Type\Introspection;
2122
use GraphQL\Validator\QueryValidationContext;
2223

2324
/**
@@ -117,6 +118,11 @@ protected function nodeComplexity(SelectionNode $node): int
117118
{
118119
switch (true) {
119120
case $node instanceof FieldNode:
121+
// Exclude __schema field and all nested content from complexity calculation
122+
if ($node->name->value === Introspection::SCHEMA_FIELD_NAME) {
123+
return 0;
124+
}
125+
120126
if ($this->directiveExcludesField($node)) {
121127
return 0;
122128
}

tests/Validator/QueryComplexityTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use GraphQL\Error\Error;
66
use GraphQL\Language\AST\NodeKind;
77
use GraphQL\Language\Parser;
8+
use GraphQL\Type\Introspection;
89
use GraphQL\Validator\DocumentValidator;
910
use GraphQL\Validator\Rules\CustomValidationRule;
1011
use GraphQL\Validator\Rules\QueryComplexity;
@@ -164,7 +165,16 @@ public function testQueryWithCustomAndSkipDirective(): void
164165

165166
public function testComplexityIntrospectionQuery(): void
166167
{
167-
$this->assertIntrospectionQuery(187);
168+
$query = Introspection::getIntrospectionQuery();
169+
170+
$this->assertDocumentValidator($query, 0);
171+
}
172+
173+
public function testMixedIntrospectionAndRegularFields(): void
174+
{
175+
$query = 'query MyQuery { __schema { queryType { name } } human { firstName } }';
176+
177+
$this->assertDocumentValidators($query, 2, 3);
168178
}
169179

170180
public function testIntrospectionTypeMetaFieldQuery(): void

0 commit comments

Comments
 (0)