Pagination
รายการใน Admin API มาเป็นหน้า ระบุขนาดหน้า แล้วตาม cursor ไปเรื่อยๆ
ฟิลด์ที่คืนข้อมูลหลายรายการ เช่น products หรือ orders เป็น connection ซึ่งคืนทีละหน้า และขอหน้าถัดไปด้วย cursor ตาม GraphQL Cursor Connections Specification
- เซิร์ฟเวอร์ของแอปคุณ → Admin API products(first: 10)
- Admin API first ได้ไม่เกิน 250 ถ้าไม่ผ่าน: error invalid_pagination_argument
- Admin API → เซิร์ฟเวอร์ของแอปคุณ 10 edges, hasNextPage: true, endCursor
- เซิร์ฟเวอร์ของแอปคุณ → Admin API products(first: 10, after: endCursor)
- Admin API → เซิร์ฟเวอร์ของแอปคุณ อีก 10 รายการ hasNextPage: true และ endCursor ใหม่
- เซิร์ฟเวอร์ของแอปคุณ → Admin API products(first: 10, after: endCursor ใหม่)
- Admin API → เซิร์ฟเวอร์ของแอปคุณ รายการสุดท้าย hasNextPage: false
- เซิร์ฟเวอร์ของแอปคุณ หยุด
ขอหนึ่งหน้า
query Products($after: String) {
products(first: 10, after: $after) {
edges { node { id title } }
pageInfo { hasNextPage endCursor }
}
}| Argument | ความหมาย |
|---|---|
first | จำนวนรายการ นับจากต้นหรือจาก after |
after | endCursor ของหน้าก่อน ไม่ต้องใส่สำหรับหน้าแรก |
last, before | แบบเดียวกันแต่ย้อนกลับ จากท้ายหรือจาก before |
query, sortKey, reverse | กรองและเรียง เมื่อฟิลด์นั้นรองรับ ดูหน้าของฟิลด์ใน reference |
คำตอบ
{
"data": {
"products": {
"edges": [
{ "node": { "id": "gid://tanqory/Product/01a1dcb0-7022-7580-86b0-ad652431da0b", "title": "Linen Throw" } }
],
"pageInfo": { "hasNextPage": true, "endCursor": "eyJsYXN0X2lkIjoi..." }
}
}
}pageInfo มี hasPreviousPage และ startCursor ด้วย และแต่ละ edge คืน cursor ของตัวเองได้
อ่านทุกหน้า
ขณะที่ hasNextPage เป็น true ให้ส่ง query เดิมโดยตั้ง after เป็น endCursor ล่าสุด
// Node.js 18 or later: every product title, 10 at a time.
const url = `https://api.tanqory.com/admin/2026-10/stores/${process.env.STORE_ID}/graphql`;
const query = `query Products($after: String) {
products(first: 10, after: $after) {
edges { node { id title } }
pageInfo { hasNextPage endCursor }
}
}`;
let after = null;
do {
const res = await fetch(url, {
method: 'POST',
headers: { Authorization: `Bearer ${process.env.TANQORY_ACCESS_TOKEN}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ query, variables: { after } }),
});
const { data, errors } = await res.json();
if (errors) throw new Error(errors[0].message);
for (const { node } of data.products.edges) console.log(node.title);
after = data.products.pageInfo.hasNextPage ? data.products.pageInfo.endCursor : null;
} while (after);# Python 3, standard library only: every product title, 10 at a time.
import json, os, urllib.request
URL = f"https://api.tanqory.com/admin/2026-10/stores/{os.environ['STORE_ID']}/graphql"
QUERY = """query Products($after: String) {
products(first: 10, after: $after) {
edges { node { id title } }
pageInfo { hasNextPage endCursor }
}
}"""
after = None
while True:
req = urllib.request.Request(URL, data=json.dumps({"query": QUERY, "variables": {"after": after}}).encode(), headers={
"Authorization": f"Bearer {os.environ['TANQORY_ACCESS_TOKEN']}",
"Content-Type": "application/json",
})
with urllib.request.urlopen(req) as res:
body = json.load(res)
if "errors" in body:
raise SystemExit(body["errors"][0]["message"])
for edge in body["data"]["products"]["edges"]:
print(edge["node"]["title"])
page = body["data"]["products"]["pageInfo"]
if not page["hasNextPage"]:
break
after = page["endCursor"]ทั้งสองแบบพิมพ์ชื่อสินค้าทุกชิ้นในร้าน ครั้งละสิบรายการ
ควรรู้
ถือว่า cursor เป็นค่าทึบ เก็บไว้แล้วส่งกลับ ห้ามสร้างหรือแก้ไขเอง
หน้าที่ใหญ่ขึ้นใช้ rate limit มากขึ้น ดู Rate limits and query cost โดย
extensions.costในแต่ละคำตอบบอกว่าหน้านั้นใช้ไปเท่าไรถ้าต้องการนับโดยไม่อ่านทุกหน้า ให้ใช้ฟิลด์นับเมื่อมี เช่น
productsCount { count }firstและlastรับได้ไม่เกิน 250 ถ้ามากกว่านั้นจะได้ errorinvalid_pagination_argument