diff --git a/src/Validator/Rules/QueryComplexity.php b/src/Validator/Rules/QueryComplexity.php index 4b836c785..9b4f9b914 100644 --- a/src/Validator/Rules/QueryComplexity.php +++ b/src/Validator/Rules/QueryComplexity.php @@ -18,6 +18,7 @@ use GraphQL\Language\VisitorOperation; use GraphQL\Type\Definition\Directive; use GraphQL\Type\Definition\FieldDefinition; +use GraphQL\Type\Introspection; use GraphQL\Validator\QueryValidationContext; /** @@ -117,6 +118,11 @@ protected function nodeComplexity(SelectionNode $node): int { switch (true) { case $node instanceof FieldNode: + // Exclude __schema field and all nested content from complexity calculation + if ($node->name->value === Introspection::SCHEMA_FIELD_NAME) { + return 0; + } + if ($this->directiveExcludesField($node)) { return 0; } diff --git a/tests/Validator/QueryComplexityTest.php b/tests/Validator/QueryComplexityTest.php index 8ea6da018..6f8997ead 100644 --- a/tests/Validator/QueryComplexityTest.php +++ b/tests/Validator/QueryComplexityTest.php @@ -5,6 +5,7 @@ use GraphQL\Error\Error; use GraphQL\Language\AST\NodeKind; use GraphQL\Language\Parser; +use GraphQL\Type\Introspection; use GraphQL\Validator\DocumentValidator; use GraphQL\Validator\Rules\CustomValidationRule; use GraphQL\Validator\Rules\QueryComplexity; @@ -164,7 +165,16 @@ public function testQueryWithCustomAndSkipDirective(): void public function testComplexityIntrospectionQuery(): void { - $this->assertIntrospectionQuery(187); + $query = Introspection::getIntrospectionQuery(); + + $this->assertDocumentValidator($query, 0); + } + + public function testMixedIntrospectionAndRegularFields(): void + { + $query = 'query MyQuery { __schema { queryType { name } } human { firstName } }'; + + $this->assertDocumentValidators($query, 2, 3); } public function testIntrospectionTypeMetaFieldQuery(): void