简体   繁体   中英

Magento 2 - load Catalog collection without use of objectmanager in GraphQl

I am trying to load the Catalog collection without using object manager by injecting the class in the construct method and fetching the collection but I am facing the issue - "Error: Call to a member function load() on null".

schema.graphqls

type Mutation {
    setsubcategoryslider (
        slidermaincategoryid : Int @doc(description: "Add Main Category Id Value")
        slidersubcategoryids : Int @doc(description: "Add Sub Category Id Value")
    ): Setsubcategorysliderotpoutput @resolver(class: "[Vendor]\\[Extension]\\Model\\Resolver\\Setslidervalues") @doc(description:"Set Sub Categories for Slider")
    
}

type Setsubcategorysliderotpoutput {
    sliderstatus: Boolean
    slidermessage: String    
}

Model\Resolver\Setslidervalues.php file:

<?php
namespace [Vendor]\[Extension]\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\Catalog\Model\Category;
use Magento\Catalog\Model\CategoryFactory;
use Magento\Catalog\Api\Data\CategoryInterface;

class Setslidervalues implements ResolverInterface{
    public $_categorydata;
    public $categoryFactory;
    public $categoryInterface;
        
    public function __construct(
        Category $category,
        CategoryFactory $categoryFactory,
        CategoryInterface $categoryInterface
    )
    {
        $this->_categorydata = $category;
        $this->categoryFactory = $categoryFactory;
        $this->categoryInterface = $categoryInterface;
    }
    /**
     * @param Field $field
     * @param \Magento\Framework\GraphQl\Query\Resolver\ContextInterface $context
     * @param ResolveInfo $info
     * @param array|null $value
     * @param array|null $args
     * @return array|\Magento\Framework\GraphQl\Query\Resolver\Value|mixed
     * @throws GraphQlInputException
     */
    public function resolve(
        Field $field,
        $context,
        ResolveInfo $info,
        array $value = null,
        array $args = null)
    {
        if (empty($args['slidermaincategoryid']) && empty($args['slidersubcategoryids'])
            ) {
              throw new GraphQlInputException(__('Invalid parameter list.'));
        }
        $output = [];
        $output['slidermaincategoryid'] = $args['slidermaincategoryid'];
        $output['slidersubcategoryids'] = $args['slidersubcategoryids'];
        try {
            $categoryid = $output['slidermaincategoryid'];
            $cate = $this->_categorydata->load($categoryid);
            $category  = $this->categoryFactory->create()->load($categoryid);
            $customData = $this->categoryInterface->setCustomAttribute([attribute_code], $output['slidersubcategoryids']);
            $cate->save();
            return ['status' => true, 'message' =>__("Sub category  is saved to the slider.")];
        }
        catch (NoSuchEntityException $e) {
            throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
            return ['status'=>false, 'message'=>$e];
        }
    }
}

Thank you in advance !

''' class HelloWorld extends \Magento\Framework\View\Element\Template {
protected $_categoryRepository;

public function __construct(
    \Magento\Backend\Block\Template\Context $context,       
    \Magento\Catalog\Model\CategoryRepository $categoryRepository,
    array $data = []
)
{
    $this->_categoryRepository= $categoryRepository;
    parent::__construct($context, $data);
}

public function getById($id)
{
    return $this->_categoryRepository->getById($id);
}

'''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM