Skip to content

Commit 7eaa67d

Browse files
committed
B2B-2606: Graphql Parser called at least 3 times per request
1 parent c31925f commit 7eaa67d

File tree

5 files changed

+34
-20
lines changed

5 files changed

+34
-20
lines changed

app/code/Magento/GraphQl/Controller/GraphQl.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class GraphQl implements FrontControllerInterface
4141
/**
4242
* @var \Magento\Framework\Webapi\Response
4343
* @deprecated 100.3.2
44+
* @see was only added to fix the constructor
4445
*/
4546
private $response;
4647

@@ -66,7 +67,8 @@ class GraphQl implements FrontControllerInterface
6667

6768
/**
6869
* @var ContextInterface
69-
* @deprecated 100.3.3 $contextFactory is used for creating Context object
70+
* @deprecated 100.3.3
71+
* @see $contextFactory is used for creating Context object
7072
*/
7173
private $resolverContext;
7274

@@ -185,12 +187,14 @@ public function dispatch(RequestInterface $request): ResponseInterface
185187

186188
// We must extract queried field names to avoid instantiation of unnecessary fields in webonyx schema
187189
// Temporal coupling is required for performance optimization
188-
$this->queryFields->setQuery($query, $variables);
190+
$data['parsedQuery'] =
191+
\GraphQL\Language\Parser::parse(new \GraphQL\Language\Source($query ?: '', 'GraphQL'));
192+
$this->queryFields->setQuery($data['parsedQuery'], $variables);
189193
$schema = $this->schemaGenerator->generate();
190194

191195
$result = $this->queryProcessor->process(
192196
$schema,
193-
$query,
197+
$data['parsedQuery'],
194198
$this->contextFactory->create(),
195199
$data['variables'] ?? []
196200
);

app/code/Magento/GraphQl/Helper/Query/Logger/LogData.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\GraphQl\Helper\Query\Logger;
99

1010
use GraphQL\Error\SyntaxError;
11+
use GraphQL\Language\AST\DocumentNode;
1112
use GraphQL\Language\AST\Node;
1213
use GraphQL\Language\AST\NodeKind;
1314
use GraphQL\Language\Parser;
@@ -43,7 +44,7 @@ public function getLogData(
4344
$logData = array_merge($logData, $this->gatherRequestInformation($request));
4445

4546
try {
46-
$complexity = $this->getFieldCount($data['query'] ?? '');
47+
$complexity = $this->getFieldCount($data['parsedQuery'] ?? $data['query'] ?? '');
4748
$logData[LoggerInterface::COMPLEXITY] = $complexity;
4849
if ($schema) {
4950
$logData = array_merge($logData, $this->gatherQueryInformation($schema));
@@ -114,18 +115,20 @@ private function gatherResponseInformation(HttpResponse $response) : array
114115
*
115116
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
116117
*
117-
* @param string $query
118+
* @param DocumentNode|string $query
118119
* @return int
119120
* @throws SyntaxError
120-
* @throws /Exception
121+
* @throws \Exception
121122
*/
122-
private function getFieldCount(string $query): int
123+
private function getFieldCount(DocumentNode|string $query): int
123124
{
124125
if (!empty($query)) {
125126
$totalFieldCount = 0;
126-
$queryAst = Parser::parse(new Source($query ?: '', 'GraphQL'));
127+
if (is_string($query)) {
128+
$query = Parser::parse(new Source($query, 'GraphQL'));
129+
}
127130
Visitor::visit(
128-
$queryAst,
131+
$query,
129132
[
130133
'leave' => [
131134
NodeKind::FIELD => function (Node $node) use (&$totalFieldCount) {

lib/internal/Magento/Framework/GraphQl/Query/Fields.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Framework\GraphQl\Query;
99

10+
use GraphQL\Language\AST\DocumentNode;
1011
use GraphQL\Language\AST\Node;
1112
use GraphQL\Language\AST\NodeKind;
1213

@@ -23,7 +24,7 @@ class Fields
2324
/**
2425
* Set Query for extracting list of fields.
2526
*
26-
* @param string $query
27+
* @param DocumentNode|string $query
2728
* @param array|null $variables
2829
*
2930
* @return void
@@ -32,9 +33,11 @@ public function setQuery($query, array $variables = null)
3233
{
3334
$queryFields = [];
3435
try {
35-
$queryAst = \GraphQL\Language\Parser::parse(new \GraphQL\Language\Source($query ?: '', 'GraphQL'));
36+
if (is_string($query)) {
37+
$query = \GraphQL\Language\Parser::parse(new \GraphQL\Language\Source($query ?: '', 'GraphQL'));
38+
}
3639
\GraphQL\Language\Visitor::visit(
37-
$queryAst,
40+
$query,
3841
[
3942
'leave' => [
4043
NodeKind::NAME => function (Node $node) use (&$queryFields) {

lib/internal/Magento/Framework/GraphQl/Query/QueryComplexityLimiter.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Magento\Framework\GraphQl\Query;
99

10-
use GraphQL\Language\AST\Node;
10+
use GraphQL\Language\AST\DocumentNode;
1111
use GraphQL\Language\AST\NodeKind;
1212
use GraphQL\Language\Parser;
1313
use GraphQL\Language\Source;
@@ -80,19 +80,22 @@ public function execute(): void
8080
* This is necessary for performance optimization, as extremely large queries require a substantial
8181
* amount of time to fully validate and can affect server performance.
8282
*
83-
* @param string $query
83+
* @param DocumentNode|string $query
8484
* @throws GraphQlInputException
8585
*/
86-
public function validateFieldCount(string $query): void
86+
public function validateFieldCount(DocumentNode|string $query): void
8787
{
8888
if (!empty($query)) {
8989
$totalFieldCount = 0;
90-
$queryAst = Parser::parse(new Source($query ?: '', 'GraphQL'));
90+
if (is_string($query)) {
91+
$query = Parser::parse(new Source($query, 'GraphQL'));
92+
}
93+
9194
Visitor::visit(
92-
$queryAst,
95+
$query,
9396
[
9497
'leave' => [
95-
NodeKind::FIELD => function (Node $node) use (&$totalFieldCount) {
98+
NodeKind::FIELD => function () use (&$totalFieldCount) {
9699
$totalFieldCount++;
97100
}
98101
]

lib/internal/Magento/Framework/GraphQl/Query/QueryProcessor.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use GraphQL\Error\DebugFlag;
1111
use GraphQL\GraphQL;
12+
use GraphQL\Language\AST\DocumentNode;
1213
use Magento\Framework\GraphQl\Exception\ExceptionFormatter;
1314
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1415
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
@@ -54,7 +55,7 @@ public function __construct(
5455
* Process a GraphQl query according to defined schema
5556
*
5657
* @param Schema $schema
57-
* @param string $source
58+
* @param DocumentNode|string $source
5859
* @param ContextInterface|null $contextValue
5960
* @param array|null $variableValues
6061
* @param string|null $operationName
@@ -63,7 +64,7 @@ public function __construct(
6364
*/
6465
public function process(
6566
Schema $schema,
66-
string $source,
67+
DocumentNode|string $source,
6768
ContextInterface $contextValue = null,
6869
array $variableValues = null,
6970
string $operationName = null

0 commit comments

Comments
 (0)