I want Stock_id in graphql. We can get this via REST API but I need this in graphql.
Can anyone help me with this?
Please refer to the attached link and screenshot here
I really appreciate any help you can provide.
I want Stock_id in graphql. We can get this via REST API but I need this in graphql.
Can anyone help me with this?
Please refer to the attached link and screenshot here
I really appreciate any help you can provide.
This following guide will help you to get "extension attribute of product in GraphQL Magento 2"
https://www.mageplaza.com/devdocs/add-extension-attributes-to-entity-magento-2.html
To get the extension attributes in the query, the good way is to change the schema description and extend it in your own module.
Create schema.graphqls file in our custom modules (Vendor/CustomGQL) etc directory. Vendor_CustomGQL/etc/schema.graphqls
In schema.graphqls file, write your code to extend the functionality as:
interface ProductInterface {
attribute_set_id: Int
@doc(description: "ID of the attribute set assigned to the product")
attribute_set_name: String
@doc(description: "Name of attribute set assigned to the product")
@resolver(class: "\\ExampleCorp\\CustomGQL\\Model\\Resolver\\ProductAttributeSetNameResolver") }
example
interface ProductInterface{
stock_id: String
@doc(description: "Name of attribute set assigned to the product")
@resolver(class: "\\CompanyName\\ModuleName\\Model\\Resolver\\ProductStockSetNameResolver"), }
The attribute_set_id field is already present in the original schema, so the field described in our new schema will override the field present in the ProductInterface object.
The attribute_set_name field is not present in the orignal schema, so the field is added to the ProductInterface object by extending it.
Resolver file
For setting value create the resolver in your module: Vendor_CustomGQL/Model/Resolver/ProductStockSetNameResolver.php
write your code to return the value of the custom extension attribute:
<?php
namespace ExampleCorp\CustomGQL\Model\Resolver;
use Magento\Catalog\Api\AttributeSetRepositoryInterface;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
/**
* Class to resolve custom attribute_set_name field in product GraphQL query
*/
class ProductAttributeSetNameResolver implements ResolverInterface
{
/**
* @var AttributeSetRepositoryInterface
*/
private $setRepository;
public function __construct(AttributeSetRepositoryInterface $setRepository)
{
$this->setRepository = $setRepository;
}
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
return $this->setRepository->get($value['attribute_set_id'])->getAttributeSetName();
}
}
For full documentation please check this: https://developer.adobe.com/commerce/webapi/graphql/develop/extend-existing-schema/