Skip to content

Commit df50e54

Browse files
authored
Merge pull request #61 from TomHAnderson/feature/controller-features
Debugging of results in controller
2 parents ba0c5e4 + 1e9cd62 commit df50e54

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

app/Http/Controllers/GraphQLController.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use App\GraphQL\Query;
1212
use Doctrine\Laminas\Hydrator\DoctrineObject;
1313
use Doctrine\ORM\EntityManager;
14+
use GraphQL\Error\DebugFlag;
1415
use GraphQL\Error\Error;
1516
use GraphQL\Error\FormattedError;
1617
use GraphQL\GraphQL;
@@ -19,7 +20,6 @@
1920
use GraphQL\Validator\DocumentValidator;
2021
use GraphQL\Validator\Rules\QueryComplexity;
2122
use Illuminate\Http\Request;
22-
use Throwable;
2323

2424
use function array_map;
2525
use function config;
@@ -48,7 +48,7 @@ public function __invoke(EntityManager $entityManager, Request $request): array
4848
'useHydratorCache' => true,
4949
]));
5050

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

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

75-
try {
76-
// Execute
77-
$result = GraphQL::executeQuery(
78-
schema: $schema,
79-
source: (string) $query,
80-
contextValue: $context,
81-
variableValues: $variables,
82-
operationName: $operationName,
83-
)
84-
->setErrorFormatter(static function (Error $error): array {
85-
$exception = $error->getPrevious() ?: $error;
86-
87-
// Local development
88-
if (config('app.debug')) {
89-
throw $exception;
90-
}
91-
92-
return FormattedError::createFromException($error);
93-
})
94-
->setErrorsHandler(static fn (array $errors, callable $formatter): array => array_map($formatter, $errors));
95-
96-
return $result->toArray();
97-
} catch (Throwable $e) {
98-
return FormattedError::createFromException($e);
75+
// Disable introspection in production
76+
/*
77+
use GraphQL\Validator\Rules\DisableIntrospection;
78+
79+
if (! config('app.debug')) {
80+
$rule = new DisableIntrospection(DisableIntrospection::ENABLED);
81+
DocumentValidator::addRule($rule);
9982
}
83+
*/
84+
85+
// Execute
86+
$result = GraphQL::executeQuery(
87+
schema: $schema,
88+
source: (string) $query,
89+
contextValue: $context,
90+
variableValues: $variables,
91+
operationName: $operationName,
92+
)
93+
->setErrorFormatter(static function (Error $error): array {
94+
return FormattedError::createFromException($error);
95+
})
96+
->setErrorsHandler(static fn (array $errors, callable $formatter): array => array_map($formatter, $errors));
97+
98+
// Show debug output if under development; else none
99+
$debugFlag = config('app.debug') ?
100+
DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE
101+
: DebugFlag::NONE;
102+
103+
return $result->toArray($debugFlag);
100104
}
101105
}

0 commit comments

Comments
 (0)