Skip to content

Commit 0c52ca4

Browse files
committed
feat!: replace plugin with LoggerInterface implementation
BREAKING CHANGE: We no longer plugin around the query processor (which is a good thing). However, Magento's out-of-box log data doesn't give the exact query sent. This is likely due to the fact that it is possible for mutation data like user passwords to appear in the log if developers fail to use variables appropriately. However, I'm not going to compensate for naive developers. To keep existing functionality, we've added the full query (not variables) to the log data when this package is enabled. I would avoid using this in production environments if at all possible if you're concerned about the quality of your development team.
1 parent b39a2ad commit 0c52ca4

File tree

4 files changed

+65
-28
lines changed

4 files changed

+65
-28
lines changed
Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,47 @@
11
<?php
22

3-
namespace Graycore\GraphQlLogger\Plugin;
3+
namespace Graycore\GraphQlLogger\Logger;
4+
5+
use Graycore\GraphQlLogger\Model\Config;
6+
use Magento\GraphQl\Model\Query\Logger\LoggerInterface;
47

58
use Graycore\GraphQlLogger\Api\LogRepositoryInterface;
69
use Graycore\GraphQlLogger\Api\Data\LogInterfaceFactory as LogFactory;
7-
use Graycore\GraphQlLogger\Model\Config;
8-
use Magento\Framework\App\RequestInterface;
910
use Magento\Framework\Exception\NoSuchEntityException;
10-
use Magento\Framework\GraphQl\Query\QueryProcessor;
11-
use Magento\Framework\GraphQl\Schema;
1211
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
1312

14-
class LoggerPlugin
13+
class Logger implements LoggerInterface
1514
{
1615
private LogRepositoryInterface $logRepository;
1716
private LogFactory $logFactory;
18-
private RequestInterface $request;
1917
private TimezoneInterface $date;
2018
private Config $config;
2119

20+
/**
21+
* @param Config $config
22+
*/
2223
public function __construct(
23-
RequestInterface $request,
2424
LogRepositoryInterface $logRepository,
2525
LogFactory $logFactory,
2626
TimezoneInterface $date,
2727
Config $config
2828
) {
29-
$this->request = $request;
3029
$this->logRepository = $logRepository;
3130
$this->logFactory = $logFactory;
3231
$this->date = $date;
3332
$this->config = $config;
3433
}
3534

36-
public function afterProcess(
37-
QueryProcessor $queryProcessor,
38-
array $value,
39-
Schema $schema,
40-
string $source
41-
): array {
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function execute(array $queryDetails)
39+
{
4240
if (!$this->config->isEnabled()) {
43-
return $value;
41+
return;
4442
}
4543

46-
$hash = hash('sha256', $source);
44+
$hash = hash('sha256', $queryDetails['query']);
4745

4846
try {
4947
$log = $this->logRepository->getByHash($hash);
@@ -52,11 +50,9 @@ public function afterProcess(
5250
}
5351

5452
$log->setHash($hash)
55-
->setMethod($this->request->getMethod())
56-
->setQuery($source)
53+
->setMethod($queryDetails[LoggerInterface::HTTP_METHOD])
54+
->setQuery($queryDetails['query'])
5755
->setUpdated($this->date->date()->format('Y-m-d H:i:s'));
5856
$this->logRepository->save($log);
59-
60-
return $value;
6157
}
6258
}

Plugin/LogDataPlugin.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Graycore\GraphQlLogger\Plugin;
4+
5+
use Graycore\GraphQlLogger\Model\Config;
6+
use Magento\Framework\App\RequestInterface;
7+
use Magento\GraphQl\Helper\Query\Logger\LogData;
8+
9+
class LogDataPlugin
10+
{
11+
private Config $config;
12+
13+
/**
14+
* @param Config $config
15+
*/
16+
public function __construct(
17+
Config $config
18+
) {
19+
$this->config = $config;
20+
}
21+
22+
public function afterGetLogData(
23+
LogData $logdata,
24+
array $result,
25+
RequestInterface $request,
26+
array $data
27+
): array {
28+
if (!$this->config->isEnabled()) {
29+
return $result;
30+
}
31+
32+
/**
33+
* Add the query to the data.
34+
*/
35+
$result['query'] = $data['query'];
36+
return $result;
37+
}
38+
}

etc/di.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,14 @@
33
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
44
<preference for="Graycore\GraphQlLogger\Api\LogRepositoryInterface" type="Graycore\GraphQlLogger\Model\LogRepository"/>
55
<preference for="Graycore\GraphQlLogger\Api\Data\LogInterface" type="Graycore\GraphQlLogger\Model\Log"/>
6+
<type name="Magento\GraphQl\Model\Query\Logger\LoggerPool">
7+
<arguments>
8+
<argument name="loggers" xsi:type="array">
9+
<item name="graycoreGraphqlLogger" xsi:type="object">Graycore\GraphQlLogger\Logger\Logger</item>
10+
</argument>
11+
</arguments>
12+
</type>
13+
<type name="Magento\GraphQl\Helper\Query\Logger\LogData">
14+
<plugin name="Graycore_GraphQlLogger::additionalLogData" type="Graycore\GraphQlLogger\Plugin\LogDataPlugin"/>
15+
</type>
616
</config>

etc/graphql/di.xml

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)