How to add GraphQL in Custom Module Magento 2

In this blog, we will explain how to add a GraphQL query in Magento 2.

GraphQL is a query language for using APIs.

GraphQL overview

GraphQL is a data query language developed internally by Facebook in 2012 before being publicly released in 2015. Adobe Commerce and Magento Open Source implement GraphQL to provide an alternative to REST and SOAP web APIs for frontend development.

Before we start I assume you already have Magento 2 custom module with Model, Resource Model, and Collection.

It is very easy to make a GraphQL query see this step:-

1. Create schema.graphqls file in app/code/Cynoinfotech/Users/etc folder to use the module with the following code.

2. Create Users.php file in app/code/Cynoinfotech/Users/Model/Resolver/Users folder with the following code.

3. Create Users.php file in app/code/Cynoinfotech/Users/Model/Resolver/DataProvider folder the with the following code.

4: Now, after executing commands

php bin/magento setup:upgrade
php bin/magento cache:clean

You can test your custom GraphQL Query in Altair GraphQL client chrome extension or you can also test in Postman Software.

You can see the result in the below link.

We have successfully created a custom GraphQL query, we hope this article helped you to know How to add Custom GraphQL in Magento 2.

Thank You !!

CynoInfotech

CynoInfotech was founded in 2019, in Ahmedabad, Gujrat. we have a trusted Web Development and Design Company that helps businesses in streamlining their operations through Magento 2 Extension and advanced modern technology solutions.

Let’s connect