Dhá straitéis: bunaithe ar offset (limit/offset) agus bunaithe ar cursor. Níl aon leathanú ionsuite ag GraphQL, ach is é an caighdeán pobail ná an tsonraíocht Relay Connection, a fhoirmiúlaíonn leathanú cursor.
Dhá straitéis: bunaithe ar offset (limit/offset) agus bunaithe ar cursor. Níl aon leathanú ionsuite ag GraphQL, ach is é an caighdeán pobail ná an tsonraíocht Relay Connection, a fhoirmiúlaíonn leathanú cursor.
items(limit: 10, offset: 20)) simplí ach briseann sé ar shonraí beo: má chuirtear ró isteach agus an t-úsáideoir ag leathanú, aistríonn míreanna agus feiceann tú dúbailtí nó bearnaí. Éiríonn sé mall freisin ag offsets doimhne (scanann an DB na rónna a scipeáiltear fós).Fillleann nasc an liosta in edges (gach ceann le cursor agus node) chomh maith le 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 },
};
Tá an cursor teimhneach — de ghnáth base64 den eochair shórtála — agus mar sin caitheann cliaint leis mar token, ní mar uimhir.
Léiríonn roghnú offset i gcomparáid le cursor go dtuigeann tú comhsheasmhacht faoi scríbhinní comhuaineacha agus feidhmíocht leathanach doimhin. Tá tábhacht le heolas ar chruth an naisc Relay toisc go bhfuil láimhseáil taisce ionsuite ag cliaint Apollo/Relay dó.
Leabharlann de cheisteanna agallaimh TF le freagraí mionsonraithe — ó Shóisearach go Sinsearach.
Síntiús