A GraphQL query retrieves data from the application server in a similar manner as a REST GET call. The current set of Adobe Commerce and Magento Open Source GraphQL queries allow a mobile app or browser to render a wide variety of information, including the following:
- A set of products to be displayed. This can include the entire catalog or those that match customer-specified criteria.
- Customer data. With a customer token, a query can retrieve basic information about a customer as well as billing and shipping addresses, wish lists, order history, and other sensitive data.
- Shopping cart contents. GraphQL supports both guest and logged-in customer carts.
- Store configuration values, including theme and CMS settings, the currency code, and supported countries.
GraphQL QUERY Implementation in Magento 2.4
Magento’s 2.4 version is already fitted with GraphQL support. we’ll go over the process of implementing GraphQL in Magento step by step. We’ll provide an example of GraphQL query.
Step 1 :- You Create registration.php
file
<?php
/**
* @author KPYXAL
* @copyright Copyright (c) 2021
* @package Kpyxal_GraphqlQuery
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Kpyxal_GraphqlQuery',
__DIR__
);
Step 2 :- create etc/module.xml
file
<?xml version="1.0"?>
<!--
/**
* @author KPYXAL
* @copyright Copyright (c) 2021
* @package Kpyxal_GraphqlQuery
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Kpyxal_GraphqlQuery" >
<sequence>
<module name="Magento_Customer"/>
<module name="Magento_Authorization"/>
<module name="Magento_GraphQl"/>
</sequence>
</module>
</config>
Step 3 :- GraphQL queries are declared under etc/schema.graphqls
type Query {
formdata: [Formdata] @resolver(class:"Kpyxal\\GraphqlQuery\\Model\\Resolver\\ProductsResolver") @doc(description:"The formdata query returns information about a customer")
}
type Formdata @doc(description: "Formdata defines the customer name and other details") {
name: String @doc(description: "Get Name")
number: String @doc(description: "Get Number")
email: String @doc(description: "Get Email")
password: String @doc(description: "Get Password")
gender: Int @doc(description: "Get Gender")
bdate: String @doc(description: "Get Birth Date")
}
In above file
type Query : declares Query operations of our module.
formdata : name of our query.
type Formdata : defines the identity of the query , including resolver (@resolver) class,document (@doc) .
formdata: [Formdata] : if we need array of data than need to put identity in array like:-
name of query = [ Identity name ]
And here identity name is Formdata same name which we are use in : type Formdata
Step 4 :- Create Resolver Class Model/Resolver/ProductsResolver.php
<?php
declare(strict_types=1);
namespace Kpyxal\GraphqlQuery\Model\Resolver;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Kpyxal\GraphqlQuery\Model\ResourceModel\Test\Collection;
/**
* Product collection resolver
*/
class ProductsResolver implements ResolverInterface
{
public function __construct(
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
Collection $collection
) {
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->collection = $collection;
}
/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
$alldata = $this->getAllData();
$arraydata= [];
if (count($alldata) > 0):
foreach ($alldata as $data):
$arraydata[] = array(
'name'=>$data->getName(),
'number'=>$data->getNumber(),
'email'=>$data->getEmail(),
'password'=>$data->getPassword(),
'gender'=>$data->getGender(),
'bdate'=>$data->getBdate()
);
endforeach;
endif;
return $arraydata;
}
/**
* @return Collection
*/
public function getAllData()
{
return $this->collection;
}
}
In above resolver file we have used
Kpyxal\GraphqlQuery\Model\ResourceModel\Test\Collection;
collection to get data and passed it to some variables like name,number,email.
Step 5 :- Final step you check output. Run this query
{
formdata{
name
number
email
password
gender
bdate
}
}
Happy Coding ..! 😉