Retrieve product data using the new product model
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.
Anchor to What you'll learnWhat you'll learn
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.
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
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
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
JSON response
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
JSON response
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
JSON response
Anchor to Next stepsNext steps
Learn how to add product data, including variants and options.
Learn how to edit product data, including variants and options.
Learn how you can sync product data from an external source into Shopify.