0

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 MageGuide\FirstModule\Model\Resolver;

use MageGuide\FirstModule\Model\BlogPost;
use MageGuide\FirstModule\Model\ResourceModel\BlogPost\Collection as BlogPosts;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

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 MageGuide\FirstModule\Model\ResourceModel\BlogPost;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

use MageGuide\FirstModule\Model\BlogPost as Model;
use MageGuide\FirstModule\Model\ResourceModel\BlogPostResource 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?

1 Answer 1

1

ensure that the object key "collection" is always refered with the same characters (copy paste them to be sure)

and change

return $results->getItems()->toArray();

to

return $results->load()->getData();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.