1111use App \GraphQL \Query ;
1212use Doctrine \Laminas \Hydrator \DoctrineObject ;
1313use Doctrine \ORM \EntityManager ;
14+ use GraphQL \Error \DebugFlag ;
1415use GraphQL \Error \Error ;
1516use GraphQL \Error \FormattedError ;
1617use GraphQL \GraphQL ;
1920use GraphQL \Validator \DocumentValidator ;
2021use GraphQL \Validator \Rules \QueryComplexity ;
2122use Illuminate \Http \Request ;
22- use Throwable ;
2323
2424use function array_map ;
2525use 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