skip to Main Content

I’m pretty frustrated with the graphql api of Magento. How is it not possible to receive the values of a custom attribute. I mean it’s easy to make a custom select-option attribute and making it available through graphql but you are only getting the integer value.

What’s the best procedure to following
1. Query the complete attribute to get the value
2. Extending the graphql schema (this involves a developer every time the client changes something)
3. Am I missing some functionality inside the graphql endpoints to Fix this?

2

Answers


  1. From Native GraqpQl we cannot get the expected result.But we can write a custom module with the graphql and achieve it.What my suggestion is get the product detail by sku using _productRepository->get($sku);. This will return the entire product details.So you can get the attribute data by using $attributes = $_product->getAttributes(); Here is my data provider file.

    /**
     * @params string $sku
     * this function return all the product data by product sku
     **/
    public function getProductBySku($sku)
    {
        return $this->_productRepository->get($sku);
    }
    /**
     * @params int $id
     * this function return all the word of the day by id
     **/
    public function getAttributesBySku( $sku ){
        $_product = $this->getProductBySku($sku);
        $attributes = $_product->getAttributes();// All Product Attributes
    
        $attributes_data = [];
        $x=0;
        foreach ($attributes as $attribute) {
            if($attribute->getIsUserDefined()){ // Removed the system product attribute by checking the current attribute is user created
                $attributeLabel = $attribute->getFrontend()->getLabel();
                $attributeValue = $attribute->getFrontend()->getValue($_product);
    
                if($attribute->getAttributeCode()=="language"){
                    $attributeLabelAndValue = $attributeLabel." - ".$attributeValue;
                    $attributes_data[$x]['atr_data'] = $attributeLabelAndValue;
                }
            }
            $x++;
        }
        return $attributes_data;
    }
    

    This code will return the need

    $attribute->getFrontend()->getLabel(); this will return the label and 
    $attribute->getFrontend()->getValue($_product); this will return the value.
    

    To check the entire graphQl query and resolver file kindly view How to get product attribute value, label using GraphQl in Magento 2.3?

    Hope this will help you.

    Login or Signup to reply.
  2. You can try creating custom graphql attribute with custom resolver.

    $product = $this->_productRepository->getById($value['entity_id']);
    $data = array();
    foreach ($args['fields'] as $fi) {
      $att = $product->getResource()->getAttribute($fi);
      if (isset($att) && $att) {
        if (in_array(
          $att->getFrontendInput(),
          ['multiselect', 'select']
        )) {
          $data[$fi . '_label'] = $product->getAttributeText($fi);
        }
        $data[$fi] = $product->getData($fi);
      }
    }
    return json_encode((object) $data);
    

    which should display all provided attributes with their labels.

    "sku": "testProduct",
    "fabric": 60,
    "work": 65,
    "dynamicAttributes": "{
        "fabric_label":"Pure Silk",
        "fabric":"60",
        "work_label":"Tanchoi",
        "work":"65"
    }",
    

    Check out entire module here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search