How to Use Product Query GraphQL Magento 2?

Product Query GraphQL Magento contains a lot of information for a product.

You can fetch the result of the product query in multiple ways, Using Search Criteria with equal or match type, By full-text search within product query.

Pass only the required field on GraphQL Query so the output will be displayed only on those fields.

If you want to explore CMS GraphQL query, check the link, Get Cms Page and Cms blocks GraphQL Query.

Product Query Demo with multiple fields that will be displayed in the Output.

{
  products(search: "crown") {
    items {
      id
      name
      sku
      stock_status
      only_x_left_in_stock
      meta_keyword
      meta_description
      special_price
      special_from_date
      special_to_date
      attribute_set_id
      manufacturer
      canonical_url
      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 Query defines all the possible fields used for the product query but in real life, you can use the only required field inside the query.

In the given Product Query, We are expecting to return all the possible field that is used on the Products like,

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

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: "crown") {
    items {
      id
      name
      sku
      special_price
    }
  }
}

2) Product by FilterEqualTypeInput attribute:

‘eq’ keywords,

{
  products(filter: { sku: { eq: "24-MB03" } }) {
    items {
      id
      name
      sku
      special_price
    }
  }
}

‘in’ keywords

{
  products(filter: { sku: { in: ["24-MB03", "24-MB04"] } }) {
    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: "Top" }, price: { from: "20", to: "50" } }
  ) {
    items {
      id
      name
      sku
      stock_status
      special_price
      price_range {
        minimum_price {
          regular_price {
            value
            currency
          }
          final_price {
            value
            currency
          }
        }
      }
    }
  }
}

You can filter either full text or match or equal by custom attribute type based on your project scope of work.