spring-projects / spring-graphql

Spring Integration for GraphQL
https://spring.io/projects/spring-graphql
Apache License 2.0
1.53k stars 303 forks source link

Pagination and Sorting Support #103

Closed rstoyanchev closed 1 year ago

rstoyanchev commented 3 years ago

Investigate ways to support Pagination, for example connecting to pagination support in Spring Data repositories. There is also a specification as well. There are a different ways to control navigation, typically offset or cursor based. Spring Data currently supports offset based pagination.

Pagination depends on a predictable sort order. There doesn't seem to be any spec or ongoing effort to converge on a common approach. There is a talk on the topic that provides a good overview.

Diluka commented 3 years ago

this is an example https://doug-martin.github.io/nestjs-query/docs/graphql/paging

cursor based and offset based paginations are compatible. just add offset to args and totalCount to result in cursor based pagination

SeriousSamV commented 2 years ago

I'm trying to implement this, but just wanted to check before. When is this feature going to come out if ever?

bclozel commented 2 years ago

@SeriousSamV I don't think this is an easy issue to tackle and we probably need to spend some time designing and iterating on that problem. This issue is not scheduled yet so we don't have a concrete plan for it. Voting for this issue and sharing useful information about use cases (see previous comments) is a way to help us make progress on this.

yangchoo commented 2 years ago

Thanks again for the fantastic work on this project, it has greatly simplified the integration of graphql-java into an existing SpringBoot project.

I don't see much movement on this issue and it doesn't look like it'll be tackled as part of RC1 so I figured I'll give it a push. Pagination support is a pretty common requirement for APIs.

In terms of use-cases, we are in the process of transitioning some GET endpoints from REST to GraphQL. Previously, these endpoints return a org.springframework.data.domain.Page object, while consuming a Pageable query parameter. This is a relatively common approach for implementing paging support.

As part of the initial transition, we are utilizing an offset-based approach supported by our client. This was straightforward to implement by exposing offset/limit query arguments and creating a Pageable object from these arguments. This object can then be directly passed into a Repository.findAll method.

Ideally though, a cursor-based implementation such the relay cursor connections specification should be chosen. Besides relay, there seems to be a shift towards clients supporting that specification as a standard (e.g. see apollo).

If the full relay connections specification looks too daunting, perhaps simpler offset/cursor-based pagination implementations can be first supported? It'll be nice to rely on an official implementation vs. hand-rolling our own.

Hopefully some of this context helps. Happy to provide more if its useful. Thanks!

saintcoder commented 2 years ago

Is there any "temporary" of implementing pagination while this issue is addressed?

agtraveso commented 2 years ago

Hi! Is there any update of this issue?

We were using https://github.com/graphql-java-kickstart/graphql-spring-boot in some of our projects. It already provides pagination using relay classes -> https://www.graphql-java-kickstart.com/tools/relay. These relay classes are already defined in com.graphql-java library, so would be available in this project too. It's not necessary to reinvent the wheel :)

Probably the first step to support relay/pagination is to recognize relay directives in the schema.graphqls dinamically, generating the necessary classes.

Michal-Kucera commented 2 years ago

Hello, I'd like to share my workaround for Pagination and Sorting Support for Spring GraphQL, using Kotlin, although it doesn't work with cursors (as per https://github.com/spring-projects/spring-data-commons/issues/2151).

https://gist.github.com/Michal-Kucera/a0ad1572a0c212dc8b67b8136c7cb78c

P.S. I didn't try it out with nested schemas..

Once Spring data common supports cursors, we may reuse classes from graphql-java, relay package, i.e.:

There is also an interesting class graphql.relay.Relay, which might be worth investigating further.

Cheers! Michal

cezary-butler commented 1 year ago

Hi. I am also interested in the pagination support. Especially in the QuerydslDataFetcher context. For adding ability to customize QuerydslDataFetcher would be sufficient. right now I don't see a way to add any code that would run at the end of ManyEntityFetcher.get method, without reimplementing big part of QuerydslDataFetcher and unexposed classes it uses.

rstoyanchev commented 1 year ago

Thanks for the comment, @cezary-butler. I've created #597 as a more specific issue for now. Let's continue the discussion there.

rstoyanchev commented 1 year ago

I've turned this into an umbrella issue. See issue description at the top for a list of sub-tasks for the actual work.

roksui commented 1 year ago

Is pagination support only possible with spring boot v3.x.x and not v2.7.x? I am currently using v2.7.8 and I wonder if there is any way to implement pagination using spring-for-graphql?

bclozel commented 1 year ago

@chop-sui this new feature will be available as of Spring for GraphQL 1.2.0 (so Spring Boot 3.1.0). We don't backport new features.

rstoyanchev commented 1 year ago

It also depends on what exactly you need from pagination. If using Spring Data pagination, then yes we'll have 1st class support for that in 1.2 (only), but that in turn depends on Spring Data's cursor support that's also in development. You can also adapt other persistence pagination mechanisms, but obviously that can't be built in, and you'll need to upgrade to make use of that.

If it's just Connection schema type generation, then it's straight forward, see ConnectionTypeDefinitionConfigurer for example.

roksui commented 1 year ago

@bclozel @rstoyanchev Thank you for comfirming. I am planning to use Spring Data pagination with connection style GraphQL pagination. I will look into migrating to Spring Boot 3 as the project I'm working on has started only recently.

rstoyanchev commented 1 year ago

I'm closing this umbrella issue since all sub-tasks are now complete.