Magento GraphQL can return not only the data of a configurable product but also its configurable attributes and associated child products.
For example, the query might look like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
{ products( filter: { sku: { eq: "CONFIGURABLE-SKU" } } ) { items { sku name ... on ConfigurableProduct { configurable_options { attribute_code label values { uid label } } variants { product { sku name stock_status } } } } } } |
The expression … on ConfigurableProduct is an inline fragment. A normal products query can return different product types; the fragment requests additional fields that exist only on configurable products.
The field configurable_options returns configurable attributes and their possible values. Under variants you can access the associated simple products, including their SKUs and stock status.
A response could look like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
{ "data": { "products": { "items": [ { "sku": "SHIRT", "name": "T-Shirt", "configurable_options": [ { "attribute_code": "color", "label": "Farbe", "values": [ { "uid": "Y29uZmlndXJhYmxlLzEwLzUy", "label": "Schwarz" }, { "uid": "Y29uZmlndXJhYmxlLzEwLzUz", "label": "White" } ] } ], "variants": [ { "product": { "sku": "SHIRT-BLACK", "name": "T-Shirt Schwarz", "stock_status": "IN_STOCK" } }, { "product": { "sku": "SHIRT-WHITE", "name": "White T-Shirt", "stock_status": "OUT_OF_STOCK" } } ] } ] } } } |
You can then process the data in your frontend, for example filtering the response to include only available variants:
|
1 2 3 4 5 6 7 8 9 10 |
const product = response.data.products.items[0]; const availableVariants = product.variants .filter(variant => variant.product.stock_status === 'IN_STOCK') .map(variant => ({ sku: variant.product.sku, name: variant.product.name })); console.log(availableVariants); |
The resulting example contains only the available child product:
|
1 2 3 4 5 6 |
[ { "sku": "SHIRT-BLACK", "name": "T-Shirt Schwarz" } ] |
A single GraphQL query can therefore return both the configurable product and the structure of its options and variants, useful when building a custom storefront.