နားလည်ထားရမယ့် အချက်
Offset pagination (`skip`, `limit`) က ရိုးရှင်းပေမယ့် underlying data ထဲ insert/delete ဖြစ်နေစဉ် page ကူးလျှင် item ကျော်/ထပ်ဖြစ်နိုင်ပါတယ်။ Cursor-based pagination ကတော့ opaque cursor (stable sort key ကို encode ထားတဲ့ string) တစ်ခုကို "ဒီနေရာကနေ ဆက်ပါ" ဆိုတဲ့ pointer အဖြစ်သုံးလို့ ပိုတည်ငြိမ်ပါတယ်။ Relay-style connection pattern က `edges` (each with `cursor` + `node`) နှင့် `pageInfo` (`hasNextPage`, `endCursor`) ကို standard shape အဖြစ် သတ်မှတ်ထားပါတယ်။
လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်
Tutorial Platform ရဲ့ catalog list ကို `tutorials(first: 20, after: $cursor): TutorialConnection!` ဖြင့် expose မယ်။ Frontend က "Load more" button ကို click ရင် `pageInfo.endCursor` ကို နောက် request ရဲ့ `after` argument အဖြစ်ပို့ပြီး `pageInfo.hasNextPage` က `false` ဖြစ်တဲ့အထိ ဆက်ခေါ်ပါမယ်။
အတူတူ စမ်းရေးကြည့်မယ်
type TutorialEdge {
cursor: String!
node: Tutorial!
}
type PageInfo {
hasNextPage: Boolean!
endCursor: String
}
type TutorialConnection {
edges: [TutorialEdge!]!
pageInfo: PageInfo!
}
type Query {
tutorials(first: Int!, after: String): TutorialConnection!
}Relay-style connection schema တစ်ခုရေးပြီး cursor-based "load more" flow ကို ရှင်းပြနိုင်မည်။၅ မိနစ် စမ်းကြည့်
`commentsConnection(first: Int!, after: String): CommentConnection!` schema တစ်ခုရေးပြီး `pageInfo.hasNextPage` ကို frontend loop ထဲ ဘယ်လိုသုံးမလဲ ရေးပါ။
သတိလေးတစ်ချက်
Cursor string ကို database row ID plain number အဖြစ် client သိအောင် ထုတ်ပြထားလျှင် client က cursor arithmetic (id+1) လုပ်ကြိုးစားနိုင်ပါတယ်—cursor ကို opaque/encoded string အဖြစ်ထားပါ။
GraphQL — Pagination — GraphQL