Skip to content

Commit 4cbb43c

Browse files
committed
Gift message GraphQl module fix
1 parent 796906f commit 4cbb43c

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

app/code/Magento/GiftMessageGraphQl/Model/Resolver/Order/GiftMessage.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ public function resolve(
5959
throw new GraphQlInputException(__('"id" value should be specified'));
6060
}
6161

62+
$orderId = (int)base64_decode($value['id']) ?: (int)$value['id'];
63+
6264
try {
63-
$orderGiftMessage = $this->orderRepository->get($value['id']);
65+
$orderGiftMessage = $this->orderRepository->get($orderId);
6466
} catch (LocalizedException $e) {
6567
throw new GraphQlInputException(__('Can\'t load gift message for order'));
6668
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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\GiftMessageGraphQl\Model\Resolver\Order\Item;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
14+
use Magento\Framework\GraphQl\Query\Resolver\Value;
15+
use Magento\Framework\GraphQl\Query\ResolverInterface;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use Magento\GiftMessage\Api\OrderItemRepositoryInterface;
18+
use Magento\GiftMessage\Helper\Message as GiftMessageHelper;
19+
20+
/**
21+
* Class provides ability to get GiftMessage for order item
22+
*/
23+
class GiftMessage implements ResolverInterface
24+
{
25+
/**
26+
* @var OrderItemRepositoryInterface
27+
*/
28+
private $orderItemRepository;
29+
30+
/**
31+
* @var GiftMessageHelper
32+
*/
33+
private $giftMessageHelper;
34+
35+
/**
36+
* @param OrderItemRepositoryInterface $itemRepository
37+
* @param GiftMessageHelper $giftMessageHelper
38+
*/
39+
public function __construct(
40+
OrderItemRepositoryInterface $itemRepository,
41+
GiftMessageHelper $giftMessageHelper
42+
) {
43+
$this->orderItemRepository = $itemRepository;
44+
$this->giftMessageHelper = $giftMessageHelper;
45+
}
46+
47+
/**
48+
* Return information about Gift message for order item
49+
*
50+
* @param Field $field
51+
* @param ContextInterface $context
52+
* @param ResolveInfo $info
53+
* @param array|null $value
54+
* @param array|null $args
55+
*
56+
* @return array|Value|mixed
57+
* @throws GraphQlInputException
58+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
59+
*/
60+
public function resolve(
61+
Field $field,
62+
$context,
63+
ResolveInfo $info,
64+
array $value = null,
65+
array $args = null
66+
) {
67+
if (!isset($value['model'])) {
68+
throw new GraphQlInputException(__('"model" value must be specified'));
69+
}
70+
71+
$orderItem = $value['model'];
72+
73+
if (!$this->giftMessageHelper->isMessagesAllowed('items', $orderItem)) {
74+
return null;
75+
}
76+
77+
if (!$this->giftMessageHelper->isMessagesAllowed('item', $orderItem)) {
78+
return null;
79+
}
80+
81+
try {
82+
$giftItemMessage = $this->orderItemRepository->get($orderItem->getOrderId(), $orderItem->getItemId());
83+
} catch (LocalizedException $e) {
84+
throw new GraphQlInputException(__('Can\'t load message for order item'));
85+
}
86+
87+
if (!isset($giftItemMessage)) {
88+
return null;
89+
}
90+
91+
return [
92+
'to' => $giftItemMessage->getRecipient() ?? '',
93+
'from' => $giftItemMessage->getSender() ?? '',
94+
'message'=> $giftItemMessage->getMessage() ?? ''
95+
];
96+
}
97+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ type SalesItemInterface {
4545
type CustomerOrder {
4646
gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Order\\GiftMessage") @doc(description: "The entered gift message for the order")
4747
}
48+
49+
interface OrderItemInterface {
50+
gift_message: GiftMessage @resolver(class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Order\\Item\\GiftMessage") @doc(description: "The selected gift message for the order item")
51+
}

0 commit comments

Comments
 (0)