Skip to content

Commit

Permalink
Handle errors thrown in lifecycle events per operation
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia authored Jul 16, 2024
1 parent 107a065 commit 921981c
Show file tree
Hide file tree
Showing 24 changed files with 150 additions and 80 deletions.
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.41.1

### Fixed

- Handle errors thrown in lifecycle events per operation https://github.com/nuwave/lighthouse/pull/2584

## v6.41.0

### Added
Expand Down
4 changes: 2 additions & 2 deletions docs/6/digging-deeper/extending-lighthouse.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ Add your custom directives to Lighthouse by listening for the [`RegisterDirectiv
```php
namespace SomeVendor\SomePackage;

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Support\ServiceProvider;
use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces;

final class SomePackageServiceProvider extends ServiceProvider
{
public function boot(Dispatcher $dispatcher): void
public function boot(EventsDispatcher $dispatcher): void
{
$dispatcher->listen(
RegisterDirectiveNamespaces::class,
Expand Down
4 changes: 2 additions & 2 deletions docs/master/digging-deeper/extending-lighthouse.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ Add your custom directives to Lighthouse by listening for the [`RegisterDirectiv
```php
namespace SomeVendor\SomePackage;

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Support\ServiceProvider;
use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces;

final class SomePackageServiceProvider extends ServiceProvider
{
public function boot(Dispatcher $dispatcher): void
public function boot(EventsDispatcher $dispatcher): void
{
$dispatcher->listen(
RegisterDirectiveNamespaces::class,
Expand Down
4 changes: 2 additions & 2 deletions src/Async/AsyncServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Nuwave\Lighthouse\Async;

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Support\ServiceProvider;
use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces;

class AsyncServiceProvider extends ServiceProvider
{
public function boot(Dispatcher $dispatcher): void
public function boot(EventsDispatcher $dispatcher): void
{
$dispatcher->listen(RegisterDirectiveNamespaces::class, static fn (): string => __NAMESPACE__);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Auth/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

use Illuminate\Container\Container;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Support\ServiceProvider;
use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces;

class AuthServiceProvider extends ServiceProvider
{
public function boot(Dispatcher $dispatcher): void
public function boot(EventsDispatcher $dispatcher): void
{
$dispatcher->listen(RegisterDirectiveNamespaces::class, static fn (): string => __NAMESPACE__);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Cache/CacheServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Nuwave\Lighthouse\Cache;

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Support\ServiceProvider;
use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces;

Expand All @@ -13,7 +13,7 @@ public function register(): void
$this->app->bind(CacheKeyAndTags::class, CacheKeyAndTagsGenerator::class);
}

public function boot(Dispatcher $dispatcher): void
public function boot(EventsDispatcher $dispatcher): void
{
$dispatcher->listen(RegisterDirectiveNamespaces::class, static fn (): string => __NAMESPACE__);
}
Expand Down
4 changes: 2 additions & 2 deletions src/CacheControl/CacheControlServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use GraphQL\Type\Definition\ScalarType;
use GraphQL\Type\Definition\WrappingType;
use GraphQL\Utils\TypeInfo;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Support\ServiceProvider;
use Nuwave\Lighthouse\Events\EndRequest;
use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces;
Expand All @@ -25,7 +25,7 @@ public function register(): void
$this->app->singleton(CacheControl::class);
}

public function boot(Dispatcher $dispatcher): void
public function boot(EventsDispatcher $dispatcher): void
{
$dispatcher->listen(RegisterDirectiveNamespaces::class, static fn (): string => __NAMESPACE__);
$dispatcher->listen(StartExecution::class, function (StartExecution $startExecution): void {
Expand Down
4 changes: 2 additions & 2 deletions src/Defer/DeferServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Nuwave\Lighthouse\Defer;

use GraphQL\Language\Parser;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Support\ServiceProvider;
use Nuwave\Lighthouse\Events\ManipulateAST;
use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces;
Expand All @@ -19,7 +19,7 @@ public function register(): void
$this->app->singleton(CreatesResponse::class, Defer::class);
}

public function boot(Dispatcher $dispatcher): void
public function boot(EventsDispatcher $dispatcher): void
{
$dispatcher->listen(RegisterDirectiveNamespaces::class, static fn (): string => __NAMESPACE__);
$dispatcher->listen(ManipulateAST::class, fn (ManipulateAST $manipulateAST) => $this->handleManipulateAST($manipulateAST));
Expand Down
4 changes: 2 additions & 2 deletions src/Federation/FederationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Nuwave\Lighthouse\Federation;

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Support\ServiceProvider;
use Nuwave\Lighthouse\Events\ManipulateAST;
use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces;
Expand All @@ -15,7 +15,7 @@ public function register(): void
$this->app->singleton(EntityResolverProvider::class);
}

public function boot(Dispatcher $dispatcher): void
public function boot(EventsDispatcher $dispatcher): void
{
$dispatcher->listen(RegisterDirectiveNamespaces::class, static fn (): string => __NAMESPACE__ . '\\Directives');
$dispatcher->listen(ManipulateAST::class, ASTManipulator::class);
Expand Down
4 changes: 2 additions & 2 deletions src/GlobalId/GlobalIdServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Nuwave\Lighthouse\GlobalId;

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Support\ServiceProvider;
use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces;

Expand All @@ -14,7 +14,7 @@ public function register(): void
$this->app->singleton(NodeRegistry::class);
}

public function boot(Dispatcher $dispatcher): void
public function boot(EventsDispatcher $dispatcher): void
{
$dispatcher->listen(RegisterDirectiveNamespaces::class, static fn (): string => __NAMESPACE__);
}
Expand Down
27 changes: 14 additions & 13 deletions src/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Illuminate\Container\Container;
use Illuminate\Contracts\Cache\Factory as CacheFactory;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Support\Collection;
use Nuwave\Lighthouse\Events\BuildExtensionsResponse;
Expand Down Expand Up @@ -52,7 +52,7 @@ class GraphQL
public function __construct(
protected SchemaBuilder $schemaBuilder,
protected Pipeline $pipeline,
protected EventDispatcher $eventDispatcher,
protected EventsDispatcher $eventDispatcher,
protected ErrorPool $errorPool,
protected ProvidesValidationRules $providesValidationRules,
protected GraphQLHelper $graphQLHelper,
Expand Down Expand Up @@ -219,27 +219,28 @@ public function executeOperation(OperationParams $params, GraphQLContext $contex
}

$queryString = $params->query;
if (is_string($queryString)) {
return $this->executeQueryString(
$queryString,
$context,
$params->variables,
null,
$params->operation,
);
}

try {
if (is_string($queryString)) {
return $this->executeQueryString(
$queryString,
$context,
$params->variables,
null,
$params->operation,
);
}

return $this->executeParsedQuery(
$this->loadPersistedQuery($params->queryId),
$context,
$params->variables,
null,
$params->operation,
);
} catch (Error $error) {
} catch (\Throwable $throwable) {
return $this->toSerializableArray(
new ExecutionResult(null, [$error]),
new ExecutionResult(null, [Error::createLocatedError($throwable)]),
);
}
}
Expand Down
50 changes: 24 additions & 26 deletions src/LighthouseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use GraphQL\Executor\ExecutionResult;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerContract;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\ServiceProvider;
Expand Down Expand Up @@ -53,7 +53,7 @@
class LighthouseServiceProvider extends ServiceProvider
{
/** @var array<int, class-string<\Illuminate\Console\Command>> */
public const COMMANDS = [
protected const COMMANDS = [
CacheCommand::class,
ClearCacheCommand::class,
DirectiveCommand::class,
Expand Down Expand Up @@ -105,7 +105,7 @@ public function provideSubscriptionResolver(FieldValue $fieldValue): \Closure
$this->commands(self::COMMANDS);
}

public function boot(ConfigRepository $configRepository, Dispatcher $dispatcher): void
public function boot(ConfigRepository $configRepository, EventsDispatcher $dispatcher): void
{
$dispatcher->listen(RegisterDirectiveNamespaces::class, static fn (): string => __NAMESPACE__ . '\\Schema\\Directives');

Expand All @@ -122,29 +122,27 @@ public function boot(ConfigRepository $configRepository, Dispatcher $dispatcher)
$exceptionHandler = $this->app->make(ExceptionHandlerContract::class);
// @phpstan-ignore-next-line larastan overly eager assumes this will always be a concrete instance
if ($exceptionHandler instanceof ExceptionHandler) {
$exceptionHandler->renderable(
function (ClientAware $error): JsonResponse {
assert($error instanceof \Throwable);

if (! $error instanceof Error) {
$error = new Error(
$error->getMessage(),
null,
null,
[],
null,
$error,
$error instanceof ProvidesExtensions ? $error->getExtensions() : [],
);
}

$graphQL = $this->app->make(GraphQL::class);
$executionResult = new ExecutionResult(null, [$error]);
$serializableResult = $graphQL->toSerializableArray($executionResult);

return new JsonResponse($serializableResult);
},
);
$exceptionHandler->renderable(function (ClientAware $error): JsonResponse {
assert($error instanceof \Throwable);

if (! $error instanceof Error) {
$error = new Error(
$error->getMessage(),
null,
null,
[],
null,
$error,
$error instanceof ProvidesExtensions ? $error->getExtensions() : [],
);
}

$graphQL = $this->app->make(GraphQL::class);
$executionResult = new ExecutionResult(null, [$error]);
$serializableResult = $graphQL->toSerializableArray($executionResult);

return new JsonResponse($serializableResult);
});
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/OrderBy/OrderByServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
use GraphQL\Language\Parser;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Support\ServiceProvider;
use Nuwave\Lighthouse\Events\ManipulateAST;
use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces;
Expand All @@ -13,7 +13,7 @@ class OrderByServiceProvider extends ServiceProvider
{
public const DEFAULT_ORDER_BY_CLAUSE = 'OrderByClause';

public function boot(Dispatcher $dispatcher): void
public function boot(EventsDispatcher $dispatcher): void
{
$dispatcher->listen(RegisterDirectiveNamespaces::class, static fn (): string => __NAMESPACE__);
$dispatcher->listen(ManipulateAST::class, static function (ManipulateAST $manipulateAST): void {
Expand Down
4 changes: 2 additions & 2 deletions src/Pagination/PaginationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Nuwave\Lighthouse\Pagination;

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Support\ServiceProvider;
use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces;

class PaginationServiceProvider extends ServiceProvider
{
public function boot(Dispatcher $dispatcher): void
public function boot(EventsDispatcher $dispatcher): void
{
$dispatcher->listen(RegisterDirectiveNamespaces::class, static fn (): string => __NAMESPACE__);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Pennant/PennantServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Nuwave\Lighthouse\Pennant;

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Support\ServiceProvider;
use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces;

final class PennantServiceProvider extends ServiceProvider
{
public function boot(Dispatcher $dispatcher): void
public function boot(EventsDispatcher $dispatcher): void
{
$dispatcher->listen(RegisterDirectiveNamespaces::class, static fn (): string => __NAMESPACE__);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Scout/ScoutServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Nuwave\Lighthouse\Scout;

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Support\ServiceProvider;
use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces;

class ScoutServiceProvider extends ServiceProvider
{
public function boot(Dispatcher $dispatcher): void
public function boot(EventsDispatcher $dispatcher): void
{
$dispatcher->listen(RegisterDirectiveNamespaces::class, static fn (): string => __NAMESPACE__);
}
Expand Down
4 changes: 2 additions & 2 deletions src/SoftDeletes/SoftDeletesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Nuwave\Lighthouse\SoftDeletes;

use GraphQL\Language\Parser;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\ServiceProvider;
use Nuwave\Lighthouse\Events\ManipulateAST;
Expand All @@ -27,7 +27,7 @@ public static function assertModelUsesSoftDeletes(string $modelClass, string $ex
}
}

public function boot(Dispatcher $dispatcher): void
public function boot(EventsDispatcher $dispatcher): void
{
$dispatcher->listen(RegisterDirectiveNamespaces::class, static fn (): string => __NAMESPACE__);
$dispatcher->listen(ManipulateAST::class, static function (ManipulateAST $manipulateAST): void {
Expand Down
Loading

0 comments on commit 921981c

Please sign in to comment.