Magento 2 How to Get Related Product GraphQL Query

In Magento 2, if you want to get all the related items of the specific product by GraphQL, you need to use the Related Product GraphQL Query to retrieve all those items.

If any product has assigned some related products and want to fetch all the related items using GraphQL query, you need to use given payload to check the output.

Please find the GraphQL query to fetch the related products of a specific product by passing the sku of main product:

{
  products(filter: { sku: { eq: "football" } }) {
    items {
      id
      name
      related_products {
        id
        sku
        stock_status
        short_description {
          html
        }
        url_key
        name
        special_price
        price_range {
          minimum_price {
            final_price {
              value
              currency
            }
          }
          maximum_price {
            final_price {
              value
              currency
            }
          }
        }
      }
    }
  }
}

In the above product query, you can check the related_products{} field to retrieve all the related products.

If the product with sku as “football” contains related products assigned, the output will have all the assigned products array with related_products key otherwise it will display an empty array.

You can also add other required fields to the output as per your requirements.

Leave a Reply