0

I'm trying to debug an issue with a custom GraphQL module. I want to log the POST data that's being sent to it. I know it is being sent, because the response works OK for the most part. There's just a small language issue, which could form a question in itself, but as it's custom code it would take a small dissertation to explain.

So for this question I just want to focus on why, when I call $this->request->getPost(), I get an empty object.

Here's my code:

<?php

declare(strict_types=1);

namespace Mycompany\CatalogGraphQl\Model\Resolver\DataProvider;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\RequestInterface;


class ConfigurableProduct {

    private $productRepository;
    private $storeManager;
    private $request;

    public function __construct(
        ProductRepositoryInterface $productRepository,
        StoreManagerInterface $storeManager,
        RequestInterface $request
    ) {
        $this->productRepository = $productRepository;
        $this->storeManager = $storeManager;
        $this->request = $request;
    }

    public function getData($productSku) {
    
        // quick and dirty logging
        $logfile = "/tmp/magento.log";
        file_put_contents($logfile, print_r($this->request->getParams(), TRUE), FILE_APPEND);
        file_put_contents($logfile, print_r($this->request->getPost(), TRUE), FILE_APPEND);
        file_put_contents($logfile, print_r($_POST, TRUE), FILE_APPEND);
        
        $currentStoreId = $this->storeManager->getStore()->getID();
        $product = $this->productRepository->get($productSku, false, $currentStoreId, true);
        return [
            'master' => $product->getData(),
            'type_id' => $product->getTypeId(),
            'row_id' => $product->getRowId(),
            'model' => $product
        ];

    }

}

As you can see I'm trying to log $this->request->getPost(), $this->request->getParams() and even $_POST. $_POST is an empty array. getParams() gives me only query string parameters. And getPost() gives me this:

Laminas\Stdlib\Parameters Object
(
    [storage:ArrayObject:private] => Array
        (
        )

)

Why is it not letting me get at the POST data?

1 Answer 1

3

It seens Magento doesn't treats GraphQL requests as "POST" requests but the implementation of RequestInferface used in your request attribute is Magento\Framework\App\Request\Http and it implements Magento\Framework\App\RequestContentInterface, so try to use $this->request->getContent() instead.

1
  • Brilliant, that worked, thank you! Feb 3, 2021 at 17:34

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.