Hello, Magento mates.
Welcome to MageComp Magento tutorials.
Today, in this Magento 2 tutorial, we will learn to add custom product attributes to existing product GraphQL.
Product attributes refer to the specific characteristics and features that describe a product. These traits help consumers understand the product’s qualities and make informed purchasing decisions.
Examples of product attributes include size, color, material, weight, functionality, and any other distinctive features that differentiate one product from another. Businesses often highlight these attributes in marketing materials to communicate the value and benefits of their products to potential customers.
Steps to Add Custom Product Attribute
Steps 1 –
To begin with the steps, first, we need to create a “schema.graphqls” file inside the extension at the following path.
app\code\Vendor\Extension\etc\
interface ProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\\ProductInterfaceTypeResolverComposite") @doc(description:"Seller Id") { seller_id: String @doc(description: "Seller Id") @resolver(class: "Vendor\\Extension\\Model\\Resolver\\Product\\Getsellerid") }
Step 2 –
After that, we need to create the “Getsellerid.php” file inside the extension at the following path.
app\code\Vendor\Extension\Model\Resolver\Product
<?php namespace Vendor\Extension\Model\Resolver\Product; use \Magento\Catalog\Model\Product; class Getsellerid implements \Magento\Framework\GraphQl\Query\ResolverInterface { protected $productdata; public function __construct( Product $productdata ) { $this->productdata = $productdata; } 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']; $productdata = $this->productdata->load($product->getId()); $sellerId = $productdata->getSellerId(); return $sellerId; } }
Step 3 –
Once all files are created in your Magento, you need to run Magento upgrade, compile and deploy commands as follows.
php bin/magento setup:upgrade php bin/magento setup:static-content:deploy -f php bin/magento setup:di:compile php bin/magento cache:clean php bin/magento cache:flush
Step 4 –
Postman to run the below query to get configuration value on storeconfig response.
Base Url: {base_url}/graphql
{ products( filter: { sku: { eq: "Simple Product" } } ){ items { id sku name seller_id } } }
Output ?
Ending Note
Incorporating custom product attributes into an existing product GraphQL setup is a powerful way to enhance the flexibility and functionality of your Magento 2 store.
By following the steps outlined in the tutorial, you can seamlessly extend the capabilities of your product data model, providing a more tailored and comprehensive experience for both administrators and end-users.
I hope this tutorial was helpful for you. If you encounter any difficulties while following these steps, please feel free to leave a comment with your queries in the comment section below. Alternatively, you can contact us through our official Facebook page or hire an experienced Magento 2 developer to assist you with any issues.
Thank you for reading.
Happy Coding!!!
Hi, I followed your tutorial and i am still getting the error:
“errors”: [
{
“message”: “parsing error: no field `stock_status` in type `ProductInterface`”,
“extensions”: {
“code”: “PARSING_ERROR”
}
}
]
What am I missing, help please!!!
If you are facing the issue then contact on support@magecomp.com