11<?php
2+
23/**
3- * Copyright 2024 Adobe
4- * All Rights Reserved .
4+ * Copyright © Magento, Inc. All rights reserved.
5+ * See COPYING.txt for license details .
56 */
7+
68declare (strict_types=1 );
79
810namespace Magento \GraphQl \Controller ;
911
10- use Exception ;
1112use Magento \Framework \App \Area ;
1213use Magento \Framework \App \AreaList ;
1314use Magento \Framework \App \FrontControllerInterface ;
2324use Magento \Framework \GraphQl \Query \QueryProcessor ;
2425use Magento \Framework \GraphQl \Query \Resolver \ContextInterface ;
2526use Magento \Framework \GraphQl \Schema \SchemaGeneratorInterface ;
26- use Magento \Framework \GraphQl \Exception \GraphQlAuthenticationException ;
27- use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
2827use Magento \Framework \Serialize \SerializerInterface ;
2928use Magento \Framework \Webapi \Response ;
3029use Magento \GraphQl \Helper \Query \Logger \LogData ;
@@ -185,7 +184,7 @@ public function dispatch(RequestInterface $request): ResponseInterface
185184 $ statusCode = 200 ;
186185 $ jsonResult = $ this ->jsonFactory ->create ();
187186 $ data = $ this ->getDataFromRequest ($ request );
188- $ result = [' errors ' => [] ];
187+ $ result = [];
189188
190189 $ schema = null ;
191190 $ query = $ data ['query ' ] ?? '' ;
@@ -206,14 +205,8 @@ public function dispatch(RequestInterface $request): ResponseInterface
206205 $ this ->contextFactory ->create (),
207206 $ data ['variables ' ] ?? []
208207 );
209- $ statusCode = $ this ->getHttpResponseCode ($ result );
210- } catch (GraphQlAuthenticationException $ error ) {
211- $ result ['errors ' ][] = $ this ->graphQlError ->create ($ error );
212- $ statusCode = 401 ;
213- } catch (GraphQlAuthorizationException $ error ) {
214- $ result ['errors ' ][] = $ this ->graphQlError ->create ($ error );
215- $ statusCode = 403 ;
216- } catch (Exception $ error ) {
208+ } catch (\Exception $ error ) {
209+ $ result ['errors ' ] = isset ($ result ['errors ' ]) ? $ result ['errors ' ] : [];
217210 $ result ['errors ' ][] = $ this ->graphQlError ->create ($ error );
218211 $ statusCode = ExceptionFormatter::HTTP_GRAPH_QL_SCHEMA_ERROR_STATUS ;
219212 }
@@ -223,7 +216,7 @@ public function dispatch(RequestInterface $request): ResponseInterface
223216 $ jsonResult ->renderResult ($ this ->httpResponse );
224217
225218 // log information about the query, unless it is an introspection query
226- if (! str_contains ($ query , 'IntrospectionQuery ' )) {
219+ if (strpos ($ query , 'IntrospectionQuery ' ) === false ) {
227220 $ queryInformation = $ this ->logDataHelper ->getLogData ($ request , $ data , $ schema , $ this ->httpResponse );
228221 $ this ->loggerPool ->execute ($ queryInformation );
229222 }
@@ -254,30 +247,4 @@ private function getDataFromRequest(RequestInterface $request): array
254247
255248 return $ data ;
256249 }
257-
258- /**
259- * Retrieve http response code based on the error categories
260- *
261- * @param array $result
262- * @return int
263- */
264- private function getHttpResponseCode (array $ result ): int
265- {
266- if (empty ($ result ['errors ' ])) {
267- return 200 ;
268- }
269- foreach ($ result ['errors ' ] as $ error ) {
270- if (!isset ($ error ['extensions ' ]['category ' ])) {
271- continue ;
272- }
273- switch ($ error ['extensions ' ]['category ' ]) {
274- case GraphQlAuthenticationException::EXCEPTION_CATEGORY :
275- return 401 ;
276- case GraphQlAuthorizationException::EXCEPTION_CATEGORY :
277- return 403 ;
278- }
279- }
280-
281- return 200 ;
282- }
283250}
0 commit comments