The @PaginationFragment property wrapper is very similar to a @Fragment, but it supports loading additional items upon request. When using a pagination fragment, you don't have to load an entire list of data all at once. You can control when you load more, and you'll be able to easily update your UI accordingly.

A @PaginationFragment expects your schema to be structured in a specific way to support paging. See the GraphQL Cursor Connections Specification for more details.

Example

import SwiftUI
import RelaySwiftUI

private let userFragment = graphql("""
fragment ToDoList_user on User
	@refetchable(queryName: "ToDoListPaginationQuery")
  @argumentDefinitions(
    count: { type: "Int", defaultValue: 10 }
    cursor: { type: "Cursor" }
  ) {
  todos(first: $count, after: $cursor)
	  @connection(key: "ToDoList_todos") {
		edges {
			node {
				id
				...ToDoItem_todo
			}
		}
	}
}
""")

struct ToDoList: View {
	@PaginationFragment<ToDoList_user> var user

	var body: some View {
		if let user = user {
			List {
				ForEach(user.todos ?? []) { todo in
					ToDoItem(todo: todo)
				}

				if user.isLoadingNext {
          Text("Loading…").foregroundColor(.secondary)
        } else if user.hasNext {
          Button("Load more tweets…") {
            user.loadNext(10)
          }
        }
			}
		}
	}
}

Requirements

There are a few special requirements for the fragment in order for it to be valid to use with a @PaginationFragment:

Parameters

Property value

The @PaginationFragment property will be a read-only optional value with the fields the fragment requests. This value will automatically update and re-render the view when more items are loaded or when the Relay store updates any relevant records.

The value will also include some additional properties related to paging:

Using @connection fields as Collections