1 | |
2 |
|
3 | |
4 | * Search Product Variants |
5 | * Search product variants across all products with Shopify's search syntax (e.g. `sku:ABC-1`, `barcode:0123`, `product_id:123`, `title:Large`, `inventory_quantity:<5`). Leave query empty to list all variants. Returns one page; pass pageInfo.endCursor as after for the next. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Shopify, |
9 | query: string | undefined, |
10 | sort_key: |
11 | | "SKU" |
12 | | "TITLE" |
13 | | "FULL_TITLE" |
14 | | "NAME" |
15 | | "POSITION" |
16 | | "INVENTORY_QUANTITY" |
17 | | "POPULAR" |
18 | | "ID" |
19 | | "RELEVANCE" |
20 | | undefined, |
21 | reverse: boolean | undefined, |
22 | first: number | undefined, |
23 | after: string | undefined |
24 | ) { |
25 | const response = await fetch( |
26 | `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`, |
27 | { |
28 | method: "POST", |
29 | headers: { |
30 | "X-Shopify-Access-Token": auth.token, |
31 | "Content-Type": "application/json", |
32 | Accept: "application/json", |
33 | }, |
34 | body: JSON.stringify({ |
35 | query: `query ($first: Int!, $after: String, $query: String, $sortKey: ProductVariantSortKeys, $reverse: Boolean) { productVariants(first: $first, after: $after, query: $query, sortKey: $sortKey, reverse: $reverse) { nodes { id legacyResourceId title displayName sku price compareAtPrice position inventoryQuantity inventoryPolicy availableForSale createdAt updatedAt selectedOptions { name value } barcodes(first: 5) { nodes { value type } } inventoryItem { id tracked } product { id title handle status } } pageInfo { hasNextPage endCursor } } }`, |
36 | variables: { |
37 | first: first || 50, |
38 | after: after || undefined, |
39 | query: query || undefined, |
40 | sortKey: sort_key || undefined, |
41 | reverse: reverse || undefined, |
42 | }, |
43 | }), |
44 | } |
45 | ) |
46 |
|
47 | if (!response.ok) { |
48 | throw new Error(`${response.status} ${await response.text()}`) |
49 | } |
50 |
|
51 | const { data, errors } = await response.json() |
52 | if (errors) { |
53 | throw new Error(JSON.stringify(errors)) |
54 | } |
55 |
|
56 | return data.productVariants |
57 | } |
58 |
|