您甚至不需要中继连接来支持分页。您的查询可以简单地接受页码和大小(或限制/偏移)作为参数并返回一个列表 - 完成。
但是,如果您想要中继连接,例如Book 类型,您会执行以下操作:
Relay relay = new Relay();
GraphQLOutputType book = ...; //build your normal Book object type
GraphQLObjectType bookEdge = relay.edgeType(book.getName(), book, null, Collections.emptyList());
GraphQLObjectType bookConnection = relay.connectionType(book.getName(), bookEdge, Collections.emptyList());
因此,您将拥有一个符合 Relay connection spec 的 BookConnection 类型。
对于基本 GraphQL 的示例,您有一个简单的 Web 应用程序 here。
连接规范自然适合支持基于光标的分页的数据存储,但在与不同的分页样式一起使用时需要一些创造力。
1) 如果您希望使用简单的基于偏移量的分页,您可以决定将after 视为偏移量(意味着将传递一个数字),并将first 视为限制:
SELECT * FROM ORDER BY timestamp OFFSET $after LIMIT $first
before 和 last 相同,只是方向不同。
2) 另一种方法是将after/before 视为排序列的最后看到的值(因此将传递实际(混淆)值):
SELECT * FROM ORDER BY timestamp WHERE timestamp > $after LIMIT $first
我还建议您看看我的项目 graphql-spqr 和 an example app,这使得开发 GraphQL API 变得非常简单。
例如,您将创建这样的分页结果:
public class BookService {
@GraphQLQuery(name = "books")
//make sure the argument names and types match the Relay spec
public Page<Book> getBooks(@GraphQLArgument(name = "first") int first, @GraphQLArgument(name = "after") String after) {
//if you decide to fetch from a SQL DB, you need the limit and offset instead of a cursor
//so, you can treat "first" as count as "after" as offset
int offset = Integer.valueOf(after);
List<Book> books = getBooksFromDB(first, offset);
Page<Book> bookPage = PageFactory.createOffsetBasedPage(books, totalBookCount, offset);
return bookPage;
}
}
还有很多其他方法可以创建Page 实例,这只是最直接的一种。
然后你会从你的 Java 类生成一个模式:
GraphQLSchema schema = new GraphQLSchemaGenerator()
.withOperationsFromSingleton(new BookService())
.generate();
GraphQL graphQL = GraphQLRuntime.newGraphQL(schema).build();
并执行查询:
ExecutionResult result = graphQL.execute("{books(first:10, after:\"20\") {" +
" pageInfo {" +
" hasNextPage" +
" }," +
" edges {" +
" cursor, node {" +
" title" +
"}}}}");
但是,如果您不使用 Relay,那么真的没有必要让事情变得过于复杂。如果您的存储自然支持基于光标的分页,那就去吧。如果没有,只需使用简单的限制/偏移参数并返回一个列表,然后忘记连接规范。创建它是为了使 Relay 能够在各种场景中自动管理分页,因此如果您不使用 Relay 和/或具有基于光标的分页的数据库,它几乎总是完全矫枉过正。