Create a Custom module using Graphql in Magento 2.

GraphQL is a new concept from Magento 2.3 and Many Magento Enthusiastic have eagerly waiting for the simple module using GraphQL and how GraphQL interacts with Magento 2 and why GraphQL is used for Magento 2 and so many other stuff around GraphQl.

I want to give a demo of a simple module of GraphQL in Magento 2. How GraphQl is actually used in Magento 2 using programmatic way and so on.

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.

I hope you are aware of creating a simple module in Magento 2, Check link. How to create a simple module in Magento 2?

For Create Simple GraphQl module using Magento 2, We are creating a Demo Graphql module using Sales Data, Our Main Aim is to fetch sales related data using Custom module.

You need to create first registration.php and module.xml file for defining our module.

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

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

Our Module is 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.

Every GraphQL service defines a set of types like String, Int, Boolean, Float, Array, Object, enum or Interface which completely describe the set of possible data you can query on specific service.

You can learn more detail for a schema by https://graphql.org/learn/schema/

Create schema.graphqls file, Path: app/code/Rbj/DemoGraphQl/etc/schema.graphqls

In this blog, we learn about only Query type of GraphQl.
We have defined the description of each field we need to use in GraphQl,

id: Int @doc(description: “Id of the Sales Order”) map to Sales Order id as Int type
customer_name: String @doc(description: “Customername of Sales Order”) map to Customer name as String Type
is_guest_customer : Boolean @doc(description: “Specifies if this order was placed by Guest customer”) map to Boolean Type
address_array: [String] @doc(description: “Array of order address types. It can have the following values: city, postcode, state”) map to Array of data type.

Now Create a Resolver Model for our custom module. Resolver Model class which is defined in schema.graphql file at above. In this Model, resolve() method will simply return data of sales Order.

Resolver method fetches the data and formats it according to the GraphQL schema type.

We need to create SalesOrder.php file from defined resolver from above schema file SalesOrder @resolver(class: “Rbj\\DemoGraphQl\\Model\\Resolver\\SalesOrder”) @doc(description: “The Sales Order query returns information about a Sales order”)

Path: app/code/Rbj/DemoGraphQl/Model/Resolver/SalesOrder.php

In the above file, resolve() method contains the method getSalesData() which is responsible for getting data of GraphQl. You need to define your custom logic in resolve method.

Now Run Upgrade command to install our module.

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

Check Using type query in ChromeiQl by,
http://127.0.0.1/magento2-2.3-develop/graphql where http://127.0.0.1/magento2-2.3-develop is your store URL.

Custom GraphQL module
Custom GraphQL module

In Request Body, You need to pass required data for Sales Order, Where id is your Order id.

The result will be,

 

 

Leave a Reply

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