Skip to content

Commit 5d1ecac

Browse files
committed
Merge branch 'graphql-issue-230-dispatch' into graphql-issue-230
2 parents fc3714a + 0a0b32b commit 5d1ecac

File tree

23 files changed

+57
-121
lines changed

23 files changed

+57
-121
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Category/CategoriesIdentity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class CategoriesIdentity implements IdentityInterface
1818
* Get category IDs from resolved data
1919
*
2020
* @param array $resolvedData
21-
* @return array
21+
* @return string[]
2222
*/
2323
public function getIdentities(array $resolvedData): array
2424
{

app/code/Magento/CatalogGraphQl/Model/Resolver/Category/CategoryTreeIdentity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class CategoryTreeIdentity implements IdentityInterface
1818
* Get category ID from resolved data
1919
*
2020
* @param array $resolvedData
21-
* @return array
21+
* @return string[]
2222
*/
2323
public function getIdentities(array $resolvedData): array
2424
{

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/Identity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Identity implements IdentityInterface
1818
* Get product ids for cache tag
1919
*
2020
* @param array $resolvedData
21-
* @return array
21+
* @return string[]
2222
*/
2323
public function getIdentities(array $resolvedData): array
2424
{

app/code/Magento/CmsGraphQl/Model/Resolver/Block/Identity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Identity implements IdentityInterface
1919
* Get block identities from resolved data
2020
*
2121
* @param array $resolvedData
22-
* @return array
22+
* @return string[]
2323
*/
2424
public function getIdentities(array $resolvedData): array
2525
{

app/code/Magento/CmsGraphQl/Model/Resolver/Page/Identity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Identity implements IdentityInterface
1919
* Get page ID from resolved data
2020
*
2121
* @param array $resolvedData
22-
* @return array
22+
* @return string[]
2323
*/
2424
public function getIdentities(array $resolvedData): array
2525
{

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
use Magento\Framework\App\FrontControllerInterface;
1111
use Magento\Framework\App\Request\Http;
1212
use Magento\Framework\App\RequestInterface;
13+
use Magento\Framework\App\ResponseInterface;
1314
use Magento\Framework\GraphQl\Exception\ExceptionFormatter;
1415
use Magento\Framework\GraphQl\Query\QueryProcessor;
1516
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
1617
use Magento\Framework\GraphQl\Schema\SchemaGeneratorInterface;
1718
use Magento\Framework\Serialize\SerializerInterface;
18-
use Magento\Framework\Controller\ResultInterface;
1919
use Magento\Framework\Webapi\Response;
20+
use Magento\Framework\App\Response\Http as HttpResponse;
2021
use Magento\Framework\GraphQl\Query\Fields as QueryFields;
2122
use Magento\Framework\Controller\Result\JsonFactory;
2223
use Magento\Framework\App\ObjectManager;
@@ -30,7 +31,7 @@
3031
class GraphQl implements FrontControllerInterface
3132
{
3233
/**
33-
* @var Response
34+
* @var \Magento\Framework\Webapi\Response
3435
* @deprecated
3536
*/
3637
private $response;
@@ -75,6 +76,11 @@ class GraphQl implements FrontControllerInterface
7576
*/
7677
private $jsonFactory;
7778

79+
/**
80+
* @var HttpResponse
81+
*/
82+
private $httpResponse;
83+
7884
/**
7985
* @param Response $response
8086
* @param SchemaGeneratorInterface $schemaGenerator
@@ -85,6 +91,8 @@ class GraphQl implements FrontControllerInterface
8591
* @param HttpRequestProcessor $requestProcessor
8692
* @param QueryFields $queryFields
8793
* @param JsonFactory|null $jsonFactory
94+
* @param HttpResponse|null $httpResponse
95+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8896
*/
8997
public function __construct(
9098
Response $response,
@@ -95,7 +103,8 @@ public function __construct(
95103
ContextInterface $resolverContext,
96104
HttpRequestProcessor $requestProcessor,
97105
QueryFields $queryFields,
98-
JsonFactory $jsonFactory = null
106+
JsonFactory $jsonFactory = null,
107+
HttpResponse $httpResponse = null
99108
) {
100109
$this->response = $response;
101110
$this->schemaGenerator = $schemaGenerator;
@@ -105,16 +114,17 @@ public function __construct(
105114
$this->resolverContext = $resolverContext;
106115
$this->requestProcessor = $requestProcessor;
107116
$this->queryFields = $queryFields;
108-
$this->jsonFactory = $jsonFactory ?:ObjectManager::getInstance()->get(JsonFactory::class);
117+
$this->jsonFactory = $jsonFactory ?: ObjectManager::getInstance()->get(JsonFactory::class);
118+
$this->httpResponse = $httpResponse ?: ObjectManager::getInstance()->get(HttpResponse::class);
109119
}
110120

111121
/**
112122
* Handle GraphQL request
113123
*
114124
* @param RequestInterface $request
115-
* @return ResultInterface
125+
* @return ResponseInterface
116126
*/
117-
public function dispatch(RequestInterface $request)
127+
public function dispatch(RequestInterface $request) : ResponseInterface
118128
{
119129
$statusCode = 200;
120130
$jsonResult = $this->jsonFactory->create();
@@ -145,7 +155,8 @@ public function dispatch(RequestInterface $request)
145155

146156
$jsonResult->setHttpResponseCode($statusCode);
147157
$jsonResult->setData($result);
148-
return $jsonResult;
158+
$jsonResult->renderResult($this->httpResponse);
159+
return $this->httpResponse;
149160
}
150161

151162
/**

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

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

1010
use Magento\Framework\App\FrontControllerInterface;
1111
use Magento\Framework\App\RequestInterface;
12-
use Magento\Framework\App\ResponseInterface;
1312
use Magento\GraphQlCache\Model\CacheableQuery;
1413
use Magento\Framework\App\Response\Http as HttpResponse;
1514
use Magento\Framework\Controller\ResultInterface;
1615
use Magento\PageCache\Model\Config;
1716
use Magento\GraphQl\Controller\HttpRequestProcessor;
17+
use Magento\Framework\App\Response\Http as ResponseHttp;
1818

1919
/**
20-
* Class Plugin
20+
* Plugin for handling controller after controller tags and pre-controller validation.
2121
*/
2222
class GraphQl
2323
{
@@ -76,24 +76,19 @@ public function beforeDispatch(
7676
}
7777

7878
/**
79-
* Plugin for GraphQL after dispatch to set tag and cache headers
79+
* Plugin for GraphQL after render from dispatch to set tag and cache headers
8080
*
81-
* The $response doesn't have a set type because it's alternating between ResponseInterface and ResultInterface
82-
* depending if it comes from builtin cache or the dispatch.
81+
* @param ResultInterface $subject
82+
* @param ResultInterface $result
83+
* @param ResponseHttp $response
84+
* @return ResultInterface
8385
*
84-
* @param FrontControllerInterface $subject
85-
* @param ResponseInterface|ResultInterface $response
86-
* @param RequestInterface $request
87-
* @return ResponseInterface|ResultInterface
8886
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
8987
*/
90-
public function afterDispatch(
91-
FrontControllerInterface $subject,
92-
$response,
93-
RequestInterface $request
94-
) {
88+
public function afterRenderResult(ResultInterface $subject, ResultInterface $result, ResponseHttp $response)
89+
{
9590
$sendNoCacheHeaders = false;
96-
if ($this->config->isEnabled() && $request->isGet()) {
91+
if ($this->config->isEnabled()) {
9792
if ($this->cacheableQuery->shouldPopulateCacheHeadersWithTags()) {
9893
$this->response->setPublicHeaders($this->config->getTtl());
9994
$this->response->setHeader('X-Magento-Tags', implode(',', $this->cacheableQuery->getCacheTags()), true);
@@ -108,6 +103,6 @@ public function afterDispatch(
108103
$this->response->setNoCacheHeaders();
109104
}
110105

111-
return $response;
106+
return $result;
112107
}
113108
}

app/code/Magento/GraphQlCache/etc/graphql/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
type="Magento\GraphQlCache\Model\Plugin\App\PageCache\Identifier" sortOrder="1"/>
2020
</type>
2121
<type name="Magento\Framework\Controller\ResultInterface">
22+
<plugin name="graphql-result-plugin" type="Magento\GraphQlCache\Controller\Plugin\GraphQl"/>
2223
<plugin name="result-builtin-cache" type="Magento\PageCache\Model\Controller\Result\BuiltinPlugin"/>
2324
<plugin name="result-varnish-cache" type="Magento\PageCache\Model\Controller\Result\VarnishPlugin"/>
2425
</type>

dev/tests/integration/testsuite/Magento/Framework/GraphQl/Config/GraphQlReaderTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,7 @@ enumValues(includeDeprecated: true) {
187187
->addHeaders(['Content-Type' => 'application/json']);
188188
$request->setHeaders($headers);
189189

190-
/** @var \Magento\Framework\App\Response\Http $response */
191-
$response = $this->objectManager->create(\Magento\Framework\App\Response\Http::class);
192-
/** @var \Magento\Framework\Controller\Result\Json $result */
193-
$result = $this->graphQlController->dispatch($request);
194-
$result->renderResult($response);
190+
$response = $this->graphQlController->dispatch($request);
195191
$output = $this->jsonSerializer->unserialize($response->getContent());
196192
$expectedOutput = require __DIR__ . '/../_files/schema_response_sdl_description.php';
197193

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

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,7 @@ public function testDispatch() : void
101101
$headers = $this->objectManager->create(\Zend\Http\Headers::class)
102102
->addHeaders(['Content-Type' => 'application/json']);
103103
$this->request->setHeaders($headers);
104-
/** @var \Magento\Framework\App\Response\Http $response */
105-
$response = $this->objectManager->create(\Magento\Framework\App\Response\Http::class);
106-
/** @var \Magento\Framework\Controller\Result\Json $result */
107-
$result = $this->graphql->dispatch($this->request);
108-
$result->renderResult($response);
104+
$response = $this->graphql->dispatch($this->request);
109105
$output = $this->jsonSerializer->unserialize($response->getContent());
110106
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
111107

@@ -147,11 +143,7 @@ public function testDispatchWithGet() : void
147143
$this->request->setPathInfo('/graphql');
148144
$this->request->setMethod('GET');
149145
$this->request->setQueryValue('query', $query);
150-
/** @var \Magento\Framework\App\Response\Http $response */
151-
$response = $this->objectManager->create(\Magento\Framework\App\Response\Http::class);
152-
/** @var \Magento\Framework\Controller\Result\Json $result */
153-
$result = $this->graphql->dispatch($this->request);
154-
$result->renderResult($response);
146+
$response = $this->graphql->dispatch($this->request);
155147
$output = $this->jsonSerializer->unserialize($response->getContent());
156148
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
157149

@@ -203,11 +195,6 @@ public function testDispatchGetWithParameterizedVariables() : void
203195
$this->request->setMethod('GET');
204196
$this->request->setParams($queryParams);
205197
$response = $this->graphql->dispatch($this->request);
206-
/** @var \Magento\Framework\App\Response\Http $response */
207-
$response = $this->objectManager->create(\Magento\Framework\App\Response\Http::class);
208-
/** @var \Magento\Framework\Controller\Result\Json $result */
209-
$result = $this->graphql->dispatch($this->request);
210-
$result->renderResult($response);
211198
$output = $this->jsonSerializer->unserialize($response->getContent());
212199
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
213200

@@ -257,11 +244,7 @@ public function testError() : void
257244
$headers = $this->objectManager->create(\Zend\Http\Headers::class)
258245
->addHeaders(['Content-Type' => 'application/json']);
259246
$this->request->setHeaders($headers);
260-
/** @var \Magento\Framework\App\Response\Http $response */
261-
$response = $this->objectManager->create(\Magento\Framework\App\Response\Http::class);
262-
/** @var \Magento\Framework\Controller\Result\Json $result */
263-
$result = $this->graphql->dispatch($this->request);
264-
$result->renderResult($response);
247+
$response = $this->graphql->dispatch($this->request);
265248
$outputResponse = $this->jsonSerializer->unserialize($response->getContent());
266249
if (isset($outputResponse['errors'][0])) {
267250
if (is_array($outputResponse['errors'][0])) {

0 commit comments

Comments
 (0)