How To Get A Product By GraphQL In Magento 2?

Today, we’re going to teach you guys how to get a product by GraphQL in Magento 2.

GraphQL is basically a query language that allows loading requested data from the server.

And in this post, we will show you how to use GraphQL to get product information in your Magento 2 store.

Steps to Get a Products by GraphQL in Magento 2

Please follow the below steps to learn how to get a products by GraphQL in Magento 2.

Step 1.

First of all, we need to get the product by using GraphQL as follows.

Endpoint: http(s)://domain.com/graphql

Method: POST

Syntax:  products(

    search: String

    filter: ProductFilterInput

    pageSize: Int

    currentPage: Int

    sort: ProductSortInput

): Products

Here’s the Request example:

{

  products(

    filter: { sku: { like: "24-WB%" } }

    pageSize: 20

    currentPage: 1

    sort: { name: DESC }

  ) {

    items {

      sku

    }

    filters {

      name

      filter_items_count

      request_var

      filter_items {

        label

        value_string

        items_count

      }

    }

  }

}

And here’s the Response:

{

    "data": {

        "products": {

            "total_count": 26,

            "items": [

                {

                    "name": "BUNDLE: Tiny Round \"Design Your Own\" Children's Necklace for Girls (50+ Optional Charms & FREE Engraving)",

                    "sku": "Tiny Round",

                    "price": {

                        "regularPrice": {

                            "amount": {

                                "currency": "USD",

                                "value": 439.78

                            }

                        }

                    }

                }],

            "page_info": {

                "page_size": 25,

                "current_page": 1

            }

        }

    }

}

Step 2.

Now, we need to write down the endpoint and your request.

Once it is done, click on the “Run” to send the request to the server.

This will get you the product by GraphQL in your Magento 2 store.

Also read: How to Get Products by Category using GraphQL in Magento 2?

Final Note

And that’s it!

This is the best way to get a product by GraphQL in your Magento 2 store.

And if you need our professional assistance, feel free to contact us anytime.