Skip to main content

Retrieve product data using the new product model

Legacy

The REST Admin API is a legacy API as of October 1, 2024. All apps and integrations should be built with the GraphQL Admin API. For details and migration steps, visit our migration guide.

After you understand the new product model, you can use it to interact with products, variants, and options in a store.

In this guide, you'll use the GraphQL Admin API to query product variant data.


In this guide, you'll learn how to request data on existing product variants using the GraphQL Admin API, taking into consideration GraphQL features like nested pagination. Nested pagination might be new to you if you're migrating from the REST Admin API.

Note

If your app or use case aligns with the database sync workflow, then use the productSet mutation to retrieve data asynchronously.


Anchor to Query product variants with fragments and cursor-based paginationQuery product variants with fragments and cursor-based pagination

Retrieving a list of product variants with the GraphQL Admin API may require pagination if the number of variants exceeds the page size limit of 250. For example, to fetch data from 300 variants in a product, you need to run a query to retrieve 250 variants and then run it again to retrieve the remaining 50.

The following examples demonstrate how to use fragments and cursor-based pagination to query product variants with the GraphQL Admin API. In this context, nested pagination refers to the process of retrieving all variants (a nested field) of a product in multiple steps due to the page size limit. Initially, the first 250 variants are fetched. Then, using the cursor from the last variant of the first batch, the remaining variants are fetched in a subsequent query.

Anchor to Fragments example: Product fieldsFragments example: Product fields

Use fragments to request the same fields in multiple queries without duplicating them:

Fragment for requesting product data across multiple queries

fragment ProductFields on Product {
id
title
description
options {
id
name
position
values
optionValues {
id
name
hasVariants
}
}
}

Anchor to Fragments example: Variant fieldsFragments example: Variant fields

Use fragments to request the same fields in multiple queries without duplicating them:

Fragment for requesting product variant data across multiple queries

fragment VariantFields on ProductVariant {
id
title
price
sku
}

Anchor to Query the first 250 variantsQuery the first 250 variants

GraphQL offers a cursor field, which acts as a reference to a node's position in a connection. To ensure that you can retrieve all of the nodes in a connection, or, all of the variants that are attached to a product, request the cursor field. You can pass this value in your next query to start retrieving where you left off.

The first query requests the first 250 variants:

POST https://{shop}.myshopify.com/api/{api_version}/graphql.json

GraphQL query

query GetProductAndFirst250Variants {
product(id: "gid://shopify/Product/456") {
...ProductFields
variants(first: 250) {
edges {
cursor
node {
...VariantFields
}
}
}
}
}

JSON response

{
"data": {
"product": {
"id": "gid://shopify/Product/18",
"title": "My Extra Cool Socks",
"description": "",
"options": [
{
"id": "gid://shopify/ProductOption/42",
"name": "Color",
"position": 1,
"values": [
"Blue",
"Yellow",
"Orange",
"Turquoise",
"Red",
"Green"
],
"optionValues": [
{
"id": "gid://shopify/ProductOptionValue/161",
"name": "Blue",
"hasVariants": true
},
{
"id": "gid://shopify/ProductOptionValue/170",
"name": "Yellow",
"hasVariants": true
},
{
"id": "gid://shopify/ProductOptionValue/171",
"name": "Orange",
"hasVariants": true
},
{

Anchor to Query the remaining variantsQuery the remaining variants

The second query requests the next set of data, or the remaining 50 variants, by passing the cursor value in an after argument:

POST https://{shop}.myshopify.com/api/{api_version}/graphql.json

GraphQL query

query GetProductAndLast50Variants {
product(id: "gid://shopify/Product/456") {
...ProductFields
variants(first: 50, after: "eyJsYXN0X2lkIjoxLCJsYXN0X3ZhbHVlIjoiMSJ9") {
edges {
node {
...VariantFields
}
}
}
}
}

JSON response

{
// The response's data structure will be the same as the return for the first 250 variants
}

Anchor to Tip: Check the number of product variantsTip: Check the number of product variants

A quick way to check the number of variants in a product is by requesting the Product object's variantCount field. This field is useful when you're managing large volumes of variants, and is highly optimized.

POST https://{shop}.myshopify.com/api/{api_version}/graphql.json

GraphQL query

query GetProductVariantsCount {
product(id: "gid://shopify/Product/456") {
variantsCount {
count
}
}
}

JSON response

{
"data": {
"product": {
"variantsCount": {
"count": 9
}
}
}
}

Add data

Learn how to add product data, including variants and options.

Edit data

Learn how to edit product data, including variants and options.

Sync data

Learn how you can sync product data from an external source into Shopify.


Was this page helpful?