I'm creating Graphql for my custom module, but after setting an endpoint and writing a query for it, does not return any data.
Below are the files I have created for Graphql,
MyVendor/RMAModule/etc/schema.graphqls
type Query {
RmaResolver (
emailId: String @doc(description: "Email Address")
): RmaResolver @resolver(class:"MyVendor\\RMAModule\\Model\\Resolver\\RmaResolver") @doc(description:"Custom Module Datapassing")
}
type RmaResolver {
rmaData: [RmaRecord] @doc(description: "RMA records")
}
type RmaRecord {
entity_id : Int @doc(description: "Entity Id")
order_id : Int @doc(description: "Order Id")
customer_id : Int @doc(description: "Customer Id")
customer_name : String @doc(description: "Customer Name")
customer_email : String @doc(description: "Customer Email")
delivery_status : String @doc(description: "Delivery Status")
additional_info : String @doc(description: "Additional Info")
rma_status : String @doc(description: "RMA Status")
created_on : String @doc(description: "Created on")
}
MyVendor/RMAModule/Model/Resolver/RmaResolver.php
<?php
namespace MyVendor\RMAModule\Model\Resolver;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use MyVendor\RMAModule\Model\RmaFactory;
class RmaResolver implements ResolverInterface
{
/**
* @var ValueFactory
*/
private $valueFactory;
/**
* @var RmaFactory
*/
protected $rmaFactory;
/**
* @var CustomerRepositoryInterface
*/
protected $customerRepository;
/**
* @var AccountManagementInterface
*/
protected $customerAccountManagement;
/**
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* @param ValueFactory $valueFactory
* @param RmaFactory $rmaFactory
* @param CustomerRepositoryInterface $customerRepository
* @param AccountManagementInterface $customerAccountManagement
* @param StoreManagerInterface $storeManager
* @param array
*/
public function __construct(
ValueFactory $valueFactory,
RmaFactory $rmaFactory,
CustomerRepositoryInterface $customerRepository,
AccountManagementInterface $customerAccountManagement,
StoreManagerInterface $storeManager,
array $data = []
) {
$this->valueFactory = $valueFactory;
$this->rmaFactory = $rmaFactory;
$this->customerRepository = $customerRepository;
$this->customerAccountManagement = $customerAccountManagement;
$this->storeManager = $storeManager;
}
/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null)
{
try{
$emailId = $this->getEmailId($args);
$rmaData = $this->getRmaData($emailId);
$result = function () use ($rmaData) {
return !empty($rmaData) ? $rmaData : [];
};
return $this->valueFactory->create($result);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
} catch (LocalizedException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
}
}
/**
* @param array $args
* @return int
* @throws GraphQlInputException
*/
public function getEmailId(array $args): int
{
if (!isset($args['emailId'])) {
throw new GraphQlInputException(__('EmailId should be specified'));
}
return (int)$args['emailId'];
}
/**
* @return array
* @throws GraphQlNoSuchEntityException
*/
public function getRmaData(string $emailId): array
{
$emailNotExist = $this->emailExistOrNot($emailId);
if($emailNotExist){
// Guest User
$rmaCollection = $this->rmaFactory->create()->getCollection()
->addFieldToFilter("customer_email", $emailId)
->addFieldToFilter('customer_id', null);
}else{
// Customer
$customerData = $this->customerRepository->get($emailId);
$customerId = (int)$customerData->getId();
$rmaCollection = $this->rmaFactory->create()->getCollection()
->addFieldToFilter("customer_id", $customerId);
}
$output['rmaData'] = [];
foreach ($rmaCollection as $rmaItem) {
// $output['rmaData'][] = [
// 'entity_id' => $rmaItem['entity_id'],
// 'order_id' => $rmaItem['order_id'],
// 'customer_id' => $rmaItem['customer_id'],
// 'customer_name' => $rmaItem['customer_name'],
// 'customer_email' => $rmaItem['customer_email'],
// 'delivery_status' => $rmaItem['delivery_status'],
// 'additional_info' => $rmaItem['additional_info'],
// 'rma_status' => $rmaItem['rma_status'],
// 'created_on' => $rmaItem['created_on']
// ];
$rmaId = $rmaItem->getId();
$output['rmaData'][$rmaId]['entity_id'] = $rmaItem['entity_id'];
$output['rmaData'][$rmaId]['order_id'] = $rmaItem['order_id'];
$output['rmaData'][$rmaId]['customer_id'] = $rmaItem['customer_id'];
$output['rmaData'][$rmaId]['customer_name'] = $rmaItem['customer_name'];
$output['rmaData'][$rmaId]['customer_email'] = $rmaItem['customer_email'];
$output['rmaData'][$rmaId]['delivery_status'] = $rmaItem['delivery_status'];
$output['rmaData'][$rmaId]['additional_info'] = $rmaItem['additional_info'];
$output['rmaData'][$rmaId]['rma_status'] = $rmaItem['rma_status'];
$output['rmaData'][$rmaId]['created_on'] = $rmaItem['created_on'];
}
return $output;
}
/**
* Returns Email Id exist or not
*
* @return bool
*/
public function emailExistOrNot($emailId): bool
{
$websiteId = (int)$this->storeManager->getWebsite()->getId();
$isEmailNotExists = $this->customerAccountManagement->isEmailAvailable($emailId, $websiteId);
return $isEmailNotExists;
}
}
I have set an endpoint as http://127.0.0.1/magento243/graphql
and the query is,
{
RmaResolver(emailId: "abc@example.com") {
rmaData{
entity_id
order_id
customer_id
customer_name
customer_email
delivery_status
additional_info
rma_status
created_on
}
}
}
But the result is blank array,
{
"data": {
"RmaResolver": {
"rmaData": []
}
}
}
I have also created an API for getting data and with the API I'm getting records. FYI defined email-id is a customer and has rma records.
Please let me know if anyone has a solution.