Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit a418a76

Browse files
ENGCOM-1585: Return full canonical product URL within product object #7
- Merge Pull Request magento/graphql-ce#7 from austris-argalis/graphql-ce:issue-2 - Merged commits: 1. d6c15d6 2. 94fb609 3. 6c04a04
2 parents 57b5fdb + 6c04a04 commit a418a76

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Model\Resolver\Product;
9+
10+
use Magento\Catalog\Model\Product;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Query\Resolver\Value;
13+
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
14+
use Magento\Framework\GraphQl\Query\ResolverInterface;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
17+
/**
18+
* Resolve data for product canonical URL
19+
*/
20+
class CanonicalUrl implements ResolverInterface
21+
{
22+
/**
23+
* @var ValueFactory
24+
*/
25+
private $valueFactory;
26+
27+
/**
28+
* @param ValueFactory $valueFactory
29+
*/
30+
public function __construct(
31+
ValueFactory $valueFactory
32+
) {
33+
$this->valueFactory = $valueFactory;
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function resolve(
40+
Field $field,
41+
$context,
42+
ResolveInfo $info,
43+
array $value = null,
44+
array $args = null
45+
): Value {
46+
if (!isset($value['model'])) {
47+
$result = function () {
48+
return null;
49+
};
50+
return $this->valueFactory->create($result);
51+
}
52+
53+
/* @var $product Product */
54+
$product = $value['model'];
55+
$url = $product->getUrlModel()->getUrl($product, ['_ignore_category' => true]);
56+
$result = function () use ($url) {
57+
return $url;
58+
};
59+
60+
return $this->valueFactory->create($result);
61+
}
62+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Test\Unit\Model\Resolver\Product;
9+
10+
use Magento\CatalogGraphQl\Model\Resolver\Product\CanonicalUrl;
11+
use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator;
12+
use Magento\Framework\GraphQl\Query\Resolver\Value;
13+
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use PHPUnit\Framework\TestCase;
17+
18+
class CanonicalUrlTest extends TestCase
19+
{
20+
/**
21+
* @var ObjectManager
22+
*/
23+
private $objectManager;
24+
25+
/**
26+
* @var CanonicalUrl
27+
*/
28+
private $subject;
29+
30+
/**
31+
* @var \PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
private $mockValueFactory;
34+
35+
/**
36+
* @var \PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
private $mockStoreManager;
39+
40+
public function testReturnsNullWhenNoProductAvailable()
41+
{
42+
$mockField = $this->getMockBuilder(\Magento\Framework\GraphQl\Config\Element\Field::class)
43+
->disableOriginalConstructor()
44+
->getMock();
45+
$mockInfo = $this->getMockBuilder(\Magento\Framework\GraphQl\Schema\Type\ResolveInfo::class)
46+
->disableOriginalConstructor()
47+
->getMock();
48+
49+
$this->mockValueFactory->method('create')->with(
50+
$this->callback(
51+
function ($param) {
52+
return $param() === null;
53+
}
54+
)
55+
);
56+
57+
$this->subject->resolve($mockField, '', $mockInfo, [], []);
58+
}
59+
60+
protected function setUp()
61+
{
62+
parent::setUp();
63+
$this->objectManager = new ObjectManager($this);
64+
$this->mockStoreManager = $this->getMockBuilder(StoreManagerInterface::class)->getMock();
65+
$this->mockValueFactory = $this->getMockBuilder(ValueFactory::class)
66+
->disableOriginalConstructor()
67+
->getMock();
68+
69+
$this->mockValueFactory->method('create')->willReturn(
70+
$this->objectManager->getObject(
71+
Value::class,
72+
['callback' => function () {
73+
return '';
74+
}]
75+
)
76+
);
77+
78+
$mockProductUrlPathGenerator = $this->getMockBuilder(ProductUrlPathGenerator::class)
79+
->disableOriginalConstructor()
80+
->getMock();
81+
$mockProductUrlPathGenerator->method('getUrlPathWithSuffix')->willReturn('product_url.html');
82+
83+
$this->subject = $this->objectManager->getObject(
84+
CanonicalUrl::class,
85+
[
86+
'valueFactory' => $this->mockValueFactory,
87+
'storeManager' => $this->mockStoreManager,
88+
'productUrlPathGenerator' => $mockProductUrlPathGenerator
89+
]
90+
);
91+
}
92+
}

app/code/Magento/CatalogGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ interface ProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\
279279
gift_message_available: String @doc(description: "Indicates whether a gift message is available")
280280
manufacturer: Int @doc(description: "A number representing the product's manufacturer")
281281
categories: [CategoryInterface] @doc(description: "The categories assigned to a product") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category")
282+
canonical_url: String @doc(description: "Canonical URL") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CanonicalUrl")
282283
}
283284

284285
interface PhysicalProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\\ProductInterfaceTypeResolverComposite") @doc(description: "PhysicalProductInterface contains attributes specific to tangible products") {

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductViewTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ public function testQueryAllFieldsSimpleProduct()
227227
updated_at
228228
url_key
229229
url_path
230+
canonical_url
230231
websites { id name code sort_order default_group_id is_default }
231232
... on PhysicalProductInterface {
232233
weight
@@ -272,6 +273,11 @@ public function testQueryAllFieldsSimpleProduct()
272273
'Filter category',
273274
$responseObject->getData('products/items/0/categories/2/name')
274275
);
276+
$storeManager = ObjectManager::getInstance()->get(\Magento\Store\Model\StoreManagerInterface::class);
277+
self::assertEquals(
278+
$storeManager->getStore()->getBaseUrl() . 'simple-product.html',
279+
$responseObject->getData('products/items/0/canonical_url')
280+
);
275281
}
276282

277283
/**

0 commit comments

Comments
 (0)