问题描述
目前,我在 graphql-java>图书馆.它确实有一些基本的中继支持,我们可以在其中创建一个 connection
,这是 Facebook 推荐的实现分页的方式.
这个 是帮助实现这一目标的方法.但是,由于没有文档,我发现很难理解这个函数是如何工作的.如果他们已经有一个允许基本查询的现有模型,例如 Add
、delete
、fetch代码>等使用graphql-java库?
Currently, I see no existing support for pagination in the graphql-java library. It does have some basic relay support, where-in, we can create a connection
, Facebook's recommended way of implementing pagination.
This is the method which helps achieve that. However, with no documentation I'm finding it hard to understand how this function works. Can someone break-down the steps they would take to add pagination support if they already have an existing model which allows basic queries like Add
, delete
, fetch
etc. using the graphql-java library?
推荐答案
您甚至不需要中继连接来支持分页.您的查询可以简单地接受页码和大小(或限制/偏移量)作为参数并返回一个列表 - 完成.但是,如果您想要中继连接,例如Book
类型,您将执行以下操作:
You don't even need Relay connections to support pagination. Your query could simply accept a page number and size (or limit/offset) as arguments and return a list - done.
But, if you wanted Relay connection for e.g. Book
type, you'd do something like the following:
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());
因此,您将拥有一个符合 BookConnection 类型"nofollow noreferrer">中继连接规范.
As a result, you'd have a BookConnection
type that conforms to the Relay connection spec.
至于使用基本 GraphQL 的示例,您有一个简单的网络应用程序here一>.
As for the example with basic GraphQL, you have a simple web app here.
连接规范自然适合支持基于光标的分页的数据存储,但在使用不同的分页样式时需要一些创造力.
The connection spec naturally fits a data store that supports cursor based pagination, but needs some creativity when used with different pagination styles.
1) 如果你想使用简单的基于偏移量的分页,你可以决定将 after
视为偏移量(意味着将传递一个数字),并将 first
视为限制:
1) If you wish to use simple offset based paging, you can decide to treat after
as the offset (meaning a number would be passed), and first
as the limit:
SELECT * FROM ORDER BY timestamp OFFSET $after LIMIT $first
before
和 last
一样,只是方向不同.
The same for before
and last
, just different direction.
2) 另一种方法是将 after
/before
视为排序列最后看到的值(因此将传递实际(混淆)值):
2) Another way is to treat after
/before
as the last seen value of the sort column (so an actual (obfuscated) value would be passed):
SELECT * FROM ORDER BY timestamp WHERE timestamp > $after LIMIT $first
我还建议您查看我的项目,graphql-spqr,使用示例应用,这使得开发 GraphQL API 变得非常简单.
I'd also recommend you take a look at my project, graphql-spqr, with an example app, that makes developing GraphQL APIs dead simple.
例如,您将创建这样的分页结果:
For example, you'd create a paginated result like this:
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
实例,这只是最直接的一种.
There's many other ways to create a Page
instance, this is just the most straight-forward one.
然后您将从您的 Java 类生成一个架构:
You'd then generate a schema from your Java class:
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 和/或基于游标的分页的数据库,它几乎总是过度杀伤.
But, again, if you are not using Relay there's really no need to overcomplicate things. If your storage supports cursor-based pagination naturally, go for it. If it doesn't, just use the simple limit/offset arguments and return a list, and forget the connection spec. It was created to enable Relay to automatically manage paging in various scenarios, so it's almost always a total overkill if you're not using Relay and/or a DB with cursor-based pagination.
这篇关于GraphQL:如何使用 graphQL-java 实现分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论