1

How to add custom product attribute into existing product graphql query in the custom module in Magento 2.

THANKS.

2 Answers 2

3

Step 1 :- You Create registration.php file

<?php
/**
 * @author Mohit Patel
 * @copyright Copyright (c) 2021
 * @package Mag_ExtendQuery
 */

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Mag_ExtendQuery',
    __DIR__
);

Step 2 :- create etc/module.xml file

<?xml version="1.0"?>
<!--
/**
 * @author Mohit Patel
 * @copyright Copyright (c) 2021
 * @package Mag_ExtendQuery
 */
-->

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Mag_ExtendQuery" setup_version="2.0.0">
        <sequence>
            <module name="Magento_Backend"/>
            <module name="Magento_GraphQl"/>
        </sequence>
    </module>
</config>

Step 3 :- GraphQL queries are declared under etc/schema.graphqls

interface ProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\\ProductInterfaceTypeResolverComposite") @doc(description: "The ProductInterface contains attributes that are common to all types of products. Note that descriptions may not be available for custom and EAV attributes.") {
    your_custom_attribute: String @doc(description: "Brand Name based on manufacturer attribute") @resolver(class: "Mag\\ExtendQuery\\Model\\Resolver\\Product\\AddCustomAttribute")
}

Setp 4 :- Create Resolver Class Model/Resolver/Product/GetCustomAttribute.php

<?php
/**
 * @author Mohit Patel
 * @copyright Copyright (c) 2021
 * @package Mag_ExtendQuery
 */

namespace Mag\ExtendQuery\Model\Resolver\Product;

class GetCustomAttribute implements \Magento\Framework\GraphQl\Query\ResolverInterface
{

    public function resolve(
        \Magento\Framework\GraphQl\Config\Element\Field $field,
        $context,
        \Magento\Framework\GraphQl\Schema\Type\ResolveInfo $info,
        array $value = null,
        array $args = null
    ) {
        $product = $value['model'];
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $_product = $objectManager->get('Magento\Catalog\Model\Product')->load($product->getId());
        return $_product->getData('product_name');
    }
}

Setp 5 :- Final step you check output. Run this query

{
  products(filter: { sku: { in: "new testing" } }){
    items {
      your_custom_attribute
    }
  }
}

Output

enter image description here

Check More Links

How to create graphql mutation :- https://magento.stackexchange.com/a/351569/85907

How to create graphql query :- https://magento.stackexchange.com/a/351567/85907

THANKS.

1
  • Yes this works But if I want to work with filters or sort, I always receive NULL in $args.. Any Solution for this ? Commented Feb 20, 2023 at 14:01
1

Now you can just add that attribute in your graphql query like below and it will show up in response:

{
  products(filter: { sku: { eq: "24-MB01" } }){
    items {
      sku
      cproduct_attribute
    }
  }
}

I have tried this in Magento 2.4

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.