0

Can i generate Shipment/invoice using GraphQL in magento 2.4.5 ?

If yes kindly let me know how to generate, any links with brief details also appreciated.

2
  • Magento 2 by default not provide it. For that, need to create custom graphql. Jan 18 at 13:24
  • @RohanHapani, Any reference link for the same?, i mean need to know the params & all if we need to generate shipment using GraphQL.
    – Rik Patel
    Jan 18 at 13:49

1 Answer 1

0
  1. Create a GraphQL Schema file schema.graphqls in your module directory.

Here is the code.

type Mutation {
    createShipment(input: ShipmentInput!): ShipmentOutput
}

input ShipmentInput {
    order_id: Int!
    items: [ShipmentItemInput]
}

input ShipmentItemInput {
    order_item_id: Int!
    qty: Float!
}

type ShipmentOutput {
    shipment: Shipment
}

type Shipment {
    increment_id: String
    trackings: [Tracking]
}

type Tracking {
    track_number: String
    carrier_code: String
}

Here's an example of how you can create a resolver class that handles the createShipment mutation in Magento 2:

<?php
namespace Vendor\Module\Model\Resolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

class CreateShipmentResolver implements ResolverInterface
{
    /**
     * @var \Magento\Sales\Api\ShipmentRepositoryInterface
     */
    private $shipmentRepository;

    /**
     * @var \Magento\Sales\Api\OrderRepositoryInterface
     */
    private $orderRepository;

    public function __construct(
        \Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepository,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
    )
    {
        $this->shipmentRepository = $shipmentRepository;
        $this->orderRepository = $orderRepository;
    }

    public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
    {
        $shipmentDatas = [];
        $order = $this->orderRepository->get($args['input']['order_id']);
        if (!$order->canShip()) {
            throw new GraphQlInputException(__('Order cannot be shipped.'));
        }

        $items = $args['input']['items'];
        $data = [];
        foreach ($items as $item) {
            $data[$item['order_item_id']] = $item['qty'];
        }

        $shipment = $this->shipmentRepository->create(
            $order,
            $data,
            __('Shipment Created from GraphQL')
        );

        $this->shipmentRepository->save($shipment);

        $increment_id = $shipment->getIncrementId();
        $trackings = $shipment->getAllTracks();

        $trackingData = array();
        foreach ($trackings as $tracking) {
            $trackingData[] = [
                'track_number' => $tracking->getNumber(),
                'carrier_code' => $tracking->getCarrierCode()
            ];
        }
        
        $shipmentDatas = [
            'increment_id' => $increment_id,
            'trackings' => $trackingData
        ];

        return $shipmentDatas;
    }
}

Let me know if any query.

THANKS.

1
  • Hello @MohitPatel, Thanks for the details code, i appreciated it, but can you please give me the argument/parameter for this shipment generate like what will we need to pass in GraphQL Client.. sorry i am newbie in GraphQL so not aware of much, I have tried like below but it gives me response null. Request: mutation { createShipment(input: { order_id: 57987 items: { order_item_id: 1231 qty: 1 } }) { shipment { increment_id trackings{ track_number carrier_code } } } }
    – Rik Patel
    Feb 3 at 4:12

Your Answer

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

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