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

Debugging of results in controller #61

Merged
Merged
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
56 changes: 30 additions & 26 deletions app/Http/Controllers/GraphQLController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\GraphQL\Query;
use Doctrine\Laminas\Hydrator\DoctrineObject;
use Doctrine\ORM\EntityManager;
use GraphQL\Error\DebugFlag;
use GraphQL\Error\Error;
use GraphQL\Error\FormattedError;
use GraphQL\GraphQL;
Expand All @@ -19,7 +20,6 @@
use GraphQL\Validator\DocumentValidator;
use GraphQL\Validator\Rules\QueryComplexity;
use Illuminate\Http\Request;
use Throwable;

use function array_map;
use function config;
Expand Down Expand Up @@ -48,7 +48,7 @@ public function __invoke(EntityManager $entityManager, Request $request): array
'useHydratorCache' => true,
]));

// Because the hydrator is used in mutation fields, set it in the Driver
// Because a hydrator is used in the mutation, set it in the Driver
// container for easy access.
$driver->set(DoctrineObject::class, new DoctrineObject($entityManager, false));

Expand All @@ -72,30 +72,34 @@ public function __invoke(EntityManager $entityManager, Request $request): array
// Limit query complexity
DocumentValidator::addRule(new QueryComplexity(350));

try {
// Execute
$result = GraphQL::executeQuery(
schema: $schema,
source: (string) $query,
contextValue: $context,
variableValues: $variables,
operationName: $operationName,
)
->setErrorFormatter(static function (Error $error): array {
$exception = $error->getPrevious() ?: $error;

// Local development
if (config('app.debug')) {
throw $exception;
}

return FormattedError::createFromException($error);
})
->setErrorsHandler(static fn (array $errors, callable $formatter): array => array_map($formatter, $errors));

return $result->toArray();
} catch (Throwable $e) {
return FormattedError::createFromException($e);
// Disable introspection in production
/*
use GraphQL\Validator\Rules\DisableIntrospection;

if (! config('app.debug')) {
$rule = new DisableIntrospection(DisableIntrospection::ENABLED);
DocumentValidator::addRule($rule);
}
*/

// Execute
$result = GraphQL::executeQuery(
schema: $schema,
source: (string) $query,
contextValue: $context,
variableValues: $variables,
operationName: $operationName,
)
->setErrorFormatter(static function (Error $error): array {
return FormattedError::createFromException($error);
})
->setErrorsHandler(static fn (array $errors, callable $formatter): array => array_map($formatter, $errors));

// Show debug output if under development; else none
$debugFlag = config('app.debug') ?
DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE
: DebugFlag::NONE;

return $result->toArray($debugFlag);
}
}
Loading