Skip to content
Merged
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.52.0

### Added

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

## v6.51.2

### 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 @@ -137,14 +137,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 @@ -153,12 +155,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
13 changes: 7 additions & 6 deletions src/Tracing/FederatedTracing/Proto/FieldStat.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions src/Tracing/FederatedTracing/Proto/ReportHeader.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 23 additions & 21 deletions src/Tracing/FederatedTracing/Proto/Trace.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions src/Tracing/FederatedTracing/Proto/Trace/Node.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/Tracing/FederatedTracing/Proto/TracesAndStats.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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