A list that pages in the store. loadNext on the lens, and a directive to add or remove an edge.
A list that pages is a Relay connection, in a fragment of its own. The fragment owns the pagination. The store owns the merged pages. A mutation adds or removes an edge with a directive, and the view reads the list like any other field. This chapter is that fragment, what the store holds after each page, and the edge directives.
Three directives make a connection. @connection(key:) names the list in the store. @refetchable(queryName:) gives the fragment a query of its own, which is how the next page is fetched. @argumentDefinitions declares the page size and the cursor as the fragment's arguments, so the screen's query need not know them.
Plate 1. The connection fragment, the row it spreads, and the screen that spreads it. The screen's query never mentions first or after.
The lens of a connection reads nodes, the records in order across every page, and also hasNext, isLoadingNext, and pageInfo with its cursors. connectionID is the key an edge directive names the list by. loadNext(count) fetches the next page with the fragment's query and its arguments. refetch() on the fragment's own lens fetches the first page again and replaces the list.
The lens of IssueList_repository, a fragment, reads a record of type Repository:
accessor
reads
as
issues
issues
a lens of IssueConnection
edges
edges
a lens of IssueEdge, a list, optional
node
node
a lens of Issue, optional
id
id
a scalar
issueRow
the parent's record
the lens of IssueRow_issue
cursor
cursor
a scalar
pageInfo
pageInfo
a lens of PageInfo
endCursor
endCursor
a scalar, optional
hasNextPage
hasNextPage
a scalar
id
id
a scalar
Plate 2. The connection as the compiler reported it. The paging surface is generated beside these accessors.
The connection lives under its key in the store, apart from the pages. A page is a slot of its own under the field's storage key, notes(first:2). The connection record under the key, __TestNotes_notes_connection, links the edges of every page in order. A refetch replaces the merged edges with the first page. A page fetched after a cursor that is no longer the connection's end is ignored.
A view attaches a handle for TestNotesQuery(id: "1") under storeOrNetwork, named notes; the transport answers its fetch with tests/notes-page-1.json.
The server answers TestNotesPaginationQuery(count: 2, cursor: "c2", id: "1") with tests/notes-page-2.json, and the environment commits it as a server batch.
The server answers TestAddNote(characterId: "1", text: "Appended", connections: ["Character:1:__TestNotes_notes_connection"]) with tests/add-note-n9.json, and the environment commits it as a server batch.
The server answers TestNotesPaginationQuery(count: 2, cursor: "c1", id: "1") with tests/notes-page-3.json, and the environment commits it as a server batch.
Notifiednothing: no observed field changed, so no body runs
A lens reads
and finds
notes · character.notes.edges.2.node.text
Appended
notes · character.notes.edges.3.node.text
null
notes · character.notes.pageInfo.endCursor
c2
Plate 3. The specification's connection script goes step by step: the first page, the second merged, a refetch that replaces, and an edge appended by a mutation. A page fetched after a cursor the connection has moved past is ignored by the merge, and it notifies nothing.
A mutation that adds to or removes from a list says so on its payload. The store does the edit in the same batch as the payload's own records.
@appendEdge(connections:) and @prependEdge(connections:) go on a payload field that is an edge.
@appendNode(connections:, edgeTypeName:) and @prependNode(connections:, edgeTypeName:) go on a payload field that is a node.
@deleteEdge(connections:) goes on a payload field holding the id of the node to remove from the lists.
@deleteRecord goes on a payload field holding the id of a record to delete outright. Every list skips that record, and every link reads it as null.
The connections variable carries the connection ids the view read from its lenses, so the same mutation can add to one list or to three. Nothing edits the cache by hand, and nothing has to know which screens show the list.
CommentComposer.swift
importBatonimportSwiftUIstructCommentComposer: View {
let subjectID: Stringlet connectionID: String@Stateprivatevar text = ""@Mutation("""mutationCommentComposerAddComment($input: AddCommentInput!, $connections: [ID!]!) {
addComment(input: $input) {
commentEdge@appendEdge(connections: $connections) {
node { idbodyTextauthor { login } }
}
}
}
""")
var addComment: CommentComposerAddComment.Actionvar body: someView {
HStack {
TextField("Leave a comment", text: $text)
Button("Comment") {
let body = text
Task {
tryawaitaddComment(
input: AddCommentInput(body: body, subjectId: subjectID),
connections: [connectionID]
)
text = ""
}
}
.disabled(text.isEmpty || addComment.isInFlight)
}
}
}
Plate 4. A comment appended to the issue's comments. The composer takes the connectionID the list's lens read and passes it in connections.
The server answers TestAddNote(characterId: "1", text: "Appended", connections: ["Character:1:__TestNotes_notes_connection"]) with tests/add-note-n9.json, and the environment commits it as a server batch.
Plate 5. What @appendEdge leaves in the store is the new note's record, the new edge, and the connection's edge list one longer, with no refetch.
A payload with no id has no field for @deleteRecord to sit on, so @deletes(type:, id:) goes on the mutation and names the record by its type and the variable its key is passed as. The store deletes that record as @deleteRecord does, in the optimistic layer and in the server's commit.
Some schemas have no cursors. AniList pages every list by number: Page(page: 1, perPage: 50) { pageInfo { hasNextPage } media(...) }, with one pageInfo for whichever list the page is asked for, and the lists on an entity, Media.characters(page:, perPage:), page the same way. Baton does not pretend such a list is a connection. It says so with @paged(key:, page:), on the list field, inside the field that takes the page:
The pages merge into a record of their own in the store, under the list and its arguments, apart from the server's Page(page: 1, perPage: 50) record, which two lists may share. The first page replaces the merged list, the next appends, a page fetched again rewrites its elements in place. The lens over Page reads the merged media, and gains hasNext, which reads the merged pageInfo.hasNextPage and nothing else (AniList's total and lastPage are not to be trusted, and are not read), isLoadingNext, and loadNext(), which runs the screen's own query again with the next page number. No @refetchable fragment is needed; a refetch of the query replaces the list. The page variable is the operation's, declared Int!, and pageInfo { hasNextPage } is selected beside the list, or the compiler says what is missing.
A view attaches a handle for TestPagedNotesQuery(page: 1, authorId: "1") under storeOrNetwork, named notes; the transport answers its fetch with tests/paged-notes-1.json.
A view attaches a handle for TestFriendsQuery(id: "1", page: 1) under storeOrNetwork, named friends; the transport answers its fetch with tests/friends-1.json.
Plate 6. The specification's paged script: the first page, the second merged, a second list under the same server page keeping its own flag, a page rewritten in place, a page past the end ignored, and a refetch that replaces.
A schema whose page info is not pageInfo { hasNextPage }, as the Rick and Morty API's info { next }, keeps reading one page per operation value: a screen with a page bar holds the current page's query, and the samples show one page at a time. The decision says what was chosen, what was not, and what would reopen it.
In Swift, ForEach takes a plural lens, a Baton.List, or a connection's nodes, through initializers Baton adds. They are keyed by the record, so no id: is needed.