In my module I defined a etc/schema.graphqls
:
type BlogPost {
blog_post_id: ID
title: String
post: String
}
input BlogpostAttributeFilterInput {
page: Int
limit: Int
}
type Query {
allBlogPosts(input: BlogpostAttributeFilterInput) :
[BlogPost] @resolver(class: "\MageGuide\FirstModule\Model\Resolver\GetBlogPosts")
@doc(description:"Retrieve All BlogPosts")
@cache(cacheable: false)
}
And I made the following resolver:
namespace MageGuideFirstModuleModelResolver;
use MageGuideFirstModuleModelBlogPost;
use MageGuideFirstModuleModelResourceModelBlogPostCollection as BlogPosts;
use MagentoFrameworkGraphQlQueryResolverInterface;
use MagentoFrameworkGraphQlConfigElementField;
use MagentoFrameworkGraphQlSchemaTypeResolveInfo;
class GetBlogPosts implements ResolverInterface
{
private BlogPosts $collection;
public function __construct(BlogPosts $collection){
$this->collection=$collection;
}
public function resolve( Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null)
{
// @TODO Get page and limit from graphql
$page = 1;
$limit = 10;
$results = $this->collection
->setPageSize($limit)
->setCurPage($page)
->addOrder('creation_dt','DESC');
return $results->getItems()->toArray();
}
}
That depends upon the following model:
namespace MageGuideFirstModuleModelResourceModelBlogPost;
use MagentoFrameworkModelResourceModelDbCollectionAbstractCollection;
use MageGuideFirstModuleModelBlogPost as Model;
use MageGuideFirstModuleModelResourceModelBlogPostResource as ResourceModel;
class Collection extends AbstractCollection
{
protected function _construct()
{
$this->_init(Model::class, ResourceModel::class);
}
}
But once I do my request:
{
allBlogPosts{
blog_post_id
title
post
}
}
I get the following error:
{
"errors": [
{
"message": "Internal server error",
"locations": [
{
"line": 2,
"column": 2
}
],
"path": [
"allBlogPosts"
],
"extensions": {
"debugMessage": "Typed property MageGuide\FirstModule\Model\Resolver\GetBlogPosts::$collection must not be accessed before initialization"
}
}
],
"data": {
"allBlogPosts": null
}
}
Does that mean that I need to modify the di.xml
in order the collection to be provided as well?