Hire magento developers and create Custom GraphQL in Magento 2. GraphQL is a query language for API to allow the customers to fetch the required data while the server returns only the required information. GraphQL was introduced in Magento 2.3 to replace REST and SOAP API. GraphQL is used to read data by query and write data to the server through a mutation.
Here we’ll discuss How to Create Custom GraphQL in Magento 2.
Let’s get started:
Magento Development Company create a “schema. graphics” file inside our extension below.
app\code\Vendor\Extension\etc\schema.graphqls
Now, add the below code
After that, we need to create a “CustomGraphql.php” file inside the below folder path of the extension.
app\code\Vendor\Extension\Model\Resolver\CustomGraphql.PHP
And add the below code
You need to test GraphQL Query in Altair GraphQL client chrome extension or test in Postman Software.
REST API characterizes asset data on the worker; the customer settles on the decision as it were. GraphQL permits customers to transfer a datasheet; worker activities should return a similar data.
The GraphQL worker just requires a single endpoint and precisely reacts to the data mentioned by the customer; rather than having multiple endpoints, every endpoint returns a fixed data structure as in REST.
Graphql was presented with Magento version 2.3 as a choice to REST/SOAP API for frontend development.
Magento utilizes two sorts of GraphQL operations:
Question: used to understand data
Change: used to compose data to the worker
As of version 2.3.4, the accompanying center modules are utilizing GraphQL inquiry language:
CatalogInventoryGraphQl
CatalogUrlRewriteGraphQl
BundleGraphQl
CatalogGraphQl
ConfigurableProductGraphQl
CustomerGraphQl
DownloadableGraphQl
EavGraphQl
GroupedProductGraphQl
TaxGraphQl
ThemeGraphQl
UrlRewriteGraphQl
WeeeGraphQl
In this model, we’ll make a simple GraphQL API inquiry to bring customer data by means of their email.
We should call our module Magenest/GraphQl. Our module should be stacked after GraphQL and Customer module so that the module.xml file would resemble this:
GraphQL queries are declared under vendor/module/etc/schema.graphqls:
Read More: Best Practices for Magento 2 Custom Theme Development
Magento development Services return data of customers with the requested email.
app/code/Magenest/GraphQl/Model/Resolver/Customer.php
For testing hire dedicated magento developers or you can check GraphQL query and response by installing a Chrome extension called “ChromeiQL”. Then, set the endpoint as “<your Magento root url>/graphql”.After that, input your GraphQL query on the left side of “ChromeiQL” interface and click on the “Set endpoint” button.
WRITTEN BY: Sunny Patel
Sunny Patel is a multi-skilled IT consultant at CMARIX, a leading web app development company offers flexible hiring models to hire a dedicated developers. With…
FEW MORE POSTS BY Sunny Patel: