Skip to content
intermediate Phase 61 · GraphQL Basics

GraphQL Queries

Working with Magento 2 GraphQL queries: products, categories, customer, and cart queries

45m
0 problems
Topic Progress 0%

Product Queries

Basic Product Query

query {
    products {
        items {
            id
            name
            sku
            url_key
            price {
                regularPrice {
                    amount {
                        value
                        currency
                    }
                }
            }
        }
        total_count
    }
}

Product by SKU

query {
    product(sku: "SKU-123") {
        id
        name
        sku
        description {
            html
        }
        short_description {
            html
        }
        image {
            url
            label
        }
        price_range {
            minimum_price {
                regular_price {
                    value
                    currency
                }
                final_price {
                    value
                    currency
                }
            }
        }
        stock_status
    }
}

Product Search

query {
    products(
        search: "shirt"
        filter: {
            price: { from: "10" to: "50" }
            category_id: { eq: "5" }
        }
        sort: { price: ASC }
        pageSize: 20
        currentPage: 1
    ) {
        total_count
        items {
            name
            sku
            price_range {
                minimum_price {
                    final_price {
                        value
                    }
                }
            }
        }
        page_info {
            page_size
            current_page
        }
        aggregations {
            attribute_code
            label
            count
            options {
                label
                value
                count
            }
        }
    }
}

Category Queries

Category Tree

query {
    categoryList {
        id
        name
        children {
            id
            name
            children_count
            children {
                id
                name
            }
        }
    }
}

Category by ID

query {
    category(id: 5) {
        id
        name
        description
        image
        url_path
        products {
            items {
                name
                sku
                price_range {
                    minimum_price {
                        final_price {
                            value
                        }
                    }
                }
            }
            total_count
        }
    }
}

Category Products with Filters

query {
    category(id: 5) {
        name
        products(
            filter: {
                price: { from: "10" to: "100" }
            }
            sort: { position: ASC }
            pageSize: 10
            currentPage: 1
        ) {
            total_count
            items {
                id
                name
                sku
                price_range {
                    minimum_price {
                        final_price {
                            value
                        }
                    }
                }
            }
            aggregations {
                attribute_code
                label
                options {
                    label
                    value
                    count
                }
            }
        }
    }
}

Customer Queries

Customer Profile

query {
    customer {
        id
        email
        firstname
        lastname
        default_billing
        default_shipping
        addresses {
            id
            firstname
            lastname
            street
            city
            region {
                region_code
                region
            }
            postcode
            country {
                code
                label
            }
            telephone
            default_shipping
            default_billing
        }
    }
}

Customer Orders

query {
    customerOrders {
        items {
            id
            increment_id
            created_at
            status
            grand_total
            items {
                product_name
                product_sku
                quantity_ordered
                row_total
            }
        }
    }
}

Customer Wishlists

query {
    customerWishlist {
        id
        name
        items {
            id
            product {
                name
                sku
                price_range {
                    minimum_price {
                        final_price {
                            value
                        }
                    }
                }
            }
        }
    }
}

Cart Queries

Get Cart

query {
    cart(cart_id: "cart123") {
        id
        total_quantity
        prices {
            grand_total {
                value
                currency
            }
            subtotal_excluding_tax {
                value
                currency
            }
            subtotal_including_tax {
                value
                currency
            }
        }
        items {
            id
            product {
                name
                sku
                image {
                    url
                }
            }
            quantity
            prices {
                row_total {
                    value
                    currency
                }
            }
        }
        shipping_addresses {
            firstname
            lastname
            street
            city
            region {
                region_code
                region
            }
            postcode
            country {
                code
            }
        }
        available_payment_methods {
            code
            title
        }
    }
}

Estimate Shipping

query {
    cart(cart_id: "cart123") {
        available_shipping_methods {
            carrier_code
            carrier_title
            method_code
            method_title
            amount {
                value
                currency
            }
        }
    }
}

Get Payment Methods

query {
    cart(cart_id: "cart123") {
        available_payment_methods {
            code
            title
        }
    }
}

Quiz

1. How do you search products in GraphQL?

Question 1 options

2. How do you get the category tree?

Question 2 options

3. How do you access customer data?

Question 3 options

Flashcards

Question

How do you search products?

Answer

products(search: "term")

Question

How do you get a product by SKU?

Answer

product(sku: "SKU-123")

Question

How do you get the category tree?

Answer

categoryList { children { ... } }

Question

How do you get customer orders?

Answer

customerOrders { items { ... } }

Question

How do you get cart details?

Answer

cart(cart_id: "...") { items { ... } }

Revision Notes

Key Takeaways

  • 1. Products can be searched, filtered, and sorted
  • 2. Categories support tree and product listing
  • 3. Customer queries require authentication
  • 4. Cart queries use cart_id parameter
  • 5. Aggregations provide filter options

Interview Tips

  • Know the common GraphQL queries
  • Understand filtering and sorting
  • Discuss query optimization
  • Be ready to write complex queries

Cheat Sheet

Products:
  products(search: "term", filter: {...}, sort: {...})
  product(sku: "SKU")

Categories:
  categoryList { children { id, name } }
  category(id: 5) { products { items { ... } } }

Customer:
  customer { email, firstname }
  customerOrders { items { ... } }

Cart:
  cart(cart_id: "...") { items { ... } }