How to get list of customers Order using graphql magento 2?

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

We need to create a simple module for getting all the order list 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 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 of query.

I hope you are aware of What is GraphQl and how GraphQL are used for the programming language like Magento 2 If You are new to GraphQL check 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 first registration.php and module.xml file for defining our module. I have taken Package name as Rbj and 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 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 the 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 extra field also here in the above schema.

Now Create a Resolver Model for our custom module.
CustomerOrder Model class which is defined in schema.graphqls file at above. In this Model, 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 above file for display response.

Now Run 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,



Leave a Reply

Your email address will not be published. Required fields are marked *