Site icon EcomPlugins Blog

Magento 2 GraphQL example to read products data

In Magento 2, GraphQL can be used to fetch product data through the GraphQL API. Here’s an example query and explanation for retrieving product information like name, price, SKU, and description.

GraphQL Query to Fetch Products

{
  products(
    search: "shirt",
    pageSize: 10,
    currentPage: 1
  ) {
    items {
      id
      sku
      name
      description {
        html
      }
      price_range {
        minimum_price {
          regular_price {
            value
            currency
          }
          final_price {
            value
            currency
          }
        }
      }
    }
    total_count
    page_info {
      page_size
      current_page
      total_pages
    }
  }
}

Explanation of the Query:

  1. products:
    • This is the main query for fetching product data.
    • It accepts filters like search, pageSize, and currentPage.
  2. Parameters:
    • search: "shirt": Searches for products containing the keyword “shirt”.
    • pageSize: 10: Specifies the number of products per page.
    • currentPage: 1: Fetches the first page of results.
  3. Fields Returned:
    • items:
      • id: The unique identifier for the product.
      • sku: The Stock Keeping Unit.
      • name: The name of the product.
      • description.html: The product’s description in HTML format.
      • price_range: Contains:
        • minimum_price: Details about the lowest price.
          • regular_price: The original price.
          • final_price: The price after discounts.
    • total_count:
      • The total number of products matching the query.
    • page_info:
      • Pagination details:
        • page_size: Number of products per page.
        • current_page: The current page number.
        • total_pages: Total number of pages.

Expected Response

Here’s an example of the JSON response:

{
  "data": {
    "products": {
      "items": [
        {
          "id": 1,
          "sku": "shirt001",
          "name": "Casual Shirt",
          "description": {
            "html": "<p>High-quality casual shirt.</p>"
          },
          "price_range": {
            "minimum_price": {
              "regular_price": {
                "value": 49.99,
                "currency": "USD"
              },
              "final_price": {
                "value": 39.99,
                "currency": "USD"
              }
            }
          }
        }
      ],
      "total_count": 100,
      "page_info": {
        "page_size": 10,
        "current_page": 1,
        "total_pages": 10
      }
    }
  }
}

How to Use

  1. Ensure Magento 2 is configured with GraphQL enabled.
  2. Access the GraphQL endpoint, typically at:
    https://your-magento-site/graphql
  3. Use a GraphQL client like:
  4. Send the query to the endpoint and inspect the response.

This query can be extended to fetch more details, like media gallery, stock status, or additional attributes, based on your requirements.

Exit mobile version