I have a requirement where I want to add a custom field in the OrderAddress query of GraphQL. Anyone who have done this can help.
1 Answer
I am not sure this is the correct answer or approach, but this solves my problem.
I was trying to solve this by using resolver which is the correct approach but, unfortunately, it was not working because the model class was missing in the core graphQl API.
Below is my solution, Create a plugin as below:
<type name="Magento\SalesGraphQl\Model\Order\OrderAddress">
<plugin name="AreaAttributeInOrder" type="Mumzworld\Assignmentthree\Plugin\OrderAddressGraphQl" sortOrder="1" disabled="false"/>
</type>
Then create plugin file.
<?php
namespace Mumzworld\Assignmentthree\Plugin;
use Magento\Sales\Api\Data\OrderInterface;
class OrderAddressGraphQl
{
/**
* Plugin to add area field in Order Shipping Address GraphQL query
* @param \Magento\SalesGraphQl\Model\Order\OrderAddress $subject
* @param callable $proceed
* @param OrderInterface $order
* @return array
*
*/
public function aroundGetOrderShippingAddress(
\Magento\SalesGraphQl\Model\Order\OrderAddress $subject,
callable $proceed,
OrderInterface $order
) {
$result = $proceed($order);
if (!empty($result) && is_array($result)) {
$shippingAddress = $order->getShippingAddress();
$result['area'] = $shippingAddress->getArea();
}
return $result;
}
/**
* Plugin to add area field in Order Billinig Address GraphQL query
* @param \Magento\SalesGraphQl\Model\Order\OrderAddress $subject
* @param callable $proceed
* @param OrderInterface $order
* @return array
*
*/
public function aroundGetOrderBillingAddress(
\Magento\SalesGraphQl\Model\Order\OrderAddress $subject,
callable $proceed,
OrderInterface $order
) {
$result = $proceed($order);
if (!empty($result) && is_array($result)) {
$address = $order->getBillingAddress();
$result['area'] = $address->getArea();
}
return $result;
}
}
-
I was able to solve it in another way that was adding a String to Address object in gralph definitions and set it to a modifier in my extension. Commented Nov 16, 2022 at 0:48
-
Can you share your code? Hope that will help others to solve the problem as well. And I will also check which is the better appraoch. Commented Nov 16, 2022 at 9:22