Skip to content

Commit 4e62e39

Browse files
committed
PWA-1311: New Relic is not being given useful transaction names for graphql requests
- fix build failures
1 parent 0caeae8 commit 4e62e39

File tree

2 files changed

+38
-32
lines changed

2 files changed

+38
-32
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ public function dispatch(RequestInterface $request): ResponseInterface
161161
$data = $this->getDataFromRequest($request);
162162
$result = [];
163163

164+
$schema = null;
164165
try {
165166
/** @var Http $request */
166167
$this->requestProcessor->validateRequest($request);
@@ -180,7 +181,7 @@ public function dispatch(RequestInterface $request): ResponseInterface
180181
$data['variables'] ?? []
181182
);
182183
} catch (\Exception $error) {
183-
$result['errors'] = isset($result) && isset($result['errors']) ? $result['errors'] : [];
184+
$result['errors'] = isset($result['errors']) ? $result['errors'] : [];
184185
$result['errors'][] = $this->graphQlError->create($error);
185186
$statusCode = ExceptionFormatter::HTTP_GRAPH_QL_SCHEMA_ERROR_STATUS;
186187
}

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

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,15 @@ class LogData
2828
*
2929
* @param RequestInterface $request
3030
* @param array $data
31-
* @param Schema $schema
32-
* @param HttpResponse $response
31+
* @param Schema|null $schema
32+
* @param HttpResponse|null $response
3333
* @return array
34-
*
35-
* @throws SyntaxError
3634
*/
3735
public function getRequestInformation(
3836
RequestInterface $request,
3937
array $data,
40-
Schema $schema,
41-
HttpResponse $response
38+
Schema $schema = null,
39+
HttpResponse $response = null
4240
) : array {
4341
$requestInformation = [];
4442
$requestInformation[LoggerInterface::HTTP_METHOD] = $request->getMethod();
@@ -51,18 +49,23 @@ public function getRequestInformation(
5149
: 'false';
5250
$requestInformation[LoggerInterface::REQUEST_LENGTH] = $request->getHeader('Content-Length') ?: '';
5351

54-
$schemaConfig = $schema->getConfig();
55-
$mutationOperations = $schemaConfig->getMutation()->getFields();
56-
$queryOperations = $schemaConfig->getQuery()->getFields();
57-
$requestInformation[LoggerInterface::HAS_MUTATION] = count($mutationOperations) > 0 ? 'true' : 'false';
58-
$requestInformation[LoggerInterface::NUMBER_OF_OPERATIONS] =
59-
count($mutationOperations) + count($queryOperations);
52+
if ($schema) {
53+
$schemaConfig = $schema->getConfig();
54+
$mutationOperations = $schemaConfig->getMutation()->getFields();
55+
$queryOperations = $schemaConfig->getQuery()->getFields();
56+
$requestInformation[LoggerInterface::HAS_MUTATION] = count($mutationOperations) > 0 ? 'true' : 'false';
57+
$requestInformation[LoggerInterface::NUMBER_OF_OPERATIONS] =
58+
count($mutationOperations) + count($queryOperations);
59+
$operationNames = array_merge(array_keys($mutationOperations), array_keys($queryOperations));
60+
$requestInformation[LoggerInterface::OPERATION_NAMES] =
61+
count($operationNames) > 0 ? implode(",", $operationNames) : 'operationNameNotFound';
62+
}
6063

61-
$operationNames = array_merge(array_keys($mutationOperations), array_keys($queryOperations));
62-
$requestInformation[LoggerInterface::OPERATION_NAMES] =
63-
count($operationNames) > 0 ? implode(",", $operationNames) : 'operationNameNotFound';
6464
$requestInformation[LoggerInterface::COMPLEXITY] = $this->getFieldCount($data['query'] ?? '');
65-
$requestInformation[LoggerInterface::HTTP_RESPONSE_CODE] = $response->getHttpResponseCode();
65+
66+
if ($response) {
67+
$requestInformation[LoggerInterface::HTTP_RESPONSE_CODE] = $response->getHttpResponseCode();
68+
}
6669

6770
return $requestInformation;
6871
}
@@ -74,25 +77,27 @@ public function getRequestInformation(
7477
*
7578
* @param string $query
7679
* @return int
77-
* @throws SyntaxError
78-
* @throws \Exception
7980
*/
8081
private function getFieldCount(string $query): int
8182
{
82-
if (!empty($query)) {
83-
$totalFieldCount = 0;
84-
$queryAst = Parser::parse(new Source($query ?: '', 'GraphQL'));
85-
Visitor::visit(
86-
$queryAst,
87-
[
88-
'leave' => [
89-
NodeKind::FIELD => function (Node $node) use (&$totalFieldCount) {
90-
$totalFieldCount++;
91-
}
83+
try {
84+
if (!empty($query)) {
85+
$totalFieldCount = 0;
86+
$queryAst = Parser::parse(new Source($query ?: '', 'GraphQL'));
87+
Visitor::visit(
88+
$queryAst,
89+
[
90+
'leave' => [
91+
NodeKind::FIELD => function (Node $node) use (&$totalFieldCount) {
92+
$totalFieldCount++;
93+
}
94+
]
9295
]
93-
]
94-
);
95-
return $totalFieldCount;
96+
);
97+
return $totalFieldCount;
98+
}
99+
} catch (SyntaxError $syntaxError) {
100+
} catch (\Exception $exception) {
96101
}
97102
return 0;
98103
}

0 commit comments

Comments
 (0)