Skip to content

Commit aa0d946

Browse files
Merge pull request magento#6 from magento-untitled-geese/jconabree/PWA-2133-cart-qty-error-details
PWA-2133 Exposing cart item error to GraphQl
2 parents 855d84b + d884139 commit aa0d946

File tree

8 files changed

+175
-1
lines changed

8 files changed

+175
-1
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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\QuoteGraphQlPwa\Model\Resolver;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Query\ResolverInterface;
13+
use Magento\Framework\GraphQl\Query\EnumLookup;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\Quote\Model\Quote\Item;
16+
17+
/**
18+
* @inheritdoc
19+
*/
20+
class CartItemErrors implements ResolverInterface
21+
{
22+
public const ERROR_UNDEFINED = 0;
23+
24+
/** @var \Magento\Framework\GraphQl\Query\EnumLookup */
25+
private $enumLookup;
26+
27+
/**
28+
* @param \Magento\Framework\GraphQl\Query\EnumLookup $enumLookup
29+
*/
30+
public function __construct(
31+
EnumLookup $enumLookup
32+
) {
33+
$this->enumLookup = $enumLookup;
34+
}
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
40+
{
41+
if (!isset($value['model'])) {
42+
throw new LocalizedException(__('"model" value should be specified'));
43+
}
44+
/** @var Item $cartItem */
45+
$cartItem = $value['model'];
46+
47+
return $this->getItemErrors($cartItem);
48+
}
49+
50+
/**
51+
* Get error messages for cart item
52+
*
53+
* @param \Magento\Quote\Model\Quote\Item $cartItem
54+
*
55+
* @return string[]|null
56+
* @throws \Magento\Framework\Exception\RuntimeException
57+
*/
58+
private function getItemErrors(Item $cartItem): ?array
59+
{
60+
$hasError = (bool) $cartItem->getData('has_error');
61+
if (!$hasError) {
62+
return null;
63+
}
64+
65+
$errors = [];
66+
foreach ($cartItem->getErrorInfos() as $error) {
67+
$errorType = $error['code'] ?? self::ERROR_UNDEFINED;
68+
$errorEnumCode = $this->enumLookup->getEnumValueFromField(
69+
'CartItemErrorType',
70+
(string)$errorType
71+
);
72+
$errors[] = [
73+
'code' => $errorEnumCode,
74+
'message' => $error['message']
75+
];
76+
}
77+
78+
return $errors;
79+
}
80+
}

QuoteGraphQlPwa/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# QuoteGraphQl PWA
2+
3+
**QuoteGraphQl PWA** provides extension of the base Quote GraphQL support for PWA Studio

QuoteGraphQlPwa/composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "magento/module-quote-graph-ql-pwa",
3+
"description": "Provides additional GraphQl functionality for the cart in PWA.",
4+
"type": "magento2-module",
5+
"license": [
6+
"OSL-3.0",
7+
"AFL-3.0"
8+
],
9+
"config": {
10+
"sort-packages": true
11+
},
12+
"require": {
13+
"php": "~7.3.0||~7.4.0",
14+
"magento/framework": "*",
15+
"magento/module-quote-graph-ql": "*"
16+
},
17+
"suggest": {
18+
"magento/module-graph-ql": "*"
19+
},
20+
"autoload": {
21+
"files": [
22+
"registration.php"
23+
],
24+
"psr-4": {
25+
"Magento\\QuoteGraphQlPwa\\": ""
26+
}
27+
}
28+
}
29+

QuoteGraphQlPwa/etc/graphql/di.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="Magento\Framework\GraphQl\Schema\Type\Enum\DefaultDataMapper">
10+
<arguments>
11+
<argument name="map" xsi:type="array">
12+
<item name="CartItemErrorType" xsi:type="array">
13+
<item name="undefined" xsi:type="string">0</item>
14+
<item name="item_qty" xsi:type="string">1</item>
15+
<item name="item_increment" xsi:type="string">2</item>
16+
</item>
17+
</argument>
18+
</arguments>
19+
</type>
20+
</config>

QuoteGraphQlPwa/etc/module.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9+
<module name="Magento_QuoteGraphQlPwa">
10+
<sequence>
11+
<module name="Magento_QuoteGraphQl"/>
12+
</sequence>
13+
</module>
14+
</config>

QuoteGraphQlPwa/etc/schema.graphqls

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright © Magento, Inc. All rights reserved.
2+
# See COPYING.txt for license details.
3+
4+
interface CartItemInterface {
5+
errors: [CartItemError!] @resolver(class: "\\Magento\\QuoteGraphQlPwa\\Model\\Resolver\\CartItemErrors") @doc(description: "An array of errors encountered while loading the cart item")
6+
}
7+
8+
type CartItemError {
9+
code: CartItemErrorType! @doc(description: "An error code that describes the error encountered")
10+
message: String! @doc(description: "A localized error message")
11+
}
12+
13+
enum CartItemErrorType {
14+
UNDEFINED
15+
ITEM_QTY
16+
ITEM_INCREMENTS
17+
}

QuoteGraphQlPwa/registration.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
use Magento\Framework\Component\ComponentRegistrar;
9+
10+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_QuoteGraphQlPwa', __DIR__);

_metapackage/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"description": "PWA metapackage",
44
"require": {
55
"magento/module-contact-graph-ql-pwa": "*",
6-
"magento/module-newsletter-graph-ql-pwa": "*"
6+
"magento/module-newsletter-graph-ql-pwa": "*",
7+
"magento/module-quote-graph-ql-pwa": "*"
78
},
89
"type": "metapackage"
910
}

0 commit comments

Comments
 (0)