Skip to content

How to override Magento 2 graphql resolver with custom plugin

Im trying to override the resolver by using a plugin in Magento 2 to change text.

vendor/magento/module-quote/Model/CouponManagement.php – I need to change this

class ApplyCouponToCart implements ResolverInterface
 { ...
 public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{ ...
   /* Check current cart does not have coupon code applied */
    $appliedCouponCode = $this->couponManagement->get($cartId);
    if (!empty($appliedCouponCode)) {
        throw new GraphQlInputException(
            __('A coupon is already applied to the cart. Please remove it to apply another')
        );
    }

Here is my plugin VendorCustom_ModulePlugin;

class AfterApplyCouponToCart {

public function afterResolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) 
{ ...
   /* Check current cart does not have coupon code applied */
    $appliedCouponCode = $this->couponManagement->get($cartId);
    if (!empty($appliedCouponCode)) {
        throw new GraphQlInputException(
            __('**This is my new text which I need to update**')
        );
    }