Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/Validator/Rules/QueryComplexity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 11 additions & 1 deletion tests/Validator/QueryComplexityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading