adyen-magento2 icon indicating copy to clipboard operation
adyen-magento2 copied to clipboard

Field "cart_id" on "Order" is not being resolved when using "setPaymentMethodAndPlaceOrder"

Open pmzandbergen opened this issue 4 months ago • 0 comments

Description The (deprecated) setPaymentMethodAndPlaceOrder mutation is broken: [object] (GraphQL\\Error\\Error(code: 0): Cannot return null for non-nullable field \"Order.cart_id\".

This is caused by: https://github.com/Adyen/adyen-magento2/blob/4b98628b994382efe2aa8f757b89fbefe48a2e97/etc/schema.graphqls#L57

This field is not returned by \Magento\QuoteGraphQl\Model\Resolver\SetPaymentAndPlaceOrder.

Fix You either need to add your own resolver (which could simply be using the input parameters) or use a plugin on the existing (Magento Core) resolver:

etc/graphql/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\QuoteGraphQl\Model\Resolver\SetPaymentAndPlaceOrder">
        <plugin name="Adyen\Payment\Plugin\SetCartIdOnSetPaymentMethodAndPlaceOrderMutation"
                type="Adyen\Payment\Plugin\SetCartIdOnSetPaymentMethodAndPlaceOrderMutation"/>
    </type>
    <!-- ... -->
</config>
<?php

declare(strict_types=1);

namespace Adyen\Payment\Plugin;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\QuoteGraphQl\Model\Resolver\SetPaymentAndPlaceOrder;

class SetCartIdOnSetPaymentMethodAndPlaceOrderMutation
{
    public function afterResolve(
        SetPaymentAndPlaceOrder $subject,
        mixed $result,
        Field $field,
        mixed $context,
        ResolveInfo $info,
        array $value = null,
        array $args = null
    ) {
        return isset($args['input']['cart_id']) && is_array($result) ? array_merge_recursive($result, [
            'order' => [
                'cart_id' => $args['input']['cart_id'],
            ],
        ]) : $result;
    }
}

This enables users to use the (deprecated) setPaymentMethodAndPlaceOrder mutation which is a temporary fix for issue #2754

pmzandbergen avatar Oct 01 '24 13:10 pmzandbergen