1

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.

2
  • Did you find a solution ? I am also looking for this. Nov 12, 2022 at 15:15
  • @GustavoUlyssea Please find my answer below. Hope this will help you. Nov 14, 2022 at 7:21

1 Answer 1

1

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;
    }
}
2
  • 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. 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. Nov 16, 2022 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.