1313
1414namespace ApiPlatform \Core \GraphQl \Action ;
1515
16+ use ApiPlatform \Core \GraphQl \Exception \ExceptionFormatterFactory ;
17+ use ApiPlatform \Core \GraphQl \Exception \ExceptionFormatterInterface ;
1618use ApiPlatform \Core \GraphQl \ExecutorInterface ;
1719use ApiPlatform \Core \GraphQl \Type \SchemaBuilderInterface ;
1820use GraphQL \Error \Debug ;
1921use GraphQL \Error \Error ;
22+ use GraphQL \Error \FormattedError ;
2023use GraphQL \Executor \ExecutionResult ;
2124use Symfony \Component \HttpFoundation \JsonResponse ;
2225use Symfony \Component \HttpFoundation \Request ;
@@ -37,8 +40,9 @@ final class EntrypointAction
3740 private $ graphiqlEnabled ;
3841 private $ graphQlPlaygroundEnabled ;
3942 private $ defaultIde ;
43+ private $ exceptionFormatterFactory ;
4044
41- public function __construct (SchemaBuilderInterface $ schemaBuilder , ExecutorInterface $ executor , GraphiQlAction $ graphiQlAction , GraphQlPlaygroundAction $ graphQlPlaygroundAction , bool $ debug = false , bool $ graphiqlEnabled = false , bool $ graphQlPlaygroundEnabled = false , $ defaultIde = false )
45+ public function __construct (SchemaBuilderInterface $ schemaBuilder , ExecutorInterface $ executor , GraphiQlAction $ graphiQlAction , GraphQlPlaygroundAction $ graphQlPlaygroundAction , ExceptionFormatterFactory $ exceptionFormatterFactory , bool $ debug = false , bool $ graphiqlEnabled = false , bool $ graphQlPlaygroundEnabled = false , $ defaultIde = false )
4246 {
4347 $ this ->schemaBuilder = $ schemaBuilder ;
4448 $ this ->executor = $ executor ;
@@ -48,6 +52,7 @@ public function __construct(SchemaBuilderInterface $schemaBuilder, ExecutorInter
4852 $ this ->graphiqlEnabled = $ graphiqlEnabled ;
4953 $ this ->graphQlPlaygroundEnabled = $ graphQlPlaygroundEnabled ;
5054 $ this ->defaultIde = $ defaultIde ;
55+ $ this ->exceptionFormatterFactory = $ exceptionFormatterFactory ;
5156 }
5257
5358 public function __invoke (Request $ request ): Response
@@ -73,7 +78,30 @@ public function __invoke(Request $request): Response
7378 }
7479
7580 try {
76- $ executionResult = $ this ->executor ->executeQuery ($ this ->schemaBuilder ->getSchema (), $ query , null , null , $ variables , $ operation );
81+ $ executionResult = $ this ->executor ->executeQuery ($ this ->schemaBuilder ->getSchema (), $ query , null , null , $ variables , $ operation )
82+ ->setErrorFormatter (function (Error $ error ) {
83+ $ formatters = $ this ->exceptionFormatterFactory ->getExceptionFormatters ();
84+ usort ($ formatters , function ($ a , $ b ) {
85+ /**
86+ * @var ExceptionFormatterInterface
87+ * @var ExceptionFormatterInterface $b
88+ */
89+ if ($ a ->getPriority () == $ b ->getPriority ()) {
90+ return 0 ;
91+ }
92+
93+ return ($ a ->getPriority () > $ b ->getPriority ()) ? -1 : 1 ;
94+ });
95+ /** @var ExceptionFormatterInterface $exceptionFormatter */
96+ foreach ($ formatters as $ exceptionFormatter ) {
97+ if (null !== $ error ->getPrevious () && $ exceptionFormatter ->supports ($ error ->getPrevious ())) {
98+ return $ exceptionFormatter ->format ($ error );
99+ }
100+ }
101+
102+ // falling back to default GraphQL error formatter
103+ return FormattedError::createFromException ($ error );
104+ });
77105 } catch (\Exception $ e ) {
78106 $ executionResult = new ExecutionResult (null , [new Error ($ e ->getMessage (), null , null , null , null , $ e )]);
79107 }
0 commit comments