இரண்டு உத்திகள்: offset-based (limit/offset) மற்றும் cursor-based. GraphQL-க்கு built-in pagination இல்லை, ஆனால் community standard என்பது Relay Connection spec ஆகும், அது cursor pagination-ஐ முறைப்படுத்துகிறது.
இரண்டு உத்திகள்: offset-based (limit/offset) மற்றும் cursor-based. GraphQL-க்கு built-in pagination இல்லை, ஆனால் community standard என்பது Relay Connection spec ஆகும், அது cursor pagination-ஐ முறைப்படுத்துகிறது.
items(limit: 10, offset: 20)) எளிமையானது ஆனால் live data-வில் உடைந்துவிடும்: user page செய்யும்போது ஒரு row insert செய்யப்பட்டால், item-கள் நகர்ந்து நீங்கள் duplicate-களையோ இடைவெளிகளையோ காண்பீர்கள். ஆழமான offset-களில் அது மெதுவாகவும் ஆகிறது (DB இன்னும் தவிர்க்கப்பட்ட row-களை scan செய்கிறது).ஒரு connection, list-ஐ edges (ஒவ்வொன்றிலும் ஒரு cursor மற்றும் node) மற்றும் pageInfo-வுடன் சேர்த்து மடிக்கிறது:
type Query {
posts(first: Int!, after: String): PostConnection! # first = page size, after = cursor
}
type PostConnection {
edges: [PostEdge!]!
pageInfo: PageInfo!
}
type PostEdge {
cursor: String! # opaque token for THIS node's position
node: Post! # the actual item
}
type PageInfo {
endCursor: String # feed this back as 'after' for the next page
hasNextPage: Boolean! # so the client knows when to stop
}
// resolver: fetch ONE extra row to compute hasNextPage cheaply
const rows = await db.posts.find({ after: args.after, limit: args.first + 1 });
const hasNextPage = rows.length > args.first;
const page = rows.slice(0, args.first);
return {
edges: page.map((p) => ({ node: p, cursor: encodeCursor(p.id) })),
pageInfo: { endCursor: page.at(-1) && encodeCursor(page.at(-1).id), hasNextPage },
};
Cursor என்பது opaque — பொதுவாக sort key-இன் ஒரு base64 — எனவே client-கள் அதை ஒரு எண்ணாக அல்ல, ஒரு token-ஆகக் கருதுகின்றன.
Offset vs cursor-ஐத் தேர்ந்தெடுப்பது, concurrent write-களின் கீழ் consistency மற்றும் deep-page performance ஆகியவற்றை நீங்கள் புரிந்துகொள்கிறீர்கள் என்பதைக் காட்டுகிறது. Relay connection வடிவத்தை அறிவது முக்கியம், ஏனெனில் Apollo/Relay client-களுக்கு அதற்கான built-in cache handling உள்ளது.
விரிவான பதில்களுடன் கூடிய IT நேர்காணல் கேள்விகளின் நூலகம் — Junior முதல் Senior வரை.
நன்கொடை