TanqoryDocs

Pagination

รายการใน Admin API มาเป็นหน้า ระบุขนาดหน้า แล้วตาม cursor ไปเรื่อยๆ

ฟิลด์ที่คืนข้อมูลหลายรายการ เช่น products หรือ orders เป็น connection ซึ่งคืนทีละหน้า และขอหน้าถัดไปด้วย cursor ตาม GraphQL Cursor Connections Specification

การอ่านทุกหน้าของ connection
  1. เซิร์ฟเวอร์ของแอปคุณ → Admin API products(first: 10)
  2. Admin API first ได้ไม่เกิน 250 ถ้าไม่ผ่าน: error invalid_pagination_argument
  3. Admin API → เซิร์ฟเวอร์ของแอปคุณ 10 edges, hasNextPage: true, endCursor
  4. เซิร์ฟเวอร์ของแอปคุณ → Admin API products(first: 10, after: endCursor)
  5. Admin API → เซิร์ฟเวอร์ของแอปคุณ อีก 10 รายการ hasNextPage: true และ endCursor ใหม่
  6. เซิร์ฟเวอร์ของแอปคุณ → Admin API products(first: 10, after: endCursor ใหม่)
  7. Admin API → เซิร์ฟเวอร์ของแอปคุณ รายการสุดท้าย hasNextPage: false
  8. เซิร์ฟเวอร์ของแอปคุณ หยุด

ขอหนึ่งหน้า

GraphQL
query Products($after: String) {
  products(first: 10, after: $after) {
    edges { node { id title } }
    pageInfo { hasNextPage endCursor }
  }
}
Argumentความหมาย
firstจำนวนรายการ นับจากต้นหรือจาก after
afterendCursor ของหน้าก่อน ไม่ต้องใส่สำหรับหน้าแรก
last, beforeแบบเดียวกันแต่ย้อนกลับ จากท้ายหรือจาก before
query, sortKey, reverseกรองและเรียง เมื่อฟิลด์นั้นรองรับ ดูหน้าของฟิลด์ใน reference

คำตอบ

JSON
{
  "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
// 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);

ทั้งสองแบบพิมพ์ชื่อสินค้าทุกชิ้นในร้าน ครั้งละสิบรายการ

ควรรู้

  • ถือว่า cursor เป็นค่าทึบ เก็บไว้แล้วส่งกลับ ห้ามสร้างหรือแก้ไขเอง

  • หน้าที่ใหญ่ขึ้นใช้ rate limit มากขึ้น ดู Rate limits and query cost โดย extensions.cost ในแต่ละคำตอบบอกว่าหน้านั้นใช้ไปเท่าไร

  • ถ้าต้องการนับโดยไม่อ่านทุกหน้า ให้ใช้ฟิลด์นับเมื่อมี เช่น productsCount { count }

  • first และ last รับได้ไม่เกิน 250 ถ้ามากกว่านั้นจะได้ error invalid_pagination_argument

สร้างจาก commit c0b3fb3fdef7 · เวอร์ชัน API 2026-10