I'm having a problem with the GraphQL API for Magento 2. I cannot retrieve the attribute value of multiselect or dropdown attributes when I retrieve my products.
In this example my attribute "material" returns an integer of the value ID instead of the textual value. I'm currently using the endpoint https://devdocs.magento.com/guides/v2.3/graphql/reference/custom-attribute-metadata.html to retrieve all the option values, but this is not very efficient.
{
products(
search: "Chair"
pageSize: 10
)
{
total_count
items {
name
sku
material
price {
regularPrice {
amount {
value
currency
}
}
}
}
page_info {
page_size
current_page
}
}
}
returns:
{
"data": {
"products": {
"total_count": 48,
"items": [
{
"name": "Brown Chair",
"sku": "4203894",
"material": 209,
"price": {
"regularPrice": {
"amount": {
"value": 89,
"currency": "EUR"
}
}
}
},
.......
Is there a way to get the textual value of the "material" attribute ("Leather") in this call?
Best Answer
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 filethis code will return the need
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.