Skip to content

Commit 3d43dc6

Browse files
committed
ACP2E-3467: [Cloud] 500 response to empty Graphql response on 2.4.7
1 parent 88660e7 commit 3d43dc6

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

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

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<?php
2-
32
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
3+
* Copyright 2017 Adobe
4+
* All Rights Reserved.
65
*/
7-
86
declare(strict_types=1);
97

108
namespace Magento\GraphQl\Controller;
@@ -181,38 +179,44 @@ public function dispatch(RequestInterface $request): ResponseInterface
181179
{
182180
$this->areaList->getArea(Area::AREA_GRAPHQL)->load(Area::PART_TRANSLATE);
183181

184-
$statusCode = 200;
182+
$statusCode = 204;
185183
$jsonResult = $this->jsonFactory->create();
186184
$data = $this->getDataFromRequest($request);
187-
$result = [];
185+
$result = null;
188186

189187
$schema = null;
190188
$query = $data['query'] ?? '';
191189
try {
192190
/** @var Http $request */
193191
$this->requestProcessor->validateRequest($request);
194-
$parsedQuery = $this->queryParser->parse($query);
195-
$data['parsedQuery'] = $parsedQuery;
196-
197-
// We must extract queried field names to avoid instantiation of unnecessary fields in webonyx schema
198-
// Temporal coupling is required for performance optimization
199-
$this->queryFields->setQuery($parsedQuery, $data['variables'] ?? null);
200-
$schema = $this->schemaGenerator->generate();
201-
202-
$result = $this->queryProcessor->process(
203-
$schema,
204-
$parsedQuery,
205-
$this->contextFactory->create(),
206-
$data['variables'] ?? []
207-
);
192+
if ($request->isGet() || $request->isPost()) {
193+
$parsedQuery = $this->queryParser->parse($query);
194+
$data['parsedQuery'] = $parsedQuery;
195+
196+
// We must extract queried field names to avoid instantiation of unnecessary fields in webonyx schema
197+
// Temporal coupling is required for performance optimization
198+
$this->queryFields->setQuery($parsedQuery, $data['variables'] ?? null);
199+
$schema = $this->schemaGenerator->generate();
200+
201+
$result = $this->queryProcessor->process(
202+
$schema,
203+
$parsedQuery,
204+
$this->contextFactory->create(),
205+
$data['variables'] ?? []
206+
);
207+
$statusCode = 200;
208+
}
208209
} catch (\Exception $error) {
209-
$result['errors'] = isset($result['errors']) ? $result['errors'] : [];
210-
$result['errors'][] = $this->graphQlError->create($error);
210+
$result = [
211+
'errors' => [$this->graphQlError->create($error)],
212+
];
211213
$statusCode = ExceptionFormatter::HTTP_GRAPH_QL_SCHEMA_ERROR_STATUS;
212214
}
213215

214216
$jsonResult->setHttpResponseCode($statusCode);
215-
$jsonResult->setData($result);
217+
if ($result !== null) {
218+
$jsonResult->setData($result);
219+
}
216220
$jsonResult->renderResult($this->httpResponse);
217221

218222
// log information about the query, unless it is an introspection query

dev/tests/integration/testsuite/Magento/GraphQl/Controller/GraphQlControllerTest.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2018 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -175,7 +175,7 @@ public function testDispatchGetWithParameterizedVariables() : void
175175
id
176176
name
177177
sku
178-
}
178+
}
179179
}
180180
}
181181
QUERY;
@@ -223,12 +223,12 @@ public function testError() : void
223223
}
224224
])
225225
{
226-
items{
226+
items{
227227
attribute_code
228228
attribute_type
229229
entity_type
230-
}
231-
}
230+
}
231+
}
232232
}
233233
QUERY;
234234

@@ -265,4 +265,13 @@ public function testError() : void
265265
}
266266
}
267267
}
268+
269+
public function testDispatchWithOptions(): void
270+
{
271+
$this->request->setPathInfo('/graphql');
272+
$this->request->setMethod('OPTIONS');
273+
$response = $this->graphql->dispatch($this->request);
274+
self::assertEquals(204, $response->getStatusCode());
275+
self::assertEmpty($response->getContent());
276+
}
268277
}

0 commit comments

Comments
 (0)