In this blog, you can see how to get all the product collections using GraphQL in Magento 2.
Step 1
First of all, we need to create a registration.php file in app/code/Webi/GraphQL/ directory to register our module by adding the following code.
|
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Webi_GraphQL', __DIR__ ); |
Step 2
Now, we need to create a module.xml file in app/code/Webi/GraphQL/etc/ directory and add the following code.
|
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Webi_GraphQL"> <sequence> <module name="Magento_Sales"/> <module name="Magento_GraphQl"/> </sequence> </module> </config> |
Step 3
In this step is now to create schema.graphqls file in app/code/Webi/GraphQL/etc/ directory and add the following code.app/code/Webi/GraphQ/etc/ directory and add the following code.
|
type Query { CustomerOrderList ( customer_id: Int @doc(description: "Id of the Customer") ): SalesOrder @resolver(class: "Webi\\GraphQL\\Model\\Resolver\\CustomerOrder") @doc(description: "The Sales Order query returns information about a customer placed order") } type SalesOrder @doc(description: "Sales Order graphql gather data of order item information") { fetchRecords : [CustomerOrderRecord] @doc(description: "An array of customer placed order fetch records") } type CustomerOrderRecord @doc(description: "Customer placed order items information") { increment_id: String @doc(description: "Increment Id of Sales Order") customer_name: String @doc(description: "Customer name of Sales Order") grand_total: String @doc(description: "Grand total of Sales Order") qty: Int @doc(description: "Order item quantity") shipping_method: String @doc(description: "Shipping method of order placed") } |
Step 4
Lastly, we need to create CustomerOrder.php as a resolver file in app/code/Webi/GraphQL/Model/Resolver/ and add the following code.
Now, execute the following commands:
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f en_US
php bin/magento cache:flush
php bin/magento cache:clean
php bin/magento indexer:reindex
Now, you need to pass query required data in the Request Body.
|
{ CustomerOrderList (customer_id: 11) { fetchRecords{ increment_id customer_name grand_total qty shipping_method } } } |

We hope this post is very helpful to you.
Thank You!
Hit 5 Stars if you find this post helpful