Get sales order data using Graphql in Magento 2.

GraphQL is a new concept from Magento 2.3 version in Magento eCommerce. Using Graphql we can get sales order data. There are many default core module uses GraphQl for getting data like, Product, CMS Page, and Block, Customer module uses Graphql for getting data of a specific entity. Magento Doesn’t use GraphQl for Sales Specific entity in core module.

I have just demonstrated how to get order data using GraphQl in Magento 2 based on order id. We can get details of Order basic entity, billing address and shipping address and Used items in Order.

For Create Order data GraphQl we need to create Module in Magento 2, If you are new to GraphQl, Please refer GraphQl Basics info for Magento 2

You need to create first registration.php and module.xml file for defining our module. Here I have used Rbj as Packagename where SalesGraphQl is a module name.

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

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

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

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

schema.graphqls is at the center 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.

Createschema.graphqls file, Path: app/code/Rbj/SalesGraphQl/etc/schema.graphqls

I have to define schema.graphql file with multiple different type for billing, shipping and items type to get info of each entity of order data. Each type has a specific field to display in GraphQl result.

You need to define each field in schema file, you want to show in response for a graphQl query. For Billing Schema type, You need to specify each field related to billing, For Shipping Schema, you need to specify a required field, same for Items type you need to pass required field to show as response.

Now we need to write our custom logic for the dynamic value of order data. For that, we need to create a resolver php file to get a dynamic value of specific order.

Create SalesOrder.php file,
Path: app/code/Rbj/SalesGraphQl/Model/Resolver/SalesOrder.php

Now we need to create Shipping.php, Billing.php and Items.php file for getting shipping, billing and items data of an order.

Get Shipping address of Placed order, We have created a separate file for fetch Shipping address data in GraphQl response. file is used for fetch shipping records of an order.

Path: app/code/Rbj/SalesGraphQl/Model/Resolver/Shipping.php

For Get Billing address of Placed order, We have created a separate file for fetch Billing address data in GraphQl response.
Path: app/code/Rbj/SalesGraphQl/Model/Resolver/Billing.php

For Display all the items of Order, We need to create Items.php as defined in the schema file. We got a response as an array of items for an placed order.

Path: app/code/Rbj/SalesGraphQl/Model/Resolver/Items.php

Now Run Upgrade command to install our custom Sales module.

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

sales order data by id Graphql
Magento 2 Get sales order data by id Graphql

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

We have pass sales_order id as 2 for fetch records of Sales Order Id 2. Based on define id in request result will be shown data of order of specific id.

Request Payload,

Result for Order id 2, I have added two products for order id 2 so in response they display both items in items array with billing and shipping info: