88namespace Magento \GraphQl \Controller ;
99
1010use Magento \Framework \App \FrontControllerInterface ;
11+ use Magento \Framework \App \ObjectManager ;
1112use Magento \Framework \App \Request \Http ;
1213use Magento \Framework \App \RequestInterface ;
14+ use Magento \Framework \App \Response \Http as HttpResponse ;
1315use Magento \Framework \App \ResponseInterface ;
16+ use Magento \Framework \Controller \Result \JsonFactory ;
1417use Magento \Framework \GraphQl \Exception \ExceptionFormatter ;
18+ use Magento \Framework \GraphQl \Query \Fields as QueryFields ;
1519use Magento \Framework \GraphQl \Query \QueryProcessor ;
1620use Magento \Framework \GraphQl \Query \Resolver \ContextInterface ;
1721use Magento \Framework \GraphQl \Schema \SchemaGeneratorInterface ;
1822use Magento \Framework \Serialize \SerializerInterface ;
1923use Magento \Framework \Webapi \Response ;
20- use Magento \Framework \App \Response \Http as HttpResponse ;
21- use Magento \Framework \GraphQl \Query \Fields as QueryFields ;
22- use Magento \Framework \Controller \Result \JsonFactory ;
23- use Magento \Framework \App \ObjectManager ;
24+ use Magento \GraphQl \Helper \Query \Logger \LogData ;
2425use Magento \GraphQl \Model \Query \ContextFactoryInterface ;
26+ use Magento \GraphQl \Model \Query \Logger \LoggerPool ;
2527
2628/**
2729 * Front controller for web API GraphQL area.
@@ -89,6 +91,16 @@ class GraphQl implements FrontControllerInterface
8991 */
9092 private $ contextFactory ;
9193
94+ /**
95+ * @var LogData
96+ */
97+ private $ logDataHelper ;
98+
99+ /**
100+ * @var LoggerPool
101+ */
102+ private $ loggerPool ;
103+
92104 /**
93105 * @param Response $response
94106 * @param SchemaGeneratorInterface $schemaGenerator
@@ -101,6 +113,8 @@ class GraphQl implements FrontControllerInterface
101113 * @param JsonFactory|null $jsonFactory
102114 * @param HttpResponse|null $httpResponse
103115 * @param ContextFactoryInterface $contextFactory
116+ * @param LogData $logDataHelper
117+ * @param LoggerPool $loggerPool
104118 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
105119 */
106120 public function __construct (
@@ -114,7 +128,9 @@ public function __construct(
114128 QueryFields $ queryFields ,
115129 JsonFactory $ jsonFactory = null ,
116130 HttpResponse $ httpResponse = null ,
117- ContextFactoryInterface $ contextFactory = null
131+ ContextFactoryInterface $ contextFactory = null ,
132+ LogData $ logDataHelper = null ,
133+ LoggerPool $ loggerPool = null
118134 ) {
119135 $ this ->response = $ response ;
120136 $ this ->schemaGenerator = $ schemaGenerator ;
@@ -127,6 +143,8 @@ public function __construct(
127143 $ this ->jsonFactory = $ jsonFactory ?: ObjectManager::getInstance ()->get (JsonFactory::class);
128144 $ this ->httpResponse = $ httpResponse ?: ObjectManager::getInstance ()->get (HttpResponse::class);
129145 $ this ->contextFactory = $ contextFactory ?: ObjectManager::getInstance ()->get (ContextFactoryInterface::class);
146+ $ this ->logDataHelper = $ logDataHelper ?: ObjectManager::getInstance ()->get (LogData::class);
147+ $ this ->loggerPool = $ loggerPool ?: ObjectManager::getInstance ()->get (LoggerPool::class);
130148 }
131149
132150 /**
@@ -136,15 +154,18 @@ public function __construct(
136154 * @return ResponseInterface
137155 * @since 100.3.0
138156 */
139- public function dispatch (RequestInterface $ request ) : ResponseInterface
157+ public function dispatch (RequestInterface $ request ): ResponseInterface
140158 {
141159 $ statusCode = 200 ;
142160 $ jsonResult = $ this ->jsonFactory ->create ();
161+ $ data = $ this ->getDataFromRequest ($ request );
162+ $ result = [];
163+
164+ $ schema = null ;
143165 try {
144166 /** @var Http $request */
145167 $ this ->requestProcessor ->validateRequest ($ request );
146168
147- $ data = $ this ->getDataFromRequest ($ request );
148169 $ query = $ data ['query ' ] ?? '' ;
149170 $ variables = $ data ['variables ' ] ?? null ;
150171
@@ -160,14 +181,21 @@ public function dispatch(RequestInterface $request) : ResponseInterface
160181 $ data ['variables ' ] ?? []
161182 );
162183 } catch (\Exception $ error ) {
163- $ result ['errors ' ] = isset ($ result) && isset ( $ result ['errors ' ]) ? $ result ['errors ' ] : [];
184+ $ result ['errors ' ] = isset ($ result ['errors ' ]) ? $ result ['errors ' ] : [];
164185 $ result ['errors ' ][] = $ this ->graphQlError ->create ($ error );
165186 $ statusCode = ExceptionFormatter::HTTP_GRAPH_QL_SCHEMA_ERROR_STATUS ;
166187 }
167188
168189 $ jsonResult ->setHttpResponseCode ($ statusCode );
169190 $ jsonResult ->setData ($ result );
170191 $ jsonResult ->renderResult ($ this ->httpResponse );
192+
193+ // log information about the query, unless it is an introspection query
194+ if (strpos ($ data ['query ' ], 'IntrospectionQuery ' ) === false ) {
195+ $ queryInformation = $ this ->logDataHelper ->getLogData ($ request , $ data , $ schema , $ this ->httpResponse );
196+ $ this ->loggerPool ->execute ($ queryInformation );
197+ }
198+
171199 return $ this ->httpResponse ;
172200 }
173201
@@ -177,7 +205,7 @@ public function dispatch(RequestInterface $request) : ResponseInterface
177205 * @param RequestInterface $request
178206 * @return array
179207 */
180- private function getDataFromRequest (RequestInterface $ request ) : array
208+ private function getDataFromRequest (RequestInterface $ request ): array
181209 {
182210 /** @var Http $request */
183211 if ($ request ->isPost ()) {
0 commit comments