2

How to get order list of customer using graphql.

I have below code to get order list using graphql but can't get it.

schema.graphqls

type Query {
    salesOrder (
        customer_id: Int @doc(description: "Id of the Sales Order")
    ): SalesOrder @resolver(class: "<vendor>\\<Module>\\Model\\Resolver\\CustomerOrder") @doc(description: "The Sales Order query returns information about a Sales order")
}

type SalesOrder @doc(description: "Sales Order graphql gather Data of specific order information") {
    order_array: [String] @doc(description: "Array of order list.")
}

CustomerOrder.php

<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
declare(strict_types=1);

namespace <Vendor>\<Module>\Model\Resolver;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
/**
 * Description of CustomerOrder
 *
 * @author dharmendra
 */
class CustomerOrder implements ResolverInterface
{
    public function __construct(
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
    ) {
        $this->orderCollectionFactory = $orderCollectionFactory;
    }

    /**
     * @inheritdoc
     */
    public function resolve(
        Field $field,
        $context,
        ResolveInfo $info,
        array $value = null,
        array $args = null
    ) {
        $salesData = $this->getSalesData($args['customer_id']); 
        return $salesData;
    }

    /**
     * @param int $orderId
     * @return array
     * @throws GraphQlNoSuchEntityException
     */
    public function getSalesData(int $customerId): array
    {
        try {
            $orders = $this->orderCollectionFactory->create();
            $orders->addFieldToSelect('*');
            $orders->addFieldToFilter('customer_id',$customerId);
            $orders->setOrder('created_at','desc');

            $orderCollection = [];
            foreach($orders as $order) {
                $orderCollection[] = [
                    'increment_id' => $order->getIncrementId(),
                    'grand_total' => $order->getGrandTotal(),
                    'customer_id' => $order->getCustomerId(),
                ];
            }
            $orderData['order_array'] = $orderCollection;
        } catch (NoSuchEntityException $e) {
            throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
        }
        return $orderData;
    }
}

I have getting single order details perfect using graphql. But I want to get list of all order placed by customer using graphql.

1

For Check What is GraphQL in Magento 2, Read Blog, GraphQl in Magento 2

Check the link, Get List of Orders of Customer using GraphQl Magento 2 full module

For Get List of orders of Customer by customer id, You need to create a schema file for getting a response from Query.

In above you have declared schema file for getting multiple records of order for a specific customer.

type SalesOrder @doc(description: "Sales Order graphql gather Data of specific order information") {
    order_array: [String] @doc(description: "Array of order list.") }

In above order_array is used for getting an array of records but in your resolver, you made mistakes for multiple records.

In $orderCollection[] you need to pass array key for each record and you forgot to add that in your code.

You can try with,

foreach($orders as $k => $order) {
            $orderCollection['order_array'][$k] = [
                'increment_id' => $order->getIncrementId(),
                'grand_total' => $order->getGrandTotal(),
                'customer_id' => $order->getCustomerId(),
            ];
        }

Let me know if you have any query.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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