How to get orders list by customer id in graphql magento 2?

From Magento 2.4 Out of the box, Magento supports Customer Orders History using GraphQL.

You can check the syntax by clicking Retrieve all the Order list of a customer by GraphQL Query

Given Article is used for the learning of GraphQL Practice with Magento. I have written an article before Magento supports the Sales GraphQL feature in the core code.

How to get a list of orders of a customer using GraphQl Magento 2?

We need to create a simple module for getting all the order lists of Customer using GraphQl. Default Magento 2, We can easily get all the orders of a customer by fetch OrderRepository.

Using Graphql, You can get an order list of a specific registered customer by customer id.

We need to create a Resolver model and add our custom logic for getting all the Customer Order list. We will display all the orders of a customer in the response to the query.

I hope you are aware of What is GraphQl and how GraphQL is used in Magento 2 If You are new to GraphQL check the link for GraphQl in Magento 2.

Now we can start the module using Magento 2 to fetch all orders of a specific customer.

You need to create the first registration.php and module.xml file for defining our module. I have taken the Package name as Rbj and the Module name as CustomerOrder.

Path: app/code/Rbj/CustomerOrder/registration.php

Create the module.xml file, Path: app/code/Rbj/CustomerOrder/etc/module.xml

Our Module depends on GraphQL and Sales Module so we have given the dependency on module.xml file for each module.

Every GraphQl Module must contain schema.graphqls file under the etc folder of a module.

schema.graphqls is the heart of any GraphQL Query implementation and describes what data can be queried. So You can define your query data basic skeleton under the schema.graphqls file.

Customer Order Graphql Schema file,
Path: app/code/Rbj/CustomerOrder/etc/schema.graphqls

In the Schema file, We have passed customer_id for getting customer id as input. In SalesOrderCollection type for getting all the records as an array,

We have created an array for [OrderRecord]. In the type OrderRecord, we have to display all the field which we required in the response of query. You can add an extra field also here in the above schema.

Now Create a Resolver Model for our custom module.
CustomerOrder Model class which is defined in the schema.graphqls file at above. In this Model, the resolve() method will simply return data of all the Orders of the specific customer.

Path: app/code/Rbj/CustomerOrder/Model/Resolver/CustomerOrder.php

We have set logic for getting all the orders from OrderRepositoryInterface. You need to set logic in the above file for display response.

Now Run the Upgrade command to install our module.

You can check your GraphQL query response by installing chrome extension ChromeiQL or Altair GraphQL addon.

order list of Customer GraphQL
order list of Customer GraphQL

In Request Body, You need to pass the required data(field) to request payload for getting a response of Products,

Request Payload,

We have passed customer id as 1 for getting all the order of a customer with id 1.

Result: The result will be all Customer Order list which ID is equal or greater than 1,