spring-graphql icon indicating copy to clipboard operation
spring-graphql copied to clipboard

Pagination and Sorting Support

Open rstoyanchev opened this issue 4 years ago • 7 comments

Investigate ways to support Pagination, for example connecting to pagination support in Spring Data repositories. There is now a specification as well. There are a few different ways, based on index/offset, on order (after a specific element), or with a cursor. Some of these may in turn require Spring Data to expose the equivalent mechanism and therefore worth investigating as early as possible.

For sorting, 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.

rstoyanchev avatar Jul 29 '21 10:07 rstoyanchev

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

Diluka avatar Aug 26 '21 09:08 Diluka

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

SeriousSamV avatar Dec 15 '21 08:12 SeriousSamV

@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.

bclozel avatar Dec 15 '21 10:12 bclozel

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!

yangchoo avatar Mar 28 '22 09:03 yangchoo

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

saintcoder avatar Jun 24 '22 17:06 saintcoder

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.

agtraveso avatar Sep 14 '22 13:09 agtraveso

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.:

  • graphql.relay.Connection
  • graphql.relay.Edge
  • graphql.relay.PageInfo
  • graphql.relay.ConnectionCursor

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

Cheers! Michal

Michal-Kucera avatar Sep 19 '22 14:09 Michal-Kucera

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.

cezary-butler avatar Jan 11 '23 12:01 cezary-butler

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

rstoyanchev avatar Jan 12 '23 11:01 rstoyanchev

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

rstoyanchev avatar Feb 22 '23 13:02 rstoyanchev

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?

roksui avatar Mar 29 '23 07:03 roksui

@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.

bclozel avatar Mar 29 '23 07:03 bclozel

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.

rstoyanchev avatar Mar 29 '23 14:03 rstoyanchev

@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.

roksui avatar Mar 31 '23 08:03 roksui

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

rstoyanchev avatar Apr 13 '23 16:04 rstoyanchev