How to get order list of customer using graphql.

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


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.")


 * 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.

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,
        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();

            $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 Answer 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.


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.