1 | |
2 |
|
3 | export type DynSelect_type = string |
4 |
|
5 | export async function type(auth: RT.Shopify) { |
6 | const response = await fetch( |
7 | `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`, |
8 | { |
9 | method: "POST", |
10 | headers: { |
11 | "X-Shopify-Access-Token": auth.token, |
12 | "Content-Type": "application/json", |
13 | Accept: "application/json", |
14 | }, |
15 | body: JSON.stringify({ |
16 | query: `query { metaobjectDefinitions(first: 250) { nodes { type name } } }`, |
17 | }), |
18 | } |
19 | ) |
20 | if (!response.ok) { |
21 | throw new Error(`${response.status} ${await response.text()}`) |
22 | } |
23 | const { data, errors } = await response.json() |
24 | if (errors) { |
25 | throw new Error(JSON.stringify(errors)) |
26 | } |
27 | return data.metaobjectDefinitions.nodes.map( |
28 | (d: { type: string; name: string }) => ({ |
29 | value: d.type, |
30 | label: `${d.name} (${d.type})`, |
31 | }) |
32 | ) |
33 | } |
34 |
|
35 | |
36 | * List Metaobjects |
37 | * List the metaobjects (entries) of one metaobject definition, with their fields. query filters with Shopify's search syntax (e.g. `display_name:Summer*`, `handle:my-entry`, `updated_at:>2026-01-01`; `fields.<key>:value` only works on fields marked admin-filterable). New entries take a few seconds to become searchable. Returns one page; pass pageInfo.endCursor as after for the next. Needs the read_metaobjects scope. |
38 | */ |
39 | export async function main( |
40 | auth: RT.Shopify, |
41 | type: DynSelect_type, |
42 | query: string | undefined, |
43 | sort_key: "updated_at" | "display_name" | "id" | "type" | undefined, |
44 | reverse: boolean | undefined, |
45 | first: number | undefined, |
46 | after: string | undefined |
47 | ) { |
48 | const response = await fetch( |
49 | `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`, |
50 | { |
51 | method: "POST", |
52 | headers: { |
53 | "X-Shopify-Access-Token": auth.token, |
54 | "Content-Type": "application/json", |
55 | Accept: "application/json", |
56 | }, |
57 | body: JSON.stringify({ |
58 | query: `query ($type: String!, $first: Int!, $after: String, $query: String, $sortKey: String, $reverse: Boolean) { metaobjects(type: $type, first: $first, after: $after, query: $query, sortKey: $sortKey, reverse: $reverse) { nodes { id handle type displayName updatedAt capabilities { publishable { status } } fields { key type value } } pageInfo { hasNextPage endCursor } } }`, |
59 | variables: { |
60 | type, |
61 | first: first || 50, |
62 | after: after || undefined, |
63 | query: query || undefined, |
64 | sortKey: sort_key || undefined, |
65 | reverse: reverse || undefined, |
66 | }, |
67 | }), |
68 | } |
69 | ) |
70 |
|
71 | if (!response.ok) { |
72 | throw new Error(`${response.status} ${await response.text()}`) |
73 | } |
74 |
|
75 | const { data, errors } = await response.json() |
76 | if (errors) { |
77 | throw new Error(JSON.stringify(errors)) |
78 | } |
79 |
|
80 | return data.metaobjects |
81 | } |
82 |
|