1

I am using graphql mutation

mutation {
  addProductsToCart(
    cartId: "KlV2AfyAkbn19yuXE8EaBmabYqh4DMiX"
    cartItems: [{ quantity: 1, sku: "S359120" }]
  ) {
    cart {
      items {
        product {
          name
          sku
          custom_category {
            category
            doll_category
            special_category
          }
        }
        quantity
      }
    }
  }
}

As you can see i pass "S359120" sku but in its resolver i get different sku.

<?php
    
    declare (strict_types = 1);
    
    namespace Vendor\ProductGraphQl\Model\Resolver;
    
    use Vendor\Segment\Helper\Tracking;
    use Magento\Framework\GraphQl\Config\Element\Field;
    use Magento\Framework\GraphQl\Query\ResolverInterface;
    use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
    use Magento\Catalog\Api\Data\ProductInterface;
    use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable;
    use Magento\Catalog\Model\Product;
    
    /**
     * Product attributes (product_category,special_category,doll_category) resolver, used for GraphQL request processing.
     */
    class ProductCustomCategoryResolver implements ResolverInterface
    {
         /**
         * @var Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable
         */
        private $configurableProduct;
    
        /**
         * @var Magento\Catalog\Model\Product
         */
        private $product;
    
        /**
         * @var Tracking
         */
        protected $segment_helper;
    
        /**
         * @param \DollsKill\Segment\Helper\Tracking $segment_helper
         */
        public function __construct(
            Tracking $segment_helper,
            Configurable $configurableProduct,
            Product $product
        )
        {
            $this->segment_helper = $segment_helper;
            $this->configurableProduct = $configurableProduct;
            $this->product             = $product;
        }
    
        /**
         * @inheritdoc
         */
        public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
        {
            $product = $value['model'];
            echo $product->getSku(); //getting same sku on every graphql mutation call
        }
    }

app/code/Vendor/ProductGraphQl/etc/schema.graphqls

interface ProductInterface {
    custom_category: [ProductCustomCategoryResolver] @doc(description: "Custom Product categories") @resolver(class: "\\DollsKill\\ProductGraphQl\\Model\\Resolver\\ProductCustomCategoryResolver")
}


type ProductCustomCategoryResolver {
    category: String
    doll_category: String
    special_category: String
}
4
  • please elaborate more
    – Devidas
    Jun 28, 2021 at 10:30
  • Hello, thanks for the comment let me add more details
    – devhs
    Jun 28, 2021 at 10:34
  • Please check now @Devidas
    – devhs
    Jun 28, 2021 at 10:35
  • no thoughts on this ?
    – devhs
    Jun 28, 2021 at 11:40

1 Answer 1

0

try below code

<?php
    
    declare (strict_types = 1);
    
    namespace Vendor\ProductGraphQl\Model\Resolver;
    
    use Vendor\Segment\Helper\Tracking;
    use Magento\Framework\GraphQl\Config\Element\Field;
    use Magento\Framework\GraphQl\Query\ResolverInterface;
    use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
    use Magento\Catalog\Api\Data\ProductInterface;
    use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable;
    use Magento\Catalog\Model\Product;
    
    /**
     * Product attributes (product_category,special_category,doll_category) resolver, used for GraphQL request processing.
     */
    class ProductCustomCategoryResolver implements ResolverInterface
    {
         /**
         * @var Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable
         */
        private $configurableProduct;
    
        /**
         * @var Magento\Catalog\Model\Product
         */
        private $product;
    
        /**
         * @var Tracking
         */
        protected $segment_helper;
    
        /**
         * @param \Vendor\Segment\Helper\Tracking $segment_helper
         */
        public function __construct(
            Tracking $segment_helper,
            Configurable $configurableProduct,
            Product $product
        )
        {
            $this->segment_helper = $segment_helper;
            $this->configurableProduct = $configurableProduct;
            $this->product             = $product;
        }
    
        /**
         * @param Field $field
         * @param $context
         * @param ResolveInfo $info
         * @param array|null $value
         * @param array|null $args
         * @return mixed
         */
        public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
        {
            if (!array_key_exists('model', $value) || !$value['model'] instanceof ProductInterface) {
            throw new LocalizedException(__('"model" value should be specified'));
            }
                $product = $value['model'];
                $data = $this->getData($product);
        }

        /**
         * @param ProductInterface $product
         * @return mixed
         */
        private function getData(ProductInterface $product)
        {
           echo $product->getSku();
        }
    }
1
  • Thanks for the answer, but Its not working still getting some other sku
    – devhs
    Jun 29, 2021 at 4:47

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.