How to implement pagination #369
-
I've seen this article for pagination, but did not understand the implementation. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 10 replies
-
https://www.graphql-java-kickstart.com/tools/relay/ |
Beta Was this translation helpful? Give feedback.
-
At Netflix we use the relay spec as well. I copied the following from an (internal) example, hopefully that helps a bit.
@DgsData(parentType = DgsConstants.MOVIE.TYPE_NAME, field = DgsConstants.MOVIE.FakeReviews)
public CompletableFuture<FakeReviewConnection> recentReviewsOnMovie(DataFetchingEnvironment dfe) {
Movie movie = dfe.getSource();
DataLoader<Integer, List<FakeReview>> dataLoader = dfe.getDataLoader(ReviewDataLoader.class.getAnnotation(DgsDataLoader.class).name());
return dataLoader.load(movie.getMovieId()).thenApply(this::createConnection);
}
/**
* Creates a Connection (for pagination) from a list of reviews.
* The Pagination spec goes into the details of how to use pagination.
* For more information refer to <i href="https://manuals.netflix.net/view/studioedge/mkdocs/master/best-practices/pagination/">Studio Edge-Pagination Best Practices</i>.
*/
private FakeReviewConnection createConnection(List<? extends FakeReview> reviews) {
if(reviews.isEmpty()) {
return new FakeReviewConnection(new DefaultPageInfo(null, null, false, false), null);
}
List<FakeReviewEdge> reviewEdges = reviews.stream().map(r -> new FakeReviewEdge(r.getId(), r)).collect(Collectors.toList());
DefaultPageInfo pageInfo = new DefaultPageInfo(
new DefaultConnectionCursor(reviewEdges.get(0).getNode().getId()),
new DefaultConnectionCursor(reviewEdges.get(reviewEdges.size()-1).getNode().getId()), false, false);
return new FakeReviewConnection(pageInfo, reviewEdges);
} |
Beta Was this translation helpful? Give feedback.
-
Just wanted to add my two cents to this as the documentation is vague as hell for me regarding "nested" pagination connection objects. So the first thing you want to do is add it to the schema:
Then you want to implement a subgraph DataFetcher that resolves for the
And that should enable you to do queries like:
Hahahaha why is the documentation and tutorials so vague on this...I have no idea...but at least that helped me. Places that use GraphQL pagination: Shopifyhttps://shopify.dev/apps/tools/graphiql-admin-api
Jirahttps://developer.atlassian.com/platform/atlassian-graphql-api/graphql/
Githubhttps://docs.github.com/en/graphql/guides/using-the-explorer
|
Beta Was this translation helpful? Give feedback.
-
Actually codegen does not process the Something like this, basically:
|
Beta Was this translation helpful? Give feedback.
At Netflix we use the relay spec as well. I copied the following from an (internal) example, hopefully that helps a bit.
As you can see, we manually define the connection types in the schema, and use API in
graphql-java
to create the results in the datafetcher.