I create a module in magento 2 to test Graphql, but it’s not work.
my schema.graphqls
#Coppy codilar Blog
type Query{
codilarblog: Codilarblog @resolver(class : "Codilar\GraphQlDemo\Model\Resolver\Blog") @doc(description: " the query return something")
}
type Codilarblog @doc(description : "Codilarblog defines the blog name and "){
entity_id: Int
blog_name: String
author: String
blog_content : String
}
my Resolver/Blog
<?php
namespace CodilarGraphQlDemoModelResolver;
use MagentoFrameworkGraphQlConfigElementField;
use MagentoFrameworkGraphQlQueryResolverContextInterface;
use MagentoFrameworkGraphQlQueryResolverValue;
use MagentoFrameworkGraphQlQueryResolverValueFactory;
use MagentoFrameworkGraphQlQueryResolverInterface;
use MagentoFrameworkGraphQlSchemaTypeResolveInfo;
class Blog implements ResolverInterface
{
/**
* @var ValueFactory
*/
private $valueFactory;
/**
* Blog constructor.
* @param ValueFactory $valueFactory
*/
public function __construct(ValueFactory $valueFactory)
{
$this->valueFactory = $valueFactory;
}
/**
* @param Field $field
* @param ContextInterface $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @return Value|mixed
* @throws Exception
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
try {
$blog = $this->getBlogArray();
$result = function () use ($blog) {
return !empty($blog) ? $blog : [];
};
return $this->valueFactory->create($result);
} catch (Exception $exception) {
throw new Exception(__($exception->getMessage()));
}
}
/**
* @return array
*/
public function getBlogArray()
{
$blog = [
'entity_id' => 1,
'blog_name' => 'Some blog name',
'author' => 'Some Author Name',
'blog_content' => 'Hi, this is blog content.'
];
return $blog;
}
}