Managing Variants
List all variants
Use the productVariants
query to query all variants:
{
productVariants(first: 15) {
edges {
node {
name
id
product {
name
id
}
status
stocks {
warehouse {
name
id
}
quantity
quantityAllocated
}
sku
}
}
}
}
List variants under a product
When querying a product your can return the variants under that product.
The following query will list all of the variants under the product matching the provided id
, and return the name, id, and description of each variant:
{
product(id: "UHJvZHVjdDoxMDQx"){
name
variants{
name
id
description
}
}
}
Searching variants
There are two search filters available when querying variants:
advancedSearch
: Targets specific field(s) for faster performance and tailored results.search
: Searches across all fields for broad search results with slower performance.
Advanced search
You can use the advancedSearch
parameter with the filter
argument for advanced filtering by search term when querying products.
The advancedSearch
filter accepts two input fields:
Field | Description |
---|---|
searchTerm (required) | A string to search with. |
searchFields | A comma separated list of fields to search across. If no search fields are defined, the filter defaults to searching only by NAME . The fields you can target for search are defined in the VariantSearchFieldEnum . |
Here is an example query that searches for variants with the term "shiny" in the product name, variant name, description, or category name:
query {
productVariants(
first: 5
filter: {
advancedSearch: {
searchTerm: "shiny"
searchFields: [NAME, PRODUCT_NAME, DESCRIPTION, CATEGORY_NAME]
}
}
) {
edges {
node {
id
name
description
}
}
}
}
Basic search
You can use the basic search
filter for a broad-match search results when querying variants. The search
filter accepts a string input that will be used to search across all product fields.
If you experience performance issues with the basic search
filter, try using advancedSearch
instead.
Here is an example query that looks for the first five variants that mention the term "juice":
{
productVariants(
first: 5
filter: {
search: "juice"
}
)
{
edges {
node {
id
name
description
}
}
}
}
Default variant
By default, the first variant on a product is assigned the default variant. The defaultVariant
field on the product
object stores the default variant.
Query default variant
While querying products, request the defaultVariant
to see which variant is the default.
{
product(id: "UHJvZHVjdDoxMDQx"){
name
defaultVariant{
name
description
id
}
}
}
Set default variant
You can reassign the default variant with the productVariantSetDefault
mutation.
mutation{
productVariantSetDefault(
productId:"UHJvZHVjdDoxMDQx"
variantId: "UHJvZHVjdFZhcmlhbnQ6MzQxNg=="
)
{
product {
id
name
description
defaultVariant {
id
name
description
}
}
}
}