diff --git a/_config/config.yml b/_config/config.yml index 3e88deeb..cc1b9e0a 100644 --- a/_config/config.yml +++ b/_config/config.yml @@ -48,4 +48,4 @@ Only: --- SilverStripe\TestSession\TestSessionEnvironment: extensions: - - SilverStripe\GraphQL\Extensions\TestSessionEnvironmentExtension \ No newline at end of file + - SilverStripe\GraphQL\Extensions\TestSessionEnvironmentExtension diff --git a/_config/middlewares.yml b/_config/middlewares.yml index 5e9a6ce4..60c16768 100644 --- a/_config/middlewares.yml +++ b/_config/middlewares.yml @@ -4,7 +4,6 @@ Name: 'graphql-middlewares' SilverStripe\Core\Injector\Injector: # default implementation SilverStripe\GraphQL\QueryHandler\QueryHandlerInterface: - class: SilverStripe\GraphQL\QueryHandler\QueryHandler properties: Middlewares: csrf: '%$SilverStripe\GraphQL\Middleware\CSRFMiddleware' diff --git a/_config/schema-default.yml b/_config/schema-default.yml index 19766608..71fc5bb2 100644 --- a/_config/schema-default.yml +++ b/_config/schema-default.yml @@ -9,7 +9,8 @@ SilverStripe\Core\Injector\Injector: schema: default # use a custom handler so it's easy to override/add middlewares in the default schema handler: '%$SilverStripe\GraphQL\QueryHandler\QueryHandlerInterface.default' - + SilverStripe\GraphQL\QueryHandler\QueryHandlerInterface.default: + class: SilverStripe\GraphQL\QueryHandler\QueryHandler SilverStripe\GraphQL\Schema\Schema: schemas: default: @@ -35,4 +36,13 @@ SilverStripe\GraphQL\Schema\Schema: before: paginateList canView: after: paginateList - +--- +Name: graphql-default-dev +Except: + environment: live +--- +# When not in live mode, throw all errors, so the JSON response doesn't get corrupted with notice/warnings +SilverStripe\Core\Injector\Injector: + SilverStripe\GraphQL\QueryHandler\QueryHandlerInterface.default: + properties: + errorHandler: [SilverStripe\GraphQL\QueryHandler\DevErrorHandler, handleError] diff --git a/src/QueryHandler/DevErrorHandler.php b/src/QueryHandler/DevErrorHandler.php new file mode 100644 index 00000000..b8faf045 --- /dev/null +++ b/src/QueryHandler/DevErrorHandler.php @@ -0,0 +1,30 @@ +errorHandler) { + set_error_handler($this->errorHandler); + } $context = $this->getContext(); $last = function ($schema, $query, $context, $vars) { return GraphQL::executeQuery($schema, $query, null, $context, $vars); @@ -174,6 +183,16 @@ public function setErrorFormatter(callable $errorFormatter): self return $this; } + /** + * @param callable $errorHandler + * @return QueryHandler + */ + public function setErrorHandler(callable $errorHandler): self + { + $this->errorHandler = $errorHandler; + return $this; + } + /** * @return QueryMiddleware[] */