I created a custom module with a custom table in magento 2.3.2 including grpahql schema.
I want to get the data using graphql by passing an argument.
I am getting the following error in Alatair GrapgQL client chrome extension.
{
"errors": [
{
"message": "Cannot query field \"wordoftheday\" on type \"Wordoftheday\".",
"category": "graphql",
"locations": [
{
"line": 3,
"column": 5
}
]
}
]
}
I passed the query like below.
{
wordoftheday ( id : 1) {
Wordoftheday{
word
verb
meaning
detail
}
}
}
/app/code/Ayakil/Wordoftheday/etc/schema.graphqls
type Query {
wordoftheday (
id: Int @doc(description: "Current Day to show WOD in Home page")
): Wordoftheday @resolver(class: "Ayakil\\Wordoftheday\\Model\\Resolver\\Wordoftheday") @doc(description: "The Sales Order query returns information about customer all placed order")
}
type Wordoftheday @doc(description: "wod information") {
word : String @doc(description: "word")
verb : String @doc(description: "verb")
meaning : String @doc(description: "meaning")
detail : String @doc(description: "detail")
}
/app/code/Ayakil/Wordoftheday/Model/Resolver/DataProvider/Wordoftheday.php
<?php
namespace Ayakil\Wordoftheday\Model\Resolver\DataProvider;
class Wordoftheday
{
protected $_wordofthedayFactory;
public function __construct(
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
\Ayakil\Wordoftheday\Model\WordofthedayFactory $wordofthedayFactory
)
{
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->_wordofthedayFactory = $wordofthedayFactory;
}
public function getWordoftheday( int $date_to_show)
{
try {
/* filter for all customer orders */
//$searchCriteria = $this->searchCriteriaBuilder->addFilter('id', $date_to_show,'eq')->create();
$wod = $this->_wordofthedayFactory->create();
$collection = $wod->getCollection();
$collection->addFieldToFilter('id',$date_to_show);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
}
return $collection;
}
}
What is my mistake here?please help me to short it out.
Best Answer
After a lot of searching, I found my mistake. My error was capturing the id in the resolver file.
$args['id'] here as per my graphQl schema I want to pass id, but I passed something else.
And also my passed query also wrong. I should pass like below to get the result as per my graphql schema.
I correct all my mistakes and got it up and run, to get a clear understanding please read How to create a Graphql schema by passing an argument for custom Magento 2 module with the custom table?.