How to get product attribute value, label using GraphQl in Magento 2?

How to get product attribute value, label using GraphQl in Magento 2? – Today we can discuss about product attribute and how to get the product attributes values in Graphql. We can directly get the attribute value from native GraphQl query. We can get the custom attributes value also by passing the attribute_code to the GraphQl query.

But we have some issues when the attribute type is select box or multi-select. For select box or multi-select attributes the native GraphQl query only returns the value not the label of the value.

As far as is searched i couldn’t find a way to get the labels. So i achieved this with the custom module.I will explain here how i did it with the custom module.

So we need to create a simple module. I am not going to explain it.Apart from that we need to create shema.gaphqls file under etc directory. Resolver file under Model/Resolver directory.
A data provider file under Module/Resolver/Dataprovider directory.We are going to pass the SKU of the product and get the attributes values.

A data provider file under Module/Resolver/Dataprovider directory.We are going to pass the SKU of the product and get the attributes values.

Custom module structure
Custom module structure

schema.graphqls file

Resolver file

Retrieve product attribute in data provider file

That’s it. I used if($attribute->getIsUserDefined()) to check the attribute is Custom or System attribute.
If we want to get only some attributes value and label.We can use if($attribute->getAttributeCode()==”language”) and get relevant data.For this just pass the custom attribute_code as an array and can get it.
Now lets see the GraphQl query and result.

query attributeValues{
attributeValues( sku : “24-MB02”) {
atr_data
}
}

This query will return entire custom product attributes.

product attribute value and label in graphql
sample GraphQl Request Payload and Result 

In This result Languages is the multi-select attribute and it returns the result like “Languages:- English, Tamil, Sinhala”.So in this article we write a custom GraphQl module to get the attribute value, label for products.

Attribute languages - Multi select
Attribute languages – Multi select

That’s it. Have a nice day.Enjoy coding , Learn , Experience , Teach and Help.

17 thoughts on “How to get product attribute value, label using GraphQl in Magento 2?”

  1. There are typos in your code.

    In resolver, you are calling getAttriNutesBySku and in DataProvider you are missing a coma in __contruct() after $productRepository 🙂

  2. I savor, lead to I discovered just what I was having a look
    for. You’ve ended my 4 day lengthy hunt! God Bless you man.
    Have a great day. Bye

  3. I have been browsing on-line more than 3 hours lately, but I never discovered any attention-grabbing article
    like yours. It’s pretty price enough for me.
    Personally, if all website owners and bloggers made excellent
    content as you probably did, the internet will likely be
    much more helpful than ever before.

Leave a Reply

Your email address will not be published. Required fields are marked *