Magento 2 How to Use Product Query GraphQL

In Magento 2, to get information of a catalog product we can use product query GraphQL which contains all the needed information about product.

To retrieve the information about product query in multiple ways, we can do it using search criteria with equal or match type or with full-text search within product query. We need to pass only the required fields on GraphQL Query so the output will display only those passed fields.

You can see the product query demo with multiple fields that will be displayed in the following output:

{
  products(search: "football") {
    items {
      id
      name
      sku
      only_x_left_in_stock
      meta_keyword
      meta_description
      stock_status
      special_price
      special_from_date
      special_to_date
      attribute_set_id
      manufacturer
      description {
        html
      }
      short_description {
        html
      }
      image {
        url
        label
        position
        disabled
      }
      small_image {
        url
        label
        position
        disabled
      }
      thumbnail {
        url
        label
        position
        disabled
      }
      new_from_date
      new_to_date
      price_tiers {
        quantity
        discount {
          percent_off
          amount_off
        }
        final_price {
          value
          currency
        }
      }
      ... on PhysicalProductInterface {
        weight
      }
      options_container
      created_at
      updated_at
      country_of_manufacture
      type_id
      websites {
        id
        name
        code
        sort_order
        default_group_id
        is_default
      }
      product_links {
        sku
        link_type
        linked_product_sku
        linked_product_type
        position
      }
      media_gallery {
        url
        label
        position
        disabled
        ... on ProductVideo {
          video_content {
            media_type
            video_provider
            video_url
            video_title
            video_description
            video_metadata
          }
        }
      }
      price_range {
        minimum_price {
          regular_price {
            value
            currency
          }
          final_price {
            value
            currency
          }
          fixed_product_taxes {
            label
            amount {
              value
              currency
            }
          }
        }
        maximum_price {
          discount {
            amount_off
            percent_off
          }
          fixed_product_taxes {
            label
            amount {
              value
              currency
            }
          }
        }
      }
      gift_message_available
      url_rewrites {
        parameters {
          name
          value
        }
      }
      related_products {
        id
        name
        sku
      }
      upsell_products {
        id
        name
        sku
      }
      crosssell_products {
        id
        name
        sku
      }
      categories {
        id
        url_key
        name
        position
        is_anchor
        url_suffix
        description
        display_mode
        meta_keywords
        path_in_store
        default_sort_by
        meta_description
        automatic_sorting
        custom_layout_update_file
        available_sort_by
        products {
          items {
            id
            sku
          }
        }
        cms_block {
          title
          content
          identifier
        }
      }
    }
  }
}

Given GraphQL query defines all the possible fields used for the product query but in real time scenarios, you can use the only required fields inside the query.

In the given Product query, we are expecting to return all the possible fields that is used on the products like,

  • Weight
  • Price Management
  • Category Management
  • Media Gallery Images
  • URL Management
  • Related
  • Upsell
  • Cross-Sell Products
  • Tier Price
  • Product New From – To
  • Custom Attributes of the Product
  • Website Assignment
  • Product Links
  • Gift Messages

There are different ways to search product query:

1) Products by search: You can filter the product by the direct full-text search with the query.

{
  products(search: "football") {
    items {
      id
      name
      sku
      special_price
    }
  }
}

2) Product by FilterEqualTypeInput attribute:

‘eq’ keywords,

{
  products(filter: { sku: { eq: "football" } }) {
    items {
      id
      name
      sku
      special_price
    }
  }
}

‘in’ keywords

{
  products(filter: { sku: { in: ["football-1", "football-2"] } }) {
    items {
      id
      name
      sku
      special_price
    }
  }
}

3) Product by FilterMatchTypeInput attribute:

{
  products(
    filter: { name: { match: "Top" } }
  ) {
    items {
      id
      name
      sku
      stock_status
      special_price
      price_range {
        minimum_price {
          regular_price {
            value
            currency
          }
          final_price {
            value
            currency
          }
        }
      }
    }
  }
}

4) Product by FilterRangeTypeInput attribute:

{
  products(
    filter: { name: { match: "Football 1" }, price: { from: "1000", to: "5000" } }
  ) {
    items {
      id
      name
      sku
      stock_status
      special_price
      price_range {
        minimum_price {
          regular_price {
            value
            currency
          }
          final_price {
            value
            currency
          }
        }
      }
    }
  }
}

You can filter with either full text or match or equal by custom attribute type based on your custom requirement.

Leave a Reply