Retrieve product data using the new product model
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.
What you'll learn
Anchor link to section titled "What 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.
Query product variants with fragments and cursor-based pagination
Anchor link to section titled "Query 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.
Fragments example: Product fields
Anchor link to section titled "Fragments example: Product fields"Use fragments to request the same fields in multiple queries without duplicating them:
Fragments example: Variant fields
Anchor link to section titled "Fragments example: Variant fields"Use fragments to request the same fields in multiple queries without duplicating them:
Query the first 250 variants
Anchor link to section titled "Query 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:
Query the remaining variants
Anchor link to section titled "Query 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:
Tip: Check the number of product variants
Anchor link to section titled "Tip: 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.