Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose query complexity score in BuildExtensionsResponse #2637

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ You can find and compare releases at the [GitHub release page](https://github.co

## Unreleased

## v6.46.0

### Added

- Expose query complexity score in lifecycle event `BuildExtensionsResponse` https://github.com/nuwave/lighthouse/pull/2637

## v6.45.1

### Fixed
Expand Down
24 changes: 14 additions & 10 deletions docs/6/api-reference/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,31 +175,35 @@ class StartExecution
### BuildExtensionsResponse

```php
use GraphQL\Executor\ExecutionResult;

/**
* Fires after a query was resolved.
*
* Listeners may return a @see \Nuwave\Lighthouse\Execution\ExtensionsResponse
* to include in the response.
*/
class BuildExtensionsResponse {}
class BuildExtensionsResponse
{
public function __construct(
/** The result of resolving a single operation. */
public ExecutionResult $result,
/** The calculated query complexity score of the operation, only available if the validation rule is enabled. */
public ?int $queryComplexity,
) {}
}
```

```php
namespace Nuwave\Lighthouse\Execution;

/**
* May be returned from listeners of @see \Nuwave\Lighthouse\Events\BuildExtensionsResponse.
*/
/** May be returned from listeners of @see \Nuwave\Lighthouse\Events\BuildExtensionsResponse. */
class ExtensionsResponse
{
public function __construct(
/**
* Will be used as the key in the response map.
*/
/** Will be used as the key in the response map. */
public string $key,
/**
* JSON-encodable content of the extension.
*/
/** JSON-encodable content of the extension. */
public mixed $content,
) {}
}
Expand Down
24 changes: 14 additions & 10 deletions docs/master/api-reference/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,31 +175,35 @@ class StartExecution
### BuildExtensionsResponse

```php
use GraphQL\Executor\ExecutionResult;

/**
* Fires after a query was resolved.
*
* Listeners may return a @see \Nuwave\Lighthouse\Execution\ExtensionsResponse
* to include in the response.
*/
class BuildExtensionsResponse {}
class BuildExtensionsResponse
{
public function __construct(
/** The result of resolving a single operation. */
public ExecutionResult $result,
/** The calculated query complexity score of the operation, only available if the validation rule is enabled. */
public ?int $queryComplexity,
) {}
}
```

```php
namespace Nuwave\Lighthouse\Execution;

/**
* May be returned from listeners of @see \Nuwave\Lighthouse\Events\BuildExtensionsResponse.
*/
/** May be returned from listeners of @see \Nuwave\Lighthouse\Events\BuildExtensionsResponse. */
class ExtensionsResponse
{
public function __construct(
/**
* Will be used as the key in the response map.
*/
/** Will be used as the key in the response map. */
public string $key,
/**
* JSON-encodable content of the extension.
*/
/** JSON-encodable content of the extension. */
public mixed $content,
) {}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Events/BuildExtensionsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ class BuildExtensionsResponse
public function __construct(
/** The result of resolving a single operation. */
public ExecutionResult $result,
/** The calculated query complexity score of the operation, only available if the validation rule is enabled. */
public ?int $queryComplexity,
) {}
}
12 changes: 3 additions & 9 deletions src/Execution/ExtensionsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@

namespace Nuwave\Lighthouse\Execution;

/**
* May be returned from listeners of @see \Nuwave\Lighthouse\Events\BuildExtensionsResponse.
*/
/** May be returned from listeners of @see \Nuwave\Lighthouse\Events\BuildExtensionsResponse. */
class ExtensionsResponse
{
public function __construct(
/**
* Will be used as the key in the response map.
*/
/** Will be used as the key in the response map. */
public string $key,
/**
* JSON-encodable content of the extension.
*/
/** JSON-encodable content of the extension. */
public mixed $content,
) {}
}
17 changes: 13 additions & 4 deletions src/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,16 @@ public function executeParsedQueryRaw(
);

if ($this->providesValidationRules instanceof CacheableValidationRulesProvider) {
$validationRules = $this->providesValidationRules->cacheableValidationRules();
$cacheableValidationRules = $this->providesValidationRules->cacheableValidationRules();

$errors = $this->validateCacheableRules($validationRules, $schema, $this->schemaBuilder->schemaHash(), $query, $queryHash);
$errors = $this->validateCacheableRules($cacheableValidationRules, $schema, $this->schemaBuilder->schemaHash(), $query, $queryHash);
if ($errors !== []) {
return new ExecutionResult(null, $errors);
}
}

$validationRules = $this->providesValidationRules->validationRules();

$result = GraphQLBase::executeQuery(
$schema,
$query,
Expand All @@ -151,12 +153,19 @@ public function executeParsedQueryRaw(
$variables,
$operationName,
null,
$this->providesValidationRules->validationRules(),
$validationRules,
);

$queryComplexityRule = $validationRules[QueryComplexity::class] ?? null;
$queryComplexity = $queryComplexityRule instanceof QueryComplexity
// TODO remove this check when updating the required version of webonyx/graphql-php
&& method_exists($queryComplexityRule, 'getQueryComplexity')
? $queryComplexityRule->getQueryComplexity()
: null;

/** @var array<\Nuwave\Lighthouse\Execution\ExtensionsResponse|null> $extensionsResponses */
$extensionsResponses = (array) $this->eventDispatcher->dispatch(
new BuildExtensionsResponse($result),
new BuildExtensionsResponse($result, $queryComplexity),
);

foreach ($extensionsResponses as $extensionsResponse) {
Expand Down
18 changes: 18 additions & 0 deletions tests/Unit/Schema/Directives/ComplexityDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use GraphQL\Validator\Rules\QueryComplexity;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Nuwave\Lighthouse\Events\BuildExtensionsResponse;
use Tests\TestCase;
use Tests\Utils\Queries\Foo;

Expand All @@ -13,6 +15,14 @@ final class ComplexityDirectiveTest extends TestCase

public function testDefaultComplexity(): void
{
$eventsDispatcher = $this->app->make(EventsDispatcher::class);

/** @var array<int, BuildExtensionsResponse> $events */
$events = [];
$eventsDispatcher->listen(BuildExtensionsResponse::class, static function (BuildExtensionsResponse $event) use (&$events): void {
$events[] = $event;
});

$max = 1;
$this->setMaxQueryComplexity($max);

Expand All @@ -33,6 +43,14 @@ public function testDefaultComplexity(): void
}
}
')->assertGraphQLErrorMessage(QueryComplexity::maxQueryComplexityErrorMessage($max, 2));

$this->assertCount(1, $events);

// TODO remove this check when updating the required version of webonyx/graphql-php
if (method_exists(QueryComplexity::class, 'getQueryComplexity')) {
$event = $events[0];
$this->assertSame(2, $event->queryComplexity);
}
}

public function testKnowsPagination(): void
Expand Down