Magento2 Query in GRAPHQL retrieves data from the application server similar to a REST GET call. Additionally, Adobe Commerce and Magento Open Source GraphQL queries enable mobile apps or browsers to display a diverse range of information, including:
- A set of products to be displayed. This can include the entire catalog or those that match customer-specified criteria.
- A customer token allows a query to access basic information, billing and shipping addresses, wish lists, order history, and other sensitive data. Moreover, GraphQL can also retrieve shopping cart contents for both guest and logged-in customers
- 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.
Implementation Magento2 Query in GRAPHQL
Built into Magento 2.4, GraphQL support simplifies data retrieval. The following sections will detail the step-by-step implementation process and provide an example of a Magento2 Query in GRAPHQL.
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 the above file, the file specifies that ‘type Query’ declares the query operations for our module; furthermore, it highlights the structure and functionality that are essential for proper implementation.
‘formdata’ represents the query name, while ‘type Formdata’ outlines the query’s structure, including the resolver (@resolver) class and documentation (@doc).
formdata: [Formdata] : if we need array of data than need to put identity in array like:-
name of query = [ Identity name ].
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;
}
}
The resolver file utilizes
Kpyxal\GraphqlQuery\Model\ResourceModel\Test\Collection
to gather data. This data is then assigned to variables including name, number, and email.
{
formdata{
name
number
email
password
gender
bdate
}
}
In conclusion, Magento2 Query in GRAPHQL offer a powerful way to retrieve data from the application server, similar to a REST GET call. They provide flexibility in accessing a wide range of information; consequently, enabling mobile apps or browsers to render content efficiently, making it an ideal solution for modern, headless commerce applications.
Happy Coding ..! 😉
Check More Like This